📖 Scenario: You are working with a Binary Search Tree (BST) that stores numbers. In a BST, all numbers to the left of a node are smaller, and all numbers to the right are larger. You want to find the largest number stored in this tree.
🎯 Goal: Build a small program that creates a BST, then finds and prints the maximum number stored in it.
📋 What You'll Learn
Create a BST with the exact numbers: 15, 10, 20, 8, 12, 17, 25
Create a variable called current to help find the maximum element
Use a while loop to move to the rightmost node in the BST
Print the maximum value found in the BST
💡 Why This Matters
🌍 Real World
Finding the maximum value in a BST is useful in many applications like databases, search engines, and file systems where data is stored in sorted order.
💼 Career
Understanding BST operations like finding maximum values is important for software developers working with data structures, algorithms, and performance optimization.
Progress0 / 4 steps
1
Create the BST nodes
Create a class called Node with a constructor that takes value and sets left and right to null. Then create the root node called root with value 15. Add the following nodes to build the BST exactly as shown: 10, 20, 8, 12, 17, 25. Connect them properly to form the BST.
DSA Javascript
Hint
Remember, each node has a value and two children: left and right. Connect nodes so smaller values go left, larger go right.
2
Set up a variable to find the maximum
Create a variable called current and set it equal to root. This will help you move through the tree to find the maximum value.
DSA Javascript
Hint
Use let current = root; to start at the root node.
3
Find the maximum value by moving right
Use a while loop with the condition current.right !== null. Inside the loop, update current to current.right. This will move current to the rightmost node, which holds the maximum value.
DSA Javascript
Hint
Keep moving current to the right child until there is no right child left.
4
Print the maximum value
Print the maximum value found in the BST by writing console.log(current.value).
DSA Javascript
Hint
Use console.log(current.value) to show the maximum number.
Practice
(1/5)
1. In a Binary Search Tree (BST), where is the maximum element always located?
easy
A. At any leaf node
B. At the leftmost node
C. At the root node
D. At the rightmost node
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 D
Quick Check:
Maximum = rightmost node [OK]
Hint: Maximum is always the rightmost node in BST [OK]
Common Mistakes:
Thinking maximum is at the root
Confusing leftmost with maximum
Assuming maximum can be any leaf
2. Which of the following JavaScript code snippets correctly finds the maximum element in a BST node structure where each node has left and right properties?
easy
A. function findMax(node) { while(node.right) { node = node.right; } return node.value; }
B. function findMax(node) { while(node.left) { node = node.left; } return node.value; }
C. function findMax(node) { return node.value; }
D. function findMax(node) { if(!node) return null; return findMax(node.left); }
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 while node.right exists, then returns node.value.
Final Answer:
function findMax(node) { while(node.right) { node = node.right; } return node.value; } -> Option A
Quick Check:
Traverse rightmost node to get max [OK]
Hint: Loop right child until none to find max [OK]
Common Mistakes:
Looping left child instead of right
Returning root value without traversal
Recursing incorrectly on left child
3. What will be the output of the following code snippet?
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));
medium
A. 20
B. 10
C. 15
D. 5
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 A
Quick Check:
Rightmost node value = 20 [OK]
Hint: Output is value of rightmost node [OK]
Common Mistakes:
Returning root value instead of max
Confusing left and right children
Not following loop to last right node
4. The following code is intended to find the maximum element in a BST but does not work correctly. What is the error?
function findMax(node) {
if (!node.right) {
return node.value;
}
return findMax(node.left);
}
medium
A. It should check node.left instead of node.right
B. It should return node.right.value instead of node.value
C. It should recurse on node.right, not node.left
D. It should use a loop instead of recursion
Solution
Step 1: Analyze recursion direction
The code checks if node.right is null, then returns node.value. Otherwise, it recurses on node.left.
Step 2: Identify correct recursion path
To find max, recursion must go right, not left. So it should recurse on node.right.
Final Answer:
It should recurse on node.right, not node.left -> Option C
Quick Check:
Max found by going right child [OK]
Hint: Recurse or loop right child to find max [OK]
Common Mistakes:
Recursing left child instead of right
Returning wrong node value
Thinking loop is mandatory over recursion
5. Given a BST where some nodes may have duplicate values, which approach correctly finds the maximum element considering duplicates are allowed only on the right subtree?