BST Insert Operation in DSA Javascript - Time & Space Complexity
We want to understand how the time to insert a value in a Binary Search Tree (BST) changes as the tree grows.
How does the number of steps needed to insert a new value change when the tree has more nodes?
Analyze the time complexity of the following code snippet.
function insertNode(root, value) {
if (root === null) {
return { val: value, left: null, right: null };
}
if (value < root.val) {
root.left = insertNode(root.left, value);
} else {
root.right = insertNode(root.right, value);
}
return root;
}
This code inserts a new value into a BST by moving left or right until it finds the right spot.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls moving down the tree nodes.
- How many times: At most once per tree level until the correct leaf spot is found.
Each insert moves down one level in the tree. The number of levels depends on the tree height.
| Input Size (n) | Approx. Operations (tree height) |
|---|---|
| 10 | About 3 to 10 steps |
| 100 | About 7 to 100 steps |
| 1000 | About 10 to 1000 steps |
Pattern observation: If the tree is balanced, steps grow slowly (logarithmic). If unbalanced, steps can grow linearly with 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 small if balanced or large if skewed.
[X] Wrong: "Insertion always takes the same time regardless of tree shape."
[OK] Correct: The shape of the tree affects how many steps we take. A tall, unbalanced tree means more steps.
Understanding how tree shape affects insert time helps you explain trade-offs and choose the right tree type in real problems.
What if we changed the BST to a balanced tree like an AVL or Red-Black tree? How would the time complexity change?
Practice
insert operation do in a Binary Search Tree (BST)?Solution
Step 1: Understand BST property
In a BST, left child nodes are smaller and right child nodes are larger than the parent node.Step 2: Purpose of insert operation
The insert operation places a new value in the tree so that this property remains true.Final Answer:
Adds a new node in the correct position to maintain BST order -> Option BQuick Check:
Insert means add node correctly [OK]
- Confusing insert with delete operation
- Thinking insert balances the tree automatically
- Assuming insert searches only without adding
val into a BST node root in JavaScript?Solution
Step 1: Understand function usage in BST insert
Insert usually returns the new root after insertion, so assignment is needed.Step 2: Check parameter order and assignment
The function takes the root and value, returns updated root, soroot = insert(root, val);is correct.Final Answer:
root = insert(root, val); -> Option AQuick Check:
Assign updated root after insert [OK]
- Calling insert without assignment
- Passing parameters in wrong order
- Using method call on root without method defined
10
/
3 15
4Insert 5 and then print inorder traversal.
Solution
Step 1: Insert 5 into BST
Start at root 10, 5 < 10 go left to 3, 5 > 3 go right to 4, 5 > 4 insert as right child of 4.Step 2: Perform inorder traversal
Inorder visits left, root, right: nodes visited in order: 3, 4, 5, 10, 15.Final Answer:
[3, 4, 5, 10, 15] -> Option CQuick Check:
Inorder sorted after insert [OK]
- Inserting 5 as left child of 4
- Mixing preorder or postorder traversal output
- Ignoring BST property during insertion
function insert(root, val) {
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
Recursive calls to insert do not assign the returned node back to root.left or root.right, so tree structure won't update.Step 2: Correct recursive assignment
Should assign:root.left = insert(root.left, val);orroot.right = insert(root.right, val);to update links.Final Answer:
Missing assignment of recursive insert result to root.left or root.right -> Option DQuick Check:
Assign recursive insert result to child [OK]
- Ignoring assignment in recursion
- Confusing base case with recursive step
- Thinking '==' causes error here
Solution
Step 1: Insert nodes step-by-step
Start with root 20, left 10, right 30.
Insert 25: 25 < 30 and > 20, goes left of 30.
Insert 5: less than 10, goes left of 10.
Insert 35: greater than 30, goes right of 30.Step 2: Preorder traversal
Preorder visits root, left subtree, right subtree.
Visit 20, then left subtree (10, 5), then right subtree (30, 25, 35).
So preorder: [20, 10, 5, 30, 25, 35]Final Answer:
[20, 10, 5, 30, 25, 35] -> Option AQuick Check:
Preorder = root-left-right [OK]
- Mixing preorder with inorder or postorder
- Inserting nodes in wrong position
- Forgetting to update tree after insert
Ready to prove it?
Pass the test and this pattern moves unknown
