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
- traverse the Left SubTree of R in InOrder
- process the root R
- traverse the Right SubTree of R in InOrder
- 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:
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 - / *
Lovely blog thanks for taking the time to share this.
ReplyDelete