A Binary Search Tree is a tree data structure where each node has at most two children. For every node, all nodes in its left subtree have smaller values, and all nodes in its right subtree have larger values.
Click to reveal answer
beginner
What is the main rule to follow when inserting a new value into a BST?
Compare the new value with the current node's value. If smaller, go to the left child; if larger, go to the right child. Repeat until you find an empty spot to insert the new node.
Click to reveal answer
intermediate
What happens if you try to insert a value that already exists in the BST?
Usually, BSTs do not allow duplicate values. The insert operation will ignore the new value or handle duplicates based on specific rules, but commonly it does not insert duplicates.
Click to reveal answer
beginner
Show the step-by-step insertion of 5 into this BST: 10 -> 4 -> 15
Start at root 10: 5 < 10, go left to 4. 5 > 4, go right which is empty. Insert 5 as right child of 4.
Click to reveal answer
intermediate
What is the time complexity of inserting a node in a BST?
The average time complexity is O(log n) if the tree is balanced, but in the worst case (like a linked list), it can be O(n).
Click to reveal answer
When inserting a new value into a BST, where do you go if the new value is greater than the current node's value?
ATo the left child
BTo the right child
CReplace the current node
DStop insertion
✗ Incorrect
In a BST, values greater than the current node go to the right child.
What is the first step when inserting a new node into a BST?
ACompare with the root node
BInsert at the leftmost position
CInsert at the rightmost position
DDelete the root node
✗ Incorrect
Insertion starts by comparing the new value with the root node.
If a BST is unbalanced and looks like a linked list, what is the worst-case time complexity for insertion?
AO(n)
BO(log n)
CO(1)
DO(n log n)
✗ Incorrect
In the worst case, insertion takes O(n) time if the BST is like a linked list.
Which of these is NOT true about BST insertion?
AInsertion compares values to decide direction
BInsertion follows the BST property
CInsertion finds the correct empty spot
DDuplicates are always inserted to the right
✗ Incorrect
Duplicates are usually not inserted or handled specially; they are not always inserted to the right.
After inserting a new node, what property must the BST maintain?
AAll left children are greater than the node
BAll right children are smaller than the node
CLeft subtree nodes are smaller, right subtree nodes are greater
DThe tree becomes balanced automatically
✗ Incorrect
BST property requires left subtree nodes to be smaller and right subtree nodes to be greater.
Explain how to insert a new value into a Binary Search Tree step-by-step.
Think about walking down the tree comparing values.
You got /6 concepts.
Describe the time complexity of inserting a node in a BST and what affects it.
Consider how deep the tree is.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the insert operation in a Binary Search Tree (BST)?
easy
A. To delete a node from the BST
B. To find the height of the BST
C. To add a new node while maintaining BST properties
D. To traverse the BST in order
Solution
Step 1: Understand BST insert operation
The insert operation adds a new node to the BST while keeping the order property intact: left child < parent < right child.
Step 2: Differentiate from other operations
Deleting nodes, finding height, or traversing are different operations and do not describe insertion.
Final Answer:
To add a new node while maintaining BST properties -> Option C
Quick Check:
Insert = Add node maintaining order [OK]
Hint: Insert means add node keeping BST order intact [OK]
Common Mistakes:
Confusing insert with delete operation
Thinking insert changes tree height only
Mixing insert with traversal methods
2. Which of the following is the correct syntax to insert a value val into a BST using a recursive function in C++?
easy
A. root = insert(root, val);
B. insert(root, val);
C. root.insert(val);
D. insert(val, root);
Solution
Step 1: Understand recursive insert signature
Recursive insert usually returns the new root after insertion, so assignment like root = insert(root, val); is needed.
Step 2: Check other options
Calling insert(root, val); without assignment ignores updated root. root.insert(val); assumes method on node object which is uncommon. insert(val, root); has wrong parameter order.
Final Answer:
root = insert(root, val); -> Option A
Quick Check:
Assign returned root after insert [OK]
Hint: Assign root = insert(root, val) for recursive insertion [OK]
Common Mistakes:
Not assigning the returned root after insertion
Using wrong parameter order in insert call
Assuming insert is a method of node object
3. Consider the following C++ code inserting values into an empty BST:
Insert order builds left 5, right 15 with left 12 [OK]
Hint: Insert smaller left, larger right; 12 is left child of 15 [OK]
Common Mistakes:
Placing 12 as right child of 15 instead of left
Swapping left and right children of 10
Ignoring null children in output
4. The following code attempts to insert a value into a BST but causes a runtime error:
Node* insert(Node* root, int val) {
if (root == nullptr) {
Node* newNode = new Node(val);
}
if (val < root->data) {
root->left = insert(root->left, val);
} else {
root->right = insert(root->right, val);
}
return root;
}
What is the error and how to fix it?
medium
A. Missing return after creating new node; add return newNode;
B. Incorrect comparison operator; use <= instead of <
C. Should not assign to root->left or root->right
D. Node constructor is missing initialization
Solution
Step 1: Identify missing return in base case
When root is nullptr, new node is created but not returned, so function continues and dereferences nullptr causing runtime error.
Step 2: Fix by returning new node immediately
Add return newNode; inside the if block to return the newly created node and stop further execution.
Final Answer:
Missing return after creating new node; add return newNode; -> Option A
Quick Check:
Return new node on nullptr base case [OK]
Hint: Always return new node immediately when root is nullptr [OK]
Common Mistakes:
Not returning new node causes nullptr dereference
Changing comparison operator unnecessarily
Misunderstanding pointer assignments
5. You have a BST with nodes 20, 10, 30, 25, 40. You want to insert 15 and 35. After insertion, which of the following is the correct in-order traversal of the BST?
hard
A. [15, 10, 20, 25, 30, 35, 40]
B. [10, 15, 20, 25, 30, 35, 40]
C. [10, 20, 15, 25, 30, 35, 40]
D. [10, 15, 20, 30, 25, 35, 40]
Solution
Step 1: Insert 15 and 35 into BST
Insert 15: less than 20, greater than 10, goes right of 10. Insert 35: less than 40, greater than 30, goes right of 30.
Step 2: Perform in-order traversal
In-order traversal visits nodes in ascending order: left subtree, root, right subtree. The order is 10, 15, 20, 25, 30, 35, 40.
Final Answer:
[10, 15, 20, 25, 30, 35, 40] -> Option B
Quick Check:
In-order traversal = sorted nodes [OK]
Hint: In-order traversal of BST is sorted list [OK]
Common Mistakes:
Placing 15 or 35 in wrong subtree
Confusing traversal order
Listing nodes unsorted in output
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.