What is Binary search tree?

A binary tree in which, for every node, all values in the left subtree are smaller and all values in the right subtree are larger than the node. Search, insertion and deletion take time proportional to the tree's height, and an in-order traversal visits the values in ascending order.

Japanese name: 二分探索木

Binary search tree appears in 4 practice questions across 1 topic, including Data Structures and Algorithms.

Field: Data Structures and Algorithms

Search, insertion and deletion

A search starts at the root and moves to the left child if the target is smaller than the node or to the right child if it is larger, repeating until it finds a match or runs out of children. Insertion searches and attaches a new node where the search ran out of children. Deletion removes a node with zero or one child and reconnects the child; a node with two children is replaced by the smallest value in its right subtree (or the largest in its left subtree), and that node is deleted instead. Every operation takes time proportional to the number of levels from the root to a leaf (the tree's height).

Unbalanced trees and self-balancing trees

A binary search tree with n nodes has a height of about log₂ n if it is balanced, but inserting values in sorted order, such as 1, 2, 3, …, produces a single chain extending to one side with height n−1. Searching it then takes O(n), the same as a linked list. AVL trees and red-black trees are self-balancing binary search trees that reshape themselves with operations called rotations on every insertion and deletion, keeping the height at O(log n); they are used for the ordered dictionaries and sets of many programming languages.

Traversal orders

Ways of visiting every node of a tree include pre-order, which visits each node first; in-order, which visits the left subtree, the node and then the right subtree; post-order, which visits the children first; and breadth-first (level-order), which visits the tree level by level. An in-order traversal of a binary search tree lists the values in ascending order, giving a sorted listing. A post-order traversal of an expression tree gives Reverse Polish notation.

Try it: questions about Binary search tree

When a binary search tree is traversed in in-order (visiting the left subtree, then the node, then the right subtree), how are the values of the visited nodes ordered? Assume all values are distinct.

  1. AIn ascending order (smallest first)
  2. BIn the order they were inserted
  3. CIn order of closeness to the root (shallowest first)
Show answer and explanation

Answer: A. In ascending order (smallest first)

In a binary search tree, for every node, all values in the left subtree are smaller than the node and all values in the right subtree are larger. Visiting the left subtree, the node and then the right subtree therefore visits the values from smallest to largest, so the result is in ascending order. Visiting nodes in order of closeness to the root is a breadth-first (level-order) traversal, which goes level by level from left to right. The insertion order affects the shape of the tree but does not match the order of any of these traversals.

The values 50, 30, 70, 20, 40, 60, 80 are inserted in this order into an empty binary search tree. In what order are the nodes visited when the tree is traversed in post-order (left subtree, right subtree, then the node)?

  1. A50, 30, 20, 40, 70, 60, 80
  2. B20, 40, 30, 60, 80, 70, 50
  3. C20, 30, 40, 50, 60, 70, 80
Show answer and explanation

Answer: B. 20, 40, 30, 60, 80, 70, 50

The root is 50; its left child is 30 (with children 20 and 40) and its right child is 70 (with children 60 and 80). Post-order visits the left subtree as 20, 40, 30 and the right subtree as 60, 80, 70, and only then the root 50. 50, 30, 20, 40, 70, 60, 80 is the pre-order result, which visits each node first, and 20, 30, 40, … is the in-order result. Post-order is used when children must be processed first, such as when deleting a tree or producing postfix notation from an expression tree.

FAQ

How does a binary search tree differ from a heap?

A binary search tree follows the rule “smaller on the left, larger on the right”, so it can search for any value and list values in order. A binary heap only follows the rule “a parent is larger (or smaller) than its children”, so it can quickly remove the maximum (or minimum) but is not suited to searching for arbitrary values.

When should a binary search tree or a B-tree be used?

In memory, self-balancing binary search trees are used; for storage read in blocks, such as disks and SSDs, B-trees (B+ trees), which keep the tree shallow by putting many keys in each node, are used.

Binary search tree in the explanations

Questions that mention Binary search tree (4)

  1. When a binary search tree is traversed in in-order (visiting the left subtree, then the node, then the right subtree), how are the values of the visited nodes ordered? Assume all values are distinct.
  2. The values 50, 30, 70, 20, 40, 60, 80 are inserted in this order into an empty binary search tree. In what order are the nodes visited when the tree is traversed in post-order (left subtree, right subtree, then the node)?
  3. The values 1, 2, 3, …, 1000 are inserted in this order into an empty binary search tree (no rebalancing is done). When searching this tree for 1000, how many nodes are compared, starting from the root, before it is found?
  4. What is the main reason that B-trees (including B+ trees), rather than binary search trees, are widely used for indexes in databases and file systems?

Books on Data Structures and Algorithms Ad

As an Amazon Associate I earn from qualifying purchases.

Semiconductor, Electronics and Computing Calculators

Related terms

Linked list Binary heap Binary search Breadth-first search Depth-first search Dijkstra's algorithm Dynamic programming Greedy algorithm Hash table Queue

Back to the glossary