Thursday, April 2, 2009

Binary Tree Traversal

There are 3 basic ways to traverse a Binary TREE:-
a) PreOder
b) InOrder
c) PostOrder

And,its very easy to define them recursively


a)Visit the Root
b)Visit the Left Sub tree in PreOrder
c)Visit the Right Sub tree in Preorder.

Same can be done for InOrder and Postorder where Root is visited at stage b) and stage c) respectively.

Here is a sample code
PreOrder ( Tree tree){


Writing recursive definition is easy,
writing iterative version is bit tricky.

You can find the iterative version of all three here. I have posted them at

Feel free to use it and send me your suggestion/comments/feedback.

Kapil Dalwani

No comments:

Post a Comment