A binary search tree keeps its keys in order, so that searching works like binary search but on a structure that can grow and shrink.

Definition

A binary search tree (BST) is a binary tree in which, for every node, all keys in its left subtree are smaller than the node's key, and all keys in its right subtree are larger. Usually duplicate keys are not allowed. Because of this order, an inorder traversal lists the keys in ascending order.

Definition: Binary search tree. A binary tree in which every node's key is greater than all keys in its left subtree and smaller than all keys in its right subtree.

Searching

To search for a key k, start at the root. If the tree is empty, k is not present. If k equals the root's key, it is found. If k is smaller, search the left subtree, otherwise the right subtree. Each step goes one level down, so the time is O(h), where h is the height. The minimum is found by going left as far as possible, and the maximum by going right.

Insertion

To insert a key, search for it as above. When an empty position (NULL) is reached, create a new node there as a leaf. The new node always becomes a leaf, and the time is O(h). Inserting 50, 30, 70, 20, 40, 60, 80 in that order gives a balanced tree of height 2.

Deletion

To delete a node, there are three cases. Case 1, a leaf: simply remove it. Case 2, one child: replace the node by its child. Case 3, two children: find its inorder successor, the smallest key in its right subtree (or its inorder predecessor, the largest in the left subtree), copy that key into the node, and then delete the successor, which has at most one child. Deletion takes O(h) time.

Height and Efficiency

If the tree is balanced, h is about log₂ n, and all operations are O(log n). But if the keys are inserted in sorted order, the tree becomes skewed, h = n − 1, and the operations become O(n), no better than a linked list. This is the motivation for self-balancing trees such as AVL trees.

Table 4.5: BST operations

Operation Balanced Skewed Method
Search O(log n) O(n) Compare and go left or right
Insert O(log n) O(n) Add as a leaf
Delete O(log n) O(n) Three cases

Example 4.5: Building and deleting in a BST

Problem: Insert 50, 30, 70, 20, 40, 60, 80 into an empty BST, then delete 50.

Solution:

  1. 50 is the root. 30 < 50 goes left, 70 right. 20 goes left of 30, 40 right of 30. 60 left of 70, 80 right of 70.
  2. 50 has two children. Its inorder successor is the smallest key in the right subtree, 60.
  3. Copy 60 into the root and delete the old 60, which is a leaf.

Result: The root becomes 60, and the inorder traversal is 20 30 40 60 70 80.

#include <stdio.h>
#include <stdlib.h>
struct n { int k; struct n *l, *r; };
struct n *ins(struct n *t, int k) {
    if (!t) { t = calloc(1, sizeof *t); t->k = k; return t; }
    if (k < t->k) t->l = ins(t->l, k); else if (k > t->k)
        t->r = ins(t->r, k);
    return t;
}
struct n *del(struct n *t, int k) {
    if (!t) return t;
    if (k < t->k) t->l = del(t->l, k);
    else if (k > t->k) t->r = del(t->r, k);
    else {
        if (!t->l || !t->r) {
            struct n *c = t->l ? t->l : t->r;
            free(t);
            return c;
        }
        struct n *s = t->r;
        while (s->l) s = s->l;
        t->k = s->k; t->r = del(t->r, s->k);
    }
    return t;
}
void in(struct n *t) {
    if (t) { in(t->l); printf("%d ", t->k); in(t->r); }
}
int main(void) {
    int a[] = {50, 30, 70, 20, 40, 60, 80};
    struct n *t = NULL;
    for (int i = 0; i < 7; i++) t = ins(t, a[i]);
    in(t); printf("\n");
    t = del(t, 50);
    in(t); printf(" root=%d\n", t->k);
    return 0;
}

Output

20 30 40 50 60 70 80
20 30 40 60 70 80  root=60

Common mistakes

  • Checking only the children, not the whole subtrees, when testing the BST property.
  • In the two-children case, deleting the node without replacing it by its successor.
  • Inserting sorted data and expecting O(log n) performance.

Key takeaways

  • Left subtree keys are smaller, right subtree keys are larger.
  • Search, insert and delete take O(h).
  • Deletion has three cases: leaf, one child, two children.
  • A skewed BST degrades to O(n).

Practice questions

  1. Define a binary search tree.
  2. What is the inorder successor?
  3. When does a BST become inefficient?

Answers to the practice questions

  1. A binary tree in which each node's key is larger than all keys in its left subtree and smaller than all in its right subtree.
  2. The smallest key in a node's right subtree.
  3. When it becomes skewed, for example when keys are inserted in sorted order.

Sources

  • Official BCA syllabus Data Structures, course code 25BCA402DS02, Unit IV