Tuesday, March 06, 2012

Non-Recursive Binary Tree Traversal Java Implementation

Now that I have more time to think about this, I have finally completed my own solution to the problem... another entry to the long (and hopefully not erroneous) list of "solutions" listed in Google. My inspiration to write my own implementation was when I was Googling for a proper solution, some solutions in the net were actually a bit dodgy and erroneous (especially when it is about the tricky non-recursive post order traversal of a binary tree).

Problem: Given a binary tree, formulate an algorithm to traverse the nodes of the binary tree without using recursion for the following modes:
  • pre-order traversal
  • in-order traversal
  • post-order traversal

Now, before we go any further, you may ask me why is this of paramount importance to find a proper non-recursive solution to this academic problem. And my response is... well it just makes me sleep well tonight blogging about this. ;) (Also hoping that Google would take notice and ahem... check out my CV).

So here goes:





No comments: