6.4Binary Search Tree with an Array
What is a binary search tree?
In the binary tree in an array lessons the values had no order in the tree; finding a value meant looking at every node. A binary search tree (BST) adds a single rule. Thanks to that rule a search rules out part of the tree at every step, and the tree keeps its values in sorted order.
- The BST rule and that it applies to the whole subtree
- How to spot a tree that looks right at first but breaks the rule
- Why an inorder traversal gives sorted output
1The rule
Every node has two conditions: every value in its left subtree is smaller than it, and every value in its right subtree is larger. The same rule repeats at every node; the left and right subtrees are binary search trees too.
To the left of 50 are 30, 20 and 40: all smaller than 50. To its right are 70, 60 and 80: all larger. 30 has 20 on its left and 40 on its right; 70 has 60 on its left and 80 on its right. The rule holds at every node.
2Checking the children is not enough
A common mistake is to check the rule only between a node and its two children. In the tree below every node looks right next to its own children, but the tree is not a binary search tree.
There is nothing wrong between 30 and 60: 60 is on the right of 30 and larger than it. But 60 is in the left subtree of 50. Someone searching for 60 goes right at the root because "60 > 50" and never finds it. A single node that breaks the rule makes the search give wrong answers.
When checking whether a tree is a BST, think of an allowed range for every node. The root's range has no limits. Going left, the node's value becomes the upper limit; going right, it becomes the lower limit. The range for 60 was between 30 and 50; 60 is outside it.
3Inorder gives sorted order
An inorder traversal visits the left subtree, then the node, then the right subtree. In a BST the whole left subtree is smaller than the node and the whole right subtree is larger, so the values come out from smallest to largest. The inorder output of the example tree is 20 30 40 50 60 70 80.
This is an easy way to tell whether a tree is a BST: if the inorder output is not strictly increasing, the tree is not a BST. The broken tree's inorder output is 20 30 60 50 65 70 80; 50 comes after 60.
4What the search gains
To find a value you start at the root. If the value is smaller than the node it cannot be in the right subtree, so you go left; if it is larger, you go right. Every comparison rules out a whole subtree. In a balanced tree that means dropping about half of the remaining nodes at every step, and the search takes O(log n). In a plain binary tree the value could be anywhere; search looks at every node, O(n).
| Number of nodes | Most comparisons in a balanced BST | Most comparisons in a plain binary tree |
|---|---|---|
| 7 | 3 | 7 |
| 1,023 | 10 | 1,023 |
| 1,048,575 | 20 | 1,048,575 |
5The same value twice
In these lessons a tree never holds the same value twice. If an existing value is inserted again, nothing is inserted and the function says so. Programs that need to count repeats keep a counter in the node, or always put equal values on the same side, for example the right; then the rule reads "everything on the right is larger or equal".
6A BST in an array
In this subtopic the BST is kept with the index layout of the tree in an array: the node at index i has its left child at 2i+1 and its right child at 2i+2. But a BST does not have to be a complete tree; the rule, not the order, decides where a value goes. So empty cells remain in the array, and an empty cell is marked with EMPTY (-1). Since the values are positive, -1 never gets mixed up with a value.
#define CAPACITY 15#define EMPTY -1 struct BST { int items[CAPACITY];}; void create(struct BST* t) { for (int i = 0; i < CAPACITY; i++) { t->items[i] = EMPTY; }}The next lessons code search, insertion and the minimum and maximum on this array. The same operations come again in the same order in the BST with linked nodes subtopic.
7Try it
Which inorder output shows for sure that the tree is not a BST?
The answer is B. The inorder output of a BST is strictly increasing. In the second option 9 comes after 12; that tree cannot be a BST.
Show the answer
The answer is B. The inorder output of a BST is strictly increasing. In the second option 9 comes after 12; that tree cannot be a BST.
If 45 is inserted into the example tree (50, 30, 70, 20, 40, 60, 80), whose child does it become?
The answer is A. 45 < 50: left, to 30. 45 > 30: right, to 40. 45 > 40: right. 40 has no right child, so 45 goes there.
Show the answer
The answer is A. 45 < 50: left, to 30. 45 > 30: right, to 40. 45 > 40: right. 40 has no right child, so 45 goes there.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.