There are three ways to traverse a binary tree:
1) First Order traversal: If the binary tree is empty, then empty operation; not NULL, first access to the root node, the first sequence to traverse the left subtree, the first sequence to traverse the right sub-tree.
2) post-order traversal: If the binary tree is empty, then empty operation; not empty, then the middle sequence traverses the left subtree, accesses the root node, and traverses the right subtree in the middle sequence.
3) post-order traversal: If the binary tree is empty, then empty operation, not empty, then the post-order traversal of the left sub-tree, the order to traverse the right subtree, access to the root node.
Cases:
1) The result of the first order traversal is: ABDECF
2) The result of the middle sequence traversal is: DBEAFC
3) post-order traversal results are: DEBFCA
Binary tree output of the idea is to convert the tree into a linear structure output, the general use of recursion, non-recursive method is to use the stack implementation, C + + recursive implementation is as follows:
1#include <iostream>2 using namespacestd;3 4typedefstructbitreenode{5 Chardata;6bitreenode*Plchild;7bitreenode*Prchild;8}bitreenode, *Bitree;9 Ten //create a two-fork tree One voidCreatebitree (Bitree &PTree) { A Charch; -CIN >>ch; - if(ch = ='*') the { -PTree =NULL; - } - Else + { -PTree =NewBitreenode; +Ptree->data =ch; ACreatebitree (ptree->plchild); atCreatebitree (ptree->prchild); - } - } - - //first-order traversal of binary tree - voidPreordertrav (Bitree &PTree) { in if(PTree! =NULL) - { tocout << Ptree->data <<'\ t'; +Preordertrav (ptree->plchild); -Preordertrav (ptree->prchild); the } * } $ Panax Notoginseng //Middle Sequence Traversal - voidInordertrav (Bitree &PTree) { the if(PTree! =NULL) + { AInordertrav (ptree->plchild); thecout << Ptree->data <<'\ t'; +Inordertrav (ptree->prchild); - } $ } $ - //Post-post traversal - voidPosordertrav (Bitree &PTree) { the if(PTree! =NULL) - {WuyiPosordertrav (ptree->plchild); thePosordertrav (ptree->prchild); -cout << Ptree->data <<'\ t'; Wu } - } About $ voidMain () - { -Bitree PTree =NULL; - Createbitree (pTree); Acout <<"first-order traversal:"<<Endl; + Preordertrav (pTree); thecout << Endl <<"Middle sequence Traversal:"<<Endl; - Inordertrav (pTree); $cout << Endl <<"Post-post traversal:"<<Endl; the Posordertrav (pTree); thecout <<Endl; the}
Binary Tree Learning 1:2 fork tree creation and traversal