Binary Tree depth-first traversal of data structures
Binary tree traversal can be divided into two types:
I. Depth
First-order traversal
Summary
Post-order traversal
2. breadth (from left to right)
Sequence Traversal
The following are three depth traversal methods:
# Include
Using namespace std; typedef struct BitNode {char data; struct BitNode * lchild, * rchild;} BitNode, * BiTree; void CreateBiTree (BiTree & T); void PreOrderTraverse (BiTree T ); void InOrderTraverse (BiTree T); void PostOrderTraverse (BiTree T); void CreateBiTree (BiTree & T) {// complete string sequence representation when creating a binary tree, // example: AB # c # char ch; scanf ("% c", & ch); if (ch = '#') T = NULL; // empty tree else {T = new BitNode; if (T) {T-> data = ch; CreateBiTree (T-> lch Ild); CreateBiTree (T-> rchild) ;}}// first traverse the binary tree void PreOrderTraverse (BiTree T) {if (T! = NULL) {cout <T-> data <endl; PreOrderTraverse (T-> lchild); PreOrderTraverse (T-> rchild );}} // traverses Binary Tree void InOrderTraverse (BiTree T) {if (T! = NULL) {InOrderTraverse (T-> lchild); cout <T-> data <endl; InOrderTraverse (T-> rchild );}} // post-order traversal of the Binary Tree void PostOrderTraverse (BiTree T) {if (T! = NULL) {PostOrderTraverse (T-> lchild); PostOrderTraverse (T-> rchild); cout <T-> data <endl ;}} int main () {BiTree T; CreateBiTree (T); cout <"the binary tree has been generated and the first traversal starts:" <endl; PreOrderTraverse (T); cout <"Start the Middle-order traversal: "<endl; InOrderTraverse (T); cout <" start post-order traversal: "<endl; PostOrderTraverse (T); return 0 ;}