First place on the definition of a binary tree
two fork Tree :
is a finite set of nodes of N (n>=0), which is either an empty tree (n=0), or a root node and two two-ary trees that are disjoint, each of which is called the Saozi right subtree.
Full two fork tree:
A two-fork tree with a depth of K and a 2^k-1 node is called a full two-tree.
All nodes except leaf nodes have two sub-nodes. The number of nodes reaches the maximum value. All leaf nodes must be on the same layer.
Complete binary tree:
If the depth of the two-fork tree is H, except for the H layer, the nodes of each layer (1~H-1) reach the maximum number, and all nodes of the H layer are continuously concentrated on the leftmost side, which is the complete binary tree.
Cases:
Assume that all two-fork trees are structured as
struct binarytrenode{ int nodevalue; *lchild, *rchild;} Bitnode;
There are three types of simple traversal of one or two-tree: pre-sequence traversal, middle-order traversal, and subsequent traversal, which are implemented as follows:
1 voidPreorder (Bitnode *parent)2 {3 if(parent!=NULL)4 {5printf"%d\t",parent->nodevalue);6Preorder (parent->lchild);7Preorder (parent->rchild);8 }9 }Ten voidInorder (Bitnode *parent) One { A if(Parent! =NULL) - { -Inorder (parent->lchild); theprintf"%d\t", parent->nodevalue); -Inorder (parent->rchild); - } - } + voidPostorder (Bitnode *parent) - { + if(parent!=NULL) A { atPostorder (parent->lchild); -Postorder (parent->rchild); -printf"%d\t", parent->nodevalue); - } -}
Second, according to the binary tree's pre-sequence traversal and the middle sequence traversal to print out two forks tree
The code is as follows:
1binarytrenode* Constructcore (int*startpreorder,int*endpreorder,int*startinorder,int*Endinorder)2 {3 //The first number of a sequence traversal series is the value of the root node4 intRootvalue = startpreorder[0];5Binarytrenode *root =NewBinarytrenode ();6Root->nodevalue =Rootvalue;7 if(startpreorder==Endpreorder)8 {9 if(Startinorder = = Endinorder && *startpreorder = = *Startinorder)Ten returnRoot; One Else A ThrowStd::exception ("Invalid input."); - } - //find the value of the root node in the Sequence traversal series the int*rootinorder =Startinorder; - while(*rootinorder! = Rootvalue&&rootinorder <=Endinorder) -++Rootinorder; - if(Rootinorder = = endinorder&&*rootinorder! =rootvalue) + ThrowStd::exception ("Invalid input."); - intLeftlength = Rootinorder-Startinorder; + int*leftendorder = Startpreorder +leftlength; A if(leftlength>0) at { - //Building the left sub-tree -Root->lchild = Constructcore (startpreorder,leftendorder,startinorder,rootinorder-1); - } - if(leftlength<endpreorder-Startpreorder) - { in //Building the right sub-tree -Root->rchild = Constructcore (leftendorder+1, endpreorder,rootinorder+1, Endinorder); to } + returnRoot; - } thebinarytrenode* Construct (int*preorder,int*inorder,intlength) * { $ if(preorder = = NULL | | inorder = NULL | | length <=0)Panax Notoginseng returnNULL; - the returnConstructcore (preorder, preorder + length-1, inorder, inorder + length-1); +}
The core of the algorithm is to find the root node, and then, according to the root node and the position and the middle order traverse to determine the number of nodes of the left subtree and the number of nodes of the right subtree, recursive calculation, so as to print out a two-fork tree.
Data structure--two fork Tree