0
0
DSA Typescriptprogramming~3 mins

Why BST Find Minimum Element in DSA Typescript?

Choose your learning style9 modes available
The Big Idea

What if you could find the smallest item instantly without checking everything?

The Scenario

Imagine you have a messy stack of books with no order, and you want to find the oldest book quickly.

You start flipping through each book one by one to find the oldest date.

The Problem

Checking every book manually takes a lot of time and effort.

It's easy to miss the oldest book or get tired and make mistakes.

This slow process wastes your time and energy.

The Solution

A Binary Search Tree (BST) organizes books so the oldest is always on the left side.

To find the oldest book, you just keep going left until you can't go further.

This way, you find the minimum quickly without checking every book.

Before vs After
Before
function findOldestBook(books: number[]): number {
  let min = books[0];
  for (let i = 1; i < books.length; i++) {
    if (books[i] < min) min = books[i];
  }
  return min;
}
After
function findMinNode(root: TreeNode | null): TreeNode | null {
  let current = root;
  while (current && current.left !== null) {
    current = current.left;
  }
  return current;
}
What It Enables

This lets you find the smallest value in a sorted structure instantly, saving time and effort.

Real Life Example

In a phone contact list sorted by name, finding the first contact alphabetically is like finding the minimum in a BST.

Key Takeaways

Manual search is slow and error-prone.

BST keeps data ordered to find minimum quickly.

Just follow left children to find the smallest value.