The evolution of algorithms: "outputting post-order Sequences Based on the first and middle order sequences of Binary Trees"

Source: Internet
Author: User
The evolution of algorithms: "outputting post-order Sequences Based on the first and middle order sequences of Binary Trees"
Clever if (Welcome to reprint, but please note the Source: http://blog.csdn.net/qiaoruozhuo)

Not long ago, I saw a job "outputting post-order Sequences Based on the first and middle order sequences of Binary Trees". At that time, I referred to the practices in the data structure and algorithm (c) exercise set, first, determine a binary tree based on the first central sequence, and then traverse the binary tree to output the post-order sequence.
The function uses a recursive algorithm to determine the sequence segment to be processed and generate the corresponding binary tree using the Left and Right boundaries of the first and middle sequence input by the function.
The basic idea is to use the first element of the first sequence of the segment as the root node of the current binary tree, and then find the root node in the middle sequence. At the root node, the central sequence is divided into two parts: Left subtree on the left and right subtree on the right. Determine the length of the Left subtree based on the central sequence, and determine the position of the rightmost and rightmost node in the first sequence of the Left subtree, so that the range of the left and right subtree In the first sequence can be determined, then recursively generate left and right subtree.
The Code is as follows:
Program code:
/*
Function Name: bitreebypreind
Function: generate a binary tree based on the first and middle orders.
Input variable: elemtype pre []: saves the array of the first sequence.
Elemtype ind []: saves the array of the central sequence.
Int preleft: left boundary of the first sequence to be processed
Int preright: right boundary of the first sequence to be processed
Int indleft: left boundary of the intermediate sequence to be processed
Int indright: right boundary of the intermediate sequence to be processed
Output variable; the root node of the binary tree generated based on the current sequence segment
*/
Bitree bitreebypreind (elemtype pre [], elemtype ind [], int preleft, int preright, int indleft, int indright)
{
Bitree head = NULL;
Int root, right;

If (preleft <= preright)
{
Head = (bitree) malloc (sizeof (bitreenode ));
If (! Head)
{
Printf ("out of space! ");
Exit (1 );
}
Head-> DATA = pre [preleft];

Root = indleft;
While (IND [root]! = Pre [preleft]) // search for the root node in the central Sequence
Root ++;

Right = preleft + root-indleft; // right indicates the position of the rightmost node in the left subtree in the forward sequence.
Head-> lchild = bitreebypreind (PRE, IND, preleft + 1, right, indleft, root-1); // generate the left subtree
Head-> rchild = bitreebypreind (PRE, IND, right + 1, preright, root + 1, indright); // generate the right subtree
}

Return head;
}

Functions can complete tasks, but function calling has too many parameters and the code is lengthy. Later, I thought that the lengths of various sequences were actually the same. I could just give the length of the segment to be processed. I can move the pointer, make sure that the passed Pointer Points to the first address of the processed sequence segment. This can also determine the boundary of the processed sequence segment.
The Code is as follows:
Program code:
/*
Function Name: bitreebypreind_2
Function: generate a binary tree based on the first and middle orders.
Input variable: elemtype pre []: saves the array of the first sequence.
Elemtype ind []: saves the array of the central sequence.
Int N: the length of the sequence segment to be processed.
Output variable; the root node of the binary tree generated based on the current sequence segment
*/
Bitree bitreebypreind_2 (elemtype pre [], elemtype ind [], int N)
{
Bitree head = NULL;
Int root;

If (n> 0)
{
Head = (bitree) malloc (sizeof (bitreenode ));
If (! Head)
{
Printf ("out of space! ");
Exit (1 );
}
Head-> DATA = pre [0];

Root = 0;
While (IND [root]! = Pre [0]) // search for the root node in the central Sequence
Root ++;

Head-> lchild = bitreebypreind_2 (pre + 1, IND, root); // generate the left subtree
Head-> rchild = bitreebypreind_2 (pre + root + 1, IND + root + 1, n-root-1); // generate the right subtree
}

Return head;
}

Both of the above functions can correctly generate a binary tree and obtain the post-order sequence through post-order traversal. However, the question does not require the construction of a binary tree. Can we use the features of a binary tree to generate a post-sequential sequence directly? Of course, we can simulate the process of traversing a binary tree in a descending order, and use a stack to save the latter sequence. Because we need to call a function recursively, we must point the call to the top of the stack (or use it as a global variable ).
The Code is as follows.
Program code:
/*
Function Name: buildpostbypreind
Function: generates post-order Sequences Based on the first and middle order sequences.
Input variable: elemtype pre []: saves the array of the first sequence.
Elemtype ind []: saves the array of the central sequence.
Elemtype post []: Stack used to save the post sequence
Int N: the length of the sequence segment to be processed.
Int * Top: The top pointer of the stack pointing to the sequent stack.
Output variable; none
*/
Void buildpostbypreind (elemtype pre [], elemtype ind [], elemtype post [], int N, int * top)
{
Int root;

If (n> 0)
{
Root = 0;
While (IND [root]! = Pre [0]) // search for the root node in the central Sequence
Root ++;
Buildpostbypreind (pre + 1, IND, post, root, top); // generates the left subtree
Buildpostbypreind (pre + root + 1, IND + root + 1, post, n-root-1, top); // generate the right subtree
Post [(* Top) ++] = pre [0];
}
}

The program seems quite good, but why not proceed further? Since we can use the moving pointer Method to Determine the boundary of the forward and middle sequence segments, why do we use the same method to determine the position of the backward sequence? In this way, you do not need to pass the stack top pointer, and the code can be more concise.
The Code is as follows:
Program code:
/*
Function Name: buildpostbypreind_2
Function: generates post-order Sequences Based on the first and middle order sequences.
Input variable: elemtype pre []: saves the array of the first sequence.
Elemtype ind []: saves the array of the central sequence.
Elemtype post []: Stack used to save the post sequence
Int N: the length of the sequence segment to be processed.
Output variable; none
*/
Void buildpostbypreind_2 (elemtype pre [], elemtype ind [], elemtype post [], int N)
{
Int root;

If (n> 0)
{
Root = 0;
While (IND [root]! = Pre [0]) // search for the root node in the central Sequence
Root ++;
Buildpostbypreind_2 (pre + 1, IND, post, root); // generates the left subtree
Buildpostbypreind_2 (pre + root + 1, IND + root + 1, post + root, n-root-1); // generate the right subtree
Post [n-1] = pre [0];
}
}

The buildpostbypreind_2 function successfully generates a post-order sequence by moving the pointer and simulating the post-order traversal process of a binary tree. Of course, if we simply output the post-order sequence, we don't need to generate the sequence. We only need to output the data, so the function can be further simplified.
The Code is as follows:
Program code:
/*
Function Name: printpostbypreind
Function: outputs a post-Order Sequence Based on the first and middle-order sequences.
Input variable: elemtype pre []: saves the array of the first sequence.
Elemtype ind []: saves the array of the central sequence.
Int N: the length of the sequence segment to be processed.
Output variable; none
*/
Void printpostbypreind (elemtype pre [], elemtype ind [], int N)
{
Int root;

If (n> 0)
{
Root = 0;
While (IND [root]! = Pre [0]) // search for the root node in the central Sequence
Root ++;
Printpostbypreind (pre + 1, IND, root); // generate the left subtree
Printpostbypreind (pre + root + 1, IND + root + 1, n-root-1); // generates the right subtree
Printf ("% C", pre [0]); // assume that the data element is of the character type.
}
}

Although the process of algorithm evolution is difficult, the refinement of thinking is sufficient, and the sense of accomplishment gained from it is hard for outsiders to understand. Come on!

The evolution of algorithms: "outputting post-order Sequences Based on the first and middle order sequences of Binary Trees"

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.