The BST rule
25 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)A binary search tree organizes data so smaller values go left and larger values go right.
Smaller on the left and larger on the right
A binary search tree (BST) is a binary tree where every node's left subtree holds smaller values and its right subtree holds larger values, so searching takes O(height).
A binary search tree (BST) is a binary tree with a strict ordering rule. Every node in the left subtree has a value smaller than the node value. Every node in the right subtree has a value larger than the node value.
TreeNode root = new TreeNode(8);root.left = new TreeNode(3); // 3 < 8, so it goes leftroot.right = new TreeNode(10); // 10 > 8, so it goes rightroot.left.right = new TreeNode(6); // 6 > 3, and also 6 < 8: it stays in 8's left subtreeThis invariant applies to all descendants, not just immediate children. A naive brute-force search on an unordered tree checks every node. That takes O(n) linear time. The ordering rule allows binary search decisions at each node, discarding half the tree.
Trace a tree with root 8. Its left child is 3 and right child is 10. All descendants under 3 must be strictly less than 8. All descendants under 10 must be strictly greater than 8.
Searching a binary search tree
Searching compares the target value against the current node value. If the target matches the current value, the search succeeds immediately. If the target is smaller, search recursively moves into the left subtree.
Searching for 6
Step 1 of 4
- target
- 6
Example 1 · Recursive BST Search
public class Main { static class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int val) { this.val = val; } }
static boolean searchBST(TreeNode root, int target) { if (root == null) return false; if (root.val == target) return true; if (target < root.val) { return searchBST(root.left, target); } return searchBST(root.right, target); }
public static void main(String[] args) { TreeNode root = new TreeNode(8); root.left = new TreeNode(3); root.right = new TreeNode(10); root.left.left = new TreeNode(1); root.left.right = new TreeNode(6);
System.out.println("Contains 6: " + searchBST(root, 6)); System.out.println("Contains 7: " + searchBST(root, 7)); }}Contains 6: true Contains 7: false
What this code does
- Compare target to current node and choose left or right branch.
- Reaching null indicates the target does not exist in the tree.
- Matching values return true without exploring remaining subtrees.
Example 2 · Iterative BST Search
public class Main { static class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int val) { this.val = val; } }
static TreeNode searchIterative(TreeNode root, int val) { TreeNode curr = root; while (curr != null && curr.val != val) { if (val < curr.val) { curr = curr.left; } else { curr = curr.right; } } return curr; }
public static void main(String[] args) { TreeNode root = new TreeNode(15); root.left = new TreeNode(10); root.right = new TreeNode(20); root.left.right = new TreeNode(12);
TreeNode result = searchIterative(root, 12); System.out.println("Found node value: " + (result != null ? result.val : "null")); }}Found node value: 12
What this code does
- A while loop descends the tree without consuming call stack frames.
- The current pointer navigates left or right based on value comparison.
- The loop returns the node reference when found or null if absent.
If the target is larger, search moves into the right subtree. If a null pointer is reached, the target does not exist in the tree. Trace searching for value 6 in a tree with root 8 and left child 3. Start at 8. Target 6 is smaller, so move left to node 3. At node 3, target 6 is larger, so move to right child 6. Target matches node 6, so return true in three comparison steps.
Predict the output
Which nodes does search visit?
Trace the search, then pick what it prints.
Round 1 of 2
Tree: root 8; its left child 3 has a right child 6; its right child is 10. What does search(root, 6) print?
Search cost depends on tree height
Each comparison eliminates an entire subtree when the tree is balanced. On a balanced tree with n nodes, height is logarithmic, giving O(log n) time.
Example 3 · Minimum and Maximum in a BST
public class Main { static class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int val) { this.val = val; } }
static int findMin(TreeNode root) { if (root == null) throw new IllegalArgumentException("Tree is empty"); TreeNode curr = root; while (curr.left != null) { curr = curr.left; } return curr.val; }
static int findMax(TreeNode root) { if (root == null) throw new IllegalArgumentException("Tree is empty"); TreeNode curr = root; while (curr.right != null) { curr = curr.right; } return curr.val; }
public static void main(String[] args) { TreeNode root = new TreeNode(20); root.left = new TreeNode(10); root.right = new TreeNode(30); root.left.left = new TreeNode(5); root.right.right = new TreeNode(40);
System.out.println("Minimum: " + findMin(root)); System.out.println("Maximum: " + findMax(root)); }}Minimum: 5 Maximum: 40
What this code does
- Following left child pointers leads to the smallest value in the tree.
- Following right child pointers leads to the greatest value in the tree.
- Both searches take O(h) time proportional to tree height.
If nodes are inserted in sorted order, the tree degenerates into a linked list. In a skewed tree, height becomes n, degrading search performance to O(n) time. Therefore, maintaining balance is crucial for predictable logarithmic operations.
Next step
Check your understanding
10 quick questions