Skip to content
Surf Wiki
Save to docs
general/articles-with-example-c-code

From Surf Wiki (app.surf) — the open knowledge base

Binary search tree

Rooted binary tree data structure

Binary search tree

Rooted binary tree data structure

FieldValue
nameBinary search tree
typetree
invented_byP.F. Windley, A.D. Booth, A.J.T. Colin, and T.N. Hibbard
invented_year1960
space_avgΘ(n)
space_worstO(n)
search_avgΘ(log n)
search_worstO(n)
insert_avgΘ(log n)
insert_worstO(n)
delete_avgΘ(log n)
delete_worstO(n)
Fig. 1: A binary search tree of size 9 and depth 3, with 8 at the root.

In computer science, a binary search tree (BST), also called an ordered or sorted binary tree, is a rooted binary tree data 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. The time complexity of operations on the binary search tree is linear with respect to the height of the tree.

Binary search trees allow binary 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 of binary logarithm. BSTs were devised in the 1960s for the problem of efficient storage of labeled data and are attributed to Conway Berners-Lee 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 of BST shows that, on average, the insert, delete and search takes \Theta(\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 were the first self-balancing binary search trees, invented in 1962 by Georgy Adelson-Velsky and Evgenii Landis.

Binary search trees can be used to implement abstract data types such as dynamic sets, lookup tables and priority queues, and used in sorting algorithms such as tree sort.

History

The binary search tree algorithm was discovered independently by several researchers, including P.F. Windley, Andrew Donald Booth, Andrew Colin, Thomas N. Hibbard. The algorithm is attributed to Conway Berners-Lee and David Wheeler, who used it for storing labeled data in magnetic tapes in 1960. One of the earliest and popular binary search tree algorithm is that of Hibbard.

The time complexity 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 trees were introduced to bound the height of the tree to O(\log n).{{cite book|title=The Art of Computer Programming|first=Donald|last=Knuth|author-link=Donald Knuth|publisher=Addison-Wesley|year=1998|chapter=Section 6.2.3: Balanced Trees|pages=458–481|volume=3|edition=2|url=https://ia801604.us.archive.org/17/items/B-001-001-250/B-001-001-250.pdf |archive-url=https://ghostarchive.org/archive/20221009/https://ia801604.us.archive.org/17/items/B-001-001-250/B-001-001-250.pdf |archive-date=2022-10-09 |url-status=live|isbn=978-0201896855

Overview

A binary search tree is a rooted binary tree in which nodes are arranged in strict total order in which the nodes with keys greater than any particular node A is stored on the right sub-trees to that node A and the nodes with keys equal to or less than A are stored on the left sub-trees to A, satisfying the binary search property.

Binary search trees are also efficacious in sortings and search 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 a singly linked list (or "unbalanced tree") like structure, thus has the same worst-case complexity as a linked list.

Binary search trees are also a fundamental data structure used in construction of abstract data structures such as sets, multisets, and associative arrays.

Operations

Searching

Searching in a binary search tree for a specific key can be programmed recursively or iteratively.

Searching begins by examining the root node. If the tree is , 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{nil}. If the searched key is not found after a \text{nil} subtree is reached, then the key is not present in the tree.

Recursive search

The following pseudocode implements the BST search procedure through recursion.

The recursive procedure continues until a \text{nil} or the \text{key} being searched for are encountered.

Iterative search

The recursive version of the search can be "unrolled" into a while loop. On most machines, the iterative version is found to be more efficient.

Since the search may proceed till some leaf node, 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{x}, finding the successor or predecessor of \text{x} is crucial. Assuming all the keys of a BST are distinct, the successor of a node \text{x} in a BST is the node with the smallest key greater than \text{x}'s key. On the other hand, the predecessor of a node \text{x} in a BST is the node with the largest key smaller than \text{x}'s key. The following pseudocode finds the successor and predecessor of a node \text{x} in a 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 the BST. Following is an iterative implementation of the insertion operation.

The procedure maintains a "trailing pointer" \text{y} as a parent of \text{x}. After initialization on line 2, the while loop along lines 4-11 causes the pointers to be updated. If \text{y} is \text{nil}, the BST is empty, thus \text{z} is inserted as the root node of the binary search tree \text{T}, if it is not \text{nil}, insertion proceeds by comparing the keys to that of \text{y} on the lines 15-19 and the node is inserted accordingly.

Deletion

Binary search tree node deletion process.

The deletion of a node, say \text{Z}, from the binary search tree \text{BST} has three cases:

  1. If \text{Z} is a leaf node, it is replaced by \text{NIL} as shown in (a).
  2. If \text{Z} has only one child, the child node of \text{Z} gets elevated by modifying the parent node of \text{Z} to point to the child node, consequently taking \text{Z}'s position in the tree, as shown in (b) and (c).
  3. If \text{Z} has both left and right children, the in-order successor of \text{Z}, say \text{Y}, displaces \text{Z} by following the two cases:
  4. If \text{Y} is \text{Z}'s right child, as shown in (d), \text{Y} displaces \text{Z} and \text{Y}'s right child remain unchanged.
  5. If \text{Y} lies within \text{Z}'s right subtree but is not \text{Z}'s right child, as shown in (e), \text{Y} first gets replaced by its own right child, and then it displaces \text{Z}'s position in the tree.
  6. Alternatively, the in-order predecessor can also be used.

The following pseudocode implements the deletion operation in a binary search tree.

The \text{BST-Delete} 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{Shift-Nodes} is used within the deletion algorithm for the purpose of replacing the node \text{u} with \text{v} in the binary search tree \text{BST}. This procedure handles the deletion (and substitution) of \text{u} from \text{BST}.

Traversal

Main article: Tree traversal

A BST can be traversed through three basic algorithms: inorder, preorder, and postorder tree walks.

  • Inorder tree walk: Nodes from the left subtree get visited first, followed by the root node and right subtree. Such a traversal visits all the nodes in the order of non-decreasing key sequence.
  • 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

Main article: Self-balancing binary search tree

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 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

Main article: Weight-balanced tree

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, treap, red-black tree, B-tree, 2–3 tree, and Splay tree.

Examples of applications

Sort

Main article: Tree sort

Binary search trees are used in sorting algorithms such as tree sort, where all the elements are inserted at once and the tree is traversed at an in-order fashion. BSTs are also used in quicksort.

Priority queue operations

Main article: Priority queue

Binary search trees are used in implementing priority 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:

  • 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.

References

References

  1. Pitassi, Toniann. (2015). "CSC263: Balanced BSTs, AVL tree". [[University of Toronto]], Department of Computer Science.
  2. Myers, Andrew. "CS 312 Lecture: AVL Trees". [[Cornell University]], Department of Computer Science.
  3. (1962). "An algorithm for the organization of information". [[Proceedings of the USSR Academy of Sciences]].
  4. (1 January 1989). "Explaining the Behaviour of Binary Search Trees Under Prolonged Updates: A Model and Simulations". [[The Computer Journal]].
  5. (28 July 1986). "Analysis of the standard deletion algorithms in exact fit domain binary search trees". [[Springer Publishing]], [[University of Waterloo]].
  6. P. F. Windley. (1 January 1960). "Trees, Forests and Rearranging". [[The Computer Journal]].
  7. Paul E. Black, "red-black tree", in Dictionary of Algorithms and Data Structures [online], Paul E. Black, ed. 12 November 2019. (accessed May 19 2022) from: https://www.nist.gov/dads/HTML/redblack.html
  8. Thareja, Reema. (13 October 2018). "Data Structures Using C". [[Oxford University Press]].
  9. (2001). "Introduction to Algorithms". [[MIT Press]].
  10. (1 February 1982). "A Short Note on Binary Search Trees". [[Oxford University Press]].
  11. Junzhou Huang. "Design and Analysis of Algorithms". [[University of Texas at Arlington]].
  12. Ray. "Binary Search Tree". [[Loyola Marymount University]], Department of Computer Science.
  13. Thornton, Alex. (2021). "ICS 46: Binary Search Trees". [[University of California, Irvine]].
  14. Brass, Peter. (January 2011). "Advanced Data Structure". [[Cambridge University Press]].
  15. (1978). "On the Average Number of Rebalancing Operations in Weight-Balanced Trees". Theoretical Computer Science.
  16. (25–28 August 1986). "A Study of Index Structures for Main Memory Database Management Systems".
  17. (1989). "30th Annual Symposium on Foundations of Computer Science". IEEE Computer Society Press.
  18. (2001). "Introduction to Algorithms". MIT Press.
  19. Comer, Douglas. (June 1979). "The Ubiquitous B-Tree". Computing Surveys.
  20. (1998). "The Art of Computer Programming". Addison Wesley.
  21. (1985). "Self-Adjusting Binary Search Trees". [[Journal of the ACM]].
  22. Narayanan, Arvind. (2019). "COS226: Binary search trees". [[Princeton University School of Engineering and Applied Science]].
  23. Xiong, Li. "A Connection Between Binary Search Trees and Quicksort". [[Oxford College of Emory University]], The Department of Mathematics and Computer Science.
Info: Wikipedia Source

This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page.

Want to explore this topic further?

Ask Mako anything about Binary search tree — get instant answers, deeper analysis, and related topics.

Research with Mako

Free with your Surf account

Content sourced from Wikipedia, available under CC BY-SA 4.0.

This content may have been generated or modified by AI. CloudSurf Software LLC is not responsible for the accuracy, completeness, or reliability of AI-generated content. Always verify important information from primary sources.

Report