0
0
DSA Goprogramming~10 mins

BST Find Maximum Element in DSA Go - 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 there is no right child left. The last node visited is the maximum.
Execution Sample
DSA Go
func FindMax(root *Node) *Node {
    current := root
    for current != nil && current.Right != nil {
        current = current.Right
    }
    return current
}
This code finds the maximum element 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
4Return current25NoReturn node with value 25Max element found: 25
💡 No right child at node 25, so 25 is the maximum element.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
currentroot (15)15202525
Key Moments - 3 Insights
Why do we keep moving to the right child to find the maximum?
In a BST, all values to the right are greater. The execution_table rows 1 to 3 show moving right until no right child exists, which means the current node is the largest.
What if the tree has only one node?
If the root has no right child (execution_table step 1 shows 'Right Child Exists?' as No), the root itself is the maximum.
Why do we stop when current.Right is nil, not when current is nil?
Stopping at current.Right == nil means current is the last node on the right path. If we moved to current == nil, we would lose the reference to the max node. See execution_table step 3 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'current' after step 2?
A25
B20
C15
D17
💡 Hint
Check the 'Current Node Value' column at step 2 in execution_table.
At which step does the algorithm stop moving right?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for the step where 'Right Child Exists?' is No in execution_table.
If the root node had no right child, what would be the maximum element?
AThe root node value
BThe left child value
CNil (no max)
DThe right child value
💡 Hint
Refer to key_moments about single node tree and execution_table step 1.
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
- Works because BST 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 the right child and repeat. Continue this until the current node has no right child. This last node is the maximum element. This works because in a BST, all values in the right subtree are greater than the current node. The provided code uses a loop to move right until no right child exists, then returns the current node as the maximum. The execution table shows each step with the current node value and pointer changes. Key moments clarify why we move right and when to stop. The visual quiz tests understanding of the steps and conditions.