Skip to content

Binary search trees: search and insert

Search and insert in a binary search tree by comparing keys from the root down, and give the cost as Θ(h), with the height h between log₂ n and n − 1.

It opens on

To find 6 in this tree of 9 numbers you start at the top, 8. Do you have to look at 3 numbers, 5 numbers, or all 9?

Why it matters

A binary search tree is a sorted collection that can grow one key at a time, which a sorted array cannot do cheaply, and whether its operations take about log n steps or about n depends entirely on its shape.

Good to know first

  • Trees and binary trees: a node has data, left and right, a tree of height h has at most 2h+1 − 1 nodes, and h is at most n − 1
  • Tree traversals: inorder lists the left subtree, then the node, then the right subtree
  • Recurrences and divide and conquer: binary search on a sorted array looks at about log₂ n items
  • Recursion and the call stack: a call waits on the stack until the call it makes returns

Step by step

  1. Search

    State the binary search tree rule, search a tree for a key by comparing it with one node at each level, and say how many nodes a successful and an unsuccessful search look at.

    Search for 6. Compare 6 with 8, which is smaller, so go left to 3. Compare 6 with 3, which is larger, so go right to 6. Compare 6 with 6, equal, so the search returns True. It looked at 3 nodes of the 9, and that answers the opening question.

    the search for 6 in the table

    A common mix-up

    A search looks at every node until it finds the key.

    In fact: Each comparison throws away a whole subtree, so the search follows one path from the root.

  2. Insert, and why the order matters

    Insert a key into a binary search tree at the place where its search ends, and show that the same keys in a different order give a different shape.

    Insert 5. At 8 go left to 3, at 3 go right to 6, at 6 go left to 4, and at 4 the right child is None, so insert returns a new node 5, and the call on 4 stores it as its right child. The tree grows by one leaf, and the keys are still in order.

    the tree with 5 below 4

    A common mix-up

    Insert puts the new key at the root, or in sorted position by shifting keys.

    In fact: It only ever adds a leaf, where the search for the key ended, and no other key moves.

  3. The cost, Θ(h)

    State the cost of search and insert as Θ(h), give the least and the greatest height for n keys, and say what sorted input does.

    A common mix-up

    A search tree always searches in Θ(log n).

    In fact: Only when its height is about log n; a tree built from sorted keys is a chain, and a search takes Θ(n).

Try it

Search for 9 in the tree built from 10, 5, 15, 3 and 7. Which nodes does it look at, and what does it return?Answer

10, 5, 7, and False

Hint: 9 is smaller than 10, larger than 5, larger than 7, and 7 has no right child

Insert 9 into the tree with 8, 3, 10, 1, 6, 14, 4, 7, 13. Where does it go?Answer

As the left child of 10

Hint: 9 is larger than 8 and smaller than 10, and 10 has no left child

A search tree has 100 keys. What are the least and the greatest height it can have?Answer

6 and 99

Hint: 2⁶ − 1 = 63 is too few and 2⁷ − 1 = 127 is enough

Search for 13. Which nodes does it look at, in order, and where does it go at each?Answer

8, then 10, then 14, then 13, going right, right, left, and finding it.

Insert 12 into this tree. Which nodes does the search pass, and where does the new node go?Answer

The search passes 8, 10, 14 and 13, and 12 becomes the left child of 13.

Binary search trees: search and insert

30 minutes every month. No card.