A Binary Search Tree is a tree data structure where each node has at most two children. The left child's value is less than the parent node's value, and the right child's value is greater than the parent node's value.
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 it is 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 use recursion or loops in BST insert operation?
Because we need to travel down the tree from the root to the correct leaf position where the new value fits, following the BST property. Recursion or loops help us move step-by-step through the nodes.
Click to reveal answer
intermediate
What happens if we 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 by a specific rule, like inserting to the right subtree.
Click to reveal answer
beginner
Show the result of inserting 7 into this BST: 5 -> 3 -> 8 -> null
After inserting 7, the BST looks like: 5 -> 3 -> 8 -> 7 -> null. The 7 is inserted as the left child of 8 because 7 < 8.
Click to reveal answer
Where do you insert a new value in a BST if it is smaller than the current node's value?
ALeft subtree
BRight subtree
CAt the current node
DAt the root
✗ 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)
CO(n log n)
DO(1)
✗ 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 new value?
ACannot insert
BAs the left child
CAs the root node
DAs the right child
✗ Incorrect
If the BST is empty, the new value becomes the root node.
What happens if you insert a value equal to a node's value in a typical BST?
AIt causes an error
BIt replaces the existing node
CIt is inserted as left child
DIt is ignored or handled by a rule
✗ Incorrect
BSTs usually do not allow duplicates or handle them by a specific rule.
Which traversal method helps to verify the BST property after insertion?
APost-order traversal
BIn-order traversal
CPre-order traversal
DLevel-order traversal
✗ Incorrect
In-order traversal of a BST prints values in sorted order, helping verify the BST property.
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 changes in the BST structure after inserting a new value.
Focus on where the new node fits and how the tree keeps order.
You got /4 concepts.
Practice
(1/5)
1. What does the insert operation do in a Binary Search Tree (BST)?
easy
A. Searches for a node with a given value
B. Adds a new node in the correct position to maintain BST order
C. Deletes a node from the tree
D. Balances the tree automatically after insertion
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 operation
The insert operation places a new value in the tree so that this property remains true.
Final Answer:
Adds a new node in the correct position to maintain BST order -> Option B
Quick Check:
Insert means add node correctly [OK]
Hint: Insert means add node keeping BST order intact [OK]
Common Mistakes:
Confusing insert with delete operation
Thinking insert balances the tree automatically
Assuming insert searches only without adding
2. Which of the following is the correct syntax to insert a value val into a BST node root in JavaScript?
easy
A. root = insert(root, val);
B. root.insert(val);
C. insert(root, val);
D. insert(val, root);
Solution
Step 1: Understand function usage in BST insert
Insert usually returns the new root after insertion, so assignment is needed.
Step 2: Check parameter order and assignment
The function takes the root and value, returns updated root, so root = insert(root, val); is correct.
Final Answer:
root = insert(root, val); -> Option A
Quick Check:
Assign updated root after insert [OK]
Hint: Insert returns new root, so assign it back [OK]
Common Mistakes:
Calling insert without assignment
Passing parameters in wrong order
Using method call on root without method defined
3. What will be the inorder traversal output after inserting 5 into this BST?
10
/
3 15
4
Insert 5 and then print inorder traversal.
medium
A. [3, 5, 4, 10, 15]
B. [3, 4, 10, 5, 15]
C. [3, 4, 5, 10, 15]
D. [5, 3, 4, 10, 15]
Solution
Step 1: Insert 5 into BST
Start at root 10, 5 < 10 go left to 3, 5 > 3 go right to 4, 5 > 4 insert as right child of 4.
Hint: Inorder traversal of BST is sorted list [OK]
Common Mistakes:
Inserting 5 as left child of 4
Mixing preorder or postorder traversal output
Ignoring BST property during insertion
4. Given this insert function snippet, what is the error?
function insert(root, val) {
if (root == null) {
return new Node(val);
}
if (val < root.val) {
insert(root.left, val);
} else {
insert(root.right, val);
}
return root;
}
medium
A. Returning root before insertion
B. Incorrect base case condition
C. Using '==' instead of '===' for null check
D. Missing assignment of recursive insert result to root.left or root.right
Solution
Step 1: Check recursive calls
Recursive calls to insert do not assign the returned node back to root.left or root.right, so tree structure won't update.
Step 2: Correct recursive assignment
Should assign: root.left = insert(root.left, val); or root.right = insert(root.right, val); to update links.
Final Answer:
Missing assignment of recursive insert result to root.left or root.right -> Option D
Quick Check:
Assign recursive insert result to child [OK]
Hint: Always assign recursive insert result to child pointer [OK]
Common Mistakes:
Ignoring assignment in recursion
Confusing base case with recursive step
Thinking '==' causes error here
5. You have a BST with nodes 20, 10, 30. You insert 25, then 5, then 35. What is the preorder traversal of the BST after all insertions?
hard
A. [20, 10, 5, 30, 25, 35]
B. [20, 30, 25, 35, 10, 5]
C. [10, 5, 20, 30, 25, 35]
D. [20, 10, 30, 5, 25, 35]
Solution
Step 1: Insert nodes step-by-step
Start with root 20, left 10, right 30. Insert 25: 25 < 30 and > 20, goes left of 30. Insert 5: less than 10, goes left of 10. Insert 35: greater than 30, goes right of 30.
Step 2: Preorder traversal
Preorder visits root, left subtree, right subtree. Visit 20, then left subtree (10, 5), then right subtree (30, 25, 35). So preorder: [20, 10, 5, 30, 25, 35]
Final Answer:
[20, 10, 5, 30, 25, 35] -> Option A
Quick Check:
Preorder = root-left-right [OK]
Hint: Preorder = root, left subtree, right subtree [OK]
Common Mistakes:
Mixing preorder with inorder or postorder
Inserting nodes in wrong position
Forgetting to update tree after insert
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.