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
What happens if you try to insert a value that already exists in the BST?
Typically, BSTs do not allow duplicate values. The insert operation will ignore the duplicate or handle it based on specific rules, such as inserting duplicates to the right subtree.
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) when the tree is balanced. In the worst case (skewed tree), it can be O(n).
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
CStop insertion
DGo to the left child
✗ Incorrect
In a BST, smaller values go to the left subtree.
What is the first step when inserting a new value into a BST?
AInsert at the leaf
BCompare with the root node
CDelete a node
DBalance the tree
✗ Incorrect
Insertion starts by comparing the new value with the root node.
If the BST is empty, where do you insert the new node?
AAs the root node
BAs the left child
CAs the right child
DCannot insert
✗ Incorrect
If the tree is empty, the new node becomes the root.
What is the worst-case time complexity of inserting into a BST?
AO(n log n)
BO(log n)
CO(n)
DO(1)
✗ Incorrect
In the worst case, the BST is skewed and insertion takes O(n) time.
Which of these is NOT true about BST insertion?
ADuplicates are always inserted to the left
BInsertion compares values to decide direction
CInsertion finds an empty spot to add the node
DInsertion maintains BST property
✗ Incorrect
Duplicates are usually not inserted or handled specifically; they are not always inserted to the left.
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 /5 concepts.
Describe the time complexity of inserting a node in a BST and what affects it.
Consider how tree shape affects search path length.
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 add a new node while keeping the BST property intact
C. To find the maximum value in the BST
D. To balance the BST automatically
Solution
Step 1: Understand the Insert operation
The Insert operation adds a new value to the BST while maintaining the order property: left child < parent < right child.
Step 2: Identify the purpose of Insert
Insert does not delete nodes, find max, or balance the tree automatically. Its main job is to add nodes correctly.
Final Answer:
To add a new node while keeping the BST property intact -> Option B
Quick Check:
Insert = Add node correctly [OK]
Hint: Insert adds nodes preserving BST order [OK]
Common Mistakes:
Confusing insert with delete operation
Thinking insert balances the tree automatically
Assuming insert finds max or min values
2. Which of the following is the correct C# method signature for inserting a value into a BST node class?
A. Missing assignment when inserting into left or right subtree
B. Incorrect base case for null root
C. Method should return void instead of Node
D. Using else instead of else if for right subtree insertion
Solution
Step 1: Analyze recursive calls
Recursive calls to Insert on left or right subtree do not assign the returned node back to root.Left or root.Right.
Step 2: Identify fix
Must assign: root.Left = Insert(value, root.Left); and similarly for right to update tree links.
Final Answer:
Missing assignment when inserting into left or right subtree -> Option A
Quick Check:
Assign recursive result to subtree [OK]
Hint: Always assign recursive insert result to child node [OK]
Common Mistakes:
Not assigning recursive insert result
Changing base case incorrectly
Confusing return type with void
5. You have a BST with nodes 10, 5, 15. You want to insert 12 and 18. After insertion, which of the following represents the correct pre-order traversal?
hard
A. 10 -> 15 -> 18 -> 12 -> 5 -> null
B. 10 -> 5 -> 12 -> 15 -> 18 -> null
C. 10 -> 15 -> 5 -> 12 -> 18 -> null
D. 10 -> 5 -> 15 -> 12 -> 18 -> null
Solution
Step 1: Insert 12 and 18 into BST
Start with root 10, left child 5, right child 15. Insert 12: 12 < 15, goes left of 15. Insert 18: 18 > 15, goes right of 15.