A binary tree example of pre-sequence traversal and middle-order traversal reconstruction in C + +

Source: Internet
Author: User


It is known that the sequence traversal results and the sequence traversal results of a binary tree are not duplicated in the sequence traversal and the middle sequence traversal results. For example, the sequence of the forward traversal of a binary tree is {1,2,4,7,3,5,6,8} in sequence traversal sequence {4,7,2,1,5,3,8,6}.
Reconstruction of binary tree by forward-sequence traversal and middle-order traversal

struct binarytreenode{
int m_nvalue;
binarytreenode* M_pleft;
binarytreenode* M_pright;
}
First, write down three traversal algorithms:
Pre-sequence traversal
void preorder (binarytreenode* binarytreenode) {
if (Binarytreenode!= NULL)
{
printf ("%d", m_pleft->m_nvalue);
Preorder (Binarytreenode->m_pleft);
Preorder (binarytreenode->m_pright);
}
}
In-sequence traversal
void Inorder (binarytreenode* binarytreenode) {
if (Binarytreenode!= NULL)
{
Inorder (Binarytreenode->m_pleft);
printf ("%d", binarytreenode->m_nvalue);
Inorder (Binarytreenode->m_pright);
}
}
Subsequent traversal
void Postorder (binarytreenode* binarytreenode) {
if (Binarytreenode!= NULL)
{
Postorder (Binarytreenode->m_pleft);
Postorder (Binarytreenode->m_pright);
printf ("%d", m_pleft->m_nvalue);
}
}

The main or use recursion, because the binary tree itself is a recursive product, but also orderly, so find a regular recursive processing can be. Depending on the diagram above, you should be able to analyze the following code:

binarytreenode* Constructcore (int* startpreorder,int* endpreorder,int* startinorder,int*) {
The first digit of the sequence of first-order traversal is the root node
int rootvalue = startinorder[0];
binarytreenode* root = new Binarytreenode ();
Root->m_nvalue = Rootvalue;
Root->m_pleft = Root->m_pright = NULL;

Root node found in middle-order traversal
int* rootinorder = Startinorder;
If the current pointer does not correspond to the root node, then the pointer moves back one time
while (Rootinorder <= endinorder && *rootinorder!= rootvalue) {
+ + Rootinorder;
}

int leftlength = Rootinorder-startinorder;
int* leftpreorderend = Startpreorder + leftlength;

if (Leftlength > 0)
{
Root->m_pleft = Constructcore (Startpreorder + 1,leftpreorderend,startinorder,rootinorder-1);
}

if (Leftlength < Endpreorder-startpreorder)
{
Root->m_pright = Constructcore (leftpreorderend + 1,endpreorder,rootinorder + 1,endinorder);
}

return root;
}

To be perfected and considered more thoughtful:

binarytreenode* Constructcore (int* startpreorder,int* endpreorder,int* startinorder,int*) {
The first digit of the sequence of first-order traversal is the root node
int rootvalue = startinorder[0];
binarytreenode* root = new Binarytreenode ();
Root->m_nvalue = Rootvalue;
Root->m_pleft = Root->m_pright = NULL;

if (Startpreorder = = Endpreorder)
{
if (Startinorder = = Endinorder && *startpreorder = = *startinorder)
{
return root;
}else{
Throw std::exception ("Invalid input.");
}
}

Root node found in middle-order traversal
int* rootinorder = Startinorder;
If the current pointer does not correspond to the root node, then the pointer moves back one time
while (Rootinorder <= endinorder && *rootinorder!= rootvalue) {
+ + Rootinorder;
}

If the result of traversing the sequence traversal is not found, the root node with the same result as the previous traversal
if (Rootinorder = = Endinorder && *rootinorder!= rootvalue)
{
Throw std::exception ("Invalid input.");
}

int leftlength = Rootinorder-startinorder;
int* leftpreorderend = Startpreorder + leftlength;

if (Leftlength > 0)
{
Root->m_pleft = Constructcore (Startpreorder + 1,leftpreorderend,startinorder,rootinorder-1);
}

if (Leftlength < Endpreorder-startpreorder)
{
Root->m_pright = Constructcore (leftpreorderend + 1,endpreorder,rootinorder + 1,endinorder);
}

return root;
}

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.