News for hackers. Stuff that matters.
Re:Space Requirements for Tree Traversal by mschuetz on Sunday October 09, 2016 @11:35AM (#12671650)
I believe "Iterative deepening depth-first search" should be mentioned since the result is equivalent to breadth-first search but the memory efficiency is basically the same as depth-first search. It also tends to be faster than breadth-first search due to the lower memory complexity.

https://en.wikipedia.org/wiki/Iterative_deepening_depth-firs...

Re:Space Requirements for Tree Traversal by eeojun on Monday October 10, 2016 @08:55AM (#12676766)
Oh boy, this is great. Thanks! I was not aware of this when I wrote the article/post. I started out by wanting to make a very generalised pair of expressions for the space requirements so I had to make very little assumptions and would be able compare it to efficiency gains by other algorithms/representations.

Again thanks, I'll try to include it this week end if I have the time.

Re:Space Requirements for Tree Traversal by Okkef on Sunday October 09, 2016 @12:34PM (#12671921)
I came here to say this :)
Re:Space Requirements for Tree Traversal by loeg on Sunday October 09, 2016 @04:44PM (#12673367)
The article claims that the space requirement for depth-first search of a binary tree is O(log N). This is incomplete for the broader category of binary trees.

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.

Re:Space Requirements for Tree Traversal by eeojun on Monday October 10, 2016 @08:49AM (#12676735)
Hi, original author here. I used "perfect" trees as I wanted to generalise it to trees of other "types", not just binary trees. But yes, you are correct that it is incomplete for the broader category of binary trees.
Re:Space Requirements for Tree Traversal by loeg on Monday October 10, 2016 @11:19AM (#12677543)
Other types of trees can also be unbalanced :-). E.g., the classic example of DFS is maze-solving, which is usually unbalanced.
Re:Space Requirements for Tree Traversal by rdtsc on Sunday October 09, 2016 @09:44PM (#12674541)
> iteratively using LIFO queues or FIFO stacks.

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?

Re:Space Requirements for Tree Traversal by blahshaw on Sunday October 09, 2016 @11:52PM (#12674924)
It does seem like the author swapped those two.
Re:Space Requirements for Tree Traversal by eeojun on Monday October 10, 2016 @08:49AM (#12676739)
Whoops, you're right. I've fixed it. Thanks for pointing it out!
Re:Space Requirements for Tree Traversal by orange_county on Sunday October 09, 2016 @12:48PM (#12671983)
If you want O(1) space complexity and you do not care about modifying the tree, you can do tree rotations essentially flattening out the tree into a singly linked list.
Re:Space Requirements for Tree Traversal by fpoling on Sunday October 09, 2016 @04:03PM (#12673128)
One does not need to flatten tree if tree modifications are allowed. Just use Deutsch-Schorr-Waite algorithm.
Re:Space Requirements for Tree Traversal by catnaroek on Sunday October 09, 2016 @01:18PM (#12672141)
Are you assuming that nodes have parent pointers?
Re:Space Requirements for Tree Traversal by orange_county on Sunday October 09, 2016 @01:32PM (#12672221)
You don't need parent pointers.

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

Re:Space Requirements for Tree Traversal by catnaroek on Sunday October 09, 2016 @01:54PM (#12672349)
Thanks!
Re:Space Requirements for Tree Traversal by eeojun on Monday October 10, 2016 @09:00AM (#12676791)
Hi, original author here. I did not expect so much discussion and awareness! Once again I have to stress that I wanted to make a very general pair of equations so I could compare the efficiency gains in other algorithms from the naive versions in an 'accurate' way. If anyone has any suggestions for the algorithms involved please let me know ([email protected]) and I'll see if it's suitable to put in. I apologise in advance if I take longer than usual to reply, A levels are a ton of work!

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.

Re:Space Requirements for Tree Traversal by faragon on Sunday October 09, 2016 @01:29PM (#12672212)
For Red-Black tree (balanced tree) preorder/inorder/postorder traversal, which involves depth first scan, 2 * log(n) additional space is required ([1]).

[1] Here is an example, look for st_tr_aux() function: https://github.com/faragon/libsrt/blob/master/src/stree.c

Re:Space Requirements for Tree Traversal by userbinator on Sunday October 09, 2016 @01:24PM (#12672177)
If the nodes have parent pointers, which does tend to be somewhat common, then depth-first can be done in constant space. I haven't implemented it before, but I believe breadth-first should also take constant space in this case.
Re:Space Requirements for Tree Traversal by catnaroek on Sunday October 09, 2016 @01:27PM (#12672196)
If the nodes have parent pointers, everything is paying extra O(n) space.
Re:Space Requirements for Tree Traversal by perfmode on Sunday October 09, 2016 @02:07PM (#12672426)
Space is already O(n)
Re:Space Requirements for Tree Traversal by junke on Sunday October 09, 2016 @01:42PM (#12672279)
Related: Morris Traversal (no parent link required).

https://en.wikipedia.org/wiki/Tree_traversal#Morris_in-order...

Trying to reimplement it without looking at the solution is fun.