1

Is it possible to traverse a tree structure (specifically an octree, the 3-D version of a binary tree) by using a fixed sized stack? I do not want to use recursion, since my octree is quite deep.

I am traversing the tree to do a range search problem, to find all the points closest to a queried point. So in my traversal, I do not walk down those subtrees rooted at nodes which my search region does not intersect.

2
  • 2
    possible duplicate of Traverse tree without recursion and stack in C Commented Jan 8, 2012 at 17:21
  • I guessed that you want to avoid the recursion because of practical limitation on the machine's call stack size. Commented Jan 8, 2012 at 17:44

3 Answers 3

3

If your octree has parent pointers, I think you can traverse it without a stack at all (see this thread, for example). Without that, you will need a stack that is as deep as the depth of your tree, regardless of how many branches are skipped.

Sign up to request clarification or add additional context in comments.

Comments

2

Of course you can traverse a tree without using a deep native call stack, using continuation passing style techniques, or (and this is grossly the same) by making a virtual machine, with its call stack implemented as a heap data, or (yet another point of view) by coding a stack automata with the stack implemented as an explicit heap data structure (e.g. a std::stack).

Think of it otherwise, your C++ naive code could run on a Turing machine, and these beasts don't have any stack.

As Ted Hopp's answer suggests, you might be inspired by Deutsch-Schorr-Waite's Garbage Collection techniques (with a few additional bits per node to temporarily flip the reference direction and remember that) to have a "stack-less" traversal (but you need additional bits in each node). But I believe that having your own stack inside a std::stack or std::vector is probably simpler.

2 Comments

"A rose by any other name..." Continuation passing style uses the stack. Using the heap to implement a stack is still a stack. Simulating a stack on a Turing machine is still using a stack. Also, OP seems to be asking about any stack, not the call stack specifically. The concern seems to be extra memory usage, not avoiding a structure explicitly called "stack".
I agree, I never thought otherwise. I just spoke of avoiding a deep native call stack; and my understanding of "avoid deep recursion" is only about the machine stack.
0

Yes, you can traverse the octree with a fixed-size stack.

The fixed-size just needs to be as big as the longest possible octree depth.

Bear in mind that with an octree, each depth traversal can be recorded with only 3 bits of memory. For each of the three dimensions, you only need to record whether you went in a positive or negative direction.

So even if your octree goes 1000-deep, you can store the recursion with 375 bytes.

2 Comments

That's not a fixed-size stack. For any fixed N, there will be octrees so deep that recursion cannot be stored in N bytes. (10^-1000)*log(N) is still O(log N).
@TedHopp: Your math is correct, but "there will be octrees so deep" is a theoretical postulate, no? This seems to be a real-world problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.