0
0
DSA Typescriptprogramming~3 mins

Why BST Over Plain Binary Tree in DSA Typescript - The Real Reason

Choose your learning style9 modes available
The Big Idea

What if you could find any name in a huge tree without searching every branch?

The Scenario

Imagine you have a big family tree drawn on paper with names scattered randomly. You want to find a cousin's name quickly, but you have to look through every name one by one.

The Problem

Searching through a random tree means checking many nodes without any order. This takes a lot of time and can be confusing, especially if the tree is large.

The Solution

A Binary Search Tree (BST) keeps names in order: smaller names go left, bigger names go right. This order helps you skip many nodes and find names faster.

Before vs After
Before
function searchNode(node, value) {
  if (!node) return false;
  if (node.value === value) return true;
  return searchNode(node.left, value) || searchNode(node.right, value);
}
After
function searchBST(node, value) {
  if (!node) return false;
  if (node.value === value) return true;
  if (value < node.value) return searchBST(node.left, value);
  else return searchBST(node.right, value);
}
What It Enables

BSTs let you find, add, or remove items quickly even in big collections by using order to skip unnecessary checks.

Real Life Example

Think of a phone book sorted by last names. You don't flip every page; you jump to the section starting with the letter you want. BST works like that for data.

Key Takeaways

Random trees require checking many nodes, making search slow.

BSTs keep data ordered to speed up search and updates.

Using BSTs is like having a sorted list that helps you find things faster.