C + + can construct a unique two-fork tree based on the pre-sequence traversal sequence and the middle sequence traversal sequence

Source: Internet
Author: User

Article reproduced from http://blog.csdn.net/touzani/article/details/1637195

A unique two-fork tree can be constructed based on the pre-sequence traversal sequence and the middle sequence traversal sequence.

Assuming that the sequence is of type string
Based on the characteristics of the pre-order traversal, the first element (Pre[0]) of the pre-order sequence (pre) is the root (root), and then the sequence (in) is searched for this root (pre[0]), according to the characteristics of the middle sequence traversal, it is known that the sequences in front of the root (root) are the left dial hand tree, There are left elements in front of the root. Then again, in the preface sequence, the left sequence of elements immediately following the root (root) (ie, pre[1...left]) is the left dial hand tree, which is the right subtree behind. And the structure of the left sub-tree problem is the same as the construction of the whole binary tree problem, just at this time before the Order column as Pre[1...left]), the sequence listed as In[0...left-1], respectively, the original sequence of sub-strings, the right sub-tree structure, it is obvious that the recursive method can be solved.

So the algorithm can be described as:
Build_tree (Sub_root, Pre, in)
1. if (Size (Pre) > 0)
2. Sub_root =new Node (pre[0]);
3. The position of Index = pre[0] In
4. In_left = In[0...index-1], In_right = In[index ...]
5. Pre_left =pre[1..index], Pre_righ = pre[index+1 ...]
6. Build_tree (Sub_root->left, Pre_left, In_left)
7. Build_tree (Sub_root->right, Pre_right, Pre_right)

The C + + code is as follows:
Template <class entry>
Binary_tree<entry>::binary_tree (String p, string s)
/* p is the ordinal sequence of a two-fork tree, and S is a sequence of two-forked trees. The algorithm establishes two-fork tree, so that the its first sequence and the ordinal sequence are p and s respectively.
Uses:pre_in_build
*/
{
Pre_in_build (Root, p, s);
}

Template <class entry>
void Binary_tree<entry>::p re_in_build (binary_node<entry> *&sub_root, string Pre, string in)
{

if (pre.length () > 0)
{
Sub_root=new binary_node<char> (pre[0]);
int Index=in.find (pre[0]);
String in_left_str=in.substr (0, index);
String In_right_str=in.substr (index+1);
String Pre_left_str=pre.substr (1, index);
String Pre_right_str=pre.substr (index+1);
Pre_in_build (Sub_root->left, Pre_left_str, IN_LEFT_STR);
Pre_in_build (Sub_root->right, Pre_right_str, IN_RIGHT_STR);
}
}


String class returns a substring function
Basic_string substr (Size_type _off = 0, Size_type _count = NPOs)
Parameter description:
The _off is the starting subscript, which defaults to 0.
_count is the size of the substring, from the start to the end of the string by default.
NPOs =-1; Represents not found or all remaining characters
Returns the substring string
If _count = 0, returns an empty string
If subscript _off >= length, also returns an empty string

C + + can construct a unique two-fork tree based on the pre-sequence traversal sequence and the middle sequence traversal sequence

Related Article

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.