473,386 Members | 1,962 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,386 developers and data experts.

Post order traversal non recursive

Introduction
-------------
In a binary tree starting from the root there always exists a path to reach any node.But for any particular node this path is unique.Each of these paths are extendible upto some leaf node.

So if we cover every path once upto the leaf nodes we cover up all the nodes once. So to traverse all the nodes if we traverse each leaf node from the root we will traverse each node the tree. For post order following is the non recursive algorithm.

Algorithm
----------
1>Here the leftmost path is chosen and the leaf node is traversed first. let this node be nl.

2>Then it's immediate parent is traversed if this parent either does not have a right son or nl itself is the right son. This process goes on .
If the parent has a right son which has not yet been traversed, then the 2nd left most path containing the right son is chosen and the leaf is traversed. Again the same process starts.

3>This process is followed until we return to root again but do not find a new next leftmost path. Then root is traversed.

In the attached jpeg leftmost path from root is 9, 8, 1. So the leaf ( i.e. 1 is traversed). As 8 does not have a right son 8 is traversed. As 9 has a right son (i.e. 10) , the second left most path containing the right son (i.e. 9, 10)is chosen. Then the leaf i.e. 10 is traversed. Then at last root i.e. 9 is traversed.


Description
------------
I developed this simple program in C in the year 2004 when started going through the subject data structure. This will give an idea of how to program tree traversals.

The screen shot and C file are self explanatory. Please go through the screenshot.
The exe file may be run in DOS mode in a windows 98 or XP machine to test the result.

But please do not use a non integral input. During that period of time as I was just a novice I did not check for wrong inputs. Extremely sorry for this.

But all other checks like null pointer and all are implemented.

Please find the attachments.

Attached Images
File Type: jpg post_order.jpg (10.1 KB, 3373 views)
Attached Files
File Type: zip POST_ORD_C.zip (1.5 KB, 1043 views)
File Type: zip Post_ord_exe.zip (32.1 KB, 402 views)
File Type: zip post_order_jpg.zip (19.6 KB, 376 views)
File Type: zip run_in_dos_jpeg.zip (5.7 KB, 235 views)
Nov 2 '11 #1
0 11101

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: ravi mannan | last post by:
Hello all, I'm trying to read an xml file and create a nested JPopupMenu from that. The first thing I want to do is to read in the xml file and put it in a Document using DOM and then do a...
2
by: AD | last post by:
Hi, I know it is not exactly a C++ problem but rather concerns algorithm. But I could not figure out any other group to post this so am posting here. I am looking for the algorithm proposed...
6
by: GrispernMix | last post by:
//ques and and level order traversal file name: lab6_build_leaf_up.cpp Instructions:
7
by: GrispernMix | last post by:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> #include <iostream> #include <string.h> using namespace std; #define TRUE 1 #define FALSE 0
9
by: nishit.gupta | last post by:
Can somebody please help me for algorithm for postorder bst traversal without recursion. It should use stack. i am not able to implement it. thnx
1
momotaro
by: momotaro | last post by:
am trying to impliment a function which first visit the node, then traverses its left subtree(in double order), then visits the node again, then traverss its right subtree(in double order) PLZ HELP!...
6
by: APEJMAN | last post by:
would you please help me? I wrote 3 separate line of code for printing my binary tree, and now I am trying to print the level-order traversal of the tree, where the nodes at each level of the tree...
4
by: APEJMAN | last post by:
would you please help me with this question? I know that a binary tree can be recovered from its pre-order traversal. That is, a tree built from the pre-order traversal should always be the same as...
8
by: APEJMAN | last post by:
hI Would you please help me with this question? Using Big O Notation, what is the running time of the level-order traversal ( the code below) in terms of the number of nodes N and the tree height...
2
by: slizorn | last post by:
hi guys, i need to make a tree traversal algorithm that would help me search the tree.. basically i need to read in a text file... shown below H H,E,L E,B,F B,A,C A,null,null c,null,D
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.