Binary Search Trees (BST) Quiz
40 comprehensive questions exploring binary search tree operations — with 16 code examples covering searching, insertion, deletion, and BST properties in this cpp quiz.
Question 1
What is a binary search tree?
Question 2
In a BST, where is the smallest element located?
BST property: left < root < right
Smallest: leftmost leafQuestion 3
What is the average time complexity for searching in a BST?
Question 4
How does BST insertion maintain the binary search property?
Insert 5 into BST with root 3:
Compare 5 > 3, go right
Insert as right childQuestion 5
What happens when you insert a duplicate value into a BST?
Question 6
In BST deletion, what is the most complex case?
Delete node with two children:
Find inorder successor
Replace node value
Delete successorQuestion 7
What is the inorder successor of a node in a BST?
Question 8
How does BST search work?
bool search(Node* root, int key) {
if (!root) return false;
if (key == root->val) return true;
if (key < root->val) return search(root->left, key);
return search(root->right, key);
}Question 9
What is the worst-case time complexity for BST operations?
Question 10
In BST insertion, where is a new node placed when the tree is empty?
Empty tree: root = new Node(value);Question 11
What is the BST invariant?
Question 12
How does BST deletion handle a leaf node?
Delete leaf: simply remove it
No children to worry aboutQuestion 13
What is the inorder predecessor of a node in BST?
Question 14
In BST, how do you find the k-th smallest element?
Use inorder traversal with counter:
Keep count during inorder traversalQuestion 15
What is the space complexity of BST operations?
Question 16
How does BST insertion handle the case where the insertion point is found?
Node* insert(Node* root, int val) {
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;
}Question 17
What is the advantage of BST over linear search?
Question 18
In BST deletion, how do you handle a node with one child?
Node has one child: replace with child
Parent points to child directlyQuestion 19
What happens to BST performance when insertions are in sorted order?
Question 20
How do you validate if a binary tree is a BST?
Check recursively:
bool isBST(Node* root, int min, int max)Question 21
What is the relationship between BST and quicksort?
Question 22
In BST, how do you find the floor of a given value?
Floor: largest value <= target
Traverse and track candidatesQuestion 23
What is the BST property violated in this tree?
5
/ \
3 7
/ \
2 6
6 > 5 but in left subtree - violation!Question 24
How does BST support range queries?
Question 25
What is the inorder traversal result for a BST?
BST inorder: always sorted ascending
Example: 2, 3, 5, 6, 7Question 26
In BST deletion with two children, why use inorder successor?
Question 27
What is the best-case height for a BST with n nodes?
Perfect BST: height = floor(log2(n))Question 28
How does BST handle duplicate values in practice?
Question 29
What is the amortized cost of BST operations?
Average case: O(log n)
Worst case: O(n)Question 30
In BST, how do you find all elements in a range [low, high]?
Question 31
What is the significance of BST in algorithm design?
BST enables:
- Fast search O(log n)
- Ordered operations
- Range queriesQuestion 32
How does BST insertion differ from binary tree insertion?
Question 33
What is the ceiling of a value in BST?
Ceiling: smallest value >= target
Similar to floor but opposite directionQuestion 34
In BST, what is the cost of finding the minimum element?
Question 35
How does BST support ordered iteration?
Inorder traversal gives sorted order:
for(auto val : bst) // sortedQuestion 36
What is the impact of deletion on BST balance?
Question 37
In BST, how do you count nodes within a range?
Range count: traverse and count
Can be optimized with subtree sizesQuestion 38
What makes BST different from hash tables?
Question 39
How does BST handle the problem of sorted input insertions?
Sorted input creates skewed tree:
1, 2, 3, 4, 5 -> linked listQuestion 40
Considering BST operations and their performance characteristics, which fundamental property makes BSTs valuable despite their worst-case performance issues in practice?
