BST Find Maximum Element in DSA Java - Time & Space Complexity
We want to understand how long it takes to find the largest value in a Binary Search Tree (BST).
How does the search time change as the tree grows bigger?
Analyze the time complexity of the following code snippet.
public int findMax(Node root) {
if (root == null) {
throw new IllegalArgumentException("Tree is empty");
}
Node current = root;
while (current.right != null) {
current = current.right;
}
return current.value;
}
This code finds the maximum element by moving right until no more right child exists.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Moving from one node to its right child.
- How many times: Once per level down the right side of the tree until the last node.
As the tree grows, the number of steps depends on the height of the tree, especially the rightmost path.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 steps if tree is skewed right |
| 100 | Up to 100 steps if skewed right |
| 1000 | Up to 1000 steps if skewed right |
Pattern observation: The time grows linearly with the height of the tree, which can be as large as the number of nodes in the worst case.
Time Complexity: O(h)
This means the time to find the maximum depends on the tree's height, moving down the right side one step at a time.
[X] Wrong: "Finding the max in a BST always takes O(log n) time because it's a tree."
[OK] Correct: If the tree is not balanced and leans heavily to one side, the height can be as big as the number of nodes, making the search take longer.
Understanding how tree shape affects search time helps you explain your reasoning clearly and shows you know how data structure design impacts performance.
"What if the BST was balanced? How would the time complexity change when finding the maximum element?"
Practice
Solution
Step 1: Understand BST property
In a BST, left child nodes are smaller and right child nodes are larger than the parent node.Step 2: Locate maximum element
The maximum element is the rightmost node because it has no larger right child.Final Answer:
At the rightmost node -> Option DQuick Check:
Maximum = rightmost node [OK]
- Confusing maximum with root node
- Thinking maximum is at leftmost node
- Assuming maximum can be any leaf
Node?Solution
Step 1: Check traversal direction for maximum
Maximum is found by moving right until no right child exists.Step 2: Verify method logic
public int findMax(Node root) { while(root.right != null) root = root.right; return root.data; } correctly loops while root.right is not null, then returns root.data.Final Answer:
public int findMax(Node root) { while(root.right != null) root = root.right; return root.data; } -> Option AQuick Check:
Traverse right to find max [OK]
- Looping left child instead of right
- Returning root data without traversal
- Not checking for null root
class Node {
int data;
Node left, right;
Node(int d) { data = d; left = right = null; }
}
public class BST {
public static int findMax(Node root) {
while(root.right != null) root = root.right;
return root.data;
}
public static void main(String[] args) {
Node root = new Node(10);
root.left = new Node(5);
root.right = new Node(20);
root.right.right = new Node(30);
System.out.println(findMax(root));
}
}Solution
Step 1: Trace the BST structure
Root=10, left=5, right=20, right.right=30. Maximum is rightmost node 30.Step 2: Follow findMax method
Loop moves from 10 to 20 to 30, then stops as 30.right is null, returns 30.Final Answer:
30 -> Option BQuick Check:
Rightmost node data = 30 [OK]
- Returning root data without traversal
- Confusing left and right children
- Ignoring right.right node
public int findMax(Node root) {
if(root == null) return -1;
while(root.left != null) {
root = root.left;
}
return root.data;
}Solution
Step 1: Analyze traversal direction
Maximum is found by moving right, but code moves left incorrectly.Step 2: Correct the while loop
Change while(root.left != null) to while(root.right != null) to find max correctly.Final Answer:
Change root.left to root.right in while loop -> Option CQuick Check:
Traverse right child for max [OK]
- Looping left child instead of right
- Returning wrong default value
- Thinking recursion is mandatory
public int findMaxWithDuplicates(Node root) {
if(root == null) return Integer.MIN_VALUE;
int maxVal = root.data;
while(root.right != null) {
root = root.right;
if(root.data > maxVal) maxVal = root.data;
}
return maxVal;
}
What is the output of findMaxWithDuplicates for this BST?
Root=15, root.left=10, root.right=20, root.right.right=20, root.right.right.right=25Solution
Step 1: Understand BST with duplicates
Duplicates can appear on right subtree; max is still rightmost largest value.Step 2: Trace method execution
Traverse right: 15 -> 20 -> 20 -> 25; maxVal updates to 25 at last node.Final Answer:
25 -> Option AQuick Check:
Rightmost node data = 25 [OK]
- Stopping at first duplicate max value
- Assuming duplicates cause errors
- Ignoring rightmost traversal
Ready to prove it?
Pass the test and this pattern moves unknown
