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

Minimum and maximum

Finding the smallest value in a takes no comparisons at all. Everything to the left of a node is smaller than it, so the job is just to go left as far as you can. The largest value is at the far right in the same way.

What you will learn
  • Why the smallest value is in the leftmost node
  • How to ask "is there a left child?" in an array
  • That the smallest value is not always a leaf
4 minTime O(h)Memory O(1)C · Java · Python

1Idea

Start at the root. If there is a left child, go down to it, because every value in the left subtree is smaller than the node you are on. If there is no left child, no value in the tree is smaller than the current node: it holds the minimum.

In an array the left child is at index 2i+1. It exists if that index is inside the array and its cell is not EMPTY. Both conditions go into the loop condition; the bound is checked first, then the cell is read. The maximum uses the same loop with 2i+2.

An empty tree has no minimum. So the function checks the root first: C returns false, Java and Python throw an error. In a non-empty tree the loop stops at the root at the latest, so in a one-node tree the minimum and the maximum are the root itself.

The minimum does not have to be a . If 25 is added to the right of 20, 20 still has no left child and the minimum is still 20, but 20 is no longer a leaf. This detail matters when deleting a node with two children.

2Step by step

The drawing shows the example tree in the array and below it. First the minimum is found, then the maximum.

5003017022034046058067891011121314500301702203404605806i
  1. findMin starts. The root is not empty, i = 0: 50.
  2. The left child 2·0 + 1 = 1 is filled: go down to 30. The right subtree is never looked at.
  3. The left child 2·1 + 1 = 3 is filled: go down to 20.
  4. The left child 2·3 + 1 = 7 is empty. The loop ends; the minimum is 20.
  5. findMax starts, again at the root: i = 0.
  6. The right child 2·0 + 2 = 2 is filled: go down to 70.
  7. The right child 2·2 + 2 = 6 is filled: go down to 80.
  8. The right child 2·6 + 2 = 14 is empty. The loop ends; the maximum is 80.

#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;    }} bool findMin(struct BST* t, int* value) {    if (t->items[0] == EMPTY) {        return false;  // the tree is empty    }    int i = 0;    while (2 * i + 1 < CAPACITY && t->items[2 * i + 1] != EMPTY) {        i = 2 * i + 1;  // go left    }    *value = t->items[i];    return true;} bool findMax(struct BST* t, int* value) {    if (t->items[0] == EMPTY) {        return false;  // the tree is empty    }    int i = 0;    while (2 * i + 2 < CAPACITY && t->items[2 * i + 2] != EMPTY) {        i = 2 * i + 2;  // go right    }    *value = t->items[i];    return true;}
class ArrayBST {    static final int EMPTY = -1;    int[] items;     ArrayBST(int capacity) {        items = new int[capacity];        java.util.Arrays.fill(items, EMPTY);    }     int findMin() {        if (items[0] == EMPTY) {            throw new IllegalStateException("tree is empty");        }        int i = 0;        while (2 * i + 1 < items.length && items[2 * i + 1] != EMPTY) {            i = 2 * i + 1;  // go left        }        return items[i];    }     int findMax() {        if (items[0] == EMPTY) {            throw new IllegalStateException("tree is empty");        }        int i = 0;        while (2 * i + 2 < items.length && items[2 * i + 2] != EMPTY) {            i = 2 * i + 2;  // go right        }        return items[i];    }}
class ArrayBST:    EMPTY = -1     def __init__(self, capacity):        self.items = [ArrayBST.EMPTY] * capacity     def find_min(self):        if self.items[0] == ArrayBST.EMPTY:            raise IndexError("tree is empty")        i = 0        while 2 * i + 1 < len(self.items) and self.items[2 * i + 1] != ArrayBST.EMPTY:            i = 2 * i + 1  # go left        return self.items[i]     def find_max(self):        if self.items[0] == ArrayBST.EMPTY:            raise IndexError("tree is empty")        i = 0        while 2 * i + 2 < len(self.items) and self.items[2 * i + 2] != ArrayBST.EMPTY:            i = 2 * i + 2  # go right        return self.items[i]

The snippets focus on the operation in this lesson. The C examples need stdlib.h.

3Complexity

Time
O(h)

Each step goes down one level; at most h steps, where h is the tree's . in a balanced tree, in a tree that is a chain to the left or right.

Memory
O(1)

Only i is used.

In a plain binary tree finding the minimum means looking at every node, O(n). The BST rule turns it into a single path.

4Common mistake

A common mistake is to look for the minimum by reading the left child's value without a bound check:

while (t->items[2 * i + 1] != EMPTY)  // wrong: no bound check

In a long chain to the left, when i is 7, 2·7 + 1 = 15 and this line reads items[15]; the array has 15 cells, so that is outside it. Check 2 * i + 1 < CAPACITY first. Another mistake is looking for "the smallest filled cell in the array": it gives the right answer, but it scans the whole array and never uses the BST rule.

5Try it

25 is added to the example tree (the right child of 20, index 8). What does findMin return and at which index does it stop?

Show the answer

The answer is B. The path is 0, 1, 3. The left child of node 3 (index 7) is empty, so the loop ends there. 25 is on the right and is larger than 20; the minimum stays 20.

In a tree that holds only the root (50) and its left child (30), what does findMax return?

Show the answer

The answer is B. The root's right child (index 2) is empty, so the loop never runs. The maximum is the root itself, 50.

Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.