| Binary search tree | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Type | tree | |||||||||||||||||||||||
| Invented | 1960 | |||||||||||||||||||||||
| Invented by | P.F. Windley,A.D. Booth,A.J.T. Colin, andT.N. Hibbard | |||||||||||||||||||||||
| ||||||||||||||||||||||||

Incomputer science, abinary search tree (BST), also called anordered orsorted binary tree, is arootedbinary treedata structure 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. Thetime complexity of operations on the binary search tree islinear with respect to the height of the tree.
Binary search trees allowbinary search 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 ofbinary logarithm. BSTs were devised in the 1960s for the problem of efficient storage of labeled data and are attributed toConway Berners-Lee andDavid 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 requirelinear search time.
Thecomplexity analysis of BST shows that,on average, the insert, delete and search takes for nodes. In the worst case, they degrade to that of a singly linked list:. 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 were the first self-balancing binary search trees, invented in 1962 byGeorgy Adelson-Velsky andEvgenii Landis.[1][2][3]
Binary search trees can be used to implementabstract data types such asdynamic sets,lookup tables andpriority queues, and used insorting algorithms such astree sort.
The binary search tree algorithm was discovered independently by several researchers, including P.F. Windley,Andrew Donald Booth,Andrew Colin,Thomas N. Hibbard.[4][5] The algorithm is attributed toConway Berners-Lee andDavid Wheeler, who used it for storinglabeled data inmagnetic tapes in 1960.[6] One of the earliest and popular binary search tree algorithm is that of Hibbard.[4]
The time complexity of a binary search tree increases boundlessly with the tree height if the nodes are inserted in an arbitrary order, thereforeself-balancing binary search trees were introduced to bound the height of the tree to.[7] Variousheight-balanced binary search trees were introduced to confine the tree height, such asAVL trees,Treaps, andred–black trees.[8]
A binary search tree is a rooted binary tree in which nodes are arranged instrict total order in which the nodes with keys greater than any particular nodeA is stored on the rightsub-trees to that nodeA and the nodes with keys equal to or less thanA are stored on the left sub-trees toA, satisfying thebinary search property.[9]: 298 [10]: 287
Binary search trees are also efficacious insortings andsearch algorithms. 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 asingly linked list (or "unbalanced tree") like structure, thus has the same worst-case complexity as alinked list.[11][9]: 299-302
Binary search trees are also a fundamental data structure used in construction ofabstract data structures such as sets,multisets, andassociative arrays.
Searching in a binary search tree for a specific key can be programmedrecursively oriteratively.
Searching begins by examining theroot node. If the tree isnil, 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. If the searched key is not found after a subtree is reached, then the key is not present in the tree.[10]: 290–291
The followingpseudocode implements the BST search procedure throughrecursion.[10]: 290
Recursive-Tree-Search(x, key)if x = NILor key = x.keythenreturn xif key < x.keythenreturn Recursive-Tree-Search(x.left, key)elsereturn Recursive-Tree-Search(x.right, key)end if |
The recursive procedure continues until a or the being searched for are encountered.
The recursive version of the search can be "unrolled" into awhile loop. On most machines, the iterative version is found to be moreefficient.[10]: 291
Iterative-Tree-Search(x, key)while x ≠ NILand key ≠ x.keydoif key < x.keythen x := x.leftelse x := x.rightend ifrepeatreturn x |
Since the search may proceed till someleaf node, the running time complexity of BST search is where is theheight of the tree. However, the worst case for BST search is where is the total number of nodes in the BST, because an unbalanced BST may degenerate to a linked list. However, if the BST isheight-balanced the height is.[10]: 290
For certain operations, given a node, finding the successor or predecessor of is crucial. Assuming all the keys of a BST are distinct, the successor of a node in a BST is the node with the smallest key greater than's key. On the other hand, the predecessor of a node in a BST is the node with the largest key smaller than's key. The following pseudocode finds the successor and predecessor of a node in a BST.[12][13][10]: 292–293
BST-Successor(x)if x.right ≠ NILthenreturn BST-Minimum(x.right)end if y := x.parentwhile y ≠ NILand x = y.rightdo x := y y := y.parentrepeatreturn y | BST-Predecessor(x)if x.left ≠ NILthenreturn BST-Maximum(x.left)end if y := x.parentwhile y ≠ NILand x = y.leftdo x := y y := y.parentrepeatreturn y |
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.[10]: 291–292
BST-Maximum(x)while x.right ≠ NILdo x := x.rightrepeatreturn x | BST-Minimum(x)while x.left ≠ NILdo x := x.leftrepeatreturn x |
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 asleaf nodes in the BST.[10]: 294–295 Following is an iterative implementation of the insertion operation.[10]: 294
1 BST-Insert(T, z)2 y := NIL3 x := T.root4while x ≠ NILdo5 y := x6if z.key < x.keythen7 x := x.left8else9 x := x.right10end if11repeat12 z.parent := y13if y = NILthen14 T.root := z15else if z.key < y.keythen16 y.left := z17else18 y.right := z19end if |
The procedure maintains a "trailing pointer" as a parent of. After initialization on line 2, thewhile loop along lines 4-11 causes the pointers to be updated. If is, the BST is empty, thus is inserted as the root node of the binary search tree, if it is not, insertion proceeds by comparing the keys to that of on the lines 15-19 and the node is inserted accordingly.[10]: 295

The deletion of a node, say, from the binary search tree has three cases:[10]: 295-297
The following pseudocode implements the deletion operation in a binary search tree.[10]: 296-298
1 BST-Delete(BST, z)2if z.left = NILthen3 Shift-Nodes(BST, z, z.right)4else if z.right = NILthen5 Shift-Nodes(BST, z, z.left)6else7 y := BST-Successor(z)8if y.parent ≠ zthen9 Shift-Nodes(BST, y, y.right)10 y.right := z.right11 y.right.parent := y12end if13 Shift-Nodes(BST, z, y)14 y.left := z.left15 y.left.parent := y16end if |
1 Shift-Nodes(BST, u, v)2if u.parent = NILthen3 BST.root := v4else if u = u.parent.leftthen5 u.parent.left := v5else6 u.parent.right := v7end if8if v ≠ NILthen9 v.parent := u.parent10end if |
The 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. Thehelper function is used within the deletion algorithm for the purpose of replacing the node with in the binary search tree.[10]: 298 This procedure handles the deletion (and substitution) of from.
A BST can betraversed through three basic algorithms:inorder,preorder, andpostorder tree walks.[10]: 287
Following is a recursive implementation of the tree walks.[10]: 287–289
Inorder-Tree-Walk(x)if x ≠ NILthen Inorder-Tree-Walk(x.left)visit node Inorder-Tree-Walk(x.right)end if | Preorder-Tree-Walk(x)if x ≠ NILthenvisit node Preorder-Tree-Walk(x.left) Preorder-Tree-Walk(x.right)end if | Postorder-Tree-Walk(x)if x ≠ NILthen Postorder-Tree-Walk(x.left) Postorder-Tree-Walk(x.right)visit nodeend if |
Without rebalancing, insertions or deletions in a binary search tree may lead to degeneration, resulting in a height of the tree (where is number of items in a tree), so that the lookup performance is deteriorated to that of a linear search.[14] Keeping the search tree balanced and height bounded by 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.[7][15]: 50
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 theAVL tree and continued by thered–black tree.[15]: 50–51 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.[15]: 52
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.[16][15]: 61 However, the difference is bound by a ratio of the weights, since a strong balance condition of cannot be maintained with rebalancing work during insert and delete operations. The-weight-balanced trees gives an entire family of balance conditions, where each left and right subtrees have each at least a fraction of of the total weight of the subtree.[15]: 62
There are several self-balanced binary search trees, includingT-tree,[17]treap,[18]red-black tree,[19]B-tree,[20]2–3 tree,[21] andSplay tree.[22]
Binary search trees are used in sorting algorithms such astree sort, where all the elements are inserted at once and the tree is traversed at an in-order fashion.[23] BSTs are also used inquicksort.[24]
Binary search trees are used in implementingpriority queues, 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:[25]
The 2–3 trees defined at the close of Section 6.2.3 are equivalent to B-Trees of order 3.