BST Insert Operation in DSA Typescript - Time & Space Complexity
We want to understand how the time to insert a new value in a Binary Search Tree (BST) changes as the tree grows.
How does the number of steps needed to insert depend on the size and shape of the tree?
Analyze the time complexity of the following code snippet.
function insert(root: TreeNode | null, val: number): TreeNode {
if (root === null) return new TreeNode(val);
if (val < root.val) {
root.left = insert(root.left, val);
} else {
root.right = insert(root.right, val);
}
return root;
}
This code inserts a value into a BST by moving down the tree until it finds the right spot.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls moving down one level of the tree.
- How many times: At most once per level from root to leaf, depending on tree height.
Each insert moves down the tree one step at a time until it finds a spot.
| Input Size (n) | Approx. Operations (steps down tree) |
|---|---|
| 10 | Up to 10 steps if tree is skewed, about 3-4 if balanced |
| 100 | Up to 100 steps if skewed, about 6-7 if balanced |
| 1000 | Up to 1000 steps if skewed, about 9-10 if balanced |
Pattern observation: In worst case, steps grow linearly with n; in average case, steps grow slowly with tree height (log n).
Time Complexity: O(h) where h is the tree height
This means the time depends on how tall the tree is, which can be as small as log n or as large as n.
[X] Wrong: "Insertion always takes the same time regardless of tree shape."
[OK] Correct: If the tree is very unbalanced (like a linked list), insertion takes longer because you must go through many nodes.
Understanding how tree shape affects insertion time helps you explain trade-offs and choose the right data structure in real problems.
What if we used a balanced BST like an AVL tree? How would the time complexity change?
Practice
insert operation in a Binary Search Tree (BST)?Solution
Step 1: Understand BST property
A BST keeps nodes ordered so that left child is smaller and right child is larger than the parent.Step 2: Purpose of insert operation
Insert adds a new node in the correct position to maintain this order.Final Answer:
To add a new node while keeping the BST property intact -> Option AQuick Check:
Insert = Add node maintaining order [OK]
- Confusing insert with delete operation
- Thinking insert balances the tree automatically
- Assuming insert finds max or min values
Solution
Step 1: Analyze parameter types
Insert needs the root node (which can be null if tree is empty) and the value to insert (a number).Step 2: Analyze return type
It returns the updated root node (Node or null if empty).Final Answer:
function insert(root: Node | null, value: number): Node | null -> Option CQuick Check:
Correct params and return type [OK]
- Swapping parameter order
- Using void return type instead of returning Node
- Using wrong value type like string
insert(root, 7), what will be the in-order traversal of the BST after insertion?Initial BST in-order: 3 -> 5 -> 8 -> 10
Code snippet:
class Node {
constructor(public val: number, public left: Node | null = null, public right: Node | null = null) {}
}
function insert(root: Node | null, val: number): Node {
if (!root) return new Node(val);
if (val < root.val) root.left = insert(root.left, val);
else root.right = insert(root.right, val);
return root;
}Solution
Step 1: Locate insertion point for 7
7 is greater than 5 but less than 8, so it goes to the left of 8.Step 2: Perform in-order traversal after insertion
In-order visits left, root, right: 3, 5, 7, 8, 10.Final Answer:
[3, 5, 7, 8, 10] -> Option DQuick Check:
In-order sorted after insert [OK]
- Placing 7 incorrectly after 8
- Confusing in-order traversal order
- Ignoring recursive insert logic
function insert(root: Node | null, val: number): Node {
if (root === null) {
return new Node(val);
}
if (val <= root.val) {
insert(root.left, val);
} else {
insert(root.right, val);
}
return root;
}Solution
Step 1: Check recursive calls
The calls to insert do not assign the result back to root.left or root.right, so tree structure is not updated.Step 2: Correct recursive assignment
Should be root.left = insert(root.left, val) or root.right = insert(root.right, val) to update links.Final Answer:
The recursive calls do not update root.left or root.right -> Option BQuick Check:
Must assign recursive insert result to child [OK]
- Forgetting to assign recursive call result
- Misunderstanding base case presence
- Changing comparison operator unnecessarily
Solution
Step 1: Start at root 10
6 is less than 10, so move to left child 5.Step 2: Compare with 5 and 7
6 is greater than 5, move to right child 7. 6 is less than 7, so insert here.Final Answer:
[10, 5, 7] -> Option AQuick Check:
Traverse nodes comparing 6 until insertion point [OK]
- Visiting wrong subtree based on comparison
- Skipping nodes in path
- Confusing insertion path with traversal order
Ready to prove it?
Pass the test and this pattern moves unknown
