BST Insert Operation in DSA C - 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 depend on the tree size?
Analyze the time complexity of the following code snippet.
struct Node {
int data;
struct Node* left;
struct Node* right;
};
struct Node* insert(struct Node* root, int val) {
if (root == NULL) {
struct Node* newNode = malloc(sizeof(struct Node));
newNode->data = val;
newNode->left = newNode->right = NULL;
return newNode;
}
if (val < root->data) {
root->left = insert(root->left, val);
} else {
root->right = insert(root->right, val);
}
return root;
}
This code inserts a value into a BST by recursively finding the correct spot and adding a new node.
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 path from root to leaf, checking nodes along the way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 4-5 steps (tree height) |
| 100 | Up to 7 steps |
| 1000 | Up to 10 steps |
Pattern observation: In a balanced tree, steps grow slowly, roughly proportional to the tree height, not total nodes.
Time Complexity: O(h) where h is the tree height
This means the time depends on how tall the tree is, which can be much smaller than total nodes if balanced.
[X] Wrong: "Insertion always takes time proportional to the number of nodes (n)."
[OK] Correct: In a balanced BST, insertion only follows one path down, so it depends on height, not total nodes.
Understanding BST insert time helps you explain how data structures keep operations efficient as data grows.
"What if the BST becomes a linked list (completely unbalanced)? 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 CQuick Check:
Insert = Add node correctly [OK]
- Confusing insert with delete operation
- Thinking insert changes tree traversal
- Assuming insert changes tree height always
Solution
Step 1: Check pointer usage in struct
BST nodes must have pointers to left and right child nodes, not direct structs or ints.Step 2: Validate correct syntax
typedef struct Node { int data; struct Node* left; struct Node* right; } Node; uses pointers to struct Node correctly, others use wrong types or missing pointers.Final Answer:
typedef struct Node { int data; struct Node* left; struct Node* right; } Node; -> Option AQuick Check:
Node pointers needed for children [OK]
- Using int instead of pointers for children
- Missing struct keyword before Node*
- Declaring children as struct instead of pointers
20 / \ 10 30 // Insert function called with root and 25
Solution
Step 1: Insert 25 in BST
25 is greater than 20, so move right to 30. 25 is less than 30, so insert as left child of 30.Step 2: Inorder traversal after insertion
Inorder visits left, root, right: 10, 20, 25, 30.Final Answer:
[10, 20, 25, 30] -> Option AQuick Check:
Inorder sorted = [10, 20, 25, 30] [OK]
- Inserting 25 as right child of 30
- Misordering inorder traversal
- Confusing preorder with inorder
Node* insert(Node* root, int val) {
if (root == NULL) {
Node* newNode = malloc(sizeof(Node));
newNode->data = val;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
if (val < root->data) {
root->left = insert(root->left, val);
} else {
root->right = insert(root->right, val);
}
return root;
}Solution
Step 1: Check base case return
When root is NULL, new node is created but not returned, so function returns garbage.Step 2: Fix missing return
Must return newNode immediately after creation to link it properly.Final Answer:
Missing return statement after creating new node -> Option BQuick Check:
Return new node in base case [OK]
- Forgetting to return new node in base case
- Incorrect pointer assignments
- Ignoring memory allocation errors
Solution
Step 1: Insert 16 in BST
16 is less than 17, so it becomes left child of 17.Step 2: Preorder traversal after insertion
Preorder visits root, left, right: 15, 10, 8, 12, 20, 17, 16, 25.Final Answer:
[15, 10, 8, 12, 20, 17, 16, 25] -> Option DQuick Check:
Preorder = root-left-right [OK]
- Inserting 16 as right child of 17
- Mixing preorder with inorder or postorder
- Skipping the new node in traversal
Ready to prove it?
Pass the test and this pattern moves unknown
