0
0
DSA Javascriptprogramming~3 mins

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

Choose your learning style9 modes available
The Big Idea

What if you could find any book in a huge messy pile in just a few steps?

The Scenario

Imagine you have a big box of unsorted books and you want to find a specific one quickly.

You start flipping through each book one by one, hoping to find the right title.

The Problem

Searching this way is slow and tiring because you have no order to follow.

You waste time checking books that are not what you want.

The Solution

A Binary Search Tree (BST) organizes books so that smaller titles go to the left and bigger titles go to the right.

This order helps you skip many books and find the one you want much faster.

Before vs After
Before
function findBook(books, title) {
  for (let i = 0; i < books.length; i++) {
    if (books[i] === title) return i;
  }
  return -1;
}
After
function findBookInBST(node, title) {
  if (!node) return null;
  if (node.value === title) return node;
  if (title < node.value) return findBookInBST(node.left, title);
  else return findBookInBST(node.right, title);
}
What It Enables

BSTs let you find, add, or remove items quickly by following a clear order.

Real Life Example

Phone contacts are often stored in a BST-like structure so you can quickly find a name without scrolling through the entire list.

Key Takeaways

Plain binary trees have no order, making search slow.

BSTs keep data sorted to speed up search and updates.

Using BSTs saves time and effort in many applications.