Describes how to create a binary tree based on its pre-and mid-order sequence.
Source: Internet
Author: User
It is known that the forward order is abcdefg, and the middle order is cbedafg.
First, 1st letters (a) In the first order are used to divide the middle order into two segments (cbed) (AFG)
A As the root (cbed) as the left subtree (FG) right subtree
Then, divide the parts following the forward order into (bcde) (FG) Parts by length)
The problem is converted
It is known that the first order is bcde, and the middle order is cbed.
And
The previous order is FG. The middle order is FG.
No, recursive solutions.
The following steps omit the analysis.
Forward and Middle Order: The left subtree of the root splits the right subtree
Bcde cbed: B (c) (ED) (de)
C: c
De ed: D (e)
E: E
FG: F (g)
G G: G
The resulting binary tree is
A
/B f
/\ C d G
/
E
/* Function:
* Rebirth the tree by it's preorder squence and inorder squence.
* Prototype:
* Struct btnode * create_tree (int * preordersquen, int * inordersquen, int squenlength)
* Input parameter:
* Preordersquen: an integer pointer, denote the start address of the preorder squence
* Inordersquen: an integer pointer, denote the start address of the inorder squence
* Squencelength: an integer, denote the length of the squence, namely the number of the tree's Node
* Output:
* A btnode pointer, point the root of the created tree
* Notice:
* The pointers, preorder and inorder, shouldn't be null
* Also, the two squence shoshould have the same set of nodes
* The value of squenlength shocould be bigger than zero
*/
Struct btnode * create_tree (int * preordersquen, int * inordersquen, int squenlength)
{
Int position_inorder; // save where the head element of preorder squence is in the inoder squence
Struct btnode * head; // point the root of the tree
If (squenlength> 0)
{
// Initialize the new node
Head = (struct btnode *) malloc (sizeof (struct btnode ));
Head-> value = * preordersquen;
Head-> leftchild = NULL;
Head-> rightchild = NULL;
Position_inorder = find (* preordersquen, inordersquen, squenlength );
/* Here ignore the condition that the function find return-1 */
Head-> leftchild = create_tree (preordersquen ++, inordersquen, position_inorder );
Head-> rightchild = create_tree (preordersquen + position_inorder), (inordersquen + position_inorder), (squencelength-position_inorder ));
Return head;
}
Else
Return NULL;
}
// Return the element x position in object_squence, from 1 to length
Int find (int x, int * object_squence, int length)
{
Int I;
I = 0;
While (I <length) & (* (object_squence + I )! = X ))
{
I ++;
}
If (I <length)
{
Return (I + 1 );
}
Else
{
Return-1;
}
}
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