Reconstructing a binary tree from RTAGs and LTAGS

13lade619

is now a game developer :)
Reaction score
399
Not necessarily a code related question, more on the algorithm.

How do you reconstruct a binary tree with the given?

0 denotes that it doesnt have a son
1 denotes that it has a son.

Code:
Postorder

NODE ABCDEFGHIJKL
LTAG 010010110010
RTAG 000010111011

from that it means that the root node (since it's in postorder) is L has no left son but has a right son
 

phyrex1an

Staff Member and irregular helper
Reaction score
447
What would the correct tree be for that input?
Here is one possible (I hope...) algo:

Input: [(ltag, rtag)]
Code:
for each e in input
    n = new node
    if e.ltag then n.left = pop_stack
    if e.rtag then n.right = pop_stack
    push_stack n
end
return pop_stack
You could change the stack to a queue and get another tree.
 

saw792

Is known to say things. That is all.
Reaction score
280
---L-----------
----\----------
-----K---------
----/--\--------
---J----I-------
---------\------
----------H-----
---------/--\----
--------G----F---
-------/--\------
------E----D-----
-----/--\---------
----C----B-------
--------/---------
-------A----------

That looks like the correct solution. I started from the root node. Not sure if that will help you with the algorithm or not.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top