Search:     Advanced search
Browse by category:
C/C++   |   Java   |   Oracle/Database   |   SAP   |   ASP .NET   |   Mainframe-DB2   |   Freshers

Construct a tree given its inorder and preorder traversal strings. Similarly construct a tree given its inorder and post For Inorder And Preorder traversals

Add comment
Views: 604
Votes: 0
Comments: 0

inorder = g d h b e i a f j c
preorder = a b d g h e i c f j


Scan the preorder left to right using the inorder sequence to separate left and right subtrees. For example, "a" is the root of the tree; "gdhbei" are in the left subtree; "fjc" are in the right subtree. "b" is the next root; "gdh" are in the left subtree; "ei" are in the right subtree. "d" is the next root; "g" is in the left subtree; "h" is in the right subtree.

 


For Inorder and Postorder traversals

Scan postorder from right to left using inorder to separate left and right subtrees.


inorder = g d h b e i a f j c
postorder = g h d i e b j f c a


Tree root is "a"; "gdhbei" are in left subtree; "fjc" are in right subtree.

 

For Inorder and Levelorder traversals

Scan level order from left to right using inorder to separate left and right subtrees.

 

inorder = g d h b e i a f j c
level order = a b c d e f g h i j


Tree root is "a"; "gdhbei" are in left subtree; "fjc" are in right subtree.


Here is some working code which creates a tree out of the Inorder and Postorder
traversals. Note that here the tree has been represented as an array. This really simplifies the whole implementation.

Converting a tree to an array is very easy

Suppose we have a tree like this


A
B C
D E F G


The array representation would be


a[1] a[2] a[3] a[4] a[5] a[6] a[7]
A B C D E F G


That is, for every node at position j in the array, its left child will be stored at position (2*j) and right child at (2*j + 1). The root starts at position 1.

 


// CONSTRUCTING A TREE GIVEN THE INORDER AND PREORDER SEQUENCE

#include
#include
#include

/*-------------------------------------------------------------
* Algorithm
*
* Inorder And Preorder
* inorder = g d h b e i a f j c
* preorder = a b d g h e i c f j
* Scan the preorder left to right using the inorder to separate left
* and right subtrees. a is the root of the tree; gdhbei are in the
* left subtree; fjc are in the right subtree.
*------------------------------------------------------------*/

static char io[]="gdhbeiafjc";
static char po[]="abdgheicfj";
static char t[100][100]={'\0'}; //This is where the final tree will be stored
static int hpos=0;

void copy_str(char dest[], char src[], int pos, int start, int end);
void print_t();

int main(int argc, char* argv[])
{
int i,j,k;
char *pos;
int posn;

// Start the tree with the root and its
// left and right elements to start off

for(i=0;i

Others in this Category
document What is an AVL tree?
document Q: What is 'LIKE' used for in WHERE clause? What are the wildcard characters?
document Q: What are the contents of a DCLGEN?
document Check if the 20th bit of a 32 bit integer is on or off?
document Q: What is DCLGEN ?
document Q: When do you use a LIKE statement?
document How many different trees can be constructed using n nodes?
document Q: How would you retrieve rows from a DB2 table in embedded SQL?
document Q: What is the COBOL picture clause for a DB2 column defined as DECIMAL(11,2)?
document What is a threaded binary tree?
» More articles



RSS