Optimal binary search trees for successor lookup?

2019-09-15 06:00发布

问题:

There are many algorithms for finding optimal binary search trees given a set of keys and the associated probabilities of those keys being chosen. The binary search tree produced this way will have the lowest expected times to look up those elements. However, this binary search tree might not be optimal with regards to other measures. For example, if you attempt to look up a key that is not contained in the tree, the lookup time might be very large, as the tree might be imbalanced in order to optimize lookups of certain elements.

I am currently interested in seeing how to build an binary search tree from a set of keys where the goal is to minimize the time required to find the successor of some particular value. That is, I would like the tree to be structured in a way where, given some random key k, I can find the successor of k as efficiently as possible. I happen to know in advance the probability that a given random key falls in-between any two of the keys the tree is constructed from.

Does anyone know of an algorithm for this problem? Or am I mistaken that the standard algorithm for building optimal binary search trees will not produce efficient trees for this use case?

回答1:

So now I feel silly, because there's an easy answer to this question. :-)

You use the standard, off-the-shelf algorithm for constructing optimal binary search trees to construct a binary search tree for the set of keys. You then annotate each node so that it stores the entire range between its key and the key before it. This means that you can find the successor efficiently by doing a standard search on the optimally-built tree. If at any point the key you're looking for is found to be contained in a range held in some node, then you're done. In other words, finding the successor is equivalent to just doing a search for the value in the BST.