0
0
DSA Typescriptprogramming

BST Insert Operation in DSA Typescript

Choose your learning style9 modes available
Mental Model
A binary search tree keeps smaller values on the left and larger on the right. Inserting means finding the right empty spot following this rule.
Analogy: Imagine placing books on a shelf where all books with smaller titles go to the left side and bigger titles to the right. To add a new book, you walk down the shelf choosing left or right until you find an empty spot.
    10
   /  \
  5    15
 / \   / \
null null null null
Dry Run Walkthrough
Input: Insert value 12 into BST: 10 -> 5 -> null, 10 -> 15 -> null
Goal: Add 12 to the BST in the correct position to keep the order
Step 1: Start at root node 10
10 ↑ -> 5 -> null, 10 -> 15 -> null
Why: We always start insertion from the root to find the correct place
Step 2: Compare 12 with 10, move right to node 15
10 -> 5 -> null, 10 -> 15 ↑ -> null
Why: 12 is greater than 10, so we go right
Step 3: Compare 12 with 15, move left where null is found
10 -> 5 -> null, 10 -> 15 -> null ↑
Why: 12 is less than 15, so we go left to find empty spot
Step 4: Insert new node with value 12 at left of 15
10 -> 5 -> null, 10 -> 15 -> 12 -> null
Why: Found empty spot, insert here to keep BST order
Result:
10 -> 5 -> null, 10 -> 15 -> 12 -> null
Annotated Code
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val: number) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

function insertIntoBST(root: TreeNode | null, val: number): TreeNode {
  if (root === null) {
    return new TreeNode(val); // insert here if spot is empty
  }
  if (val < root.val) {
    root.left = insertIntoBST(root.left, val); // go left
  } else {
    root.right = insertIntoBST(root.right, val); // go right
  }
  return root; // return unchanged root
}

function printBST(root: TreeNode | null): string {
  if (root === null) return "null";
  const leftStr = printBST(root.left);
  const rightStr = printBST(root.right);
  return `${root.val} -> ${leftStr}, ${root.val} -> ${rightStr}`;
}

// Driver code
const root = new TreeNode(10);
root.left = new TreeNode(5);
root.right = new TreeNode(15);
const newRoot = insertIntoBST(root, 12);
console.log(printBST(newRoot));
if (root === null) { return new TreeNode(val); }
insert new node when empty spot found
if (val < root.val) { root.left = insertIntoBST(root.left, val); }
go left if value is smaller
else { root.right = insertIntoBST(root.right, val); }
go right if value is bigger or equal
return root;
return current root to link back after insertion
OutputSuccess
10 -> 5 -> null, 10 -> null, 10 -> 15 -> 12 -> null, 12 -> null, 15 -> null
Complexity Analysis
Time: O(h) where h is the height of the tree because we follow one path down to insert
Space: O(h) due to recursion stack in worst case equal to tree height
vs Alternative: Compared to inserting in an unsorted tree which is O(1), BST insert is slower but keeps data ordered for fast search
Edge Cases
Empty tree (root is null)
New node becomes the root
DSA Typescript
if (root === null) { return new TreeNode(val); }
Insert value smaller than all nodes
Traverse left until null and insert there
DSA Typescript
if (val < root.val) { root.left = insertIntoBST(root.left, val); }
Insert value larger than all nodes
Traverse right until null and insert there
DSA Typescript
else { root.right = insertIntoBST(root.right, val); }
When to Use This Pattern
When you need to add a value to a sorted tree structure and keep order, use BST insert to find the correct spot by comparing values.
Common Mistakes
Mistake: Inserting without checking if current node is null, causing errors
Fix: Always check if current node is null to insert new node there
Mistake: Not linking the inserted node back to parent, losing the new node
Fix: Assign the result of recursive insert back to left or right child pointer
Summary
Inserts a new value into the binary search tree while keeping it ordered.
Use when you want to add data to a BST and maintain its sorted property.
The key is to follow left or right pointers based on comparisons until you find an empty spot.