- Self-balancing binary search tree
computer science, a self-balancing binary search tree or height-balanced binary search tree is a binary search treethat attempts to keep its "height", or the number of levels of nodes beneath the root, as small as possible at all times, automatically. It is one of the most efficient ways of implementing ordered lists and can be used for other data structures such as associative arraysand sets.
Most operations on a binary search tree take time directly proportional to the tree's height, so it is desirable to keep the height small. Ordinary
binary search trees have the primary disadvantage that they can attain very large heights in rather ordinary situations, such as when the keys are inserted in order. The result is a data structure similar to a linked list, making all operations on the tree expensive. If we know all the data ahead of time, we can keep the height small on average by adding values in a random order, but we don't always have this luxury, particularly in online algorithms.
Self-balancing binary trees solve this problem by performing transformations on the tree (such as
tree rotations) at key times, in order to reduce the height. Although a certain overheadis involved, it is justified in the long run by drastically decreasing the time of later operations.
The height must always be at least the ceiling of "log2 n", since there are at most 2"k" nodes on the "k"th level; a "complete" or "full" binary tree has exactly this many levels. Balanced BSTs are not always so precisely balanced, since it can be expensive to keep a tree at minimum height at all times; instead, most algorithms keep the height within a constant factor of this lower bound.
Times for various operations in terms of number of nodes in the tree "n":For some implementations these times are worst-case, while for others they are amortized.
Popular data structures implementing this type of tree include:
Self-balancing binary search trees can be used in a natural way to construct and maintain
ordered lists, such as priority queues.
They can also be used for
associative arrays; key-value pairs are simply inserted with an ordering based on the key alone. In this capacity, self-balancing BSTs have a number of advantages and disadvantages over their main competitor, hash tables. Lookup is somewhat complicated in the case where the same key can be used multiple times.
Many algorithms can exploit self-balancing BSTs to achieve good worst-case bounds with very little effort. For example, if
binary tree sortis done with a BST, we have a very simple-to-describe yet asymptotically optimalO("n" log "n") sorting algorithm (although such an algorithm has practical disadvantages due to bad cache behavior). Similarly, many algorithms in computational geometryexploit variations on self-balancing BSTs to solve problems such as the line segment intersectionproblem and the point locationproblem efficiently.
Self-balancing BSTs are a flexible data structure, in that it's easy to extend them to efficiently record additional information or perform new operations. For example, one can record the number of nodes in each subtree having a certain property, allowing one to count the number of nodes in a certain key range with that property in O(log "n") time. These extensions can be used, for example, to optimize database queries or other list-processing algorithms.
* [http://www.nist.gov/dads/HTML/heightBalancedTree.html Dictionary of Algorithms and Data Structures: Height-balanced binary search tree]
Donald Knuth. "The Art of Computer Programming", Volume 3: "Sorting and Searching", Third Edition. Addison-Wesley, 1997. ISBN 0-201-89685-0. Section 6.2.3: Balanced Trees, pp.458–481.
Wikimedia Foundation. 2010.