0
0
DSA Javascriptprogramming~10 mins

BST Find Maximum Element in DSA Javascript - Execution Trace

Choose your learning style9 modes available
Concept Flow - BST Find Maximum Element
Start at root node
Check if right child exists?
NoCurrent node is max
Yes
Move to right child
Back to check right child
Start at the root and keep moving to the right child until no right child exists; that node is the maximum.
Execution Sample
DSA Javascript
function findMax(root) {
  let current = root;
  while (current.right !== null) {
    current = current.right;
  }
  return current.value;
}
This code finds the maximum value in a BST by moving right until no right child exists.
Execution Table
StepOperationCurrent Node ValueRight Child Exists?Pointer ChangesVisual State
1Start at root15Yescurrent = root (15)15 / \ 10 20 / \ 17 25
2Move right20Yescurrent = current.right (20)15 / \ 10 20 / \ 17 25
3Move right25Nocurrent = current.right (25)15 / \ 10 20 / \ 17 25
4No right child25NoStop15 / \ 10 20 / \ 17 25
💡 No right child at node 25, so 25 is the maximum element.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
current.value1515202525
Key Moments - 3 Insights
Why do we keep moving to the right child to find the maximum?
In a BST, all right children have values greater than their parent. The execution_table rows 1 to 3 show moving right to find larger values until no right child exists.
What if the tree has only one node?
If the root has no right child (like step 1 with no right child), the root itself is the maximum, as shown in the exit condition at step 4.
Why do we stop when current.right is null, not when current is null?
Stopping when current.right is null means current is the largest node. If we moved until current is null, we would go past the largest node, losing the reference. See step 4 in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of current after step 2?
A20
B15
C25
D17
💡 Hint
Check the 'Current Node Value' column at step 2 in execution_table.
At which step does the algorithm stop because there is no right child?
AStep 3
BStep 2
CStep 4
DStep 1
💡 Hint
Look for the step where 'Right Child Exists?' is 'No' and operation is 'No right child' in execution_table.
If the node with value 25 had a right child 30, what would be the current node value after step 3?
A25
B30
C20
D17
💡 Hint
If right child exists, current moves right as per execution_table steps 2 and 3.
Concept Snapshot
BST Find Maximum Element:
- Start at root node
- While right child exists, move right
- When no right child, current node is max
- Return current node's value
- Works because right subtree has larger values
Full Transcript
To find the maximum element in a Binary Search Tree (BST), start at the root node. Check if the current node has a right child. If yes, move to that right child. Repeat this until the current node has no right child. The node without a right child is the maximum element. This works because in BSTs, right children always have greater values than their parents. The code uses a loop to move right until no right child is found, then returns that node's value.