473,406 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

postorder traversal of binary search tree without recursion

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

Apr 19 '07 #1
9 15226
ni**********@st.com wrote:
Can somebody please help me for algorithm for postorder bst traversal
without recursion.
It should use stack. i am not able to implement it.
What have you tried?

--
Ian Collins.
Apr 19 '07 #2
for inorder
do{
while(p is not equal to NULL){
push it on stack
p = p->left
}
pop(s)
print info(p)
p=p->right
}while(!empty(s))

for preorder it needs little modification.
But i think for post order tree structure should be modified with a
flag comtaining info that node has right or left child or both!
but m not able to think how to proceed???
Ian Collins wrote:
ni**********@st.com wrote:
Can somebody please help me for algorithm for postorder bst traversal
without recursion.
It should use stack. i am not able to implement it.

What have you tried?

--
Ian Collins.
Apr 19 '07 #3

nishit.gu...@st.com wrote:
for inorder
do{
while(p is not equal to NULL){
push it on stack
p = p->left
}
pop(s)
print info(p)
p=p->right
}while(!empty(s))

for preorder it needs little modification.
But i think for post order tree structure should be modified with a
flag comtaining info that node has right or left child or both!
but m not able to think how to proceed???
Ian Collins wrote:
ni**********@st.com wrote:
Can somebody please help me for algorithm for postorder bst traversal
without recursion.
It should use stack. i am not able to implement it.
What have you tried?

--
Ian Collins.
ohh sorry for top post!!

Apr 19 '07 #4
On 19 Apr, 07:40, nishit.gu...@st.com wrote:
Can somebody please help me for algorithm for postorder Â*bst traversal
without recursion.
It should use stack. i am not able to implement it.
from wiki:

postorder(node)
if node.left â‰* null then postorder(node.left)
if node.right â‰* null then postorder(node.right)
print node.value

this took me about 10s to find
--
Nick Keighley

Apr 19 '07 #5
On Apr 19, 2:01 pm, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote:
On 19 Apr, 07:40, nishit.gu...@st.com wrote:
Can somebody please help me for algorithm for postorder bst traversal
without recursion.
It should use stack. i am not able to implement it.

from wiki:

postorder(node)
if node.left â‰* null then postorder(node.left)
if node.right â‰* null then postorder(node.right)
print node.value

this took me about 10s to find

--
Nick Keighley
but this is done by recursion.
the problem here is to do it without recursion.

Apr 19 '07 #6

Nick Keighley wrote:
On 19 Apr, 07:40, nishit.gu...@st.com wrote:
Can somebody please help me for algorithm for postorder bst traversal
without recursion.
It should use stack. i am not able to implement it.

from wiki:

postorder(node)
if node.left â‰* null then postorder(node.left)
if node.right â‰* null then postorder(node.right)
print node.value

this took me about 10s to find
--
Nick Keighley
Thanks for your efforts ;-) but u used recursion
****************************************
with recursion, for preorder :
-------------------------
preorder(node){
if(node){
print info(node)
preorder(node->left)
preorder(node->right)
}
with recursion, for postorder :
-------------------------
postorder(node){
if(node){

postorder(node->left)
postorder(node->right)
print(info(node))
with recursion, for postorder :
-------------------------
inorder(node){
if(node){

inorder(node->left)
inorder(info(node))
inorder(node->right)
}
**********************************

i hope it clrifies the intent...
i need the same output without any recursive call
it must be same as i have done above for inorder,
preorder just needs a little modification
for postorder one needs to store some info about left/rigth child
but i am not sure what???

Apr 19 '07 #7
Nick Keighley wrote:
On 19 Apr, 07:40, nishit.gu...@st.com wrote:
>Can somebody please help me for algorithm for postorder bst traversal
without recursion.
It should use stack. i am not able to implement it.

from wiki:

postorder(node)
if node.left â‰* null then postorder(node.left)
if node.right â‰* null then postorder(node.right)
print node.value

this took me about 10s to find
And it looks like recursion, forbidden by the given conditions.
Apr 19 '07 #8
On Apr 19, 11:40 am, nishit.gu...@st.com wrote:
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
looks like this algorithm will work.

for each node there are 2 states.
1)wait state
2)process state

initialise stack to the (root,wait)
while(stack is not empty)
{
pop an element from stack;
if (state=process)
then print the node;
else(i.e state is wait)
{
push the (element,process);
if(node.right!=NULL)push(node->right,wait);
if(node.left!=NULL)push(node->left,wait);
}
}
this algorithm should work.
i have not written all the details but this is the basic algorithm.
if there are any loopholes,please tell me.
thank you.

Apr 20 '07 #9
On Apr 20, 3:24 am, saki <sakethstud...@gmail.comwrote:
On Apr 19, 11:40 am, nishit.gu...@st.com wrote:
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

looks like this algorithm will work.

for each node there are 2 states.
1)wait state
2)process state

initialise stack to the (root,wait)
while(stack is not empty)
{
pop an element from stack;
if (state=process)
then print the node;
else(i.e state is wait)
{
push the (element,process);
if(node.right!=NULL)push(node->right,wait);
if(node.left!=NULL)push(node->left,wait);
}

}

this algorithm should work.
i have not written all the details but this is the basic algorithm.
if there are any loopholes,please tell me.
thank you.
yessss....algo is correct....basically login is OK

Apr 20 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: j | last post by:
Hi, Anyone out there with binary search tree experience. Working on a project due tomorrow and really stuck. We need a function that splits a binary tree into a bigger one and smaller one(for a...
2
by: dannielum | last post by:
Hi all, I am trying to write a Binary Search Tree that each of its node will have 3 node pointers: left, right and parent. I need a parent pointer for some the purpose of my project. Without the...
4
by: Tarique Jawed | last post by:
Alright I needed some help regarding a removal of a binary search tree. Yes its for a class, and yes I have tried working on it on my own, so no patronizing please. I have most of the code working,...
7
by: sugaray | last post by:
the binary search tree node here contains another structure as it's data field, programs did successfully work when data field is int, char, this time i got stucked, don't know why ? if there's...
1
by: mathon | last post by:
hi, now i facing a problem which i do not know how to solve it...:( My binary search tree structures stores a double number in every node, whereby a higher number is appended as right child...
1
by: hn.ft.pris | last post by:
I have the following code: Tree.h defines a simple binary search tree node structure ########## FILE Tree.h ################ #ifndef TREE_H #define TREE_H //using namespace std; template...
5
gekko3558
by: gekko3558 | last post by:
I am writing a simple binary search tree (nodes are int nodes) with a BSTNode class and a BST class. I have followed the instructions from my C++ book, and now I am trying to get a remove method...
11
by: Defected | last post by:
Hi, How i can create a Binary Search Tree with a class ? thanks
17
by: AceKnocks | last post by:
I have been given this interface, interface BinarySearchTree { public void insert(Integer data); public int size(); public int height(); public boolean contains(Integer target); }
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
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...

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.