0

I am not asking for a expression conversion

conversion from infix to prefix

I am just asking that for a BST, if the input is given in the form of prefix notation, i.e,preorder traversal of BST, Then how do i convert the sequence of values to infix notation, i.e Inorder traversal of a BST.

                 8
               /  \
              1    12
              \     /
               5   9
             /   \
            4     7
                 /
                6

for example the preorder traversal would give 8 1 5 4 7 6 12 9

how do i convert these sequence of values(inputs) to a inorder traversal expression 1 4 5 6 7 8 9 12.

AS in some cases inorder expression is easier to handle...

Community
  • 1
  • 1
Sumit Kumar Saha
  • 799
  • 1
  • 12
  • 25

2 Answers2

0

With a BST:
Prefix: Left subtree, node, right subtree.
Infix: Node, left subtree, right subtree.
Postfix: right subtree, left subtree, node.

The conversion depends on how you traverse the tree.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • I know that but i just wanted to know , if a sequence is given in prorder form , how do i convert the sequence of values into inorder form. – Sumit Kumar Saha Nov 15 '12 at 20:27
  • Put it into a sorted BST, with left-most leaf containing the first node and the right-most leaf containing the maximum value. Then traverse it. Otherwise re-order the nodes (organizing into a new tree often helps vs. inplace re-ordering). – Thomas Matthews Nov 15 '12 at 20:40
  • For your application, you should not tailor the tree to pre-order, but something more generic. – Thomas Matthews Nov 15 '12 at 20:41
  • BSTs can be in any order. Sorted BSTs can produce different results by how you traverse them. Take a pen and paper and try the different methods I posted. When "node" is encountered, write the number. – Thomas Matthews Nov 15 '12 at 20:57
0

Let

V = Vertex

L = Left Subtree

R = Right Subtree

Preorder = VLR

Inorder = LVR

Postorder = LRV

(Just switch order to fix and it's the same)

One way to put them back to make prefix values to infix values is to create a Binary Search Tree again with prefix values and do the Inorder traversal.

OR!

Just sort dude... (BST is always sorted if u squash them to a line (squash from top to bottom))

LarsChung
  • 703
  • 5
  • 10
  • 24