Refer to "Big talk data Structure" p178~184--two cross-tree traversal.
With this binary tree on the book:
The code and explanation are as follows (VS2012 test pass):
1#include <iostream>2 using namespacestd;3 4 //two fork tree two-fork linked list node structure definition5typedefstructBitnode6 {7 Chardata;8 structBitnode *lchild,*Rchild;9 }bitnode;Ten One //Enter a pre-order traversal to create a two-fork tree A //Enter abdh#k## #E # #CFI # # #G #j## - voidCreatebitree (Bitnode **t)//*t is a pointer to Bitnode - { the*t=NewBitnode; - if(*t==null)//if *t still points to null, indicating memory allocation failure, exiting the program - exit (OVERFLOW); - Charch; +Cin>>ch; - if(ch=='#') +*t=NULL; A Else at { -(*t)->data=ch;//The data assignment for the node that *t points to, which is to generate the root node -Createbitree (& (*t)->lchild);//Create & (*t)->lchild temporary variable, pass in Createbitree, construct left subtree -Createbitree (& (*t)->rchild);//Create & (*t)->rchild temporary variable, pass in Createbitree, construct right subtree - //equivalent - //Bitnode **p1; in //p1=& ((*t)->lchild);//not directly p1=&lchild . - //Createbitree (p1); to //Bitnode **p2; + //p2=& ((*t)->rchild);//not directly p2=&rchild . - //Createbitree (p2); the } * } $ Panax Notoginseng //the pre-sequence traversal of a two-fork tree - voidPreordertraverse (Bitnode *B) the { + if(B==null)return;//If you point to an empty node, return directly Acout<<b->data<<" ";//outputs the data for this node. thePreordertraverse (B->lchild);//pre-sequence traversal left subtree +Preordertraverse (B->rchild);//Pre-sequence traversal right subtree - return; $ } $ - //The middle sequence traversal of a two-fork tree - voidInordertraverse (Bitnode *B) the { - if(B==null)return;//If you point to an empty node, return directlyWuyiPreordertraverse (B->lchild);//Middle sequence traversal left subtree thecout<<b->data<<" ";//outputs the data for this node. -Preordertraverse (B->rchild);//the middle sequence traverses the right sub-tree Wu return; - } About $ //Post -sequential traversal of two-fork tree - voidPostordertraverse (Bitnode *B) - { - if(B==null)return;//If you point to an empty node, return directly APreordertraverse (B->lchild);//To traverse the left subtree +Preordertraverse (B->rchild);//To traverse the right sub-tree thecout<<b->data<<" ";//outputs the data for this node. - return; $ } the the intMain () the { theBitnode **pp;//defines a two-level pointer to Bitnode pp -Bitnode *p;//defines a pointer to bitnode p inpp=&p;//pp Pointing P theP=null;//initialize p to point to null the AboutCreatebitree (PP);//Pass in the address pointing to p to create a two-fork tree the thePreordertraverse (P);//incoming address to Bitnode, pre-order traversal creates a good two-fork tree thecout<<Endl; +Inordertraverse (P);//Pass in an address pointing to Bitnode, and the middle sequence iterates through the created two-fork tree -cout<<Endl; thePostordertraverse (P);//Pass in the address that points to bitnode, and then iterate through the created two-fork treeBayicout<<Endl; the}
Operation Result:
Easy to understand recursion. But it is not so troublesome to write recursive code, after all, the human brain is better at thinking about recursion. Focus on the solution target, as well as the conditions for the end of recursion.
Three kinds of traversal of binary tree (pre-order, middle order, sequential)