A Binary Search Tree is a tree data structure where each node has at most two children. For every node, all values in the left subtree are smaller, and all values in the right subtree are larger.
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
Why do we insert nodes recursively or iteratively in a BST?
Because we need to follow the BST property at each step, checking left or right child until we find the correct 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 can ignore the new value or handle duplicates in a specific way, like storing counts or placing duplicates in a consistent subtree.
Click to reveal answer
beginner
Show the state of BST after inserting 5, 3, 7 in order into an empty BST.
The BST looks like this: 5 ├─ 3 └─ 7 Where 5 is the root, 3 is left child, and 7 is right child.
Click to reveal answer
When inserting a value into a BST, where do you go if the value is less than the current node?
AReplace the current node
BGo to the right child
CGo to the left child
DStop insertion
✗ Incorrect
In a BST, smaller values go to the left subtree.
What is the time complexity of inserting a value in a balanced BST?
AO(log n)
BO(n log n)
CO(1)
DO(n)
✗ Incorrect
In a balanced BST, insertion takes O(log n) time because we halve the search space at each step.
If the BST is empty, where do you insert the first value?
AAs the left child
BAs the root node
CAs the right child
DNowhere, BST must have nodes first
✗ Incorrect
The first inserted value becomes the root of the BST.
What do you do if the value to insert equals the current node's value?
AReplace the current node
BInsert in the right subtree
CInsert in the left subtree
DIgnore or handle duplicates specially
✗ Incorrect
BSTs usually do not allow duplicates or handle them in a specific way.
Which traversal order would show the BST values in sorted order?
AIn-order
BPre-order
CPost-order
DLevel-order
✗ Incorrect
In-order traversal visits nodes in ascending order in a BST.
Explain step-by-step how to insert a new value into a BST.
Think about walking down the tree comparing values.
You got /5 concepts.
Describe what the BST looks like after inserting values 10, 5, 15, 3, 7 in order.
Draw the tree step by step as you insert.
You got /5 concepts.
Practice
(1/5)
1. What is the main purpose of the insert operation in a Binary Search Tree (BST)?
easy
A. To balance the BST automatically
B. To add a new value while keeping the BST property intact
C. To find the maximum value in the BST
D. To delete a node from the BST
Solution
Step 1: Understand the BST property
A BST keeps values in order: left child smaller, right child larger.
Step 2: Purpose of insert operation
Insert adds a new value so the tree remains ordered by BST rules.
Final Answer:
To add a new value while keeping the BST property intact -> Option B
Quick Check:
Insert = Add value maintaining order [OK]
Hint: Insert means add value keeping order [OK]
Common Mistakes:
Confusing insert with delete
Thinking insert balances the tree automatically
Believing insert finds max value
2. Which of the following is the correct Python function signature for inserting a value into a BST node?
easy
A. def insert(root, value):
B. def insert(value, root):
C. def insert(value):
D. def insert(root):
Solution
Step 1: Identify parameters needed for insertion
We need the root node to start and the value to insert.
Step 2: Check function signature correctness
Signature with root first, then value is common and clear: def insert(root, value):.
Final Answer:
def insert(root, value): -> Option A
Quick Check:
Insert(root, value) signature [OK]
Hint: Insert needs root node and value [OK]
Common Mistakes:
Swapping parameter order
Missing root parameter
Using only one parameter
3. What will be the output of the BST after inserting 7 into the BST shown below?