Insertion in BST in Data Structures Theory - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When inserting a new value into a Binary Search Tree (BST), the time it takes depends on how deep we must go to find the right spot.
We want to understand how the number of steps grows as the tree gets bigger.
Analyze the time complexity of the following code snippet.
function insert(node, value) {
if (node == null) {
return new Node(value);
}
if (value < node.value) {
node.left = insert(node.left, value);
} else {
node.right = insert(node.right, value);
}
return node;
}
This code inserts a value into the BST by moving down the tree until it finds an empty 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 tree level, until a null spot is found.
Each insertion moves down the tree levels. The number of steps depends on the tree height.
| Input Size (n) | Approx. Operations (steps down tree) |
|---|---|
| 10 | Up to 4 steps |
| 100 | Up to 7 steps |
| 1000 | Up to 10 steps |
Pattern observation: In a balanced tree, steps grow slowly with input size, roughly proportional to the tree height.
Time Complexity: O(h)
This means the time to insert depends on the tree height, which is the number of levels from root to leaf.
[X] Wrong: "Insertion always takes the same time regardless of tree shape."
[OK] Correct: If the tree is unbalanced and looks like a list, insertion can take much longer because you must go through many nodes.
Understanding how insertion time depends on tree shape helps you explain trade-offs and why balanced trees matter in real applications.
"What if the BST is always perfectly balanced? How would the insertion time complexity change?"
Practice
Solution
Step 1: Understand BST insertion rule for smaller values
In a BST, if the new value is smaller than the current node, it must go to the left subtree.Step 2: Apply the rule to the insertion process
The new value is inserted into the left subtree, maintaining BST order.Final Answer:
The new value is inserted in the left subtree of the current node. -> Option CQuick Check:
Smaller value goes left [OK]
- Inserting smaller values to the right subtree
- Replacing the current node instead of inserting
- Ignoring the new value
Solution
Step 1: Identify insertion behavior for an empty BST
When the BST is empty, the first inserted value becomes the root node.Step 2: Confirm the correct insertion method
Setting the new value as the root node initializes the BST correctly.Final Answer:
Set the new value as the root node of the BST. -> Option AQuick Check:
Empty tree insertion sets root [OK]
- Trying to insert as child without a root
- Ignoring insertion in empty tree
- Inserting randomly without root
10 / 5 15What will the BST look like after inserting the value
12?Solution
Step 1: Compare 12 with root and right child
12 is greater than 10, so move to right subtree. 12 is less than 15, so it goes to the left of 15.Step 2: Insert 12 as left child of 15
Place 12 as the left child of node 15 to maintain BST order.Final Answer:
12 is inserted as left child of 15. -> Option AQuick Check:
12 > 10 and 12 < 15, so left of 15 [OK]
- Inserting 12 as right child of 15
- Inserting 12 as right child of 10
- Ignoring BST order rules
function insert(node, value):
if node is null:
return new Node(value)
if value > node.value:
node.left = insert(node.left, value)
else:
node.right = insert(node.right, value)
return node
Solution
Step 1: Analyze insertion direction for larger values
The code inserts values greater than current node into the left subtree, which is incorrect.Step 2: Correct insertion direction for BST
Larger values should be inserted into the right subtree to maintain BST order.Final Answer:
The code inserts larger values to the left subtree instead of right. -> Option DQuick Check:
Larger values go right, not left [OK]
- Swapping left and right insertion conditions
- Missing base case for null node
- Returning wrong node after insertion
Solution
Step 1: Understand BST insertion for equal values
Standard BST insertion places values equal to the current node in the right subtree.Step 2: Insert new 30 as right child of existing 30
The new 30 should be inserted as the right child of the existing 30 node.Final Answer:
As the right child of the existing 30 node. -> Option BQuick Check:
Equal values go right subtree [OK]
- Inserting duplicates to the left subtree
- Replacing existing nodes instead of inserting
- Ignoring duplicates insertion rules
