0
0
DSA Typescriptprogramming~3 mins

Why BST Find Maximum Element in DSA Typescript?

Choose your learning style9 modes available
The Big Idea

What if you could find the biggest number instantly without checking every single one?

The Scenario

Imagine you have a big stack of books sorted by height, but they are all mixed up on a table. You want to find the tallest book quickly.

If you try to check each book one by one, it will take a long time and you might lose track.

The Problem

Checking every book manually is slow and tiring. You might miss some books or get confused about which is tallest.

Doing this with many numbers or data points by hand is even harder and error-prone.

The Solution

A Binary Search Tree (BST) organizes data so that the biggest item is always easy to find by just following one path.

Finding the maximum means going right until you can't go further, making it fast and simple.

Before vs After
Before
function findMaxManual(arr: number[]): number {
  let max = arr[0];
  for (let i = 1; i < arr.length; i++) {
    if (arr[i] > max) max = arr[i];
  }
  return max;
}
After
function findMaxBST(root: TreeNode | null): number | null {
  if (!root) return null;
  let current = root;
  while (current.right) {
    current = current.right;
  }
  return current.value;
}
What It Enables

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

Real Life Example

In a leaderboard for a game, finding the highest score fast is important. A BST helps find the top score quickly.

Key Takeaways

Manual search is slow and error-prone.

BST structure makes max value easy to find by going right.

Efficient for large sorted data sets.