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 than the 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 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 duplicate or handle it based on specific rules, like inserting duplicates to the right subtree.
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, but in the worst case (like a skewed tree), it can be O(n), where n is the number of nodes.
Click to reveal answer
When inserting a value into a BST, if the value is less than the current node, where do you go next?
AReplace current node
BRight child
CLeft child
DStop insertion
✗ Incorrect
In a BST, smaller values go to the left child.
What is the first step when inserting a new value into an empty BST?
ACreate a new root node with the value
BSearch for the value
CInsert as left child
DInsert as right child
✗ Incorrect
If the BST is empty, the new value becomes the root node.
What is the worst-case time complexity of inserting a node in a BST?
AO(log n)
BO(n)
CO(1)
DO(n log n)
✗ Incorrect
In the worst case, the BST becomes skewed, making insertion O(n).
If a BST already contains the value you want to insert, what usually happens?
AInsert duplicate to the left
BInsert duplicate to the right
CReplace the existing node
DIgnore the duplicate
✗ Incorrect
Most BST implementations ignore duplicates to keep unique values.
Which traversal method is NOT used to insert a node in a BST?
AIn-order traversal
BGoing left or right child based on comparison
CFollowing BST property by comparing values
DPre-order traversal
✗ Incorrect
In-order traversal is for visiting nodes, not for insertion.
Explain step-by-step how to insert a new value into a Binary Search Tree.
Think about how you decide where to place the new value by comparing it with nodes.
You got /5 concepts.
Describe the time complexity of inserting a node in a BST and what affects it.
Consider how the shape of the tree changes the steps needed.
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 add a new node while keeping the BST property intact
B. To delete a node from the BST
C. To find the maximum value in the BST
D. To balance the BST after insertion
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 A
Quick Check:
Insert = Add node maintaining order [OK]
Hint: Insert adds nodes without breaking 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 TypeScript function signature for inserting a value into a BST node?
easy
A. function insert(root: Node, value: number): void
B. function insert(value: number, root: Node): void
C. function insert(root: Node | null, value: number): Node | null
D. function insert(root: Node, value: string): Node
Solution
Step 1: Analyze parameter types
Insert needs the root node (which can be null if tree is empty) and the value to insert (a number).
Step 2: Analyze return type
It returns the updated root node (Node or null if empty).
Final Answer:
function insert(root: Node | null, value: number): Node | null -> Option C
Quick Check:
Correct params and return type [OK]
Hint: Insert returns updated root, accepts root or null [OK]
Common Mistakes:
Swapping parameter order
Using void return type instead of returning Node
Using wrong value type like string
3. Given the following BST and the insert operation call insert(root, 7), what will be the in-order traversal of the BST after insertion?
Initial BST in-order: 3 -> 5 -> 8 -> 10
Code snippet:
class Node {
constructor(public val: number, public left: Node | null = null, public right: Node | null = null) {}
}
function insert(root: Node | null, val: number): Node {
if (!root) return new Node(val);
if (val < root.val) root.left = insert(root.left, val);
else root.right = insert(root.right, val);
return root;
}
medium
A. [3, 5, 8, 7, 10]
B. [3, 5, 8, 10, 7]
C. [7, 3, 5, 8, 10]
D. [3, 5, 7, 8, 10]
Solution
Step 1: Locate insertion point for 7
7 is greater than 5 but less than 8, so it goes to the left of 8.
Step 2: Perform in-order traversal after insertion