0
0
DSA Javascriptprogramming~10 mins

BST Insert Operation in DSA Javascript - Execution Trace

Choose your learning style9 modes available
Concept Flow - BST Insert Operation
Start at root
Compare new value with current node
Go left
Is child null?
Insert new node
Back to Compare
Start at the root, compare the new value with current node, go left if smaller, right if greater or equal, repeat until a null child is found, then insert the new node there.
Execution Sample
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.left = null;
    this.right = null;
  }
}

function insert(root, value) {
  if (!root) return new Node(value);
  if (value < root.data) root.left = insert(root.left, value);
  else root.right = insert(root.right, value);
  return root;
}
This code inserts a value into a BST by recursively finding the correct position and adding a new node.
Execution Table
StepOperationCurrent NodeComparisonPointer ChangesTree State
1Start at root5050 ? 30None 50 / ∅ ∅
2Compare 30 < 505030 < 50 → go leftNone 50 / ∅ ∅
3Left child is null, insert 3050Left child null50.left → Node(30) 50 / 30 ∅
4Insert complete30Inserted at left of 50None 50 / 30 ∅
💡 Insertion done when a null child is found and new node is linked.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
rootNode(50)Node(50)Node(50)Node(50)
currentNode(50)Node(50)null (left child)Node(30) inserted
root.leftnullnullNode(30)Node(30)
Key Moments - 3 Insights
Why do we check if the left or right child is null before inserting?
Because the new node must be inserted at a position where there is no existing child. The execution_table row 3 shows insertion happens only when left child is null.
What happens if the new value is equal to a node's data?
The code treats equal values as greater or equal and moves to the right child. This is shown in the concept_flow where 'Greater or Equal' leads right.
Why do we return the root after insertion?
Returning the root ensures the tree structure is maintained and updated correctly up the recursive calls, as seen in the code sample.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the tree state after step 3?
A30 as root node only
B50 with no children
C50 with left child 30 and right child null
DEmpty tree
💡 Hint
Check the 'Tree State' column in row 3 of execution_table
At which step does the insertion actually happen?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look for 'insert' or 'Pointer Changes' in execution_table rows
If we insert 70 instead of 30, which direction will the traversal go from root 50?
ALeft
BRight
CNo movement, insert at root
DBoth left and right
💡 Hint
Refer to concept_flow where values greater or equal go right
Concept Snapshot
BST Insert Operation:
- Start at root node
- Compare new value with current node
- Go left if smaller, right if greater or equal
- Repeat until null child found
- Insert new node at null position
- Return root to maintain tree structure
Full Transcript
The BST Insert Operation starts at the root node. We compare the new value with the current node's data. If the new value is smaller, we move to the left child; if greater or equal, we move to the right child. We continue this until we find a null child pointer, where we insert the new node. The root is returned to maintain the tree structure. The execution table shows step-by-step how the tree changes, including pointer updates and the tree's visual state. Key moments clarify why we check for null children before insertion, how equal values are handled, and why returning the root is important.