Space Requirements for Tree Traversal
eugene-eeo.github.io
eugene-eeo.github.io
https://en.wikipedia.org/wiki/Iterative_deepening_depth-firs...
Again thanks, I'll try to include it this week end if I have the time.
The space requirement for depth-first search of a non-balanced binary tree is pretty clearly O(N), not O(log N). (Consider the "linked list" tree.)
For balanced trees, O(log N) is correct. This is because space required for depth-first search is identical to maximum tree depth. And balanced trees bound depth at O(log N), while non-balanced trees can be O(N) deep.
While the article leaves out the mention of the term balanced, the described "perfect" trees happen to be balanced.
Aren't queues FIFO and stacks LIFO? Or am I just confused and there is some FIFO stack thing I haven't heard of yet?
For converting binary tree into a linked list in O(n) time and O(1) space.
You have two pointers, the root and the tail (right child node).
If root has a left subtree, set tail->right = left subtree. Update tail again so it is the last right child node.
root = root->right
Repeat until root is NULL
Also I was originally going to see how this applied to traversing really large, cross machine trees so I did not consider the algorithms that mutated the tree, but thanks for making me aware of those.
[1] Here is an example, look for st_tr_aux() function: https://github.com/faragon/libsrt/blob/master/src/stree.c
https://en.wikipedia.org/wiki/Tree_traversal#Morris_in-order...
Trying to reimplement it without looking at the solution is fun.