ArticleslgStudy

science

Binary search tree

Binary search tree is a 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 Binary search tree rather than just read about it. In short: 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 tree — main illustration
Binary search tree — illustration

Key takeaways

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

Reference excerpt

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 Θ(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). Various height-balanced binary search trees were introduced to confine the tree height, such as AVL trees, Treaps, and red–black trees.

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 nil, 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 nil. If the searched key is not found after a 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 nil or the 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 x, finding the successor or predecessor of x is crucial. Assuming all the keys of a BST are distinct, the successor of a node x in a BST is the node with the smallest key greater than x's key. On the other hand, the predecessor of a node x in a BST is the node with the largest key smaller than x's key. The following pseudocode finds the successor and predecessor of a node 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" y as a parent of x. After initialization on line 2, the while loop along lines 4-11 causes the pointers to be updated. If y is nil, the BST is empty, thus z is inserted as the root node of the binary search tree T, if it is not nil, insertion proceeds by comparing the keys to that of y on the lines 15-19 and the node is inserted accordingly.

Deletion

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

… excerpt ends here. Continue reading the full article.

Illustrations

Binary search tree: Fig. 1: A binary search tree of size 9 and depth 3, with 8 at the root.
Fig. 1: A binary search tree of size 9 and depth 3, with 8 at the root.
Binary search tree: Binary search tree node deletion process.
Binary search tree node deletion process.

Worked examples

Example 1 — a first encounter with Binary search tree

Start with the simplest possible case. Write down what Binary search tree claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In 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 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 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 Binary search tree

In research
Binary search tree appears in 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 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
Binary search tree is common in secondary-school and first-year university syllabi. It links to neighbouring topics Binary trees, Search trees, so understanding it makes those chapters shorter.
In everyday life
Look for 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 “Binary search tree” →

How to study Binary search tree in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what 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 Binary search tree out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Binary search tree in simple terms?

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

Why does Binary search tree matter?

Because it connects several 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 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 Binary search tree.

Tags

  • Binary trees
  • Search trees

Keep exploring