Non-Linear Data Structures MCQs for the NSCT
Non-linear data structures let one element connect to several others, so the data forms a hierarchy or a network rather than a single sequence. The core four are trees, binary search trees (BSTs), heaps and graphs. They are the third of 12 topics in the Data Structures & Algorithms area, which carries 10% of the NSCT according to HEC's Areas of Competencies.
This page covers the structures themselves. Algorithms that run on them are covered in Tree Algorithms and Graph Algorithms.
Trees
A tree is a hierarchy of nodes with one root, where every other node has exactly one parent. A leaf has no children. The depth of a node is its distance from the root, and the height of a tree is the longest root-to-leaf path. Most texts, including CLRS, count edges (a single node has height 0); some count nodes, so read the question.
In a binary tree each node has at most two children. Facts worth memorising:
| Fact | Formula | Example |
|---|---|---|
| Max nodes at height h | 2^(h+1) - 1 | h = 4: 31 nodes |
| Min height for n nodes | floor(log₂ n) | n = 20: height 4 |
| Leaves vs two-child nodes | n0 = n2 + 1 | 7 two-child nodes: 8 leaves |
| Edges in a tree of n nodes | n - 1 | 10 nodes: 9 edges |
Terminology differs here too. Usually a full binary tree has 0 or 2 children at every node, and a complete one fills every level except possibly the last, from the left. CLRS uses "complete" for a tree with every level full, which others call perfect.
Exam angle: the three depth-first traversals are preorder (node, left, right), inorder (left, node, right) and postorder (left, right, node). Given preorder and inorder, you can rebuild the tree.
BST
A binary search tree keeps every key in a node's left subtree smaller than the node, and every key in its right subtree larger. Inorder traversal therefore gives sorted order.
Insert 8, 3, 10, 1, 6, 14, 4 into an empty BST:
8
/ \
3 10
/ \ \
1 6 14
/
4
Search, insert and delete all cost O(h). That is O(log n) when the tree is balanced, but O(n) when keys arrive in sorted order and the tree becomes a chain. To delete a node with two children, replace it with its inorder successor (smallest key on the right) or predecessor; textbooks differ on which, so both are accepted.
Exam angle: a validity check that only compares each node with its direct children is wrong. The rule applies to whole subtrees.
Heaps
A binary heap is a complete binary tree with the heap property: in a min-heap each parent is ≤ its children (max-heap: ≥). It is stored in an array; with 0-based indices the children of i are 2i + 1 and 2i + 2, and its parent is floor((i - 1) / 2).
| Operation | Cost |
|---|---|
| Read min (max) | O(1) |
| Insert (sift-up) | O(log n) |
| Remove min (sift-down) | O(log n) |
| Build heap bottom-up | O(n) |
| Search for any key | O(n) |
Inserting 7, 3, 5, 1 into an empty min-heap gives [1, 3, 5, 7]: 1 enters at index 3 and swaps up past 7 and then 3.
Exam angle: a heap is not sorted. In a min-heap the maximum is somewhere among the leaves, so finding it is O(n). Heaps are the usual way to implement a priority queue.
Graphs
A graph G = (V, E) is a set of vertices joined by edges, which may be directed or undirected, weighted or unweighted. A tree is a connected graph with no cycles.
| Representation | Space | Edge (u, v) check | List neighbours |
|---|---|---|---|
| Adjacency matrix | Θ(V²) | O(1) | O(V) |
| Adjacency list | Θ(V + E) | O(degree) | O(degree) |
Use a matrix for dense graphs and fast edge checks, and a list for sparse graphs. In an undirected graph the degrees add up to 2E (the handshaking lemma). A simple undirected graph has at most n(n - 1)/2 edges, and a directed one at most n(n - 1). A directed graph has a topological order exactly when it has no cycle (a DAG).
Exam angle: BFS uses a queue and DFS uses a stack or recursion; both run in O(V + E) on an adjacency list.
Common traps
- Confusing the heap property (parent vs children) with the BST property (whole left and right subtrees).
- Assuming a BST search is always O(log n). An unbalanced BST can be O(n).
- Mixing 0-based and 1-based heap index formulas.
- Counting height in nodes when the question counts edges, or the reverse.
- Forgetting to halve the count of 1s in an undirected adjacency matrix to get the number of edges.
- Thinking a heap's inorder traversal is sorted.
Worked examples
1. Understanding. In a max-heap, which element is at the root? Answer: the largest. Every parent is at least as large as its children, so the maximum cannot sit below anything.
2. Application. Insert 8, 3, 10, 1, 6, 14, 4 into an empty BST. What is the postorder traversal? Answer: 1 4 6 3 14 10 8. Postorder lists the left subtree (1, then 4 and 6 under 3), then the right subtree (14, then 10), then the root 8.
3. Analysis. A social network has 1 million users, each with about 200 friends. Should it use an adjacency matrix? Answer: no. A matrix needs 10¹² cells. An adjacency list stores each friendship twice, about 2 × 10⁸ entries. The graph is sparse, so a list is far smaller.
Practise
The quiz below has 100 questions on this topic, filterable by tier and difficulty.
Practise: 100 questions
FAQs
Why are trees and graphs called non-linear?
In a linear structure, each element has at most one successor, so you can lay the data out in one line. In a tree a node can have several children, and in a graph a vertex can link to any number of others. Visiting everything therefore needs a traversal strategy such as BFS or DFS.
What is the difference between a BST and a binary heap?
Both are binary trees, but they are ordered differently. A BST orders whole subtrees (left smaller, right larger), so it supports search and sorted traversal. A heap only orders each parent against its children, so it gives fast access to the minimum or maximum but slow search for other keys.
Is every tree a graph?
Yes. A tree is a connected, acyclic, undirected graph with n - 1 edges for n vertices. A rooted tree adds a chosen root, which gives parent and child directions. A collection of disjoint trees is a forest.
When should I use an adjacency matrix instead of a list?
Use a matrix when the graph is dense, with E close to V², or when you need constant-time checks for whether an edge exists. Use a list for sparse graphs, which covers most real networks, because it uses Θ(V + E) memory and iterates over neighbours efficiently.
Next step
Move on to Searching Algorithms MCQs, see all topics on the DSA MCQs hub, or test yourself in an NSCT mock test.