0
0
DSA Goprogramming

BST Find Maximum Element in DSA Go

Choose your learning style9 modes available
Mental Model
In a BST, the biggest number is always the rightmost node. Just keep going right until you can't go further.
Analogy: Imagine a line of people standing from shortest to tallest, left to right. The tallest person is at the far right end.
      10
     /  \
    5    15
          \
           20
            \
             25
↑root
Dry Run Walkthrough
Input: BST with nodes 10 -> 5, 15 -> 20 -> 25; find max element
Goal: Find the largest value in the BST by moving right until no more right child
Step 1: Start at root node 10
10 -> [curr->15] -> 20 -> 25
5 (left child of 10)
Why: We begin at the root to find the maximum
Step 2: Move curr pointer from 10 to right child 15
10 -> 15 -> [curr->20] -> 25
5 (left child of 10)
Why: Maximum is always in the right subtree, so move right
Step 3: Move curr pointer from 15 to right child 20
10 -> 15 -> 20 -> [curr->25]
5 (left child of 10)
Why: Keep moving right to find bigger values
Step 4: Move curr pointer from 20 to right child 25
10 -> 15 -> 20 -> 25 [curr]
5 (left child of 10)
Why: Continue moving right until no more right child
Step 5: No right child of 25, stop and return 25
10 -> 15 -> 20 -> 25 [curr]
5 (left child of 10)
Why: Rightmost node found, this is the maximum
Result:
10 -> 15 -> 20 -> 25 [max]
5 (left child of 10)
Maximum element is 25
Annotated Code
DSA Go
package main

import "fmt"

// Node represents a BST node
type Node struct {
    val   int
    left  *Node
    right *Node
}

// Insert inserts a value into the BST
func (n *Node) Insert(val int) *Node {
    if n == nil {
        return &Node{val: val}
    }
    if val < n.val {
        n.left = n.left.Insert(val)
    } else {
        n.right = n.right.Insert(val)
    }
    return n
}

// FindMax finds the maximum value in the BST
func (n *Node) FindMax() int {
    curr := n
    for curr.right != nil {
        curr = curr.right // move right to find bigger values
    }
    return curr.val // rightmost node value is max
}

func main() {
    var root *Node
    values := []int{10, 5, 15, 20, 25}
    for _, v := range values {
        root = root.Insert(v)
    }
    maxVal := root.FindMax()
    fmt.Printf("Maximum element is %d\n", maxVal)
}
curr := n
start at root node
for curr.right != nil {
keep moving right while right child exists
curr = curr.right // move right to find bigger values
advance curr to right child to find max
return curr.val // rightmost node value is max
return value of rightmost node
OutputSuccess
Maximum element is 25
Complexity Analysis
Time: O(h) because we only traverse down the right children where h is the tree height
Space: O(1) because we use only a pointer variable and no extra space
vs Alternative: Compared to scanning all nodes (O(n)), this is faster as it uses BST property to go directly right
Edge Cases
Empty tree (root is nil)
Function is not called or handled outside; no max to find
DSA Go
No explicit guard in FindMax; caller must check root != nil
Tree with single node
FindMax returns that node's value immediately
DSA Go
for curr.right != nil { ... } loop exits immediately
All nodes only on left side (no right children)
FindMax returns root value as no right child exists
DSA Go
for curr.right != nil { ... } loop exits immediately
When to Use This Pattern
When asked to find the largest element in a BST, look for the rightmost node by following right children until none remain.
Common Mistakes
Mistake: Trying to search both left and right subtrees for max instead of just going right
Fix: Use BST property and only move right until no right child
Mistake: Not handling empty tree and calling FindMax on nil
Fix: Check if root is nil before calling FindMax
Summary
Finds the largest value in a BST by moving right until no more right child.
Use when you need the maximum element quickly in a BST.
The maximum is always the rightmost node in a BST.