How to traverse each node of a tree efficiently without recursion in C (no C++)?
Suppose I have the following node structure of that tree:
struct Node
{
struct Node* next; /* sibling node linked list */
struct Node* parent; /* parent of current node */
struct Node* child; /* first child node */
}
- It’s not homework.
- I prefer depth first.
- I prefer no additional data struct needed (such as stack).
- I prefer the most efficient way in term of speed (not space).
- You can change or add the member of
Nodestruct to store additional information.
If you don’t want to have to store anything, and are OK with a depth-first search:
Will traverse the tree;
processis to keep it from re-hitting parent nodes when it travels back up.