A method for solving two-tree traversal sequences by C + + _c language

Source: Internet
Author: User

In this paper, the solution of the two-tree traversal sequence in C + + is described in detail, which has a good reference value for the study of data structure and algorithm. The specific analysis is as follows:

Two-fork tree constructed by traversal sequence

As shown in the figure above a binary tree, it is known that its traversal sequence is:

First-order traversal: Abdecfg

In-sequence traversal: DBEAFCG

Subsequent traversal: DEBFGCA

What we need to know is that a binary tree can be uniquely determined by the first order sequence and the middle sequence sequence of the two-fork tree; a binary tree can be uniquely determined by the sequential sequence and the sequence of the two-forked tree; But if you know only the sequence of first order and sequence, then you cannot uniquely determine a binary tree.

Second, the first sequence and the sequence of the two-fork tree are given, and the sequence of the second order is obtained.

Because the first sequence and the sequence of the two-forked tree can uniquely determine a binary tree, it can then uniquely determine its subsequent traversal. The first node must be the root node of the two-forked tree in the preceding sequence traversal, in the middle sequence traversal, the root node is bound to divide the ordinal sequence into two subsequence, the first subsequence is the Zuozi sequence, and the latter is the middle sequence of the right subtree. According to the length of these two subsequence sequences, we can find the corresponding Zuozi sequence and the first sequence of the right subtree in the sequence. The first node of the Zuozi sequence is the root node of the Zuozi, and the first node of the first sequence of the right subtree is the root node of the right sub tree. So recursively, it is only possible to determine the binary tree.

C + + Implementation code is as follows:

/************************************************************************* > File Name:Test.cpp > Author: Songlee ************************************************************************/#include <iostream> using

namespace Std;
  struct TreeNode {struct treenode* left;
  struct treenode* right;
Char Elem;


};
  treenode* postorderfromorderings (char* inorder, char* preorder, int length) {if (length = 0) {return NULL;
  } treenode* node = new TreeNode;
  Node->elem = *preorder;
  int rootindex = 0;
  for (; rootindex < length; rootindex++)//Find the length of the left subtree {if (inorder[rootindex] = = *preorder) break;
  } node->left = Postorderfromorderings (inorder, preorder + 1, rootindex);
  Node->right = postorderfromorderings (inorder + rootindex + 1, preorder + rootindex + 1, Length-(rootindex + 1));  cout << Node->elem << "";
To find the sequence, so the last output root node return node;
  int main () {char* pre = ' abdecfg ';
  char* in = "DBEAFCG"; PostoRderfromorderings (in, Pre, 7);
  cout << Endl;
return 0;

 }

Third, the sequential sequence and the sequence of the binary tree are known, and the first sequence is obtained.

Similarly, a binary tree can be uniquely determined by sequence sequences of two-fork trees and in sequence sequences, so that the sequence of first order traversal can be uniquely determined. Because the last node of the sequential sequence is like the first node of the first sequence sequence, the sequence of sequences can be divided into two subsequence and then recursively divided by a similar method.

C + + Implementation code is as follows:

/************************************************************************* > File Name:Test1.cpp > Author: Songlee ************************************************************************/#include <iostream> using

namespace Std;
  struct TreeNode {struct treenode* left;
  struct treenode* right;
Char Elem;

};
  treenode* preorderfromorderings (char* inorder, char* postorder, int length) {if (length = 0) {return NULL;
  } treenode* node = new TreeNode;
  Node->elem = Postorder[length-1];
  int rootindex = 0;
  for (; rootindex < length; rootindex++)//Find the length of the left subtree {if (inorder[rootindex) = = Postorder[length-1]) break;  } cout << Node->elem << "";
  First order sequence, so the first output root node Node->left = preorderfromorderings (inorder, Postorder, Rootindex);
  Node->right = preorderfromorderings (inorder + rootindex + 1, postorder + rootindex, Length-(rootindex + 1));
return node;
  the int main () {char* post = "DEBFGCA"; char* in = "DbeaFCG ";
  Preorderfromorderings (in, post, 7);
  cout << Endl;
return 0;

 }

Believe that the example described in this article will help us to learn the data structure and algorithms.

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.