Tree Algorithms MCQs for NSCT

By the NSCT Exam Editorial Team · Updated 2026-11-24 · Facts checked against official sources on 2026-11-24

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

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

Interactive quiz loading. The first 10 questions are shown below.

  1. Which traversal of a binary search tree (BST) visits the keys in ascending order?

    1. Preorder
    2. Inorder
    3. Postorder
    4. Level order
    Show answer

    Answer: B. Inorder

    Inorder visits the left subtree, then the node, then the right subtree. In a BST every left key is smaller and every right key is larger, so this order is sorted ascending.

  2. Which data structure does a standard level-order (breadth-first) traversal of a binary tree use to hold nodes waiting to be visited?

    1. Stack
    2. Priority queue
    3. Hash table
    4. Queue
    Show answer

    Answer: D. Queue

    Level order visits nodes depth by depth, left to right. A FIFO queue gives exactly that: children are added at the back and visited after every node already waiting on the current level.

  3. You must free every node of a binary tree so that no node is deleted before its children. Which traversal order should the deletion follow?

    1. Postorder
    2. Preorder
    3. Inorder
    4. Level order
    Show answer

    Answer: A. Postorder

    Postorder processes the left subtree, the right subtree and only then the node itself, so both children are already freed when a node is deleted.

  4. In an AVL tree, the balance factor of a node is height(left subtree) minus height(right subtree). Which set lists every value allowed in a valid AVL tree?

    1. {0, 1}
    2. {-2, -1, 0, 1, 2}
    3. {0}
    4. {-1, 0, 1}
    Show answer

    Answer: D. {-1, 0, 1}

    An AVL tree lets the two subtree heights of any node differ by at most 1, so the balance factor must be -1, 0 or +1. A value of +2 or -2 triggers a rotation.

  5. Using Knuth's definition, a B-tree of order m lets each node have at most m children. What is the maximum number of keys in one node?

    1. m
    2. m + 1
    3. 2m - 1
    4. m - 1
    Show answer

    Answer: D. m - 1

    A node with k keys has k + 1 child pointers, so m children means at most m - 1 keys. CLRS instead uses a minimum degree t, where the maximum is 2t - 1 keys; read which definition a question uses.

  6. In a B-tree of order m (Knuth's definition), what is the minimum number of children of an internal node that is not the root?

    1. 2
    2. m - 1
    3. ceil(m/2)
    4. floor(m/2) - 1
    Show answer

    Answer: C. ceil(m/2)

    Every non-root internal node must be at least half full: at least ceil(m/2) children, which means at least ceil(m/2) - 1 keys. Only the root may have as few as 2 children.

  7. What is the worst-case time to search for a key in an AVL tree holding n keys?

    1. O(n)
    2. O(1)
    3. O(log n)
    4. O(n log n)
    Show answer

    Answer: C. O(log n)

    An AVL tree's height is always O(log n) (at most about 1.44 log2 n), and a search follows one root-to-leaf path, so the worst case is O(log n).

  8. Keys 1, 2, 3, ..., n are inserted in that order into an ordinary (unbalanced) BST. What is the worst-case time of a later search?

    1. O(log n)
    2. O(n)
    3. O(1)
    4. O(n log n)
    Show answer

    Answer: B. O(n)

    Sorted insertions make every new key the right child of the previous one, so the BST degenerates into a chain of height n - 1. A search may have to walk the whole chain: O(n).

  9. After an AVL insertion, a node has balance factor +2 and the new key went into the RIGHT subtree of its LEFT child (the LR case). Which fix restores balance?

    1. Left rotation on the left child, then right rotation on the node
    2. One right rotation on the node
    3. One left rotation on the node
    4. Right rotation on the left child, then left rotation on the node
    Show answer

    Answer: A. Left rotation on the left child, then right rotation on the node

    The LR case is a double rotation. A left rotation on the left child turns it into the LL shape, and a right rotation on the unbalanced node then fixes it. A single right rotation alone would leave the tree unbalanced the other way.

  10. Which pair of traversals is NOT always enough to rebuild a unique binary tree (keys all distinct)?

    1. Preorder and inorder
    2. Postorder and inorder
    3. Level order and inorder
    4. Preorder and postorder
    Show answer

    Answer: D. Preorder and postorder

    Without the inorder sequence you cannot tell whether a single child is a left or a right child. For example, root A with only child B gives preorder AB and postorder BA whether B is on the left or the right. Any order paired with inorder is enough.

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.