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

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

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.

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.

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,

Member Avatar for iamthwee

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?

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:

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.