Sunday 28 July 2013

various standard ways of Binary Tree Traversal:

3 standard ways are out there for traversing a Binary Tree. these ways or ( you may call it Algorithms) are given billow:
  • PreOrder
  • InOrder
  • PostOrder

working process: if the tree is T with root R:

PreOrder:
  • process the root R
  • traverse the Left SubTree of R in PreOrder
  • traverse the Right SubTree of R in PreOrder
InOrder:
  •  traverse the Left SubTree of R in InOrder
  •  process the root R
  •  traverse the Right SubTree of R in InOrder
PostOrder:
  • traverse the Left SubTree of R in PostOrder
  • traverse the Right SubTree of R in PostOrder
  • process the root R

example:
lets consider an  mathematical equation 
       [ a + ( b - c ) ] * [ ( d - e ) / ( f + g -h ) ]
now the orders will be like these:
PreOrder   :        * + a - b c / - d e - + f g h 
InOrder     :        a + b - c * d - e / f  + g - h
PostOrder  :        a b c - + d e - f g + h - / * 



1 comment: