Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

I'm pretty sure I screwed up the iterative example logically and in terms of efficient code flow, so please edit/comment with fixes. Wikipedia has a good article on tree traversal for better examples (most seem to be binary trees unfortunately). There's also a way to avoid using a stack if you maintain some extra pointers in the Node object as per this StackOverflow answerthis StackOverflow answer.

I'm pretty sure I screwed up the iterative example logically and in terms of efficient code flow, so please edit/comment with fixes. Wikipedia has a good article on tree traversal for better examples (most seem to be binary trees unfortunately). There's also a way to avoid using a stack if you maintain some extra pointers in the Node object as per this StackOverflow answer.

I'm pretty sure I screwed up the iterative example logically and in terms of efficient code flow, so please edit/comment with fixes. Wikipedia has a good article on tree traversal for better examples (most seem to be binary trees unfortunately). There's also a way to avoid using a stack if you maintain some extra pointers in the Node object as per this StackOverflow answer.

deleted 2 characters in body
Source Link
michael.bartnett
  • 7.6k
  • 1
  • 37
  • 45

Do the easy thing (recursion, or occasionally iteration, depending on the problem) first and build something more robust or performant as you need it, but also don't 100% depend on your method, be willing and able to switch if it's called for. I ran into this issue on Unity recently. Our tree by necessity had much more depth than breadth, so the iterative method cut out a 4 seconds of loading time (for our data and our processing, NOT A BENCHMARK) compared to recursive.

Do the easy thing (recursion, or occasionally iteration, depending on the problem) first and build something more robust or performant as you need it, but also don't 100% depend on your method, be willing and able to switch if it's called for. I ran into this issue on Unity recently. Our tree by necessity had much more depth than breadth, so the iterative method cut out a 4 seconds of loading time (for our data and our processing, NOT A BENCHMARK) compared to recursive.

Do the easy thing (recursion, or occasionally iteration, depending on the problem) first and build something more robust or performant as you need it, but also don't 100% depend on your method, be willing and able to switch if it's called for. I ran into this issue on Unity recently. Our tree by necessity had much more depth than breadth, so the iterative method cut out 4 seconds of loading time (for our data and our processing, NOT A BENCHMARK) compared to recursive.

added clarification about the 4 seconds bit
Source Link
michael.bartnett
  • 7.6k
  • 1
  • 37
  • 45

Do the easy thing (recursion, or occasionally iteration, depending on the problem) first and build something more robust or performant as you need it, but also don't 100% depend on your method, be willing and able to switch if it's called for. I ran into this issue on Unity recently. Our tree by necessity had much more depth than breadth, so the iterative method cut out a 4 seconds of loading time (for our data and our processing, NOT A BENCHMARK) compared to recursive.

Do the easy thing (recursion, or occasionally iteration, depending on the problem) first and build something more robust or performant as you need it, but also don't 100% depend on your method, be willing and able to switch if it's called for. I ran into this issue on Unity recently. Our tree by necessity had much more depth than breadth, so the iterative method cut out a 4 seconds of loading time compared to recursive.

Do the easy thing (recursion, or occasionally iteration, depending on the problem) first and build something more robust or performant as you need it, but also don't 100% depend on your method, be willing and able to switch if it's called for. I ran into this issue on Unity recently. Our tree by necessity had much more depth than breadth, so the iterative method cut out a 4 seconds of loading time (for our data and our processing, NOT A BENCHMARK) compared to recursive.

altered code, added wikipedia link; added 50 characters in body
Source Link
michael.bartnett
  • 7.6k
  • 1
  • 37
  • 45
Loading
Source Link
michael.bartnett
  • 7.6k
  • 1
  • 37
  • 45
Loading