Construct a binary tree algorithm based on the pre-order traversal sequence and the middle-order traversal Sequence

Source: Internet
Author: User

A forward traversal sequence and a central traversal sequence can determine a unique binary tree.

According to the features of presequence traversal, the first element (Presequence [0]) is the root of a binary tree (Root ),
Then, inSequence) To find the root (Root). Based on the features of the central traversal, it is known that the root (Root) front edge is the central traversal sequence of the Left subtree of the root, the following sequence is the central traversal sequence of the right subtree of the root. Set to the ordinal traversal Sequence(Insequence)The front of the root has left elements.
Then in the presequence, followed by the Left element sequence of the root (namelyPresequence [1 .. Left])
The first traversal sequence of the Left subtree of the root, and the first traversal sequence of the right subtree of the root. the problem of constructing the left subtree is actually the same as that of constructing the entire binary tree, but the first sequence isPresequence[1... Left]), where the central sequence isInsequence[0... Left-1],
They are the substrings of the original sequence, and the right subtree is constructed similarly. It can be solved by recursion.

Binary Tree is defined below:

// Binary linked list indicates Binary Tree typedef struct binode {char data; // node data struct binode * lchild; // left child struct binode * rchild; // right child} binode, * bitree;

 

ByThe algorithms used to determine a unique binary tree are left behind:

// Void createbitree (bitree & T, string presequence, string insequence)/T is the binary tree to be created, presequence and insequence are pre-order and mid-order SEQUENCES {If (presequence. length () = 0) {T = NULL; return;} Char rootnode = presequence [0]; // root int Index = insequence. find (rootnode); // The Position of the root in the central sequence string lchild_insequence = insequence. substr (0, index); // The left child's central sequence string rchild_insequence = insequence. substr (index + 1); // The middle sequence of the right child int lchild_l Ength = lchild_insequence.length (); // The length of the left child int rchild_length = rchild_insequence.length (); // The length of the right child string lchild_presequence = presequence. substr (1, lchild_length); // string rchild_presequence = presequence. substr (1 + lchild_length); // The forward sequence of the right child t = (bitree) malloc (sizeof (binode); If (T! = NULL) {T-> DATA = rootnode; createbitree (t-> lchild, lchild_presequence, lchild_insequence); // recursively create the left child createbitree (t-> rchild, rchild_presequence, rchild_insequence); // recursively create right child }}

The complete program code remains:

// Construct a binary tree using the forward and middle sequence. cpp: defines the entry point of the console application. // # Include "stdafx. H "# include <string >#include <iostream> using namespace STD; // binary linked list indicates Binary Tree typedef struct binode {char data; // node data struct binode * lchild; // left child struct binode * rchild; // right child} binode, * bitree; // void createbitree (bitree & T, string presequence, string insequence) // T is the binary tree to be created. presequence and insequence are pre-order and mid-order SEQUENCES {If (presequence. length () = 0) {T = NULL; return;} Char rootnode = prese Quence [0]; // The root int Index = insequence. find (rootnode); // The Position of the root in the central sequence string lchild_insequence = insequence. substr (0, index); // The left child's central sequence string rchild_insequence = insequence. substr (index + 1); // The middle sequence of the right child int lchild_length = lchild_insequence.length (); // The length of the left child int rchild_length = rchild_insequence.length (); // The length of the right child string lchild_presequence = presequence. substr (1, lchild_length); // string rchild_presequence = pres Equence. substr (1 + lchild_length); // The forward sequence T = (bitree) malloc (sizeof (binode) of the right child; if (T! = NULL) {T-> DATA = rootnode; createbitree (t-> lchild, lchild_presequence, lchild_insequence); // recursively create the left child createbitree (t-> rchild, rchild_presequence, rchild_insequence); // recursively create right child} // check whether the creation is successful // recursively traverse the binary tree void preordertraverse (bitree & T) {If (T! = NULL) {cout <t-> data; preordertraverse (t-> lchild); preordertraverse (t-> rchild );}} // recursively traverse Binary Tree in ascending order void inordertraverse (bitree & T) {If (T! = NULL) {inordertraverse (t-> lchild); cout <t-> data; inordertraverse (t-> rchild) ;}} int _ tmain (INT argc, _ tchar * argv []) {bitree t; string presequence = "abcdefg"; string insequence = "cbedafg"; createbitree (T, presequence, insequence); preordertraverse (t ); cout <Endl; inordertraverse (t); System ("pause"); Return 0 ;}

The result is as follows:

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.