BST Find Maximum Element in DSA C++ - Time & Space Complexity
We want to understand how long it takes to find the biggest number in a Binary Search Tree (BST).
How does the time needed change when the tree grows bigger?
Analyze the time complexity of the following code snippet.
// Find maximum value in a BST
int findMax(Node* root) {
if (root == nullptr) return -1; // empty tree
Node* current = root;
while (current->right != nullptr) {
current = current->right;
}
return current->data;
}
This code moves right in the tree until it finds the largest value, which is the rightmost node.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Moving from one node to its right child.
- How many times: Up to the height of the tree (number of right edges).
As the tree grows taller, the number of steps to reach the rightmost node grows roughly with the tree height.
| Input Size (n) | Approx. Operations (steps to rightmost node) |
|---|---|
| 10 | Up to 10 (if tree is a straight line) |
| 100 | Up to 100 (worst case) |
| 1000 | Up to 1000 (worst case) |
Pattern observation: The time grows linearly with the height of the tree, which can be as big as the number of nodes in the worst case.
Time Complexity: O(h) where h is the height of the BST.
This means the time depends on how tall the tree is, not just how many nodes it has.
[X] Wrong: "Finding the max in a BST always takes the same time no matter the tree size."
[OK] Correct: If the tree is very unbalanced (like a linked list), it takes longer because you must follow many right children.
Knowing how tree shape affects search time helps you explain and improve data structures in real projects.
"What if the BST was balanced? How would the time complexity change?"
Practice
Solution
Step 1: Understand BST property
In a BST, left child nodes have smaller values and right child nodes have larger values than their parent.Step 2: Locate maximum element
The maximum element is the rightmost node because it has no right child and is greater than all other nodes.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
root?Solution
Step 1: Check traversal direction for maximum
Maximum is found by moving right until no right child exists.Step 2: Verify code correctness
int findMax(Node* root) { while(root->right) root = root->right; return root->data; } correctly loops whileroot->rightexists and returnsroot->data.Final Answer:
int findMax(Node* root) { while(root->right) root = root->right; return root->data; } -> Option CQuick Check:
Traverse right child to find max [OK]
- Traversing left child instead of right
- Returning root data without traversal
- Using parent pointer which may not exist
struct Node {
int data;
Node* left;
Node* right;
};
Node* root = new Node{10, nullptr, nullptr};
root->right = new Node{20, nullptr, nullptr};
root->right->right = new Node{30, nullptr, nullptr};
int findMax(Node* root) {
while(root->right) root = root->right;
return root->data;
}
std::cout << findMax(root) << std::endl;Solution
Step 1: Trace the BST structure
Root is 10, right child 20, right child's right child 30.Step 2: Follow findMax function
Loop moves from 10 to 20, then 20 to 30, then stops as 30 has no right child.Final Answer:
30 -> Option AQuick Check:
Rightmost node data = 30 [OK]
- Returning root data without traversal
- Confusing left and right pointers
- Assuming runtime error due to nullptr
int findMax(Node* root) {
if (root == nullptr) return -1;
while(root->left) {
root = root->left;
}
return root->data;
}Solution
Step 1: Analyze traversal direction
The function incorrectly traverses left child to find maximum, but maximum is at rightmost node.Step 2: Correct traversal logic
Changewhile(root->left)towhile(root->right)to find maximum correctly.Final Answer:
Should traverse right child, not left child -> Option AQuick Check:
Maximum = rightmost node, not leftmost [OK]
- Traversing left child instead of right
- Ignoring nullptr check
- Returning wrong default value
struct Node {
int data;
Node* left;
Node* right;
};
// Function to find max element
int findMax(Node* root) {
// Your code here
}Solution
Step 1: Understand duplicates in BST
Even with duplicates allowed on the right subtree, BST property ensures right subtree values are >= parent, so maximum is always the rightmost node.Step 2: Correct traversal for maximum
Traverse right whileroot->rightexists only. No data comparison needed due to BST invariants.Final Answer:
Traverse right while root->right exists only -> Option BQuick Check:
Traverse right child pointer until null [OK]
- Using strict > check which stops early on duplicates
- Traversing left instead of right
- Returning root data without traversal
Ready to prove it?
Pass the test and this pattern moves unknown
