What if you could find the biggest number without looking at every single one?
Why BST Find Maximum Element in DSA Javascript?
Imagine you have a messy pile of books with no order. You want to find the thickest book, but you have to check each one by hand, flipping through all of them one by one.
Checking every book takes a lot of time and effort. You might miss some books or get tired and make mistakes. It is slow and frustrating to find the thickest book this way.
A Binary Search Tree (BST) organizes books by thickness from smallest to largest. To find the thickest book, you just follow the path to the rightmost book. This saves time and effort because you don't check every book.
function findMax(arr) {
let max = arr[0];
for (let i = 1; i < arr.length; i++) {
if (arr[i] > max) max = arr[i];
}
return max;
}function findMaxInBST(root) {
let current = root;
while (current.right !== null) {
current = current.right;
}
return current.value;
}This lets you quickly find the largest value in a sorted structure without checking every item.
Finding the highest score in a sorted leaderboard where scores are stored in a BST, so you just look at the rightmost player.
Manual search checks every item and is slow.
BST organizes data so max is always at the rightmost node.
Finding max in BST is fast and simple by following right children.