Home » Interview Questions » Interviews » Story Details
Printable Version

Interview Question: Basic tree traversals (inorder, preorder & postorder)

by Kamal Rawat on Oct 19, 2011

Write functions for basic traversals (pre order, inorder & postorder) of a Binary tree.
Comments: 0    Views: 1828

MCN Professionals | Interview Question of the day

To receive interview question in your mailbox daily, please subscribe to "A Question A Day" at MCN Professionals home page.

Today's Question:

If the node of a Binary tree is defined as below:
struct Node{
Node * lptr; // Pointer to Left subtree
int data;
Node *rptr; // Pointer to Right subtree
};
write functions which will traverse the tree in in-Order, Pre-Order and Post-Order.

Solution:

In earlier questions related to tree, "Counting number of leaf nodes" and "Has-Path sum problem", I have written that tree algorithms are better written using recursive algorithms. These traversal algorithms also are easy if we write them recursively.

For demo purpose consider the below tree.
tree1.jpg

The three traversals (pre, in and post order) are named so because of the positioning of root node w.r.t the left and Right sub tree. In pre-order traversal, you visit the root node, then you visit the left subtree in pre-order traversal and then the right subtree in pre-order. In In-Order, we traverse the left subtree in In-Order then we visit the root and finally the Right sub-tree in In-Order, Similarly in Post-Order traversal.

Lets start our traversals:

Pre-order traversal

Visit the root node first (pre)

Algorithm:

  1. Visit the root (we will print it when we visit to show the order of visiting)
  2. Traverse the left subtree in pre-order
  3. Traverse the right subtree in pre-order
Code:
/* Print Pre-order traversal of the tree */
void preOrder(node* r){
if(r){
cout << r->data << " ";
preOrder(r->left);
preOrder(r->right);
}
}

Output:
10 5 4 1 8 30 40

The root node comes before the left and right subtree (for every node of the tree)

pre.jpg

In-order traversal

Visit the root node in between the left and right (in)

Algorithm:

  1. Traverse the left subtree in in-order.
  2. Visit the root (we will print it when we visit to show the order of visiting)
  3. Traverse the right subtree in in-order
Code:
/* Print in-order traversal of the tree */
void inOrder(node* r){
if(r){
inOrder(r->left);
cout << r->data << " ";
inOrder(r->right);
}
}

Output:
1 4 5 8 10 30 40

You may want to observe in the output that root lies in between the left and right traversals.
 pre-traversal.jpg

Post-order traversal

Visit the root node after (post) visiting the left and right subtree.

Algorithm:

  1. Traverse the left subtree in post-order.
  2. Traverse the right subtree in post-order
  3. Visit the root (we will print it when we visit to show the order of visiting)
Code:
/* Print Post-order traversal of the tree */
void postOrder(node* r){
if(r){
postOrder(r->left);
postOrder(r->right);
cout<<r->data<< " ";
}
}
Output:
1 4 8 5 40 30 10

 

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