The order rule
After this: Say the short rule, and use it to find a number or show that it is not there.
On this page · 5 sections · 3 checks
Imagine a program that keeps a list of ticket numbers. Someone asks whether ticket 40 is in the list. If the numbers are in no particular order, the program has to look at them one at a time, until it finds 40 or runs out of numbers. With seven numbers, that can take seven looks. With a million numbers, it can take a million looks.
This tutorial shows a better way to store numbers. Each look tells the program which way to go next, so most of the numbers are never looked at.
Here is an everyday picture. You are walking along a road, looking for house number 30, and you reach a fork. A sign at the fork says: "Numbers below 50 are on the left road. Numbers above 50 are on the right road." You take the left road, and you never have to walk down the right road at all.
A tree with three numbers
We store the numbers in a binary tree, which you met in the binary trees tutorial. Here is a small one, and a quick reminder of the words.
- Each circle is a .
- The node at the top, 50, is the .
- 30 is the left of 50, and 70 is its right child.
- 30 and 70 have no children. A node with no children is called a .
The rule
Look at where the numbers sit. 30 is smaller than 50, and it is on the left. 70 is larger than 50, and it is on the right. This is called the , and here is its short form: smaller goes left, larger goes right.
A binary tree that follows this rule is called a , or BST for short.
The number stored in a node is called its . The key is the thing we search by. In a real program, a node can hold more data beside its key, such as a customer's name beside their ID number. In this tutorial a key is one whole number, and no key appears twice. So two different keys always have one smaller and one larger.
Finding a number
Let us use the rule to find 30 in the tree above.
- Start at the root. Compare 30 with 50. 30 is smaller, so go left.
- The left child is 30. Compare 30 with 30. They are equal, so we have found it.
That took two comparisons. A comparison is one check of the key we want against the key in a node. The right side of the tree was never looked at.
Now look for 60, which is not in the tree.
- Start at the root. Compare 60 with 50. 60 is larger, so go right.
- Compare 60 with 70. 60 is smaller, so go left.
- 70 has no left child, so there is nowhere to go. 60 is not in the tree.
That also took two comparisons, and we never looked at 30. If 60 were in the tree, the rule says that it would have to be on the left of 70, and nothing is there.
Now run these searches yourself. Press the button to run each step.
- Search for 30Search for 30. It is smaller than 50, so the search goes left and finds it.
- Search for 70
- Search for 60
Run an operation to begin.
Check yourself
You are searching for 40 in the tree of three numbers (root 50, left child 30, right child 70). You are at the root. Which way do you go?
These four trees hold the numbers 10, 20 and 30. Which one follows the rule: smaller goes left, larger goes right?
You search for 20 in the tree of three numbers (root 50, left child 30, right child 70). What happens?
What comes next
You can now find a number in a small tree, and you can tell when it is not there. The next lesson uses the same rule on a tree of seven keys, and shows the one thing the short form of the rule leaves out.
- Say what a binary search tree is, and say the short rule: smaller goes left, larger goes right.
- Use the rule to find a number, one comparison at a time.
- Say when a search can stop and answer that a number is not in the tree.