What if you could find the biggest item instantly without searching everything?
Why BST Find Maximum Element in DSA C?
Imagine you have a messy pile of books with no order. You want to find the thickest book, but you have to check each one by hand, flipping through all of them.
Looking through every book one by one takes a lot of time and effort. You might lose track or miss the thickest book because the pile is unorganized and large.
A Binary Search Tree (BST) organizes books by thickness so the thickest book is always at one end. To find it, you just follow the path to the rightmost book, saving time and effort.
int findMax(int arr[], int size) {
int max = arr[0];
for (int i = 1; i < size; i++) {
if (arr[i] > max) max = arr[i];
}
return max;
}int findMaxInBST(Node* root) {
Node* current = root;
while (current->right != NULL) {
current = current->right;
}
return current->data;
}This lets you quickly find the largest value in a sorted structure without checking every element.
Finding the highest score in a leaderboard where scores are stored in a BST, so you just go to the rightmost node to get the top score instantly.
Manual search is slow and error-prone for large, unordered data.
BST organizes data so the maximum is always at the rightmost node.
Finding max in BST is fast by following right children only.
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 CQuick Check:
Maximum = rightmost node [OK]
- Confusing maximum with root node
- Thinking maximum is at leftmost node
- Assuming maximum can be any leaf
Solution
Step 1: Analyze code logic
while(root->right != NULL) root = root->right; return root->data; moves right until no right child exists, which is correct for max in BST.Step 2: Check other options
while(root->left != NULL) root = root->left; return root->data; moves left (minimum), C returns root data without traversal, D returns root data directly.Final Answer:
while(root->right != NULL) root = root->right; return root->data; -> Option BQuick Check:
Traverse rightmost node for max [OK]
- Traversing left child instead of right
- Returning root data without traversal
- Not checking for NULL pointers
typedef struct Node {
int data;
struct Node* left;
struct Node* right;
} Node;
int findMax(Node* root) {
while(root->right != NULL) {
root = root->right;
}
return root->data;
}
// BST structure:
// 10
// / \
// 5 20
// / \
// 15 30
int main() {
// Assume BST is created as above
printf("%d", findMax(root));
return 0;
}Solution
Step 1: Understand BST structure
The rightmost node is 30, which is the maximum element.Step 2: Trace findMax function
The function moves right from root (10) to 20, then to 30, stops as 30->right is NULL, returns 30.Final Answer:
30 -> Option AQuick Check:
Max element = 30 [OK]
- Returning root data without traversal
- Confusing max with root or left child
- Not following right pointers correctly
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
The function moves left to find max, but max is at rightmost node in BST.Step 2: Check other issues
Returning -1 is acceptable for empty tree; checking root NULL is done; recursion is optional.Final Answer:
It traverses left instead of right to find maximum -> Option DQuick Check:
Max is rightmost, not leftmost [OK]
- Confusing left and right child traversal
- Ignoring NULL checks
- Thinking recursion is mandatory
int findMax(Node* root) {
if(root == NULL) return INT_MIN;
while(root->right != NULL) {
root = root->right;
}
return root->data;
}What is the output if the BST has nodes with values: 10, 20, 20, 30 (duplicates on right)?
Solution
Step 1: Understand duplicates in BST
Duplicates allowed on right means equal values go to right subtree, so max is still rightmost node.Step 2: Trace findMax with duplicates
Traversal moves right from 10 to 20, then to next 20, then to 30, which is rightmost and max.Final Answer:
30 -> Option AQuick Check:
Duplicates on right do not affect max = rightmost node [OK]
- Assuming duplicates cause errors
- Stopping at first duplicate value
- Confusing duplicates location in BST
Ready to prove it?
Pass the test and this pattern moves unknown
