Tree traversal

I have a problem.

I have a tree that i need to traverse...

each node as two children.

1
2
3
4
p0    |
p1   / \
p2  / \/ \
   {  ... }


The above tree has 3 generations...

want to generate a list of movements in 0 and 1, 0 being left child and 1 being right child

code would be:

1
2
3
4
for(int a=0; a<2; ++a)
   for(int b=0; b<2; ++b)
      for(int c=0; c<2; ++c)
         /** Output current **/


output would be:

1
2
3
4
5
6
7
8
000
001
010
011
100
101
110
111


however, the size (generations) of the tree in my problem is unknown

how can i compile a list of all the possible traversals in a string of 0s and 1s using a simular algorithm?
Normally with trees you wouldn't need to know the height of the tree in order to traverse it. You'd just write a recursive function like this:
1
2
3
4
5
void traverseTree(Node* root){
	doStuff(root); // do something with the current node
	if (root->leftchild  != NULL){traverseTree(root->leftchild );}
	if (root->rightchild != NULL){traverseTree(root->rightchild);}
}

Will this accomplish what you're trying to do?
yes i think it will. thanks alot.

i hadn't thought about it like that :)
Topic archived. No new replies allowed.