algoritmauzmanı
Sign in
TREN
6.4Binary Search Tree with an Array
Lesson 6.4.5

The trouble with a BST in an array

The 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.

What you will learn
  • 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
5 min

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.

InsertPathIndex
10root0
20right of 102·0 + 2 = 2
30right of 202·2 + 2 = 6
40right of 302·6 + 2 = 14
50right of 402·14 + 2 = 30: outside the array
10012023453067891011121340141002023064014
Four values in cells 0, 2, 6 and 14. The eleven faded cells are empty, but the place for 50 is cell 30; the array has run out.

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 orderIndex of the last valueArray size needed
101
223
367
41415
101,0221,023
201,048,5741,048,575
n2^n - 22^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.

5003017023458067891011127513901450030170280675139014
70 (index 2) is to be deleted. Its only child is 80, and 80 has children 75 and 90.

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.

NodeOld indexNew index
8062
75135
90146
500301802347559067891011121314500301802755906
After the deletion. 80, 75 and 90 were all copied to other cells; the old cells were cleared.

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 arrayBST with linked nodes
MemoryDepends on the shape, 2^n - 1 cells at worstOne node per value, n nodes
CapacityFixed; can fill up with empty cells leftAs much as memory allows
Search and insertO(h)O(h)
DeleteA subtree has to move, O(n)Pointers change, O(h)
Reaching a childCompute: 2i+1, 2i+2Follow 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?

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?

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.