Creating Binary Tree from a given Postorder/Inorder/Preorder traversal data 05-27-2014, 02:34 PM
#1
Suppose we have a sequence of nodes of a binary tree in Preorder and Inorder traversal scheme:
[table]
[row]
[cell]PreOrder [/cell]
[cell]G [/cell]
[cell]B [/cell]
[cell]Q [/cell]
[cell]A [/cell]
[cell]C [/cell]
[cell]K [/cell]
[cell]F [/cell]
[cell]P [/cell]
[cell]D [/cell]
[cell]E [/cell]
[cell]R [/cell]
[cell]H [/cell]
[/row]
[row]
[cell]InOrder [/cell]
[cell]Q [/cell]
[cell]B [/cell]
[cell]K [/cell]
[cell]C [/cell]
[cell]F [/cell]
[cell]A [/cell]
[cell]G [/cell]
[cell]P [/cell]
[cell]E [/cell]
[cell]D [/cell]
[cell]H [/cell]
[cell]R [/cell]
[/row]
[/table]
I've got to construct a Binary Tree using the Traversal Data above. I'm sure G is the Root node, as its the first element in Preorder traversal. But I can't figure out how to proceed finding the Left and Right Child. I don't need any code to implement this. All I need is the logic to do this. Thanks
[table]
[row]
[cell]PreOrder [/cell]
[cell]G [/cell]
[cell]B [/cell]
[cell]Q [/cell]
[cell]A [/cell]
[cell]C [/cell]
[cell]K [/cell]
[cell]F [/cell]
[cell]P [/cell]
[cell]D [/cell]
[cell]E [/cell]
[cell]R [/cell]
[cell]H [/cell]
[/row]
[row]
[cell]InOrder [/cell]
[cell]Q [/cell]
[cell]B [/cell]
[cell]K [/cell]
[cell]C [/cell]
[cell]F [/cell]
[cell]A [/cell]
[cell]G [/cell]
[cell]P [/cell]
[cell]E [/cell]
[cell]D [/cell]
[cell]H [/cell]
[cell]R [/cell]
[/row]
[/table]
I've got to construct a Binary Tree using the Traversal Data above. I'm sure G is the Root node, as its the first element in Preorder traversal. But I can't figure out how to proceed finding the Left and Right Child. I don't need any code to implement this. All I need is the logic to do this. Thanks
![[Image: MUJ8qSW.png]](http://i.imgur.com/MUJ8qSW.png)
-----------------------------------
Now learning:
Android Development, Java
Working on:
An FTP Client for Android
-----------------------------------


![[+]](https://sinister.li/images/modern/collapse_collapsed.png)
![[Image: OilyCostlyEwe.gif]](http://fat.gfycat.com/OilyCostlyEwe.gif)