BST Insert Operation in DSA Go - Time & Space Complexity
We want to understand how the time to insert a value into a Binary Search Tree 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.
func Insert(root *Node, val int) *Node {
if root == nil {
return &Node{Val: 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 Binary Search Tree 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 depending on tree shape |
| 100 | About 7 to 100 steps depending on tree shape |
| 1000 | About 10 to 1000 steps depending on tree shape |
Pattern observation: If the tree is balanced, steps grow slowly (logarithmic). If unbalanced, steps grow linearly with n.
Time Complexity: O(h) where h is the tree height
This means the time to insert 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 tree shape affects height; a tall, skewed tree means more steps to insert.
Understanding how tree height affects insertion helps you explain and improve tree operations in real problems.
"What if the tree is always perfectly balanced? How would that change the time complexity of insertion?"
Practice
Insert operation 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
Insert adds a new node in the correct position to keep this order intact.Final Answer:
To add a new node while keeping the BST property intact -> Option BQuick Check:
Insert = Add node correctly [OK]
- Confusing insert with delete operation
- Thinking insert balances the tree automatically
- Assuming insert finds max value
Solution
Step 1: Understand pointer usage in Go structs
BST nodes must use pointers for left and right to link nodes dynamically.Step 2: Check each option
type Node struct { value int; left *Node; right *Node } uses pointers (*Node) for left and right, correct for BST links.Final Answer:
type Node struct { value int; left *Node; right *Node } -> Option AQuick Check:
Use pointers for child nodes [OK]
- Using value types instead of pointers for children
- Mixing pointer and non-pointer types incorrectly
- Incorrect field names like 'val' instead of 'value'
root := &Node{value: 5}
root.left = &Node{value: 3}
root.right = &Node{value: 7}
Insert(root, 6)
// Inorder traversal function prints nodes in ascending orderSolution
Step 1: Understand BST structure before insertion
Initial tree: 5 root, 3 left child, 7 right child.Step 2: Insert 6
6 is less than 7 but greater than 5, so it becomes left child of 7.Step 3: Inorder traversal
Inorder visits left, root, right: 3, 5, 6, 7.Final Answer:
3 5 6 7 -> Option AQuick Check:
Inorder after insert = 3 5 6 7 [OK]
- Placing 6 as right child of 5 directly
- Confusing inorder traversal order
- Ignoring BST property during insertion
func Insert(root *Node, val int) *Node {
if root == nil {
return &Node{value: val}
}
if val < root.value {
root.left = Insert(root.left, val)
} else if val > root.value {
root.right = Insert(root.right, val)
}
return root
}Solution
Step 1: Review function logic
Function inserts recursively left or right based on value comparison.Step 2: Check duplicate handling
When val equals root.value, no insertion or special handling occurs.Step 3: Identify error
Duplicates are ignored silently; function should handle or document this case.Final Answer:
The function does not handle duplicate values explicitly -> Option CQuick Check:
Duplicates need explicit handling [OK]
- Assuming duplicates insert automatically
- Returning nil instead of root
- Using wrong comparison signs
Solution
Step 1: Start at root 10
6 is less than 10, move to left child 5.Step 2: Compare with 5
6 is greater than 5, move to right child 7.Step 3: Compare with 7
6 is less than 7, insert as left child of 7.Final Answer:
10 -> 5 -> 7 -> Option DQuick Check:
Traverse nodes comparing values [OK]
- Visiting wrong child nodes
- Including inserted node in visited sequence
- Confusing greater and smaller comparisons
Ready to prove it?
Pass the test and this pattern moves unknown
