6.4Binary Search Tree with an Array
The trouble with a BST in an array
The binary search tree you built with an array sometimes refuses a new value while it holds only four. This is not a bug in the code; the shape of a BST depends on the order the values arrive in, and the array has to set aside room for every possible shape. This lesson shows where the problem comes from and why a BST is usually coded with linked nodes.
- Why values that arrive in order use up so many cells
- How the number of cells needed grows exponentially with the number of values
- Why deleting in an array means moving a whole subtree
1Values that arrive in order
Insert 10, 20, 30, 40 in this order into an empty array (capacity 15). Each value is larger than all the ones before it, so the path always goes right and each value becomes the right child of the previous one.
| Insert | Path | Index |
|---|---|---|
| 10 | root | 0 |
| 20 | right of 10 | 2·0 + 2 = 2 |
| 30 | right of 20 | 2·2 + 2 = 6 |
| 40 | right of 30 | 2·6 + 2 = 14 |
| 50 | right of 40 | 2·14 + 2 = 30: outside the array |
The tree has really turned into a list that grows to the right. With eleven cells empty, a fifth value cannot be inserted. Raising the capacity does not help either: the sixth value needs cell 62 and the seventh cell 126.
2How many cells are needed?
In a chain to the right, value number k (counting from 0) goes to index 2^(k+1) - 2. So for n values the last index is 2^n - 2 and the array must have 2^n - 1 cells. One more value doubles the room needed.
| Values inserted in order | Index of the last value | Array size needed |
|---|---|---|
| 1 | 0 | 1 |
| 2 | 2 | 3 |
| 3 | 6 | 7 |
| 4 | 14 | 15 |
| 10 | 1,022 | 1,023 |
| 20 | 1,048,574 | 1,048,575 |
| n | 2^n - 2 | 2^n - 1 |
If you insert in decreasing order, the chain grows to the left and the values go to indexes 0, 1, 3, 7, 15. That is a little cheaper (2^(n-1) cells for n values), but still exponential. In a balanced tree, n values fit in about n cells: 7 values in 7 cells, 15 values in 15 cells.
Sorted data is common in real life: records with increasing ids, events arriving in time order, a file that is already sorted. Here the worst case is not a rare exception but an everyday one.
3Why is deletion hard?
In an array a node's place is its index, and the index is worked out only from its parent. When a node is deleted, its child has to move up into its place; but when the child's place changes, everything below it changes too. In the tree below, let 70, which has one child, be deleted.
With linked nodes this deletion is a single pointer change: the right pointer of 50 points to 80 and everything below 80 stays where it is. In an array, the whole subtree of 80 has to move up one level and every node is copied to a new index.
| Node | Old index | New index |
|---|---|---|
| 80 | 6 | 2 |
| 75 | 13 | 5 |
| 90 | 14 | 6 |
The number of nodes moved is the size of the subtree. If a child of the root is deleted, almost half the tree changes place; deletion stops being an O(1) pointer change and becomes an O(n) copy. That is why the array BST lessons do not code deletion.
4When does an array fit?
An array works well when the shape of the tree is known in advance. In the binary tree in an array insertion always goes into the next free place, so the tree is always complete and no cell is wasted. A heap is a tree like that too and is almost always coded with an array. The shape of a BST, however, is decided by its values, and the array has to make room for every possible shape.
| BST in an array | BST with linked nodes | |
|---|---|---|
| Memory | Depends on the shape, 2^n - 1 cells at worst | One node per value, n nodes |
| Capacity | Fixed; can fill up with empty cells left | As much as memory allows |
| Search and insert | O(h) | O(h) |
| Delete | A subtree has to move, O(n) | Pointers change, O(h) |
| Reaching a child | Compute: 2i+1, 2i+2 | Follow a pointer: left, right |
The next subtopic does the same operations with linked nodes and covers deletion with its three cases. Linked nodes fix the room problem, but the chain built by sorted input still slows the search down there; the unbalanced tree lesson shows this.
5Try it
If 5, 10, 15 are inserted in this order into an empty array BST, which index does 15 go to?
The answer is B. 5 goes to the root (0), 10 to its right (2·0 + 2 = 2) and 15 to the right of 10 (2·2 + 2 = 6).
Show the answer
The answer is B. 5 goes to the root (0), 10 to its right (2·0 + 2 = 2) and 15 to the right of 10 (2·2 + 2 = 6).
Why can deleting a node with one child be expensive in an array BST?
The answer is B. In an array the place is the index, worked out from the parent. When a subtree moves up one level, the index of every node in it changes and all of them are moved. Searching is still O(h).
Show the answer
The answer is B. In an array the place is the index, worked out from the parent. When a subtree moves up one level, the index of every node in it changes and all of them are moved. Searching is still O(h).
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.