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.
Practice
Solution
Step 1: Understand BST property
In a BST, left child nodes have smaller values and right child nodes have larger values than their parent.Step 2: Locate maximum element
The maximum element is the largest value, so it must be the rightmost node in the tree.Final Answer:
At the rightmost node -> Option DQuick Check:
Maximum = rightmost node [OK]
- Thinking maximum is at the root
- Confusing leftmost with maximum
- Assuming maximum can be any leaf
left and right properties?Solution
Step 1: Check traversal direction
To find maximum, we must move right until no more right child exists.Step 2: Verify code correctness
function findMax(node) { while(node.right) { node = node.right; } return node.value; } correctly loops whilenode.rightexists, then returnsnode.value.Final Answer:
function findMax(node) { while(node.right) { node = node.right; } return node.value; } -> Option AQuick Check:
Traverse rightmost node to get max [OK]
- Looping left child instead of right
- Returning root value without traversal
- Recursing incorrectly on left child
class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
const root = new Node(10);
root.left = new Node(5);
root.right = new Node(15);
root.right.right = new Node(20);
function findMax(node) {
while(node.right) {
node = node.right;
}
return node.value;
}
console.log(findMax(root));Solution
Step 1: Trace the tree structure
Root is 10, right child 15, right of 15 is 20. The rightmost node is 20.Step 2: Follow findMax function
The function moves right until no right child. It stops at node with value 20 and returns it.Final Answer:
20 -> Option AQuick Check:
Rightmost node value = 20 [OK]
- Returning root value instead of max
- Confusing left and right children
- Not following loop to last right node
function findMax(node) {
if (!node.right) {
return node.value;
}
return findMax(node.left);
}Solution
Step 1: Analyze recursion direction
The code checks ifnode.rightis null, then returnsnode.value. Otherwise, it recurses onnode.left.Step 2: Identify correct recursion path
To find max, recursion must go right, not left. So it should recurse onnode.right.Final Answer:
It should recurse on node.right, not node.left -> Option CQuick Check:
Max found by going right child [OK]
- Recursing left child instead of right
- Returning wrong node value
- Thinking loop is mandatory over recursion
function findMax(node) {
while(node.right && node.right.value >= node.value) {
node = node.right;
}
return node.value;
}What is the main reason for the condition
node.right.value >= node.value in the loop?Solution
Step 1: Understand duplicates in BST
Duplicates allowed only on right subtree means equal values can appear on right child nodes.Step 2: Analyze loop condition
The loop moves right while right child exists and its value is greater or equal to current node, ensuring duplicates are included in max search.Final Answer:
To move right only if the right child is greater or equal, handling duplicates correctly -> Option BQuick Check:
Right child >= current node handles duplicates [OK]
- Ignoring duplicates on right subtree
- Using strict greater than instead of greater or equal
- Confusing left subtree duplicates
Ready to prove it?
Pass the test and this pattern moves unknown
