Home » Interview Questions » Interviews » Story Details
Printable Version

Interview Question: Make tree from traversals

by Kamal Rawat on Jan 11, 2012

Given the preorder an inorder traversal of a Binary tree, construct the Tree.
Comments: 0    Views: 1560

MCN Professionals | Interview Question of the day

MCN Professionals is starting Industrial Training for MCA-2012 Batch. Have a look at our Industrial Training Program and Course Details.
-----------------------------------------------------------------------------------------------------------------------------------------

Today's Question:

Given the pre-order and in-order traversal of a Binary Tree. Construct the tree from these traversals.
InOrder:  1 4 5 8 10 30 40
PreOrder: 10 5 4 1 8 30 40
Can you construct the Tree if only post-order and in-order traversals are given?

Can you construct a tree if only pre-order and post-order traversal of the tree is given?
Solution:
To learn more about traversals see our previous question on Basic Tree Traversals.
Given traversals are :
InOrder:  1 4 5 8 10 30 40
PreOrder: 10 5 4 1 8 30 40
In a PreOrder traversal the first element (leftmost) is the root of the tree. Hence, 10 is the root of our Binary Tree.

If we search 10 in InOrder traversal, we can say All elements on the left side of 10 in InOrder traversal are in the left sub-tree of 10 and all elements on the Right side of 10 are in the Right subtree of 10. Hence, in the first step, we are able to find out, that our tree will look something like this

       10
      /  \
     /    \
    /      \
1 4 5 8   30 40

If we recursively follow the above steps for left and Right subtrees also, we can find the entire tree.

For example:
  • In the Right-subtree, we have 30 & 40, Now in Preorder, 30 comes first, so 30 is at the root ( in preorder, root comes first).
  • Now 40 can be the right-child or left child of 30 (this is not clear from pre-order, for this we will have to look into in-order)
  • In in-order traversal, 40 comes after 30 , hence 40 is right child of 30 (in in-order, root comes in between left and right child)
Hence, the Right sub-tree of 10 is constructed as below:

       10
      /  \
     /    30
    /      \
1 4 5 8    40

Similarly, we can construct the left subtree of 10.

The final tree will be as shown below:
tree.png
Can you construct the Tree if only post-order and in-order traversals are given?
Yes.

The logic will remain similar to the above (when in-order & pre-order is given). Except, in post-order traversal the last element (rightmost) will represent the root of the tree.
Can you construct a tree if only pre-order and post-order traversal of the tree is given?
No.

From both the orders (pre & post) we will be able to find the root of the binary tree, but we will not be able to separate elements on the left & right side of the tree, from any one of them.
---------------------------------------------------------------------------

Interview Questions Archive:

         To see all the questions in the category click here...


Post a Comment
*
DevExpress PowerBuilder Web Development Windows Development Languages Software Engineering Databases
iPhone Architecture Secutiry UML & Modeling Operating Systems Networking Testing
Graphics Design Project Management Hardware Open Source Games Development Business Intelligence Visual Studio LightSwitch 2011
MonoDevelop Visual Studio 2010 ASP.NET HTML, DHTML XML PHP JavaScript
Silverlight Web Services WCF Windows Forms WPF Windows Services Dynamic Link Libraries
ActiveX COM, DCOM, ATL C# VB.NET C++ F# Java
Pascal SQL Server Oracle DB2 MS-Access Windows Servers Windows
Linux Unix SAP LINQ .NET Framework ADO.NET Reporting
Crystal Reports SQL Server Reporting Services Igenda Reports Active Reports Adobe Fireworks Arrays & Collections Hosting
Future Trends Android Windows Phone Smart Devices Business M&A Investment & Funding
Web Browsers Internet Explorer Firefox Safari Common Entrepreneurs Students
Consulting Wiki Gadgets MobileMe iCloud iOS Social Media
Facebook Twitter LinkedIn Google+ Microsoft Kinect XBox
Wii Playstation DirectX i OS OS X CIO, CTO, CEO Windows 8
Web Design Expression Blend 4 Photoshop CS5 Creative Suite 5.5 Expression Web 4 Expression Studio 4 Creative Suite® 5.5 Design
Creative Suite 5.5 Web Creative Suite 5.5 Production Startups Funding M&A Laptops Smart Phones
Desktops Cameras & Camcorders Netbooks Tablets Virtualization Microsoft Surface WordPress
Software Products Cloud Computing Current Affairs Technology TV TV
Earnings XAML E-Commerce MonoTouch Mono for Android Deals Electronics
Mobile Phone Laptop Tablet Book Computer Press Releases Reviews
Products Books Companies Windows Azure SQL Azure Interviews Mac
Web Browsers Symbian Windows Forms WPF Windows Services HTML 5 Office 365
SharePoint 2010 Exchange Server Adobe Visual Studio 2012 iPad Flex / Flash Games
Windows 9
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id / Email:
Password:  
Forgot Password | Forgot UserName