Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a balanced binary search tree from a stream of integers

I have just finished a job interview and I was struggling with this question, which seems to me as a very hard question for giving on a 15 minutes interview.

The question was: Write a function, which given a stream of integers (unordered), builds a balanced search tree. Now, you can't wait for the input to end (it's a stream), so you need to balance the tree on the fly.

My first answer was to use a Red-Black tree, which of course does the job, but i have to assume they didn't expect me to implement a red black tree in 15 minutes.

So, is there any simple solution for this problem i'm not aware of?

Thanks,

Dave

like image 763
Dave Avatar asked Aug 29 '11 21:08

Dave


1 Answers

I personally think that the best way to do this would be to go for a randomized binary search tree like a treap. This doesn't absolutely guarantee that the tree will be balanced, but with high probability the tree will have a good balance factor. A treap works by augmenting each element of the tree with a uniformly random number, then ensuring that the tree is a binary search tree with respect to the keys and a heap with respect to the uniform random values. Insertion into a treap is extremely easy:

  1. Pick a random number to assign to the newly-added element.
  2. Insert the element into the BST using standard BST insertion.
  3. While the newly-inserted element's key is greater than the key of its parent, perform a tree rotation to bring the new element above its parent.

That last step is the only really hard one, but if you had some time to work it out on a whiteboard I'm pretty sure that you could implement this on-the-fly in an interview.

Another option that might work would be to use a splay tree. It's another type of fast BST that can be implemented assuming you have a standard BST insert function and the ability to do tree rotations. Importantly, splay trees are extremely fast in practice, and it's known that they are (to within a constant factor) at least as good as any other static binary search tree.

Depending on what's meant by "search tree," you could also consider storing the integers in some structure optimized for lookup of integers. For example, you could use a bitwise trie to store the integers, which supports lookup in time proportional to the number of bits in a machine word. This can be implemented quite nicely using a recursive function to look over the bits, and doesn't require any sort of rotations. If you needed to blast out an implementation in fifteen minutes, and if the interviewer allows you to deviate from the standard binary search trees, then this might be a great solution.

Hope this helps!

like image 91
templatetypedef Avatar answered Sep 27 '22 20:09

templatetypedef