Tree Algorithms MCQs for NSCT
Tree algorithms are the procedures that walk, search, insert into, delete from and rebalance trees. For the NSCT that means traversal orders, binary search tree (BST) operations, AVL rotations and B-tree splits. Tree Algorithms is topic 7 of the Data Structures & Algorithms area, which carries 10% of the NSCT according to HEC's Areas of Competencies. In 2026 candidates averaged 40.2% in DSA, per the HEC NSCT report and APP.
The tree structures themselves (nodes, heights, heaps) are covered in Non-Linear Data Structures. This page is about what you do with them, and most questions here ask you to trace an algorithm by hand.
Traversals
A traversal visits every node exactly once. The three depth-first orders differ only in when the node itself is visited:
| Traversal | Order | Typical use |
|---|---|---|
| Preorder | node, left, right | Copying or saving a tree |
| Inorder | left, node, right | Sorted output of a BST |
| Postorder | left, right, node | Freeing nodes, postfix expressions |
| Level order | depth by depth, using a queue | Printing by level, shortest depth |
Insert 50, 30, 70, 20, 40, 60, 80 into an empty BST. Preorder is 50 30 20 40 70 60 80; inorder is 20 30 40 50 60 70 80; postorder is 20 40 30 60 80 70 50. Exam angle: inorder plus either preorder or postorder rebuilds a unique tree, but preorder plus postorder alone does not. The tree traversals page has longer drills.
BST search, insert and delete
Search and insert follow one path from the root, comparing at each node, so they cost O(h), where h is the height. Deletion has three cases: a leaf is simply removed, a node with one child is replaced by that child, and a node with two children is replaced by its inorder successor (the minimum of its right subtree). CLRS uses the successor; some textbooks use the inorder predecessor instead, and both give a valid BST.
Exam angle: sorted input turns a plain BST into a chain of height n − 1, so search becomes O(n). That is the reason balanced trees exist.
AVL rotations
An AVL tree is a BST where every node's balance factor, height(left) − height(right), is −1, 0 or +1. After an insertion, the lowest node that reaches ±2 is fixed with one of four rotations:
| Case | New key went into | Fix |
|---|---|---|
| LL | left subtree of left child | single right rotation |
| RR | right subtree of right child | single left rotation |
| LR | right subtree of left child | left on child, then right on node |
| RL | left subtree of right child | right on child, then left on node |
Insert 30, 10, 20: node 30 gets balance +2 with the new key in its left child's right subtree. That is LR, and 20 becomes the root. Inserting 1 to 7 in order triggers four RR rotations and ends as the perfect tree rooted at 4. Exam angle: one insertion needs at most one single or one double rotation, but a deletion can need a rotation at every level.
B-trees
A B-tree is a balanced multi-way search tree built for disks. Every leaf is at the same depth, and each node holds many keys so the tree stays shallow. Using Knuth's order m, a node has at most m children and m − 1 keys, and every non-root node has at least ⌈m/2⌉ children. CLRS uses a minimum degree t instead (at most 2t − 1 keys), so always check which definition a question uses.
Insertion goes into a leaf. If the leaf overflows, it splits and its middle key moves up to the parent, which may split in turn. Inserting 10, 20, 30, 40, 50 into an order-5 B-tree overflows at 50, and 30 becomes the new root over [10 20] and [40 50]. Exam angle: a B-tree grows in height only when the root splits.
Common traps
- Forgetting that a node's right child can still have a left child, which changes postorder (65 before 60 in a subtree 60 → 65).
- Calling LR an LL case because you only looked at the unbalanced node, not where the new key went.
- Mixing Knuth's order m (max m − 1 keys) with CLRS's minimum degree t (max 2t − 1 keys).
- Checking only parent-child pairs to validate a BST; every key must also respect all its ancestors.
- Using different conventions for height: edges (single node = 0) or levels (single node = 1).
Worked examples
Understanding. Which traversal of an expression tree gives the prefix (Polish) form? Answer: preorder. It writes each operator before its two operand subtrees. Postorder gives postfix and inorder gives infix.
Application. Insert 40, 20, 10 into an empty AVL tree. What is the root afterwards? Answer: 20. Node 40 gets balance +2 with the key in its left child's left subtree (LL), so a single right rotation makes 20 the root with children 10 and 40.
Analysis. A B-tree of order 3 (at most 2 keys) receives 5, 15, 25, 35, 45 in order. How many nodes does it have at the end? Answer: 4. 25 overflows [5 15 25], sending 15 up. 45 overflows [25 35 45], sending 35 up, so the root becomes [15 35] over leaves [5], [25] and [45]. The root does not overflow, so the height stays 1.
Practise
The quiz below has 98 Tree Algorithms questions, which you can filter by tier and difficulty.
Practise: 98 questions
FAQs
Is a heap a tree algorithm topic or a data structure topic?
Both. The heap shape belongs with non-linear data structures, but building a heap, sift-down and heap insertion are algorithms on a tree. Know that bottom-up build-heap runs in O(n), while n separate insertions cost O(n log n).
Which is better, an AVL tree or a red-black tree?
AVL trees are more strictly balanced, so lookups are slightly faster. Red-black trees need fewer rotations on insertion and deletion, which is why many standard libraries use them. Both guarantee O(log n) search, insert and delete.
Why do databases use B+ trees rather than B-trees?
A B+ tree keeps all records in the leaves and links the leaves together, so range scans walk along the leaf level. Internal nodes hold only keys, which raises fan-out and lowers height. Most textbooks, including Elmasri-Navathe, describe database indexes as B+ trees.
How many rotations does an AVL deletion need?
Possibly one at every level on the path back to the root, so O(log n) in the worst case. That differs from insertion, where one single or double rotation always restores balance.
Next step
Continue with Graph Algorithms MCQs, go back to the DSA hub, or try a timed NSCT mock test.