6.5Binary Search Tree with Linked Nodes
Minimum and maximum
Finding the smallest value in a binary search tree needs no comparisons at all. Smaller values always sit on the left, so you start at the root and go left as far as you can. The largest value is found the same way, by always going right.
- 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
1Idea
Each step to the left from the root moves to smaller values, because by the BST rule the left subtree 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 leaf; 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.
- Start. First we will find the minimum.
- The tree is not empty.
currpoints to the root, 50. - The left of 50 is not empty:
currgoes down to 30. - The left of 30 is not empty:
currgoes down to 20. - The left of 20 is empty. The loop ends: the minimum is 20. Three nodes were visited and no values were compared.
- Now the maximum.
currstarts at the root again. - The right of 50 is not empty:
currgoes down to 70. - The right of 70 is not empty:
currgoes down to 80. - 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 currThe snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
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 leaf 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?
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.
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?
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.
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.