Binary search tree (BST) questions in Data Structures exams mostly follow one script: “Construct a BST by inserting these keys, write its traversals, then delete some keys and redraw the tree.” Around that core come definitions, algorithm writing (search, insert, delete), tracing a recursive function, and complexity, especially the worst case. This guide solves one complete example in exam format, then gives a question bank with model answers for written, lab and viva exams.
Tree & Graph series, step 6 of 8. Learn the operations first in the BST guide. Companion: Tree Questions for University Exams. Next: What Are DFS and BFS?
Part 1: Definitions
Q1. Define a binary search tree
Model answer. A Binary Search Tree (BST) is a type of binary tree data structure in which each node contains a unique key and satisfies a specific ordering property:
- All nodes in the left subtree of a node contain values strictly less than the node’s value.
- All nodes in the right subtree of a node contain values strictly greater than the node’s value.
This structure enables efficient operations for searching, insertion, and deletion of elements, especially when the tree remains balanced.
An empty tree is also a BST.
Q2. State the properties of a BST
- Inorder traversal visits the keys in ascending order.
- The minimum key is the leftmost node; the maximum is the rightmost node.
- Search, insert and delete each follow a single root-to-leaf path, so they take time.
- A new key is always inserted as a leaf.
- The shape depends on the insertion order: the same keys can give a balanced tree (height about ) or a chain (height ).
Q3. Differentiate BST and binary tree
| Basis | Binary tree | Binary search tree |
|---|---|---|
| Definition | Each node has at most two children | Binary tree + left < node < right for whole subtrees |
| Ordering | None | Strict ordering of keys |
| Searching | Must check every node, | One path, |
| Inorder output | Any order | Sorted |
| Insert position | Not fixed | Unique position as a leaf |
| Duplicates | Allowed freely | Usually not allowed |
| Example use | Expression trees, heaps | Dictionaries, sets, indexing |
Every BST is a binary tree, but a binary tree is a BST only if the ordering holds at every node.
Part 2: Construct a BST From a Given Sequence
Q4. Construct a binary search tree by inserting the following keys in the given order: 45, 15, 79, 90, 10, 55, 12, 20, 50.
Insert each key by comparing from the root: go left if smaller, right if larger, and attach it where you reach an empty position.
| Step | Key | Comparison path | Placed as |
|---|---|---|---|
| 1 | 45 | (tree is empty) | root |
| 2 | 15 | 15 < 45 | left child of 45 |
| 3 | 79 | 79 > 45 | right child of 45 |
| 4 | 90 | 90 > 45, 90 > 79 | right child of 79 |
| 5 | 10 | 10 < 45, 10 < 15 | left child of 15 |
| 6 | 55 | 55 > 45, 55 < 79 | left child of 79 |
| 7 | 12 | 12 < 45, 12 < 15, 12 > 10 | right child of 10 |
| 8 | 20 | 20 < 45, 20 > 15 | right child of 15 |
| 9 | 50 | 50 > 45, 50 < 79, 50 < 55 | left child of 55 |
- Height: 3 (path 45 → 15 → 10 → 12, or 45 → 79 → 55 → 50).
- Minimum: 10 (keep going left from 45 until you cannot).
- Maximum: 90 (keep going right).
Exam tip: draw the tree after every two or three insertions while practising. In the exam, the table above plus the final drawing earns full marks and makes your reasoning visible.
Part 3: Traversals of the Constructed BST
- Inorder: 10 12 15 20 45 50 55 79 90 (sorted, which is your built-in correctness check)
- Preorder: 45 15 10 12 20 79 55 50 90 (first key = root; also equals the order a BST could be rebuilt from)
- Postorder: 12 10 20 15 50 55 90 79 45 (last key = root)
- Level order: 45 15 79 10 20 55 90 12 50
If your inorder is not sorted, you placed a key incorrectly. Re-check that key’s comparison path.
- Output
Part 4: Search and Insert
Q5. Show the steps to search for 50 and for 25.
Search 50:
- 50 > 45 → go right to 79.
- 50 < 79 → go left to 55.
- 50 < 55 → go left to 50.
- 50 = 50 → found (4 comparisons).
Search 25:
- 25 < 45 → go left to 15.
- 25 > 15 → go right to 20.
- 25 > 20 → go right, but the right child of 20 is NULL.
- Not found (3 comparisons).
Q6. Insert 60 into the BST and redraw it.
- 60 > 45 → go right to 79.
- 60 < 79 → go left to 55.
- 60 > 55 → the right child of 55 is empty.
- Attach 60 as the right child of 55.
Check: inorder after insertion is 10 12 15 20 45 50 55 60 79 90, still sorted.
Algorithms (pseudocode)
BST-SEARCH(ROOT, ITEM)
1. Set PTR := ROOT.
2. Repeat while PTR ≠ NULL and INFO[PTR] ≠ ITEM:
If ITEM < INFO[PTR], set PTR := LEFT[PTR]
Else set PTR := RIGHT[PTR]
3. If PTR = NULL, print "not found"; else print "found at PTR".
4. Exit.
BST-INSERT(ROOT, ITEM) // returns the new root
1. If ROOT = NULL:
Create NEW node; INFO[NEW] := ITEM; LEFT[NEW] := RIGHT[NEW] := NULL
Return NEW.
2. If ITEM < INFO[ROOT]: LEFT[ROOT] := BST-INSERT(LEFT[ROOT], ITEM)
Else if ITEM > INFO[ROOT]: RIGHT[ROOT] := BST-INSERT(RIGHT[ROOT], ITEM)
(Else ITEM already exists: do nothing.)
3. Return ROOT.
BST-MIN(ROOT) // assumes ROOT ≠ NULL
1. Set PTR := ROOT.
2. Repeat while LEFT[PTR] ≠ NULL: PTR := LEFT[PTR]
3. Return INFO[PTR]. // BST-MAX is the same with RIGHT
Part 5: Deletion (All Three Cases)
Q7. From the BST constructed in Part 2, delete 20, then 10, then 45. Draw the tree after each deletion. Use the inorder successor for a node with two children.
(a) Delete 20: case 1, leaf
- 20 has no children.
- Set the right pointer of its parent (15) to NULL.
- Free the node.
(b) Delete 10: case 2, one child
- 10 has only a right child, 12.
- Link 10’s parent (15) directly to 12:
LEFT[15] := 12. - The order is preserved because 12 was already in 15’s left subtree.
(c) Delete 45: case 3, two children
- Find the inorder successor: go to the right child (79), then as far left as possible (55, then 50).
- The successor is 50.
- Copy 50 into the root.
- Delete the old 50 from the right subtree. It is a leaf (case 1).
- New root: 50.
Result after all three deletions
- Level order: 50 15 79 12 55 90
- Inorder: 12 15 50 55 79 90 (sorted ✓)
Alternative for (c), using the inorder predecessor
- The predecessor is the maximum of the left subtree: 15 (after steps a and b, the left subtree is 15 with left child 12).
- Copy 15 into the root.
- Delete the old 15. It has one child, so 12 moves up.
- The result is also a valid BST.
Use whichever method the question asks for. If it does not say, state which one you are using.
Why each case needs a different approach
- A leaf has no subtrees to preserve, so it can simply be removed.
- A node with one child has exactly one subtree, and that subtree can take the node’s place, since all its keys already satisfy the constraints of the node’s ancestors.
- A node with two children has two subtrees but its parent has only one pointer for them. Replacing the key with the successor (or predecessor), the only key that is greater than everything on the left and smaller than everything else on the right, keeps the order without restructuring the tree. The successor has no left child, so removing it is always case 1 or 2.
Deletion pseudocode
BST-DELETE(ROOT, KEY) // returns the new root
1. If ROOT = NULL, return NULL. // KEY not found
2. If KEY < INFO[ROOT]: LEFT[ROOT] := BST-DELETE(LEFT[ROOT], KEY)
Else if KEY > INFO[ROOT]: RIGHT[ROOT] := BST-DELETE(RIGHT[ROOT], KEY)
Else: // ROOT holds KEY
a. If LEFT[ROOT] = NULL: TEMP := RIGHT[ROOT]; free ROOT; return TEMP
b. If RIGHT[ROOT] = NULL: TEMP := LEFT[ROOT]; free ROOT; return TEMP
c. SUCC := BST-MIN-NODE(RIGHT[ROOT]) // two children
INFO[ROOT] := INFO[SUCC]
RIGHT[ROOT] := BST-DELETE(RIGHT[ROOT], INFO[SUCC])
3. Return ROOT.
Step 2a also handles a leaf (both children NULL → returns NULL).
Part 6: Complete C++ Program (Lab Exam)
The program below performs every operation from Parts 2–5 on the exam sequence and prints the results, so you can check your hand-drawn answers.
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
struct Node {
int key;
Node* left;
Node* right;
explicit Node(int k) : key(k), left(nullptr), right(nullptr) {}
};
Node* insert(Node* root, int key) {
if (root == nullptr) {
return new Node(key);
}
if (key < root->key) {
root->left = insert(root->left, key);
} else if (key > root->key) {
root->right = insert(root->right, key);
}
return root;
}
// Prints the comparison path, then whether the key was found.
bool search(Node* root, int key) {
while (root != nullptr) {
cout << root->key << ' ';
if (key == root->key) {
return true;
}
root = (key < root->key) ? root->left : root->right;
}
return false;
}
Node* minNode(Node* root) {
while (root->left != nullptr) {
root = root->left;
}
return root;
}
Node* maxNode(Node* root) {
while (root->right != nullptr) {
root = root->right;
}
return root;
}
Node* deleteKey(Node* root, int key) {
if (root == nullptr) {
return nullptr;
}
if (key < root->key) {
root->left = deleteKey(root->left, key);
} else if (key > root->key) {
root->right = deleteKey(root->right, key);
} else {
if (root->left == nullptr) {
Node* t = root->right;
delete root;
return t;
}
if (root->right == nullptr) {
Node* t = root->left;
delete root;
return t;
}
Node* succ = minNode(root->right); // inorder successor
root->key = succ->key;
root->right = deleteKey(root->right, succ->key);
}
return root;
}
void inorder(Node* t) {
if (!t) {
return;
}
inorder(t->left);
cout << t->key << ' ';
inorder(t->right);
}
void preorder(Node* t) {
if (!t) {
return;
}
cout << t->key << ' ';
preorder(t->left);
preorder(t->right);
}
void postorder(Node* t) {
if (!t) {
return;
}
postorder(t->left);
postorder(t->right);
cout << t->key << ' ';
}
void levelOrder(Node* root) {
queue<Node*> q;
if (root) {
q.push(root);
}
while (!q.empty()) {
Node* t = q.front();
q.pop();
cout << t->key << ' ';
if (t->left) {
q.push(t->left);
}
if (t->right) {
q.push(t->right);
}
}
}
int height(Node* t) {
return t ? 1 + max(height(t->left), height(t->right)) : -1;
}
// Counts keys smaller than x, skipping right subtrees that cannot qualify.
int countLess(Node* t, int x) {
if (t == nullptr) {
return 0;
}
if (t->key < x) {
return 1 + countLess(t->left, x) + countLess(t->right, x);
}
return countLess(t->left, x);
}
void destroy(Node* t) {
if (!t) {
return;
}
destroy(t->left);
destroy(t->right);
delete t;
}
int main() {
Node* root = nullptr;
for (int key : {45, 15, 79, 90, 10, 55, 12, 20, 50}) {
root = insert(root, key);
}
cout << "Inorder: ";
inorder(root);
cout << '\n';
cout << "Preorder: ";
preorder(root);
cout << '\n';
cout << "Postorder: ";
postorder(root);
cout << '\n';
cout << "Level order: ";
levelOrder(root);
cout << '\n';
cout << "Height = " << height(root) << ", min = " << minNode(root)->key
<< ", max = " << maxNode(root)->key << '\n';
cout << "Search 50 path: ";
bool found = search(root, 50);
cout << (found ? "-> found" : "-> not found") << '\n';
cout << "Search 25 path: ";
found = search(root, 25);
cout << (found ? "-> found" : "-> not found") << '\n';
cout << "countLess(root, 50) = " << countLess(root, 50) << '\n';
root = insert(root, 60);
cout << "After inserting 60, inorder: ";
inorder(root);
cout << '\n';
root = deleteKey(root, 60); // undo, to continue with the original tree
for (int key : {20, 10, 45}) {
root = deleteKey(root, key);
cout << "After deleting " << key << ", level order: ";
levelOrder(root);
cout << '\n';
}
destroy(root);
}
Output:
Inorder: 10 12 15 20 45 50 55 79 90
Preorder: 45 15 10 12 20 79 55 50 90
Postorder: 12 10 20 15 50 55 90 79 45
Level order: 45 15 79 10 20 55 90 12 50
Height = 3, min = 10, max = 90
Search 50 path: 45 79 55 50 -> found
Search 25 path: 45 15 20 -> not found
countLess(root, 50) = 5
After inserting 60, inorder: 10 12 15 20 45 50 55 60 79 90
After deleting 20, level order: 45 15 79 10 55 90 12 50
After deleting 10, level order: 45 15 79 12 55 90 50
After deleting 45, level order: 50 15 79 12 55 90
Part 7: Tracing Recursive BST Functions
Q8. Trace countLess(root, 50) on the BST from Part 2. What does the function compute?
int countLess(Node* t, int x) {
if (t == nullptr) {
return 0;
}
if (t->key < x) {
return 1 + countLess(t->left, x) + countLess(t->right, x);
}
return countLess(t->left, x);
}
| Call | key < 50? | Action | Returns |
|---|---|---|---|
| countLess(45) | yes | 1 + countLess(15) + countLess(79) | 1 + 4 + 0 = 5 |
| countLess(15) | yes | 1 + countLess(10) + countLess(20) | 1 + 2 + 1 = 4 |
| countLess(10) | yes | 1 + countLess(NULL) + countLess(12) | 1 + 0 + 1 = 2 |
| countLess(12) | yes | 1 + 0 + 0 | 1 |
| countLess(20) | yes | 1 + 0 + 0 | 1 |
| countLess(79) | no | countLess(55) (right subtree skipped) | 0 |
| countLess(55) | no | countLess(50) | 0 |
| countLess(50) | no | countLess(NULL) | 0 |
Answer:
- It counts the keys smaller than
x: 10, 12, 15, 20, 45 → 5. - When a key is not smaller than
x, every key in its right subtree is even larger, so the function skips that subtree. - This skipping (pruning) is possible only because of the BST property.
Q9. Trace the recursive insert of 12 into the tree that already contains 45, 15, 79, 90, 10, 55.
Going down (the calls):
insert(45, 12): 12 < 45, so callLEFT[45] := insert(15, 12).insert(15, 12): 12 < 15, so callLEFT[15] := insert(10, 12).insert(10, 12): 12 > 10, so callRIGHT[10] := insert(NULL, 12).insert(NULL, 12): empty spot found, so create and return a new node 12.
Coming back up (the returns):
RIGHT[10]now points to the new node 12. This is the only pointer that changes.insert(10, 12)returns node 10, soLEFT[15]is set to 10 again (unchanged).insert(15, 12)returns node 15, soLEFT[45]is set to 15 again (unchanged).insert(45, 12)returns 45, which stays the root.
Part 8: Complexity, Worst Case, and Balance
Time complexity
| Operation | Best / average (balanced) | Worst (skewed) |
|---|---|---|
| Search | ||
| Insert | ||
| Delete | ||
| Find min / max | ||
| Traversal | ||
| Build from keys by insertion |
All single-key operations are . A BST with nodes has height between and .
Q10. Insert 10, 20, 30, 40, 50 into an empty BST. Draw the tree and comment on its height and search time.
- Every key is larger than all previous keys, so each one goes to the right of the last.
- The result is a right-skewed tree of height .
- Searching for 50 needs 5 comparisons, the same as a linear search in a linked list.
- In general, sorted or reverse-sorted input produces this worst case: per operation.
Balanced vs unbalanced BST
| Balanced BST | Unbalanced (skewed) BST | |
|---|---|---|
| Height | about | up to |
| Search / insert / delete | ||
| Example | Insert 30, 20, 40, 10, 25, 35, 50 | Insert 10, 20, 30, 40, 50 |
| How to guarantee | Self-balancing trees (AVL, red-black) using rotations | Plain BST, no guarantee |
An AVL tree keeps the balance factor (height of left subtree minus height of right subtree) in at every node and restores it with rotations after each insertion or deletion. AVL trees are usually examined in a separate chapter, so learn them after you are comfortable with plain BST operations.
Question Bank With Model Answers
All questions are representative exam-style questions.
Short questions (1–2 marks)
Q11. Define a BST. A binary tree where, for every node, all keys in the left subtree are smaller and all keys in the right subtree are larger.
Q12. Which traversal of a BST gives sorted order? Inorder.
Q13. Where is the minimum element of a BST? At the leftmost node.
Q14. What is the worst-case time to search a BST with nodes? , when the tree is skewed.
Q15. Where is a new key always inserted? As a leaf.
Q16. What is the inorder successor of a node with a right subtree? The minimum (leftmost) node of the right subtree.
Q17. Why can’t we simply remove a node with two children? Its parent has one pointer but there are two subtrees to reattach.
Q18. Give one input order that makes a BST degenerate. Sorted input, such as 1, 2, 3, 4, 5.
Q19. How many comparisons are needed to find 12 in the exam BST? 4 (45, 15, 10, 12).
Q20. How many structurally different BSTs can be built with 3 distinct keys? 5 (Catalan number ).
Medium questions (3–5 marks)
Q21. Construct a BST by inserting 30, 20, 40, 10, 25, 35, 50 and write its preorder and postorder.
- Root: 30, with children 20 and 40.
- Under 20: 10 and 25. Under 40: 35 and 50.
- It is a perfect tree of height 2.
- Preorder: 30 20 10 25 40 35 50
- Postorder: 10 25 20 35 50 40 30
Q22. Differentiate a BST from a binary tree (any four points). See the table in Part 1.
Q23. Write the BST search algorithm and state its complexity. BST-SEARCH in Part 4; : balanced, worst.
Q24. Explain why inorder traversal of a BST is sorted.
- Inorder prints the left subtree first, and all its keys are smaller than the node.
- Then it prints the node.
- Then it prints the right subtree, and all its keys are larger.
- The same holds inside every subtree, so each key is printed after all smaller keys. The output is ascending.
Q25. Construct the BST whose preorder traversal is 30 20 10 25 40 35 50.
- In a BST, the inorder is just the sorted keys: 10 20 25 30 35 40 50.
- Build the tree from preorder + inorder, or simply insert the preorder keys in order.
- Both give the same tree as Q21.
Broad questions (5–10 marks)
Q26. (a) Construct a BST from 45, 15, 79, 90, 10, 55, 12, 20, 50. (b) Write its inorder, preorder and postorder. (c) Delete 20, 10 and 45 in that order, drawing the tree after each deletion.
- (a) See Q4 in Part 2.
- (b) See Part 3.
- (c) See Q7 in Part 5.
Q27. Write the algorithm to delete a node from a BST, covering all three cases, and explain each case with a diagram.
- Algorithm: the deletion pseudocode in Part 5.
- Diagrams: the three cases in the BST guide.
Q28. “The performance of a BST depends on the order of insertion.” Justify with examples and explain how this problem is solved.
- Balanced example: insert 30, 20, 40, 10, 25, 35, 50 → height 2, .
- Skewed example: insert 10, 20, 30, 40, 50 → height 4, .
- Solution: self-balancing trees (AVL, red-black). See Part 8.
Q29. Write a C++ program that builds a BST from user input and prints its inorder, preorder and postorder traversals.
Use the Part 6 program and replace the fixed list of keys with a cin loop.
Construction and drawing problems
Q30. Insert the following into an empty BST and draw it: 50, 40, 60, 30, 45, 55, 70, 35.
- Root: 50.
- Left child 40, with children 30 and 45. 35 is the right child of 30.
- Right child 60, with children 55 and 70.
- Inorder: 30 35 40 45 50 55 60 70.
Q31. Draw the BST after inserting 8, 3, 10, 1, 6, 14, 4, 7, 13.
- Root: 8.
- Left child 3, with children 1 and 6. 6 has children 4 and 7.
- Right child 10, with right child 14. 14 has left child 13.
- Height: 3.
Q32. Insert the months JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG (alphabetical comparison) and give the inorder traversal.
- Root: JAN.
- Left: FEB. APR is the left child of FEB, and AUG is the right child of APR.
- Right: MAR. JUN is the left child of MAR, and JUL is the left child of JUN. MAY is the right child of MAR.
- Inorder: APR AUG FEB JAN JUL JUN MAR MAY (alphabetical).
Traversal output problems
Q33. Give the level order of the Q30 tree.
50 40 60 30 45 55 70 35
Q34. The inorder of a BST is always known without drawing it. Why?
It is simply the keys in sorted order.
Q35. The postorder of a BST is 10 25 20 35 50 40 30. What is the root, and what is the preorder?
- Root: the last element of postorder, 30.
- Preorder: 30 20 10 25 40 35 50.
Deletion problems
Q36. In the Q31 tree, delete 3 using the inorder successor.
- The successor of 3 is 4, the minimum of the subtree rooted at 6.
- Copy 4 into 3’s node.
- Delete the old leaf 4.
- New left subtree of 8: 4, with children 1 and 6. 6 keeps only its right child, 7.
Q37. In the Q31 tree, delete 10.
- 10 has one child, 14.
- Link 8’s right pointer directly to 14.
- 14 keeps its left child, 13.
Q38. In the Q31 tree, delete 8 (the root) using the inorder predecessor.
- The predecessor is the maximum of the left subtree: 7.
- Copy 7 into the root.
- Delete the old leaf 7.
Pseudocode and algorithm problems
Q39. Write a recursive algorithm to find the maximum key in a BST.
MAX(T)
1. If RIGHT[T] = NULL, return INFO[T].
2. Else return MAX(RIGHT[T]).
Q40. Write an algorithm to count the nodes of a BST whose keys lie in [a, b].
Work like countLess in Q8:
- If the tree is empty, return 0.
- If key < a, recurse on the right subtree only.
- If key > b, recurse on the left subtree only.
- Otherwise, count 1 and recurse on both sides.
Q41. Write an algorithm to check whether a binary tree is a BST.
Pass the allowed range down the tree:
ISBST(T, LOW, HIGH)
1. If T = NULL, return TRUE.
2. If INFO[T] ≤ LOW or INFO[T] ≥ HIGH, return FALSE.
3. Return ISBST(LEFT[T], LOW, INFO[T]) AND ISBST(RIGHT[T], INFO[T], HIGH).
Start with ISBST(ROOT, −∞, +∞). Also explain why checking only a node’s children is wrong (see the validate BST example).
Viva questions
Q42. What is the key property of a BST? Left subtree keys < node < right subtree keys, at every node.
Q43. What is the time complexity of searching a BST? : if balanced, if skewed.
Q44. Inorder successor vs inorder predecessor? The next larger key vs the next smaller key.
Q45. Why does the inorder successor never have a left child (in the two-children case)? It is the minimum of the right subtree, so a left child would be even smaller, a contradiction.
Q46. What happens if you insert duplicate keys? Depends on the policy; by the strict definition they are not allowed.
Q47. How do you find the height of a BST? Same as any binary tree: 1 + max(height(left), height(right)), with −1 for NULL.
Q48. Is a heap a BST? No; a heap only orders parents relative to children and must be complete.
Q49. What is a self-balancing BST? Give two examples. A BST that restructures itself to keep the height : AVL tree, red-black tree.
Q50. What is a rotation? A local restructuring that changes the shape of a subtree while preserving the inorder order; used to rebalance.
Q51. Which C++ STL containers are built on balanced BSTs? std::set, std::map, std::multiset, std::multimap (typically red-black trees).
Q52. What is the best case for BST search? The key is at the root: .
Q53. Can a BST be stored in an array? Yes, using the , scheme, but a skewed BST wastes enormous space, so the linked representation is preferred.
Exam-Writing Tips for BST Questions
- Write the comparison path for each insertion, at least for the tricky keys; it shows method even if you make a slip.
- Check with inorder: after building or deleting, the inorder must be sorted.
- Name the deletion case (“leaf”, “one child”, “two children”) and the method (“inorder successor”), then draw the result.
- Deletions are cumulative. “Delete 20, then 10, then 45” means each deletion applies to the tree left by the previous one.
- Mention the worst case whenever you give a BST complexity.
Next Steps
- Next: What Are DFS and BFS?, a beginner’s guide to graph traversal.
- Revise BST operations with code in the BST guide, or go further with BST interview questions.
- Tree (non-BST) exam practice: Tree Questions for University Exams in Bangladesh.