0
0
DSA C++programming

BST Find Maximum Element in DSA C++

Choose your learning style9 modes available
Mental Model
In a Binary Search Tree, the biggest number is always the rightmost node. We just keep going right until we can't go further.
Analogy: Imagine a line of boxes arranged from smallest on the left to biggest on the right. To find the biggest box, you just walk to the rightmost box in the line.
      10
     /  \
    5    15
          \
           20
            \
             25

Start at root (10) and keep moving right to find max.
Dry Run Walkthrough
Input: BST with nodes: 10 -> 5 (left), 15 (right), 15 -> 20 (right), 20 -> 25 (right)
Goal: Find the maximum element in the BST
Step 1: Start at root node 10
10 [curr->10] -> 5 (left), 15 (right)
Why: We begin at the root to find the maximum
Step 2: Move curr to right child 15
10 -> 15 [curr->15] -> 20 (right)
Why: Maximum is always in the right subtree, so move right
Step 3: Move curr to right child 20
10 -> 15 -> 20 [curr->20] -> 25 (right)
Why: Keep moving right to find bigger values
Step 4: Move curr to right child 25
10 -> 15 -> 20 -> 25 [curr->25] -> null
Why: 25 has no right child, so it is the maximum
Result:
10 -> 15 -> 20 -> 25 [curr->25] -> null
Maximum element is 25
Annotated Code
DSA C++
#include <iostream>
#include <stdexcept>
using namespace std;

struct Node {
    int data;
    Node* left;
    Node* right;
    Node(int val) : data(val), left(nullptr), right(nullptr) {}
};

int findMax(Node* root) {
    if (root == nullptr) {
        throw runtime_error("Tree is empty");
    }
    Node* curr = root;
    while (curr->right != nullptr) {
        curr = curr->right; // advance curr to right child to find max
    }
    return curr->data; // curr is rightmost node, max value
}

int main() {
    Node* root = new Node(10);
    root->left = new Node(5);
    root->right = new Node(15);
    root->right->right = new Node(20);
    root->right->right->right = new Node(25);

    cout << "Maximum element is " << findMax(root) << endl;
    return 0;
}
while (curr->right != nullptr) {
keep moving curr to right child to find the maximum
curr = curr->right; // advance curr to right child to find max
advance curr pointer to next right node
return curr->data; // curr is rightmost node, max value
return the data of the rightmost node as maximum
OutputSuccess
Maximum element is 25
Complexity Analysis
Time: O(h) because we only traverse down the right edge of the tree where h is the height
Space: O(1) because we use only a few pointers and no extra space proportional to input
vs Alternative: Compared to traversing all nodes (O(n)), this is faster as it uses BST property to go directly right
Edge Cases
Empty tree (root is nullptr)
Throws an error indicating the tree is empty
DSA C++
if (root == nullptr) { throw runtime_error("Tree is empty"); }
Tree with only one node
Returns that single node's value as maximum
DSA C++
while (curr->right != nullptr) { ... } // loop skipped if no right child
When to Use This Pattern
When you see a BST and need the largest value, look for the rightmost node by following right children until none remain.
Common Mistakes
Mistake: Trying to find max by checking all nodes instead of just going right
Fix: Use the BST property and move only to the right child until null
Mistake: Not handling empty tree and causing null pointer errors
Fix: Add a check for root == nullptr and handle it properly
Summary
Finds the maximum element by moving to the rightmost node in the BST.
Use when you want the largest value in a BST quickly.
The maximum is always the rightmost node because BST nodes to the right are bigger.