0
0
DSA Typescriptprogramming~10 mins

BST Find Maximum Element in DSA Typescript - 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 right until no right child exists; that node is the maximum.
Execution Sample
DSA Typescript
function findMax(root: TreeNode | null): number | null {
  if (!root) return null;
  let current = root;
  while (current.right) {
    current = current.right;
  }
  return current.val;
}
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 to right child20Yescurrent = current.right (20)15 / \ 10 20 / \ 17 25
3Move to right child25Nocurrent = current.right (25)15 / \ 10 20 / \ 17 25
4Check right child25Nocurrent stays at 2515 / \ 10 20 / \ 17 25
5Return max value25N/AReturn 2515 / \ 10 20 / \ 17 25
💡 No right child at node 25, so 25 is the maximum element.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
current.valN/A1520252525
current.rightN/ANode(20)Node(25)nullnullnull
Key Moments - 3 Insights
Why do we keep moving to the right child to find the maximum?
Because in a BST, all right children have values greater than their parent. The execution_table rows 1 to 4 show moving right until no right child exists.
What if the tree is empty (root is null)?
The code returns null immediately (see execution_sample line 2). No steps occur in execution_table because the tree is empty.
Why do we stop when current.right is null, not when current is null?
Because current is always a valid node. We stop when there is no right child to move to, meaning current is the max. See execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'current.val' at Step 3?
A25
B15
C20
D17
💡 Hint
Check the 'Current Node Value' column at Step 3 in execution_table.
At which step does the condition 'Right Child Exists?' become No?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Right Child Exists?' column in execution_table.
If the root had no right child, how would the execution_table change?
AIt would move to Step 2 and beyond
BIt would return null immediately
CIt would stop at Step 1 with 'Right Child Exists?' as No
DIt would cause an error
💡 Hint
Refer to the flow where right child check is done; if none exists at root, we stop immediately.
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 BST right children are larger
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 and repeat. Continue moving right until you reach a node with no right child. That node contains the maximum value in the BST. If the tree is empty, return null. This works because in BSTs, right children always have greater values than their parents. The code sample shows this logic using a while loop. The execution table traces each step, showing the current node and pointer changes until the maximum is found.