6.4Binary Search Tree with an Array
Search: left or right?
Searching a binary search tree means starting at the root and asking one question at every node: is the value I want smaller or larger than this one? In an array, going left means i = 2i+1 and going right means i = 2i+2. At every step a whole subtree is ruled out.
- Why the search goes in only one direction at each node
- What going left and right means in array indexes
- The three ways the search stops: found, an EMPTY cell, the end of the array
1Idea
The search starts with i = 0, the root. If the value equals items[i], the job is done and i is returned. If it is smaller, the value can only be in the left subtree, so i becomes the left child's index 2i+1. If it is larger, the search goes down to the right subtree, 2i+2.
The loop ends in two cases. If i runs past the array or items[i] is empty (EMPTY), the place where the value would have to be is empty: the value is not in the tree and -1 is returned. There is no need to look in another branch, because the BST rule says the value could only be on this path.
The order of the conditions matters: i < CAPACITY is checked first, then items[i] is read. && skips the second condition when the first is false, so nothing outside the array is ever read.
The same search with linked nodes asks the same questions; it just follows the left pointer instead of 2i+1 and right instead of 2i+2. In a plain binary tree, search has to look at every node.
2Step by step
The 15-cell array is at the top of the drawing and the same tree is below it. First 60 is searched for and found. Then 35, which is not in the tree, is searched for.
- search(60) starts.
i = 0: the root, 50. - 60 is not 50 and is larger. The left subtree (30, 20, 40) is ruled out.
i = 2·0 + 2 = 2: 70. - 60 is smaller than 70. 80 is ruled out.
i = 2·2 + 1 = 5: 60. - 60 is found. The function returns 5. Three comparisons were enough in a tree of seven nodes.
- search(35) starts. 35 is smaller than 50: the right subtree is ruled out.
i = 2·0 + 1 = 1: 30. - 35 is larger than 30. 20 is ruled out.
i = 2·1 + 2 = 4: 40. - 35 is smaller than 40.
i = 2·4 + 1 = 9. Cell 9 is empty: 40 has no left child. - The loop ends:
items[9]is EMPTY. If 35 were in the tree it would be in exactly this cell. The function returns -1.
#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; }} int search(struct BST* t, int value) { int i = 0; while (i < CAPACITY && t->items[i] != EMPTY) { if (value == t->items[i]) { return i; // found } if (value < t->items[i]) { i = 2 * i + 1; // go left } else { i = 2 * i + 2; // go right } } return -1; // not in the tree}class ArrayBST { static final int EMPTY = -1; int[] items; ArrayBST(int capacity) { items = new int[capacity]; java.util.Arrays.fill(items, EMPTY); } int search(int value) { int i = 0; while (i < items.length && items[i] != EMPTY) { if (value == items[i]) { return i; // found } if (value < items[i]) { i = 2 * i + 1; // go left } else { i = 2 * i + 2; // go right } } return -1; // not in the tree }}class ArrayBST: EMPTY = -1 def __init__(self, capacity): self.items = [ArrayBST.EMPTY] * capacity def search(self, value): i = 0 while i < len(self.items) and self.items[i] != ArrayBST.EMPTY: if value == self.items[i]: return i # found if value < self.items[i]: i = 2 * i + 1 # go left else: i = 2 * i + 2 # go right return -1 # not in the treeThe snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
Each comparison goes down one level, so there are at most h + 1 comparisons (h is the tree's height). In a balanced tree h is about log₂ n, so the search takes O(log n); in a chain built by inserting sorted values it takes O(n).
Only the variable i is used.
Since items[i] is read directly, every step is O(1); the search takes as many steps as the search with linked nodes.
4Common mistake
The most common mistake is writing the conditions in the wrong order:
while (t->items[i] != EMPTY && i < CAPACITY) // wrong: reads outside the array firstIf the value is large enough to belong to the right of 40, i jumps from 14 to 30. This line reads items[30] first; in C that is undefined behaviour and in Java it throws ArrayIndexOutOfBoundsException. Check the bound first, then the contents. Another mistake is to leave the path and try the other branch when the value is not found; a BST never needs that.
5Try it
In the example tree (50, 30, 70, 20, 40, 60, 80), which indexes does search(65) look at and what does it return?
The answer is A. 65 > 50: i = 2. 65 < 70: i = 5. 65 > 60: i = 12. Cell 12 is EMPTY, so the loop ends and -1 is returned. 80 is never looked at.
Show the answer
The answer is A. 65 > 50: i = 2. 65 < 70: i = 5. 65 > 60: i = 12. Cell 12 is EMPTY, so the loop ends and -1 is returned. 80 is never looked at.
In an array BST with capacity 15, what happens when i = 14 and the value is larger than items[14]?
The answer is A. 2·14 + 2 = 30. Since i < CAPACITY is false, the loop ends without ever reading items[30]. The value is not in the tree.
Show the answer
The answer is A. 2·14 + 2 = 30. Since i < CAPACITY is false, the loop ends without ever reading items[30]. The value is not in the tree.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.