AQA 7517, section 4.3.4.3
A binary tree search finds a value in a binary search tree (BST). A BST is a binary tree in which, for every node:
- everything in the left subtree is smaller than the node;
- everything in the right subtree is larger.
This ordering means that at each node you can tell which direction the target must be in, and ignore the other subtree completely. It is the tree version of the idea behind a binary search.
Algorithm
Start at the root.
- If the current node is empty (
None), the value is not in the tree. - If the target equals the node's value, it's found.
- If the target is smaller, repeat the search in the left subtree.
- If the target is larger, repeat the search in the right subtree.
class Node:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def tree_search(node, target):
if node is None:
return False # reached an empty spot: not found
if target == node.value:
return True
if target < node.value:
return tree_search(node.left, target)
return tree_search(node.right, target)
The same thing can be done with a loop:
def tree_search_loop(node, target):
while node is not None:
if target == node.value:
return True
if target < node.value:
node = node.left
else:
node = node.right
return False
Trace
Using this tree:
Searching for 6:
| Step | Current node | Comparison | Move |
|---|---|---|---|
| 1 | 8 | 6 < 8 | go left |
| 2 | 3 | 6 > 3 | go right |
| 3 | 6 | 6 = 6 | found |
Searching for 7:
| Step | Current node | Comparison | Move |
|---|---|---|---|
| 1 | 8 | 7 < 8 | go left |
| 2 | 3 | 7 > 3 | go right |
| 3 | 6 | 7 > 6 | go right |
| 4 | empty | not found |
Time complexity
Each step moves down one level and ignores a whole subtree. If the tree is balanced, the height is about , so a search takes about steps. The time complexity is .
| Tree shape | Height for nodes | Search time |
|---|---|---|
| Balanced (full and even) | about | |
| Completely lopsided (like a linked list) |
If values are inserted into a BST already in order (1, 2, 3, 4, ...), every node goes to the right and the tree becomes a long chain. The search then has to check each node in turn, no better than a linear search. The figure assumes the tree is reasonably balanced.
Compared with binary search on a list
| Binary search (list) | Binary tree search | |
|---|---|---|
| Needs | A sorted array | A binary search tree |
| Adding or removing an item | Slow: items have to be moved to keep it sorted | Quick: add or remove a node |
| Time to search | if balanced |
Worked example
Task: build a BST from the values 50, 30, 70, 20, 40, 60 (inserted in that order), then say how many comparisons are needed to find 60.
Insert each value starting from the root:
- 50 becomes the root.
- 30 < 50: left of 50.
- 70 > 50: right of 50.
- 20 < 50, then < 30: left of 30.
- 40 < 50, then > 30: right of 30.
- 60 > 50, then < 70: left of 70.
To find 60: compare with 50 (go right), compare with 70 (go left), compare with 60 (found). That is 3 comparisons, even though there are 6 values.
Exam tip: for a trace question, list each node you visit in order, and write "left" or "right" for each decision. For complexity, say it is because each comparison removes about half the remaining tree, as long as the tree is balanced.
Check yourself
What property of a binary search tree makes searching quick?
For every node, values in the left subtree are smaller and values in the right subtree are larger, so each comparison tells you which subtree to search and the other can be ignored.
What is the time complexity of a binary tree search, and when does it apply?
, when the tree is balanced. A lopsided tree can degrade to .
In the example tree, which nodes are visited when searching for 14?
8, then 10, then 14.
What happens in the algorithm if the search reaches an empty (None) subtree?
The value is not in the tree, so the search stops and reports that it was not found.