A BST is a tree where each node has at most two children. The left child's value is less than the parent's value, and the right child's value is greater than the parent'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
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 steps to insert the value 7 into this BST: 5 -> left: 3, right: 8
Start at root 5. Since 7 > 5, move right to 8. Since 7 < 8, move left which is empty. Insert 7 as left child of 8.
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) because you halve the search space each step. In the worst case (unbalanced 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?
AGo to the left child
BGo to the right child
CReplace the current node
DStop insertion
✗ Incorrect
In a BST, smaller values go to the left child.
What do you do if you reach a null (empty) child while inserting a value in a BST?
AInsert the new node here
BGo back to the root
CIgnore the value
DDelete the parent node
✗ Incorrect
You insert the new node at the empty spot found.
What is the worst-case time complexity of BST insert operation?
AO(1)
BO(log n)
CO(n log n)
DO(n)
✗ Incorrect
In the worst case, the BST is like a linked list, so insertion takes O(n).
If the BST is empty, what happens when you insert the first value?
AIt becomes the left child
BIt becomes the root node
CInsertion fails
DIt becomes the right child
✗ Incorrect
The first inserted value becomes the root of the BST.
Which traversal order is best to print BST values in sorted order?
APost-order
BPre-order
CIn-order
DLevel-order
✗ Incorrect
In-order traversal visits nodes in ascending order in a BST.
Explain the step-by-step process of inserting a new value into a BST.
Think about how you decide where to put the new value by comparing it to nodes.
You got /5 concepts.
Describe how the BST property helps in efficient insertion.
Consider how the tree structure guides where to insert.
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 find the height of the BST
B. To delete a node from the BST
C. To add a new node while keeping the BST property intact
D. To traverse the BST in order
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 C
Quick Check:
Insert = Add node correctly [OK]
Hint: Insert means add node keeping order [OK]
Common Mistakes:
Confusing insert with delete operation
Thinking insert changes tree traversal
Assuming insert changes tree height always
2. Which of the following is the correct way to declare a BST node structure in C for insertion?
easy
A. typedef struct Node { int data; struct Node* left; struct Node* right; } Node;
B. struct Node { int data; int left; int right; };
C. typedef struct Node { int data; Node left; Node right; } Node;
D. struct Node { int data; struct Node left; struct Node right; };
Solution
Step 1: Check pointer usage in struct
BST nodes must have pointers to left and right child nodes, not direct structs or ints.
Step 2: Validate correct syntax
typedef struct Node { int data; struct Node* left; struct Node* right; } Node; uses pointers to struct Node correctly, others use wrong types or missing pointers.
Final Answer:
typedef struct Node { int data; struct Node* left; struct Node* right; } Node; -> Option A
Quick Check:
Node pointers needed for children [OK]
Hint: Use pointers for left and right child nodes [OK]
Common Mistakes:
Using int instead of pointers for children
Missing struct keyword before Node*
Declaring children as struct instead of pointers
3. Given the following BST and insertion code snippet, what will be the inorder traversal output after inserting 25?
20
/ \
10 30
// Insert function called with root and 25
medium
A. [10, 20, 25, 30]
B. [10, 20, 30, 25]
C. [10, 25, 20, 30]
D. [20, 10, 25, 30]
Solution
Step 1: Insert 25 in BST
25 is greater than 20, so move right to 30. 25 is less than 30, so insert as left child of 30.
Step 2: Inorder traversal after insertion
Inorder visits left, root, right: 10, 20, 25, 30.
Final Answer:
[10, 20, 25, 30] -> Option A
Quick Check:
Inorder sorted = [10, 20, 25, 30] [OK]
Hint: Inorder traversal of BST is sorted list [OK]
Common Mistakes:
Inserting 25 as right child of 30
Misordering inorder traversal
Confusing preorder with inorder
4. Identify the error in the following BST insert function snippet: