Binary Search Trees
   HOME

TheInfoList



OR:

In
computer science Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to Applied science, practical discipli ...
, a binary search tree (BST), also called an ordered or sorted binary tree, is a rooted
binary tree In computer science, a binary tree is a k-ary k = 2 tree data structure in which each node has at most two children, which are referred to as the ' and the '. A recursive definition using just set theory notions is that a (non-empty) binary t ...
data structure In computer science, a data structure is a data organization, management, and storage format that is usually chosen for efficient access to data. More precisely, a data structure is a collection of data values, the relationships among them, a ...
with the key of each internal node being greater than all the keys in the respective node's left subtree and less than the ones in its right subtree. The
time complexity In computer science, the time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations performed by ...
of operations on the binary search tree is
directly proportional In mathematics, two sequences of numbers, often experimental data, are proportional or directly proportional if their corresponding elements have a constant ratio, which is called the coefficient of proportionality or proportionality constant ...
to the height of the tree. Binary search trees allow
binary search In computer science, binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the m ...
for fast lookup, addition, and removal of data items. Since the nodes in a BST are laid out so that each comparison skips about half of the remaining tree, the lookup performance is proportional to that of
binary logarithm In mathematics, the binary logarithm () is the power to which the number must be raised to obtain the value . That is, for any real number , :x=\log_2 n \quad\Longleftrightarrow\quad 2^x=n. For example, the binary logarithm of is , the b ...
. BSTs were devised in the 1960s for the problem of efficient storage of labeled data and are attributed to
Conway Berners-Lee Conway Maurice Berners-Lee (19 September 1921 – 1 February 2019) was an English mathematician and computer scientist who worked as a member of the team that developed the Ferranti Mark 1, the world's first commercial stored program electronic ...
and David Wheeler. The performance of a binary search tree is dependent on the order of insertion of the nodes into the tree since arbitrary insertions may lead to degeneracy; several variations of the binary search tree can be built with guaranteed worst-case performance. The basic operations include: search, traversal, insert and delete. BSTs with guaranteed worst-case complexities perform better than an unsorted array, which would require linear search time. The
complexity analysis In computer science, the analysis of algorithms is the process of finding the computational complexity of algorithms—the amount of time, storage, or other resources needed to execute them. Usually, this involves determining a function that re ...
of BST shows that, on average, the insert, delete and search takes O(\log n) for n nodes. In the worst case, they degrade to that of a singly linked list: O(n). To address the boundless increase of the tree height with arbitrary insertions and deletions, self-balancing variants of BSTs are introduced to bound the worst lookup complexity to that of the binary logarithm.
AVL trees In computer science, an AVL tree (named after inventors Adelson-Velsky and Landis) is a self-balancing binary search tree. It was the first such data structure to be invented. In an AVL tree, the heights of the two child subtrees of any node d ...
were the first self-balancing binary search trees, invented in 1962 by
Georgy Adelson-Velsky Georgy Maximovich Adelson-Velsky (russian: Гео́ргий Макси́мович Адельсо́н-Ве́льский; name is sometimes transliterated as Georgii Adelson-Velskii) (8 January 1922 – 26 April 2014) was a Soviet and Israeli m ...
and
Evgenii Landis Evgenii Mikhailovich Landis (russian: Евге́ний Миха́йлович Ла́ндис, ''Yevgeny Mikhaylovich Landis''; 6 October 1921 – 12 December 1997) was a Soviet mathematician who worked mainly on partial differential equations. ...
. Binary search trees can be used to implement
abstract data type In computer science, an abstract data type (ADT) is a mathematical model for data types. An abstract data type is defined by its behavior (Semantics (computer science), semantics) from the point of view of a ''User (computing), user'', of the dat ...
s such as dynamic sets,
lookup table In computer science, a lookup table (LUT) is an array that replaces runtime computation with a simpler array indexing operation. The process is termed as "direct addressing" and LUTs differ from hash tables in a way that, to retrieve a value v wi ...
s and
priority queues In computer science, a priority queue is an abstract data type , abstract data-type similar to a regular queue (abstract data type), queue or stack (abstract data type), stack data structure in which each element additionally has a ''priority'' a ...
, and used in
sorting algorithm In computer science, a sorting algorithm is an algorithm that puts elements of a List (computing), list into an Total order, order. The most frequently used orders are numerical order and lexicographical order, and either ascending or descending. ...
s such as
tree sort A tree sort is a sort algorithm that builds a binary search tree from the elements to be sorted, and then traverses the tree ( in-order) so that the elements come out in sorted order. Its typical use is sorting elements online: after each insert ...
.


History

The binary search tree algorithm was discovered independently by several researchers, including P.F. Windley,
Andrew Donald Booth Andrew Donald Booth (11 February 1918 – 29 November 2009)
, Andrew Colin, Thomas N. Hibbard. The algorithm is attributed to
Conway Berners-Lee Conway Maurice Berners-Lee (19 September 1921 – 1 February 2019) was an English mathematician and computer scientist who worked as a member of the team that developed the Ferranti Mark 1, the world's first commercial stored program electronic ...
and David Wheeler, who used it for storing
labeled data Labeled data is a group of samples that have been tagged with one or more labels. Labeling typically takes a set of unlabeled data and augments each piece of it with informative tags. For example, a data label might indicate whether a photo conta ...
in
magnetic tape Magnetic tape is a medium for magnetic storage made of a thin, magnetizable coating on a long, narrow strip of plastic film. It was developed in Germany in 1928, based on the earlier magnetic wire recording from Denmark. Devices that use magne ...
s in 1960. One of the earliest and popular binary search tree algorithm is that of Hibbard. The time complexities of a binary search tree increases boundlessly with the tree height if the nodes are inserted in an arbitrary order, therefore
self-balancing binary search tree 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.Donald ...
s were introduced to bound the height of the tree to O(log n). Various height-balanced binary search trees were introduced to confine the tree height, such as
AVL tree In computer science, an AVL tree (named after inventors Adelson-Velsky and Landis) is a self-balancing binary search tree. It was the first such data structure to be invented. In an AVL tree, the heights of the two child subtrees of any node d ...
s,
Treap In computer science, the treap and the randomized binary search tree are two closely related forms of binary search tree data structures that maintain a dynamic set of ordered keys and allow binary searches among the keys. After any sequence of in ...
s, and
red–black tree In computer science, a red–black tree is a kind of self-balancing binary search tree. Each node stores an extra bit representing "color" ("red" or "black"), used to ensure that the tree remains balanced during insertions and deletions. When the ...
s. The AVL tree was invented by
Georgy Adelson-Velsky Georgy Maximovich Adelson-Velsky (russian: Гео́ргий Макси́мович Адельсо́н-Ве́льский; name is sometimes transliterated as Georgii Adelson-Velskii) (8 January 1922 – 26 April 2014) was a Soviet and Israeli m ...
and
Evgenii Landis Evgenii Mikhailovich Landis (russian: Евге́ний Миха́йлович Ла́ндис, ''Yevgeny Mikhaylovich Landis''; 6 October 1921 – 12 December 1997) was a Soviet mathematician who worked mainly on partial differential equations. ...
in 1962 for the efficient organization of information. It was the first self-balancing binary search tree to be invented.


Overview

A binary search tree is a rooted binary tree in which the nodes are arranged in
total order In mathematics, a total or linear order is a partial order in which any two elements are comparable. That is, a total order is a binary relation \leq on some set X, which satisfies the following for all a, b and c in X: # a \leq a ( reflexive) ...
in which the nodes with keys greater than any particular node is stored on the right sub-trees and the ones with equal to or less than are stored on the left sub-tree satisfying the
binary search In computer science, binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the m ...
property. Binary search trees are also efficacious in
sorting Sorting refers to ordering data in an increasing or decreasing manner according to some linear relationship among the data items. # ordering: arranging items in a sequence ordered by some criterion; # categorizing: grouping items with similar pro ...
s and
search algorithm In computer science, a search algorithm is an algorithm designed to solve a search problem. Search algorithms work to retrieve information stored within particular data structure, or calculated in the search space of a problem domain, with eith ...
s. However, the search complexity of a BST depends upon the order in which the nodes are inserted and deleted; since in worst case, successive operations in the binary search tree may lead to degeneracy and form a
singly linked list In computer science, a linked list is a linear collection of data elements whose order is not given by their physical placement in memory. Instead, each element points to the next. It is a data structure consisting of a collection of nodes whic ...
(or "unbalanced tree") like structure, thus has the same worst-case complexity as a
linked list In computer science, a linked list is a linear collection of data elements whose order is not given by their physical placement in memory. Instead, each element points to the next. It is a data structure consisting of a collection of nodes whic ...
. Binary search trees are also a fundamental data structure used in construction of abstract data structures such as sets,
multisets In mathematics, a multiset (or bag, or mset) is a modification of the concept of a set that, unlike a set, allows for multiple instances for each of its elements. The number of instances given for each element is called the multiplicity of that e ...
, and
associative array In computer science, an associative array, map, symbol table, or dictionary is an abstract data type that stores a collection of (key, value) pairs, such that each possible key appears at most once in the collection. In mathematical terms an as ...
s.


Operations


Searching

Searching in a binary search tree for a specific key can be programmed
recursively Recursion (adjective: ''recursive'') occurs when a thing is defined in terms of itself or of its type. Recursion is used in a variety of disciplines ranging from linguistics to logic. The most common application of recursion is in mathematics ...
or iteratively. Searching begins by examining the
root node In computer science, a tree is a widely used abstract data type that represents a hierarchical tree structure with a set of connected nodes. Each node in the tree can be connected to many children (depending on the type of tree), but must be con ...
. If the tree is \text, the key being searched for does not exist in the tree. Otherwise, if the key equals that of the root, the search is successful and the node is returned. If the key is less than that of the root, the search proceeds by examining the left subtree. Similarly, if the key is greater than that of the root, the search proceeds by examining the right subtree. This process is repeated until the key is found or the remaining subtree is \text. If the searched key is not found after a \text subtree is reached, then the key is not present in the tree.


Recursive search

The following
pseudocode In computer science, pseudocode is a plain language description of the steps in an algorithm or another system. Pseudocode often uses structural conventions of a normal programming language, but is intended for human reading rather than machine re ...
implements the BST search procedure through
recursion Recursion (adjective: ''recursive'') occurs when a thing is defined in terms of itself or of its type. Recursion is used in a variety of disciplines ranging from linguistics to logic. The most common application of recursion is in mathematics ...
. The recursive procedure continues until a \text or the \text being searched for are encountered.


Iterative search

The recursive version of the search can be "unrolled" into a
while loop In most computer programming languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The ''while'' loop can be thought of as a repeating if statement. Overview The '' ...
. On most machines, the iterative version is found to be more efficient. Since the search may proceed till some
leaf node In computer science, a tree is a widely used abstract data type that represents a hierarchical tree structure with a set of connected nodes. Each node in the tree can be connected to many children (depending on the type of tree), but must be conn ...
, the running time complexity of BST search is O(h) where h is the height of the tree. However, the worst case for BST search is O(n) where n is the total number of nodes in the BST, because an unbalanced BST may degenerate to a linked list. However, if the BST is height-balanced the height is O(\log n).


Successor and predecessor

For certain operations, given a node \text, finding the successor or predecessor of \text is crucial. Assuming all the keys of the BST are distinct, the successor of a node \text in BST is the node with the smallest key greater than \text's key. On the other hand, the predecessor of a node \text in BST is the node with the largest key smaller than \text's key. Following is pseudocode for finding the successor and predecessor of a node \text in BST. Operations such as finding a node in a BST whose key is the maximum or minimum are critical in certain operations, such as determining the successor and predecessor of nodes. Following is the pseudocode for the operations.


Insertion

Operations such as insertion and deletion cause the BST representation to change dynamically. The data structure must be modified in such a way that the properties of BST continue to hold. New nodes are inserted as
leaf nodes In computer science, a tree is a widely used abstract data type that represents a hierarchical tree structure with a set of connected nodes. Each node in the tree can be connected to many children (depending on the type of tree), but must be conn ...
in the BST. Following is an iterative implementation of the insertion operation. The procedure maintains a "trailing pointer" \text as a parent of \text. After initialization on line 2, the while loop along lines 4-11 causes the pointers to be updated. If \text is \text, the BST is empty, thus \text is inserted as the root node of the binary search tree \text, if it is not \text, insertion proceeds by comparing the keys to that of \text on the lines 15-19 and the node is inserted accordingly.


Deletion

Deletion of a node, say \text, from a binary search tree \text should abide three cases: # If \text is a leaf node, the parent node's pointer to \text gets replaced with \text and consequently \text gets removed from the tree. # If \text has a single child node, the child gets elevated as either left or right child of \text's parent depending on the position of \text within the BST, as shown in fig. 2 part (a) and part (b), and as a result, \text gets removed from the tree. # If \text has both a left and right child, the successor of \text (let it be \text) takes the position of \text in the tree. This depends on the position of \text within the BST: ##If \text is \text's immediate right child, \text gets elevated and \text's left child made point to \text's initial left sub-tree, as shown in fig. 2 part (c). ##If \text is not the immediate right child of \text, deletion proceeds by replacing the position of \text by its right child, and \text takes the position of \text in the BST, as shown in fig. 2 part (d). The following pseudocode implements the deletion operation in a binary search tree. The \text procedure deals with the 3 special cases mentioned above. Lines 2-3 deal with case 1; lines 4-5 deal with case 2 and lines 6-16 for case 3. The helper function \text is used within the deletion algorithm for the purpose of replacing the node \text with \text in the binary search tree \text. This procedure handles the deletion (and substitution) of \text from the BST.


Traversal

A BST can be traversed through three basic algorithms:
inorder In computer science, tree traversal (also known as tree search and walking the tree) is a form of graph traversal and refers to the process of visiting (e.g. retrieving, updating, or deleting) each node in a tree data structure, exactly once. S ...
,
preorder In mathematics, especially in order theory, a preorder or quasiorder is a binary relation that is reflexive and transitive. Preorders are more general than equivalence relations and (non-strict) partial orders, both of which are special c ...
, and
postorder In computer science, tree traversal (also known as tree search and walking the tree) is a form of graph traversal and refers to the process of visiting (e.g. retrieving, updating, or deleting) each node in a tree data structure, exactly once. S ...
tree walks. *Inorder tree walk: Nodes from the left subtree get visited first, followed by the root node and right subtree. *Preorder tree walk: The root node gets visited first, followed by left and right subtrees. *Postorder tree walk: Nodes from the left subtree get visited first, followed by the right subtree, and finally the root. Following is a recursive implementation of the tree walks.


Balanced binary search trees

Without rebalancing, insertions or deletions in a binary search tree may lead to degeneration, resulting in a height n of the tree (where n is number of items in a tree), so that the lookup performance is deteriorated to that of a linear search. Keeping the search tree balanced and height bounded by O(\log n) is a key to the usefulness of the binary search tree. This can be achieved by "self-balancing" mechanisms during the updation operations to the tree designed to maintain the tree height to the binary logarithmic complexity.


Height-balanced trees

A tree is height-balanced if the heights of the left sub-tree and right sub-tree are guaranteed to be related by a constant factor. This property was introduced by the
AVL tree In computer science, an AVL tree (named after inventors Adelson-Velsky and Landis) is a self-balancing binary search tree. It was the first such data structure to be invented. In an AVL tree, the heights of the two child subtrees of any node d ...
and continued by the Red-Black tree. The heights of all the nodes on the path from the root to the modified leaf node have to be observed and possibly corrected on every insert and delete operation to the tree.


Weight-balanced trees

In a weight-balanced tree, the criterion of a balanced tree is the number of leaves of the subtrees. The weights of the left and right subtrees differ at most by 1. However, the difference is bound by a ratio \alpha of the weights, since a strong balance condition of 1 cannot be maintained with O(\log n) rebalancing work during insert and delete operations. The \alpha-weight-balanced trees gives an entire family of balance conditions, where each left and right subtrees have each at least a fraction of \alpha of the total weight of the subtree.


Types

There are several self-balanced binary search trees, including
T-tree In computer science a T-tree is a type of binary tree data structure that is used by main memory database, main-memory databases, such as Datablitz, eXtremeDB, MySQL Cluster, TimesTen, Oracle TimesTen and MobileLite. A T-tree is a Height-balan ...
,
treap In computer science, the treap and the randomized binary search tree are two closely related forms of binary search tree data structures that maintain a dynamic set of ordered keys and allow binary searches among the keys. After any sequence of in ...
, red-black tree,
B-tree In computer science, a B-tree is a self-balancing tree data structure that maintains sorted data and allows searches, sequential access, insertions, and deletions in logarithmic time. The B-tree generalizes the binary search tree, allowing for n ...
,
2–3 tree In computer science, a 2–3 tree is a tree data structure, where every node with children (internal node) has either two children (2-node) and one data element or three children (3-nodes) and two data elements. A 2–3 tree is a B-tree of order 3 ...
, and Splay tree.


Examples of applications


Sort

Binary search trees are used in sorting algorithms such as
tree sort A tree sort is a sort algorithm that builds a binary search tree from the elements to be sorted, and then traverses the tree ( in-order) so that the elements come out in sorted order. Its typical use is sorting elements online: after each insert ...
, where all the elements are inserted at once and the tree is traversed at an in-order fashion. BSTs are also used in
quicksort Quicksort is an efficient, general-purpose sorting algorithm. Quicksort was developed by British computer scientist Tony Hoare in 1959 and published in 1961, it is still a commonly used algorithm for sorting. Overall, it is slightly faster than ...
.


Priority queue operations

Binary search trees are used in implementing
priority queue In computer science, a priority queue is an abstract data-type similar to a regular queue or stack data structure in which each element additionally has a ''priority'' associated with it. In a priority queue, an element with high priority is se ...
s, using the node's key as priorities. Adding new elements to the queue follows the regular BST insertion operation but the removal operation depends on the type of priority queue: * If it is an ascending order priority queue, removal of an element with the lowest priority is done through leftward traversal of the BST. * If it is a descending order priority queue, removal of an element with the highest priority is done through rightward traversal of the BST.


See also

*
Search tree In computer science, a search tree is a tree data structure used for locating specific keys from within a set. In order for a tree to function as a search tree, the key for each node must be greater than any keys in subtrees on the left, and less ...
*
Join-based tree algorithms In computer science, join-based tree algorithms are a class of algorithms for self-balancing binary search trees. This framework aims at designing highly-parallelized algorithms for various balanced binary search trees. The algorithmic framework is ...
*
Optimal binary search tree In computer science, an optimal binary search tree (Optimal BST), sometimes called a weight-balanced binary tree, is a binary search tree which provides the smallest possible search time (or expected search time) for a given sequence of accesses ...


References


Further reading

* * * * * *


External links

* Ben Pfaff
''An Introduction to Binary Search Trees and Balanced Trees''.
(PDF; 1675 kB) 2004.
Binary Tree Visualizer
(JavaScript animation of various BT-based data structures) {{DEFAULTSORT:Binary search tree Articles with example C++ code Articles with example Python (programming language) code Binary trees Search trees