algoritmauzmanı
Sign in
TREN
6.5Binary Search Tree with Linked Nodes
Lesson 6.5.3

Minimum and maximum

Finding the smallest value in a needs no comparisons at all. Smaller values always sit on the left, so you start at the and go left as far as you can. The largest value is found the same way, by always going right.

What you will learn
  • Why the smallest value is in the leftmost node
  • The short loop that goes down to the left or to the right
  • Why this operation comes back in the deletion lesson
4 minTime O(h)Memory O(1)C · Java · Python

1Idea

Each step to the left from the root moves to smaller values, because by the BST rule the left holds values smaller than the node. When you reach a node whose left is empty, no smaller value is left in the tree: that is the node you want. It does not have to be a ; it may have a right child.

The loop starts the curr pointer at the root and moves it on until its left field is NULL. It returns the node itself, not just the value, so the caller can read the value and also do other work with the node. If the tree is empty, it returns NULL.

The maximum is the mirror image: go right until the right field is NULL. The two functions have the same shape, so understanding one means understanding both.

This little function comes back when you delete a node with two children. The deleted node is replaced by the smallest node of its right subtree, and that node is found with exactly this loop.

2Step by step

The drawing finds the minimum and then the maximum in a tree holding 50, 30, 70, 20, 40, 60 and 80. The curr pointer sits below its node.

500x1a40300x1a90700x1b20200x1b70400x1bc0600x1c10800x1c60rootcurr
  1. Start. First we will find the minimum.
  2. The tree is not empty. curr points to the root, 50.
  3. The left of 50 is not empty: curr goes down to 30.
  4. The left of 30 is not empty: curr goes down to 20.
  5. The left of 20 is empty. The loop ends: the minimum is 20. Three nodes were visited and no values were compared.
  6. Now the maximum. curr starts at the root again.
  7. The right of 50 is not empty: curr goes down to 70.
  8. The right of 70 is not empty: curr goes down to 80.
  9. The right of 80 is empty. The loop ends: the maximum is 80.

struct TreeNode {    int data;    struct TreeNode* left;    struct TreeNode* right;}; struct TreeNode* findMin(struct TreeNode* root) {    if (root == NULL) return NULL;           // empty tree    struct TreeNode* curr = root;    while (curr->left != NULL)        curr = curr->left;                   // always left    return curr;} struct TreeNode* findMax(struct TreeNode* root) {    if (root == NULL) return NULL;           // empty tree    struct TreeNode* curr = root;    while (curr->right != NULL)        curr = curr->right;                  // always right    return curr;}
class TreeNode {    int data;    TreeNode left, right;    TreeNode(int data) { this.data = data; }} TreeNode findMin(TreeNode root) {    if (root == null) return null;           // empty tree    TreeNode curr = root;    while (curr.left != null)        curr = curr.left;                    // always left    return curr;} TreeNode findMax(TreeNode root) {    if (root == null) return null;           // empty tree    TreeNode curr = root;    while (curr.right != null)        curr = curr.right;                   // always right    return curr;}
class TreeNode:    def __init__(self, data):        self.data = data        self.left = None        self.right = None def find_min(root):    if root is None:        return None  # empty tree    curr = root    while curr.left is not None:        curr = curr.left  # always left    return curr def find_max(root):    if root is None:        return None  # empty tree    curr = root    while curr.right is not None:        curr = curr.right  # always right    return curr

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

3Complexity

Time
O(h)

The walk goes down a single path from the root: at most h steps. in a balanced tree, in a tree that has become one branch.

Memory
O(1)

Only the curr pointer is used: .

You could write the same thing with recursion: if the left is empty return the node, otherwise look again in the left child. But the loop is shorter and needs no extra space.

4Common mistake

A common mistake is to assume the minimum is a and to write the loop with the wrong condition:

while (curr->left != NULL || curr->right != NULL)    curr = (curr->left != NULL) ? curr->left : curr->right;

This loop does not stop at the node whose left is empty; it moves right to larger values. For a tree made of 30 and its right child 40, the minimum is 30, but this code returns 40. The condition must look at the left field only. And do not forget the empty tree: when root is NULL, root->left cannot be read.

5Try it

Which is always true for the node holding the smallest value in a BST?

Show the answer

The answer is B. If it had a left child, a smaller value would be in the tree. It can have a right child, so it need not be a leaf. If the root is alone or its left subtree is empty, the minimum is the root itself.

If the left subtree of the root is empty, where is the minimum?

Show the answer

The answer is B. Every value to the right of the root is larger than the root. If there are no values on the left, nothing smaller than the root is left. The loop never runs and the root is returned.

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