0
0
DSA Goprogramming~3 mins

Why BST Find Maximum Element in DSA Go?

Choose your learning style9 modes available
The Big Idea

What if you could find the biggest thing in a huge pile without looking at every single item?

The Scenario

Imagine you have a messy pile of books with different heights, and you want to find the tallest one. You start picking up each book one by one, checking its height, and comparing it with the tallest you found so far.

The Problem

This manual way is slow and tiring because you have to check every single book. If the pile is huge, it takes a lot of time and you might lose track or make mistakes.

The Solution

A Binary Search Tree (BST) organizes books so that the tallest book is always at one end. To find the tallest book, you just follow the path to the rightmost book, saving time and effort.

Before vs After
Before
maxHeight := books[0]
for i := 1; i < len(books); i++ {
    if books[i] > maxHeight {
        maxHeight = books[i]
    }
}
After
func findMax(node *Node) int {
    current := node
    for current.Right != nil {
        current = current.Right
    }
    return current.Value
}
What It Enables

This lets you quickly find the largest value in a sorted structure without checking every element.

Real Life Example

In a phone contact list sorted by names, finding the last contact alphabetically is like finding the maximum element in a BST.

Key Takeaways

Manual search checks every item and is slow.

BST stores data so max is always at the rightmost node.

Finding max in BST is fast and simple by going right.