knowledge : In the sequence traversal, the first node is the root of the binary tree, and in the middle sequence traversal, the root node is bound to divide the sequence into two sub-sequences, the first sub-sequence is the sequence of the left subtree of the root node, and the second is the middle sequence of the right subtree of the root node. Similarly, a binary tree can also be uniquely identified by a sequence of sequences and sequence sequences in a given sequence and sequence. However, if we know the sequence and sequence of the binary tree, it is not possible to determine a binary tree in the first order.
Example: for a given sequence of first order and sequence, a binary tree is established, and the algorithm for reconstructing the binary tree is given:
algorithm idea : Use recursive thought. The first node is found in the sequence of Binate, which is the root. Scan the sequence of sequences, find the value of the root node, and then you can get the collection of the middle order sequence points of the left and right sub-trees. At the same time, by the size of the left and right sub-tree, the first order sequence is shifted, and the first sequence traversal sequence of the subtree is also obtained. Recursive to the left and right sub-tree to make achievements.
Algorithm steps:
If the binary tree is not empty
1. Pass in two pairs of pointers, respectively, representing the range of the first order and the sequence;
2, the value of the root node in the binate sequence;
3. Create the root node of the new tree, assign a value to the root node, and initialize the left and right pointer fields to NULL;
4, consider the boundary condition: when the sequence has only one node, if not the same, then illegal input, otherwise return the root of the new tree pointer
5. Traverse the sequence of sequences to find the same node as the root node in the sequence of first order, that is, the root node;
6, calculate the left and right sub-tree respective starting and ending pointers;
7. Create the left and right sub-tree recursively.
The code is as follows:
1typedefstructBintreenode2 {3 intdata;4Bintreenode *LC;5Bintreenode *RC;6}bintreenode,*Bintree;7 8Bintreenode * CONSTRUCT (int*preorder,int*inorder,intlength)9 {Ten if(preorder==null| | inorder==null| | length<=0) One returnNULL; A returnConstructcore (preorder,preorder+length-1, inorder,inorder+length-1); - } - theBintreenode *constructcore (int*prestart,int*preend,int*instart,int*inend) - { - intRootval = prestart[0];//gets the value of the root node. -Bintreenode root =NewBintreenode ();//Create a node +Root->data =Rootval; -Root-lc=root->rc=NULL; + if(Prestart = = preend)//boundary conditions, only one node. A { at if(Instart = = Inend && *prestart = = *Instart) - returnRoot; - Else - ThrowStd::exception ("Invalid input.");//Illegal input - } - in //finding the root node in a sequential traversal sequence - int*inroot =Instart; to while(Inroot<=inend && *inroot! = rootval)//Rootval is the root node of the pre-sequence traversal +++inroot;//until the root node is found or to the end of the middle sequence sequence - if(Inroot = = Inend && *inroot!=rootval) the ThrowStd::exception ("Invalid input.");//The input sequence is illegal when the root node is not found at the end of the order sequence * intLeftlen = Inroot-instart;//the arithmetic Element (subtraction) of the pointer gets the offset $ int*leftpreend = prestart+Len;Panax Notoginseng - //Building subtrees the if(leftlen>0) + { AROOT-LC = Constructcore (prestart+1, leftpreend,instart.inroot-1); the } + if(Leftlen<preend-Prestart) - { $ROOT-RC = Constructcore (leftpreend+1, preend,inroot+1, inend); $ } - - returnRoot; the}
Rebuilding a binary tree