Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Self-balancing binary search tree
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{short description|Any node-based binary search tree that automatically keeps its height the same}} {{refimprove|date=November 2010}} [[Image:Unbalanced binary tree.svg|thumb|right|251px|An example of an '''unbalanced''' tree; following the path from the root to a node takes an average of 3.27 node accesses]] [[Image:AVLtreef.svg|thumb|right|251px|The same tree after being height-balanced; the average path effort decreased to 3.00 node accesses]] In [[computer science]], a '''self-balancing binary search tree''' (BST) is any [[node (computer science)|node]]-based [[binary search tree]] that automatically keeps its height (maximal number of levels below the root) small in the face of arbitrary item insertions and deletions.<ref name="knuth">[[Donald Knuth]]. ''[[The Art of Computer Programming]]'', Volume 3: ''Sorting and Searching'', Second Edition. Addison-Wesley, 1998. {{ISBN|0-201-89685-0}}. Section 6.2.3: Balanced Trees, pp.458–481.</ref> These operations when designed for a self-balancing binary search tree, contain precautionary measures against boundlessly increasing tree height, so that these [[abstract data type|abstract data structures]] receive the attribute "self-balancing". For '''height-balanced''' binary trees, the height is defined to be logarithmic <math>O(\log n)</math> in the number <math>n</math> of items. This is the case for many binary search trees, such as [[AVL tree]]s and [[red–black tree]]s. [[Splay tree]]s and [[treap]]s are self-balancing but not height-balanced, as their height is not guaranteed to be logarithmic in the number of items. Self-balancing binary search trees provide efficient implementations for mutable ordered [[list (computing)|lists]], and can be used for other abstract data structures such as [[associative array]]s, [[priority queue]]s and [[set (abstract data type)|sets]]. == Overview == [[File:BinaryTreeRotations.svg|thumb|300px|Tree rotations are very common internal operations on self-balancing binary trees to keep perfect or near-to-perfect balance.]] Most operations on a binary search tree (BST) take time directly proportional to the height of the tree, so it is desirable to keep the height small. A binary tree with height ''h'' can contain at most [[Geometric series#Closed-form formula|2<sup>0</sup>+2<sup>1</sup>+···+2<sup>''h''</sup> = 2<sup>''h''+1</sup>−1]] nodes. It follows that for any tree with ''n'' nodes and height ''h'': :<math>n\le 2^{h+1}-1</math> And that implies: :<math>h\ge\lceil\log_2(n+1)-1\rceil\ge \lfloor\log_2 n\rfloor</math>. In other words, the minimum height of a binary tree with ''n'' nodes is {{nowrap|[[logarithm|log]]<sub>2</sub>(''n''),}} [[floor and ceiling functions|rounded down]]; that is, <math> \lfloor \log_2 n\rfloor</math>.<ref name="knuth"/> However, the simplest algorithms for BST item insertion may yield a tree with height ''n'' in rather common situations. For example, when the items are inserted in sorted [[key (database)|key]] order, the tree degenerates into a [[linked list]] with ''n'' nodes. The difference in performance between the two situations may be enormous: for example, when ''n'' = 1,000,000, the minimum height is <math> \lfloor \log_2(1,000,000) \rfloor = 19 </math>. If the data items are known ahead of time, the height can be kept small, in the average sense, by adding values in a random order, resulting in a [[random binary search tree]]. However, there are many situations (such as [[online algorithm]]s) where this [[randomized algorithm|randomization]] is not viable. Self-balancing binary trees solve this problem by performing transformations on the tree (such as [[tree rotation]]s) at key insertion times, in order to keep the height proportional to {{nowrap|log<sub>2</sub>(''n'').}} Although a certain [[Computational overhead|overhead]] is involved, it is not bigger than the always necessary lookup cost and may be justified by ensuring fast execution of all operations. While it is possible to maintain a BST with minimum height with expected <math>O(\log n)</math> time operations (lookup/insertion/removal), the additional space requirements required to maintain such a structure tend to outweigh the decrease in search time. For comparison, an [[AVL tree]] is guaranteed to be within a factor of 1.44 of the optimal height while requiring only two additional bits of storage in a naive implementation.<ref name="knuth"/> Therefore, most self-balancing BST algorithms keep the height within a constant factor of this lower bound. In the [[asymptotic]] ("[[Big O notation|Big-O]]") sense, a self-balancing BST structure containing ''n'' items allows the lookup, insertion, and removal of an item in <math>O(\log n)</math> worst-case time, and [[in-order iteration|ordered enumeration]] of all items in <math>O(n)</math> time. For some implementations these are per-operation time bounds, while for others they are [[amortized analysis|amortized]] bounds over a sequence of operations. These times are asymptotically optimal among all data structures that manipulate the key only through comparisons. == Implementations == Data structures implementing this type of tree include: * [[AA tree]] * [[AVL tree]] * [[Red–black tree]] * [[Scapegoat tree]] * [[Tango tree]] * [[Treap]] * [[Weight-balanced tree]] == Applications == Self-balancing binary search trees can be used in a natural way to construct and maintain ordered lists, such as [[priority queue]]s. They can also be used for [[associative array]]s; key-value pairs are simply inserted with an ordering based on the key alone. In this capacity, self-balancing BSTs have [[associative array#Efficient representations|a number of advantages and disadvantages]] over their main competitor, [[hash table]]s. One advantage of self-balancing BSTs is that they allow fast (indeed, asymptotically optimal) enumeration of the items ''in key order'', which hash tables do not provide. One disadvantage is that their lookup algorithms get more complicated when there may be multiple items with the same key. Self-balancing BSTs have better worst-case lookup performance than most<ref>[[Cuckoo hashing]] provides worst-case lookup performance of <math>O(1)</math>.</ref> hash tables (<math>O(\log n)</math> compared to <math>O(n)</math>), but have worse average-case performance (<math>O(\log n)</math> compared to <math>O(1)</math>). Self-balancing BSTs can be used to implement any algorithm that requires mutable ordered lists, to achieve optimal worst-case asymptotic performance. For example, if [[binary tree sort]] is implemented with a self-balancing BST, we have a very simple-to-describe yet [[asymptotically optimal]] <math>O(n \log n)</math> sorting algorithm. Similarly, many algorithms in [[computational geometry]] exploit variations on self-balancing BSTs to solve problems such as the [[line segment intersection]] problem and the [[point location]] problem efficiently. (For average-case performance, however, self-balancing BSTs may be less efficient than other solutions. Binary tree sort, in particular, is likely to be slower than [[merge sort]], [[quicksort]], or [[heapsort]], because of the tree-balancing overhead as well as [[cache (computing)|cache]] access patterns.) Self-balancing BSTs are flexible data structures, 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 <math>O(\log n)</math> time. These extensions can be used, for example, to optimize database queries or other list-processing algorithms. == See also == * [[Search data structure]] * [[Day–Stout–Warren algorithm]] * [[Fusion tree]] * [[Skip list]] * [[Sorting]] == References == {{reflist}} == External links == * [https://xlinux.nist.gov/dads/HTML/heightBalancedTree.html Dictionary of Algorithms and Data Structures: Height-balanced binary search tree] * [http://adtinfo.org/ GNU libavl], a LGPL-licensed library of binary tree implementations in C, with documentation {{CS-Trees}} {{Data structures}} {{DEFAULTSORT:Self-Balancing Binary Search Tree}} [[Category:Binary trees]] [[Category:Trees (data structures)]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:CS-Trees
(
edit
)
Template:Data structures
(
edit
)
Template:ISBN
(
edit
)
Template:Nowrap
(
edit
)
Template:Refimprove
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)