0

I have seen the implemented before but cannot recall how...

Has anyone solved this problem before and care to share the implementation of a tree traversal algorithm that DOES NOT use recursion but a while and a for loop (or similar)???

Alex.Barylski
  • 2,843
  • 4
  • 45
  • 68
  • how do you want to traverse the tree? – Dany Khalife Jul 17 '13 at 13:44
  • 1
    depending on the method, using either a queue (for Breadth-First) or a stack (for Depth-First) will do – Dany Khalife Jul 17 '13 at 13:44
  • Also, a few implementation details would be nice. Just traverse, print, count, etc? – Robert DeBoer Jul 17 '13 at 13:46
  • http://stackoverflow.com/questions/5278580/non-recursive-depth-first-search-algorithm – Dave Jul 17 '13 at 13:52
  • This problem is one of those ones that's intrinsically easier solved with recursion. If you did it iteratively you'd need a data structure to keep track of the position in the tree. A recursive solution would basically work as a stack that keeps track of the location on the tree (assuming depth-first), so a recursive solution could be mimicked with a stack. – Zip184 Jul 17 '13 at 13:52
  • @Dany: That is essentially the answer I was looking for to confirm my suspicions :) – Alex.Barylski Jul 17 '13 at 14:20

0 Answers0