Form a binary tree from the given order?

77 views
Skip to first unread message

Saddam Khalil

unread,
Jun 6, 2015, 8:37:54 AM6/6/15
to dsfas...@googlegroups.com
In-order : d h b i e a f c g j 

Post-order : h d i e b f j g c a


Is it necessary the tree we form should  be same  as  the tree from where we get these orders?  

Mehreen Saeed

unread,
Jun 6, 2015, 10:14:18 AM6/6/15
to Saddam Khalil, dsfas...@googlegroups.com
Saddam

Given these two orders, you can probably only form one unique binary tree.  Try it out.  If 'a' is at the end in post order it means 'a' is the root.

Now look at inorder: d h b i e a f c g j :  This means dhbje are at left of root (a) and fcgj are at the right of a

Keep going like this to construct the binary tree.  Your resultant tree should satisfy the two given traversals.

Mehreen


--
You received this message because you are subscribed to the Google Groups "DSFAST2015" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dsfast2015+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dsfast2015/656aaea3-c97b-4e0c-9074-c24dc645282e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Saddam Khalil

unread,
Jun 6, 2015, 11:10:50 AM6/6/15
to dsfas...@googlegroups.com, saddam...@gmail.com

Mam, how to select node from fcgj ?

    and how to select left, right and node from dh?

Mehreen Saeed

unread,
Jun 6, 2015, 12:04:59 PM6/6/15
to Saddam Khalil, dsfas...@googlegroups.com
I give you a hint only.  Post order always prints the root of a subtree/tree in the end.  So if we have a set of nodes, from their postorder we can decide their root as it is the last one visited.  Does this help??? Post us the solution.

Mehreen

Saddam Khalil

unread,
Jun 6, 2015, 12:28:25 PM6/6/15
to dsfas...@googlegroups.com, saddam...@gmail.com
sorry Mam for not mentioning that i was forming tree from inorder   d h b i e a f c g j 
i found parent(dhbie) of 'dh'   and 'ie'   i.e b. But how  to find the parent from dh and ie. and how to place(direction) the chlids from dh and  ie . 
how to find parent from (fcgj)?

Mehreen Saeed

unread,
Jun 6, 2015, 1:34:42 PM6/6/15
to Hassan Mirza, dsfas...@googlegroups.com
In-order : d h b i e a f c g j 
Post-order : h d i e b f j g c a

Look at only fcgj and look at its traversal for only fcgj

post order: fjgc
inorder: fcgj

this means c is the root (from postorder)

now from inorder f is left and gj is right

Keep repeating in the same way for gj
Does this clear the confusion???  Please post the final solution.

Mehreen



On Sat, Jun 6, 2015 at 9:16 PM, Hassan Mirza <hassanm...@gmail.com> wrote:
mam, if we do this in in-order, what will be node and what will be the child node in  this given tree??
please  clear this ambiguity..

Saddam Khalil

unread,
Jun 6, 2015, 2:05:33 PM6/6/15
to dsfas...@googlegroups.com

Solution from In-order : d h b i e a f c g j

Saddam Khalil

unread,
Jun 6, 2015, 2:53:20 PM6/6/15
to dsfas...@googlegroups.com

both give the same result when we form inorders 


On Saturday, June 6, 2015 at 5:37:54 PM UTC+5, Saddam Khalil wrote:

Mehreen Saeed

unread,
Jun 6, 2015, 10:38:34 PM6/6/15
to Saddam Khalil, dsfas...@googlegroups.com, Muhammad Zain
Saddam, the placement of i is not correct. Your tree doesnot satisfy inorder. Zain's solution is correct. The final tree should satisfy both inorder and postorder. 

Mehreen



-------- Original message --------
From Saddam Khalil <saddam...@gmail.com>
Date: 06/06/2015 23:53 (GMT+05:00)
To dsfas...@googlegroups.com
Subject Re: Form a binary tree from the given order?
--
You received this message because you are subscribed to the Google Groups "DSFAST2015" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dsfast2015+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages