473,624 Members | 2,471 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Post order traversal non recursive

18 New Member
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, 3378 views)
Attached Files
File Type: zip POST_ORD_C.zip (1.5 KB, 1044 views)
File Type: zip Post_ord_exe.zip (32.1 KB, 403 views)
File Type: zip post_order_jpg.zip (19.6 KB, 377 views)
File Type: zip run_in_dos_jpeg.zip (5.7 KB, 236 views)
Nov 2 '11 #1
0 11150

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

Similar topics

2
4603
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 post-order traversal of the DOM tree. This will let me start at the bottom of the tree, which will be the deepest selections in the menu, and add the leaves(JMenuItem's) to the parents(JMenu's) and those parents to the root(JPopupMenu). Get the idea?...
2
13877
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 by Morris for inorder traversal of a Binary Tree without using explicit stack. Searched on net but could not find many clues.
6
4064
by: GrispernMix | last post by:
//ques and and level order traversal file name: lab6_build_leaf_up.cpp Instructions:
7
2829
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
15241
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
3131
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! this is my function is it correct? void doubleOrdrerTraversal(Tree *root){ if (root == NULL) return; else{ visit(root); doubleOrdrerTraversal(root -> left); visit(root)
6
20009
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 are printed on a separate line. my codes are below,( for printing inorder, preorder and post order) I have no Idea how I can print them in , level-order traversal I think I should use a Queue, but how? do you have any code that can help me? would...
4
3980
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 the original tree. Is it true that the pre-order traversal tells the order in which the values were inserted?
8
4180
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 h? Based on your knowledge of the possible values of h, what are the average, best, and worst running times in terms of just N? would you please explain to me? void Tree<T> :: level_order_traversal(std::ostream& out, TreeNode<T>* rootNode) { ...
2
3805
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
8246
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8685
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8341
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8490
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7174
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4184
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.