To restore the binary tree based on the middle sequence traversal and the pre-sequence traversal of the binary tree

Source: Internet
Author: User


Now there is a problem, known as the binary tree's pre-sequence traversal and the middle sequence traversal:
Preorder:gdafemhz
Inorder:adefghmz
How do we restore this binary tree and find out his post-order traversal?

We are based on the fact that the middle sequence traversal must be a collection of nodes in the right subtree of the node collection in the left subtree {},root,{}, and the role of the pre-order traversal is to find the root position of each subtree.

Algorithm 1
Input: Pre-order traversal, middle sequence traversal
1, looking for the root of the tree, the first node of the pre-sequence traversal g is root.
2, observe the pre-sequence traversal gdafemhz, know that G is root, the rest of the nodes must be in the root of the left or right sub-tree node.
3, observe the sequence traversal adefghmz. The adef on the left side of the root node is necessarily the node in the left subtree of root, and the hmz on the right of G is necessarily the node in the right subtree of root, and root is not at the end of the sequence traversal or at the beginning of the two subtree of the root node are not empty.
4, observe the left subtree adef, according to the order of the pre-order traversal to Dafe, so Zuozi root node is D, and a is the node in the left subtree of Zuozi, EF is the node in the right subtree of Zuozi.
5, the same reason, observe the right sub-tree node HMZ, the preamble is MHz, so the right subtree root node is m, left child node H, right child node Z.

The observation shows that the above procedure is recursive. First find the root node of the current tree, then divide it into left dial hand tree, right subtree, and then go to the left subtree to repeat the above procedure, and then go to the right subtree to repeat the above procedure. Finally, you can restore a tree:

and get postorder:aefdhzmg.
Improved:
Further, in fact, if only required to write a follow-up traversal, or even do not specifically occupy space to save the restored tree. Only one array will be used to save the post-run that is going to be achieved:

Algorithm 2
Input: A sequential array, a sequence traversal, a sequence traversal
1. Identify the root and place it at the end of the array
2. Determine the index range of the left subtree, placed in the same index position in the array.
3, determine the right sub-tree index range, placed in the array corresponding to the position of the index, just can put down.
4, with Zuozi of the pre-sequence traversal and the middle sequence traversal, the post-order traversal saved in the corresponding index location
5, with Zuozi of the pre-sequence traversal and the middle sequence traversal, the post-order traversal saved in the corresponding index location

Extended questions

Similarly, we can restore this tree with the middle sequence traversal and post-order traversal.

However, if it is a pre-sequence traversal and post-order traversal, you cannot restore the tree because you cannot find the middle point and note the following two situations:

The first sequence of the two trees is the same, and the order of the two trees is the same. In other words, if there is a subtree, a subtree of its root node is an empty tree, then it cannot be judged that the subtree is an empty tree.


The code on algorithm 1 and algorithm 2:

//Algorithm 1#include<iostream>#include<fstream>#include<string>structtreenode{structtreenode*Left ; structtreenode*Right ; CharElem;}; TreeNode* Binarytreefromorderings (Char* Inorder,Char* Preorder,intlength) {  if(Length = =0)    {      returnNULL; } TreeNode* node =NewTreeNode; Node->elem = *preorder; intRootindex =0;  for(; rootindex < length; rootindex++)    {      if(Inorder[rootindex] = = *preorder) Break; } node->left = Binarytreefromorderings (inorder, preorder +1, Rootindex); Node->right = binarytreefromorderings (inorder + Rootindex +1, preorder + Rootindex +1, Length-(Rootindex +1)); Std::cout<<node->elem<<Std::endl;
Free (node); returnNULL;}intMainintargcChar**argv) { Char* pr="Gdafemhz"; Char*inch="ADEFGHMZ"; Binarytreefromorderings (inch, PR,8); printf"\ n");return 0;}

The topic only requires the output of subsequent traversal, you can directly save the value of the current node in a char.

#include <stdio.h>#include<stdio.h>#include<iostream>using namespacestd;structtreenode{structtreenode*Left ; structtreenode*Right ; CharElem;};voidBinarytreefromorderings (Char* Inorder,Char* Preorder,intlength) {  if(Length = =0)    {      //cout<< "Invalid length";      return; }  CharNode_value = *preorder; intRootindex =0;  for(; rootindex < length; rootindex++)    {      if(Inorder[rootindex] = = *preorder) Break; }  // LeftBinarytreefromorderings (inorder, preorder +1, Rootindex); // RightBinarytreefromorderings (inorder + Rootindex +1, preorder + Rootindex +1, Length-(Rootindex +1)); cout<<node_value<<Endl; return;}intMainintargcChar*argv[]) {printf ("Hello world!\n"); Char* pr="Gdafemhz"; Char*inch="ADEFGHMZ"; Binarytreefromorderings (inch, PR,8); printf ("\ n"); return 0;}

Restore the binary tree based on the binary tree's middle sequence traversal and the pre-order traversal

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.