The full rule: whole subtrees
After this: Say the rule for whole subtrees, and use it to find the smallest key.
On this page · 7 sections · 3 checks
In The order rule you used "smaller goes left, larger goes right" to find a number in a tree of three. This lesson uses the same rule on a bigger tree, and shows the one thing the short form leaves out.
The same rule with seven numbers
A child can have children of its own, so the tree can grow. Here is a tree with seven keys. Every lesson from here on uses it, so we will call it the example tree.
The top three keys are the same as before, and four more keys sit below them. The rule holds at each node. For example, 20 is smaller than 30 and sits on its left, and 40 is larger than 30 and sits on its right.
Let us find 40.
- Compare 40 with 50. 40 is smaller, so go left, to 30.
- Compare 40 with 30. 40 is larger, so go right, to 40.
- Compare 40 with 40. They are equal, so we have found it.
That is three comparisons for seven keys. After the first comparison, we knew that 70, 60 and 80 could not be the answer. After the second, we knew that 20 could not be the answer.
Watch it happen
Here are three searches in the example tree. Watch which way each one turns, and which nodes it never looks at.
- Search for 60Search for 60. It is larger than 50, so go right. It is smaller than 70, so go left. Nothing on the left of 50 is looked at.
- Search for 20
- Search for 70
Run an operation to begin.
The full rule: whole subtrees
The short form of the rule needs one addition. To explain it, we need one more word. A node, together with everything below it, is called a . In the example tree, the left subtree of 50 holds 30, 20 and 40. The right subtree of 50 holds 70, 60 and 80.
Here is the full : for every node, every key in its left subtree is smaller than the node's key, and every key in its right subtree is larger. A tree is a binary search tree only if this full rule holds.
Check it at 50. The keys 30, 20 and 40 are all smaller than 50. The keys 70, 60 and 80 are all larger than 50.
Why does the rule say "every key", and not only "each child"? Replace 40 in the example tree with 55.
Look only at 30 and its two children, and all seems well: 20 is smaller than 30, and 55 is larger than 30. But 55 is in the left subtree of 50, where every key must be smaller than 50. So this tree breaks the rule. Now watch what goes wrong when we search for 55.
- Compare 55 with 50. 55 is larger, so go right, to 70.
- Compare 55 with 70. 55 is smaller, so go left, to 60.
- Compare 55 with 60. 55 is smaller, so go left. Nothing is there.
The search says that 55 is not in the tree, but it is. A search can only trust its turns when the full rule holds.
Finding the smallest key
The rule gives us one more quick answer. Each left turn takes you to a smaller key, so the smallest key is as far left as you can go.
In the example tree, you go from 50 to 30, then to 20. 20 has no left child, so no key in the tree is smaller. The node you stop at does not have to be a leaf. It may have a right child, but that child is larger.
In code
A node holds a key and two links, one to each child. This is the same shape of node as in the binary trees tutorial, with value now called key.
class Node: def __init__(self, key): self.key = key self.left = None # everything down here is smaller than key self.right = None # everything down here is largerpublic class Node { int key; Node left; // everything down here is smaller than key Node right; // everything down here is larger
Node(int key) { this.key = key; }}left and right start as Nonenull, which means "no child here". Notice that the class does not check the rule. It stores whatever we give it, so we have to connect the nodes correctly. A later lesson shows how insert does this for us.
Here is the tree of three numbers, connected by hand.
root = Node(50)root.left = Node(30)root.right = Node(70)print(root.left.key, root.key, root.right.key)30 50 70
Node root = new Node(50);root.left = new Node(30);root.right = new Node(70);System.out.println(root.left.key + " " + root.key + " " + root.right.key);30 50 70
And here is the search for the smallest key, starting from the root of the example tree. The loop keeps moving to the left child, and it stops at the node that has no left child.
node = rootwhile node.left is not None: # keep turning left until you cannot node = node.leftprint(node.key)20
Node node = root;while (node.left != null) node = node.left; // keep turning left until you cannotSystem.out.println(node.key);20
Check yourself
Which statement is the binary search tree rule?
A tree has root 50 with left child 30, and 30 has right child 55. There are no other nodes. Is it a binary search tree?
In any binary search tree, where is the smallest key?
Connect
- Say the full rule: for every node, the whole left subtree is smaller and the whole right subtree is larger.
- Say why checking only a parent and its children is not enough, using 55 under 30 as the example.
- Find the smallest key by turning left until you cannot.