954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

recursive algorithm

hi, can someone help me with writing down a recursive algorithm for traversing a binary tree in reverse-postorder that also returns the rank of each node? So far i have (and i know thatit isnt much but this problem giving me issues).

postorder:
save current posistion
      if(lchild)
                move to lchild
                postorder
                if(sibling)
                            move to sibling
                            postorder
                 endif
      endif
      restore saved position
      visit//do something with the node


i hope that someone can give me some serious help thanks in advance

bajanstar
Newbie Poster
7 posts since Nov 2004
Reputation Points: 18
Solved Threads: 0
 

What language is this, Basic? This is the C/C++ forum, please ask your question in the forum for the appropiate lanuage.

Anonymusius
Posting Whiz in Training
238 posts since Aug 2006
Reputation Points: 129
Solved Threads: 11
 

no its just an algorithm for the problem. Im suppose to be writing it in C or C++ but i decided to write the algorithm first. But im having problems with it.So any one who can help me solve this problem i would greatly appreciate it.

bajanstar
Newbie Poster
7 posts since Nov 2004
Reputation Points: 18
Solved Threads: 0
 

What do you exactly mean by reverse postorder? In postorder the value is accessed after accessign all the left and the right children or branches.
So if you want reverse then it must mean preorder traversal.

[quote]
Preorder:

1. Print out the value and save the node or value to stack.
2. If left child not null call Preorder (left)
3. If right child not null call Preorder (right).

If this is not your problem stmt then restate it with examples.
Best of luck,

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 
So if you want reverse then it must mean preorder traversal.

It seems odd that he would state preorder as being reverse post order. Why not just as for a preorder traversal algo. But than nothing surprises me these days so you could be right?

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 
It seems odd that he would state preorder as being reverse post order. Why not just as for a preorder traversal algo. But than nothing surprises me these days so you could be right?

Yes its possible I may be wrong but lookign from his code and the limited senario he has painted in front of me, i thought maybe he didnt know about preorder and needed some direction. But then again you cant know the mind of these young developers, maybe he wouldb be the first one to write a technical paper on Reverse Postorder :mrgreen:

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

Reverse postorder means you traverse each node's children from right to left.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You