Mirror a binary tree in constant time - algorithm

This is not a homework question. I heard that it is possible to mirror a binary tree i.e. flip it, in constant time. Is this really the case?

Sure, depending on your data structure, you would just do the equivalent of: instead of traversing down the left node and then the right node, you would traverse down the right node, and then the left node. This could be a parameter passed into the recursive function that traverses the tree (i.e. in C/C++, a bool bDoLeftFirst, and an if-statement that uses that parameter to decide which order to traverse the child nodes in).

Did you mean "invert binary tree", the problem which Max Howell could not solve and thus rejected by Google?
https://leetcode.com/problems/invert-binary-tree/
You can find solutions in the "discuss" section.

Related

Evaluating an expression tree without a stack

Is it possible to evaluate an expression tree (pre/postfix) without using a stack? Had this question while talking about trees in the algorithm class at school. My guess is no.
Yes, you can.
Do a breadth first traversal of the tree (like a search, but do through all the tree). You can do this with a vector/queue/list in a iterative way.
Once you are done you can go backwards through the list/vector/queue you generated in the previous step. At each point compute the value of the node in the list. Since you have all the children already visited (you are going backwards) all you have to do is to lookup their value and apply the instructions in the node.

While serializing BST why is in-order essential?

I learnt that, to retain the structure of a BST while serialiing it, one needs to store in-order and one of either pre-order or post-order notations of the tree.
What makes in-order notation essential?
Note: Rewrote the answer, the previous version was incorrect.
For a general binary tree (with unique elements) your statement would be correct. Consider these two inputs (not very prettily drawn ;-) ):
If you serialize these using in-order traversal, both yield ABC. Similar cases exist for the other traversal types.
So why is a combination of in-order and pre-order enough?
The serialized shape of pre-order is [root][left subtree][right subtree]. The root is easy to identify, but you don't know where the left subtree ends and the right subtree begins.
Now consider in-order serialized: [left subtree][root][right subtree]. You know what the root is (thanks to pre-order), so it is really easy to identify the left and right subtrees.
Note that this is still not enough if the weights are not unique. If in the above example we change B into A, both trees would yield [AAC] for both traversal types.
For binary search trees deserialization is much easier. Why? Well, every subtree has the property that the nodes in the left subtree are smaller than the root, whereas the nodes in the right subtree are bigger. Therefore, the pre-order serialization [root][left subtree][right subtree] can easily and unambiguously be parsed again. So, in conclusion, the person who told you that at least two serialization approaches are needed for a BST was mistaken (maybe he also forgot about the properties of a BST).
Storing BSTs in some sort of order while serializing likely makes it simpler to build upon retrieval. Imagine that you have your BST and just pick nodes at random to serialize and store. When retrieving, it will retrieve in the order stored and then after the fact, something would have to go through and connect all of the nodes. While that should be possible - all the information is there - it seems unnecessary. Each node is just sort of floating; the deserialization process/program has to maintain a list of all the nodes (or similar) while it walks through the list connecting piece by piece.
On the other hand, if you store them in some sort of prescribed order, it can build the tree while reading in each node - it knows where to connect the nodes since they are in order (for clarity: this doesn't imply the next node must be connected to the previously-read node, in the case of adjacent leaves; it's just much simpler to hop up enough levels to the appropriate branch). This should be faster, and potentially use less memory (no list/container while building).

Threaded Binary Search Trees Advantage

An explanation about Threaded Binary Search Trees (skip it if you know them):
We know that in a binary search tree with n nodes, there are n+1 left and right pointers that contain null. In order to use that memory that contain null, we change the binary tree as follows -
for every node z in the tree:
if left[z] = NULL, we put in left[z] the value of tree-predecessor(z) (i.e, a pointer to the node which contains the predecessor key),
if right[z] = NULL, we put in right[z] the value of tree-successor(z) (again, this is a pointer to the node which contains the successor key).
A tree like that is called a threaded binary search tree, and the new links are called threads.
And my question is:
What is the main advatage of Threaded Binary Search Trees (in comparison to "Regular" binary search trees).
A quick search in the web has told me that it helps to implement in-order traversal iteratively, and not recursively.
Is that the only difference? Is there another way we can use the threads?
Is that so meaningful advantage? and if so, why?
Recursive traversal costs O(n) time too, so..
Thank you very much.
Non-recursive in-order scan is a huge advantage. Imagine that somebody asks you to find the value "5" and the four values that follow it. That's difficult using recursion. But if you have a threaded tree then it's easy: do the recursive in-order search to find the value "5", and then follow the threaded links to get the next four values.
Similarly, what if you want the four values that precede a particular value? That's difficult with a recursive traversal, but trivial if you find the item and then walk the threaded links backwards.
The main advantage of Threaded Binary Search Trees over Regular one is in Traversing nature which is more efficient in case of first one as compared to other one.
Recursively traversing means you don't need to implement it with stack or queue .Each node will have pointer which will give inorder successor and predecessor in more efficient way , while implementing traversing in normal BST need stack which is memory exhaustive (as here programming language have to consider implementation of stack) .

Fair deletion of nodes in Binary Search Tree

The idea of deleting a node in BST is:
If the node has no child, delete it and update the parent's pointer to this node as null
If the node has one child, replace the node with its children by updating the node's parent's pointer to its child
If the node has two children, find the predecessor of the node and replace it with its predecessor, also update the predecessor's parent's pointer by pointing it to its only child (which only can be a left child)
the last case can also be done with use of a successor instead of predecessor!
It's said that if we use predecessor in some cases and successor in some other cases (giving them equal priority) we can have better empirical performance ,
Now the question is , how is it done ? based on what strategy? and how does it affect the performance ? (I guess by performance they mean time complexity)
What I think is that we have to choose predecessor or successor to have a more balanced tree ! but I don't know how to choose which one to use !
One solution is to randomly choose one of them (fair randomness) but isn't better to have the strategy based on the tree structure ? but the question is WHEN to choose WHICH ?
The thing is that is fundamental problem - to find correct removal algorithm for BST. For 50 years people were trying to solve it (just like in-place merge) and they didn't find anything better then just usual algorithm (with predecessor/successor removing). So, what is wrong with classic algorithm? Actually, this removing unbalances the tree. After several random operations add/remove you'll get unbalanced tree with height sqrt(n). And it is no matter what you choosed - remove successor or predecessor (or random chose beetwen these ways) - the result is the same.
So, what to choose? I'm guessing random based (succ or pred) deletion will postpone unbalancing of your tree. But, if you want to have perfectly balanced tree - you have to use red-black ones or something like that.
As you said, it's a question of balance, so in general the method that disturbs the balance the least is preferable. You can hold some metrics to measure the level of balance (e.g., difference from maximal and minimal leaf height, average height etc.), but I'm not sure whether the overhead worth it. Also, there are self-balancing data structures (red-black, AVL trees etc.) that mitigate this problem by rebalancing after each deletion. If you want to use the basic BST, I suppose the best strategy without apriori knowledge of tree structure and the deletion sequence would be to toggle between the 2 methods for each deletion.

How to get parent in a k-ary level order succint trie?

I'm implementing a level order succint trie and I wan't to be able for a given node to jump back to his parent.
I tried several combination of rank/level but I can't wrap my head around this one...
I'm using this article as a base documentation :
http://stevehanov.ca/blog/index.php?id=120
It explain how to traverse childs, but not how to go up.
Thanks to this MIT lecture (http://www.youtube.com/watch?v=1MVVvNRMXoU) I know this is possible (in constant time as stated at 15:50), but the speaker only explain it for binary trie (eg: using the formula select1(floor(i/2)) ).
How can I do that on a k-ary trie?
Well, I don't know what select1() is, but the other part (floor(i/2)) looks like the trick you would use in an array-embedded binary tree, like those described here. You would divide by 2 because every parent has exactly 2 children --> every level uses twice the space of the parent level.
If you don't have the same number of children in every node (excepting leafs and perhaps one node with less children), you can't use this trick.
If you want to know the parent of any given node, you will need to add a pointer to the parent in every node.
Though, since trees are generally traversed starting at the root and going down, the usual thing to do is to store, in an array, the pointers to the nodes of the path. At any given point, the parent of the current node is the previous element in the array. This way you don't need to add a pointer to the parent in every node.
I think I've found my answer. This paper of Guy Jacobson explains it in section 3.2 Level-order unary degree sequence.
parent(x){ select1(rank0(x)) }
Space-efficient Static Trees and Graphs
http://www.cs.cmu.edu/afs/cs/project/aladdin/wwwlocal/compression/00063533.pdf
This work pretty good, as long as you don't mess up your node numbering like I was.

Resources