ArticleslgStudy

computer science

Self-balancing binary search tree

Self-balancing binary search tree is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Self-balancing binary search tree rather than just read about it. In short: In computer science, a self-balancing binary search tree (BST) is any 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. These operations when designed for a self-balancing binary search tree, contain precautionary measures against boundlessly increasing tree height, so that these abstract data stru…

Self-balancing binary search tree — main illustration
Self-balancing binary search tree — illustration

Key takeaways

  • Self-balancing binary search tree belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Self-balancing binary search tree to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Self-balancing binary search tree from memory before moving on to harder problems.

Reference excerpt

In computer science, a self-balancing binary search tree (BST) is any 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. These operations when designed for a self-balancing binary search tree, contain precautionary measures against boundlessly increasing tree height, so that these abstract data structures receive the attribute "self-balancing". For height-balanced binary trees, the height is defined to be logarithmic O(log⁡n) in the number n of items. This is the case for many binary search trees, such as AVL trees and red–black trees. Splay trees and treaps 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 lists, and can be used for other abstract data structures such as associative arrays, priority queues and sets.

Overview

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 20+21+···+2h = 2h+1−1 nodes. It follows that for any tree with n nodes and height h:

n≤2h+1−1 And that implies:

h≥⌈log2(n+1)−1⌉≥⌊log2n⌋. In other words, the minimum height of a binary tree with n nodes is log2(n), rounded down; that is, ⌊log2n⌋. 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 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 ⌊log2(1,000,000)⌋=19. 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 algorithms) where this randomization is not viable. Self-balancing binary trees solve this problem by performing transformations on the tree (such as tree rotations) at key insertion times, in order to keep the height proportional to log2(n). Although a certain 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 O(log⁡n) 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. Therefore, most self-balancing BST algorithms keep the height within a constant factor of this lower bound. In the asymptotic ("Big-O") sense, a self-balancing BST structure containing n items allows the lookup, insertion, and removal of an item in O(log⁡n) worst-case time, and ordered enumeration of all items in O(n) time. For some implementations these are per-operation time bounds, while for others they are 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 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. 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 hash tables (O(log⁡n) compared to O(n)), but have worse average-case performance (O(log⁡n) compared to O(1)). 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 O(nlog⁡n) 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 access patterns.) Self-balancing BSTs are flexible data structures, in that it is 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.

See also Search data structure Day–Stout–Warren algorithm Fusion tree Skip list Sorting

References

External links Dictionary of Algorithms and Data Structures: Height-balanced binary search tree GNU libavl, a LGPL-licensed library of binary tree implementations in C, with documentation

Illustrations

Self-balancing binary search tree: An example of an unbalanced tree; following the path from the root to a node takes an average of 3.27 node accesses
An example of an unbalanced tree; following the path from the root to a node takes an average of 3.27 node accesses
Self-balancing binary search tree: The same tree after being height-balanced; the average path effort decreased to 3.00 node accesses
The same tree after being height-balanced; the average path effort decreased to 3.00 node accesses
Self-balancing binary search tree: Tree rotations are very common internal operations on self-balancing binary trees to keep perfect or near-to-perfect balance.
Tree rotations are very common internal operations on self-balancing binary trees to keep perfect or near-to-perfect balance.

Worked examples

Example 1 — a first encounter with Self-balancing binary search tree

Start with the simplest possible case. Write down what Self-balancing binary search tree claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Self-balancing binary search tree before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Self-balancing binary search tree ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Self-balancing binary search tree

In research
Self-balancing binary search tree appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Self-balancing binary search tree in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Self-balancing binary search tree is common in secondary-school and first-year university syllabi. It links to neighbouring topics Binary trees, Trees (data structures), so understanding it makes those chapters shorter.
In everyday life
Look for Self-balancing binary search tree outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Self-balancing binary search tree” →

How to study Self-balancing binary search tree in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Self-balancing binary search tree means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Self-balancing binary search tree out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Self-balancing binary search tree in simple terms?

In computer science, a self-balancing binary search tree (BST) is any 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. These operations when designed for a self-balancing binary…

Why does Self-balancing binary search tree matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Self-balancing binary search tree?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Self-balancing binary search tree.

Tags

  • Binary trees
  • Trees (data structures)

Keep exploring