The Great Tree-List Recursion Problem (2000)
cslibrary.stanford.edu
cslibrary.stanford.edu
You'd start by counting the number of nodes in your circular array O(N), and making an array of pointers to each node O(N), then finding the middle node, which is the root node at count / 2. You'd then re-link the nodes for a sub-array and recurse on both sides using your in-order array of pointers, and replacing the smaller and larger node pointers with the pointers from the array. The total recursive algorithm visits each node in the new tree only once.
The Great Tree-List Recursion Problem - https://news.ycombinator.com/item?id=19324604 - March 2019 (21 comments)
The Great Tree-List Recursion Problem (2000) - https://news.ycombinator.com/item?id=15347519 - Sept 2017 (38 comments)
So this is my amended problem: convert the tree to a same-ordered doubly-linked list without using recursion or an explicit stack.
1. The double-linking and circularity make the solution uglier and more fiddly without making it really conceptually any harder. In particular, it means you end up mixing recursion with mutation, which is a little gunky. I wish they'd just have you output a single-linked list: same bang, less buck.
2. The problem mentions that the double-linked list nodes look a lot like the tree nodes, in the sense that they're a value with two pointers, but in practice that didn't have any effect on the solution, and I'm not really sure it's very interesting.
3. A thing that always comes up in these kinds of recursion problems is where to put the null checks, which in this case are a little complicated. I think neither my solution and the Java one in the link do a great job on that front
[1] https://gist.github.com/icambron/4c03e1b19c3ca795d3930ed1720...
class BinaryTree:
def __init__(self, data: List):
""" create from a list """
def asList(self)->List:
""" return my data as a list """
class DoubleLinkedList:
def __init__(self, data: List):
""" create from a list """
def asList(self)->List:
""" return my data as a list """
Then making a binary tree from a double-linked list is simply: BinaryTree(dll.asList())The practical use case for in-place algorithms is operating on large data sets that are directly on a disk. Think some kind of database organizing data directly on a hard drive. You want to avoid having to double the number of hard drives you need in order to do some kind of data transformation, and perform any linking/relinking directly on the disk.
If there's a way of doing it that's trivial, and another way that's complex, I will do it the trivial way unless there's a very good reason to do it the complex way.
> The practical use case for in-place algorithms is operating on large data sets that are directly on a disk. Think some kind of database organizing data directly on a hard drive.
I just hope the system doesn't crash while it's in the middle of doing it!