0
0
DSA Javascriptprogramming~10 mins

BST Inorder Predecessor in DSA Javascript - Execution Trace

Choose your learning style9 modes available
Concept Flow - BST Inorder Predecessor
Start at root node
Find node with given key
Does node have left child?
NoGo up to ancestors
Find ancestor where node is in right subtree
Go to left child
Go to rightmost node in left subtree
This node is inorder predecessor
Done
To find the inorder predecessor, first find the node. If it has a left child, go to that left subtree and find its rightmost node. Otherwise, move up ancestors until you find a node that is a right child of its parent.
Execution Sample
DSA Javascript
function inorderPredecessor(root, key) {
  let node = findNode(root, key);
  if (!node) return null;
  if (node.left) {
    let pred = node.left;
    while (pred.right) pred = pred.right;
    return pred;
  }
  let ancestor = root;
  let predecessor = null;
  while (ancestor !== node) {
    if (node.val > ancestor.val) {
      predecessor = ancestor;
      ancestor = ancestor.right;
    } else {
      ancestor = ancestor.left;
    }
  }
  return predecessor;
}
This code finds the inorder predecessor of a node with given key in a BST.
Execution Table
StepOperationNode VisitedPointer ChangesVisual State
1Start at root50root points to 5050
2Find node with key=4050move left50 -> 30 -> 40
3Find node with key=4030move right50 -> 30 -> 40
4Node found40node = 4050 -> 30 -> 40
5Check if node has left child40node.left = 3540 has left child 35
6Go to left child35pred = 3540 -> 35
7Check right child of pred35pred.right = null35 has no right child
8Rightmost node in left subtree found35pred = 35Inorder predecessor is 35
9Return predecessor35return 35Done
10Exit--Predecessor found: 35
💡 Predecessor found as rightmost node in left subtree of node 40
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6After Step 8Final
nodenull4040404040
prednullnullnull353535
ancestor------
predecessornullnullnullnullnullnull
Key Moments - 3 Insights
Why do we look for the rightmost node in the left subtree?
Because the inorder predecessor is the largest node smaller than the current node, which is the rightmost node in its left subtree, as shown in steps 6-8 in the execution_table.
What if the node has no left child? How do we find the predecessor?
We move up to ancestors until we find a node where the current node is in the right subtree. This is not shown in this example but is part of the code logic after step 8.
Why do we stop moving right when pred.right is null?
Because the rightmost node has no right child, so we have found the largest node in the left subtree, as shown in step 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 6, what is the value of pred?
A35
B40
C30
Dnull
💡 Hint
Check the 'Pointer Changes' and 'Node Visited' columns at step 6.
At which step does the algorithm confirm the inorder predecessor?
AStep 4
BStep 8
CStep 7
DStep 10
💡 Hint
Look for the step where 'Rightmost node in left subtree found' is noted.
If node 40 had no left child, what would the algorithm do next?
AReturn null immediately
BLook for the rightmost node in the right subtree
CMove up ancestors to find a node where current node is in right subtree
DReturn the root node as predecessor
💡 Hint
Refer to the concept_flow and code logic after checking left child.
Concept Snapshot
BST Inorder Predecessor:
- Find node with given key.
- If node has left child, go to left subtree's rightmost node.
- Else, move up ancestors to find a node where current node is in right subtree.
- That node is the inorder predecessor.
- Returns null if no predecessor exists.
Full Transcript
To find the inorder predecessor in a BST, start by locating the node with the given key. If the node has a left child, the predecessor is the rightmost node in that left subtree. If there is no left child, move up the tree to find an ancestor where the node lies in the right subtree. This ancestor is the predecessor. The execution table traces these steps with node visits and pointer changes, showing how the predecessor is found as node 35 for key 40 in this example.