Tree Representation Quiz
40 comprehensive questions exploring different ways to represent trees in memory and code — with 16 code examples covering pointer-based structures, arrays, adjacency lists, and tree properties like degree, height, and depth in this cpp quiz.
Question 1
What is pointer-based tree representation?
struct Node {
int data;
Node* left;
Node* right;
};Question 2
In array-based tree representation, where would the left child of node at index i be located?
Array: [A, B, C, D, E, F, G]
Index: 0 1 2 3 4 5 6Question 3
What is the degree of a node in tree representation?
Question 4
In adjacency list representation for trees, how is each node represented?
vector<vector<int>> adj = {
{1, 2}, // node 0
{0, 3, 4}, // node 1
{0, 5}, // node 2
{1}, // node 3
{1}, // node 4
{2} // node 5
};Question 5
What is the height of a tree in terms of representation?
Question 6
In binary tree array representation, what is the maximum number of nodes that can be stored in an array of size n?
Array size: n
Root at index 0
Left child: 2*i + 1
Right child: 2*i + 2Question 7
What is the depth of a node in tree representation?
A (depth 0)
/ \
B C (depth 1)
/ \
D E (depth 2)Question 8
When would you choose adjacency list representation over pointer-based representation for trees?
Question 9
In array representation of a complete binary tree, what is the parent index of a node at index i?
Child indices: 2*i + 1 (left), 2*i + 2 (right)
Parent index: ?Question 10
What is the memory overhead of pointer-based tree representation compared to array representation?
Question 11
How does array representation handle missing nodes in an incomplete binary tree?
Array: [A, B, null, D, E]
Tree: A
/ \
B null
/ \
D EQuestion 12
What is the advantage of adjacency list representation for trees with high degree nodes?
Question 13
In pointer-based representation, how is a binary tree node typically implemented?
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};Question 14
What is the space complexity of array representation for a complete binary tree with n nodes?
Question 15
How does adjacency list representation differ from adjacency matrix for tree representation?
Adjacency List: vector<vector<int>> adj(n);
Adjacency Matrix: vector<vector<int>> matrix(n, vector<int>(n, 0));Question 16
In array representation, how do you calculate the level of a node at index i?
Level = floor(log2(i + 1))
Index 0: level 0
Index 1-2: level 1
Index 3-6: level 2Question 17
What is the main disadvantage of array representation for trees?
Question 18
In pointer-based representation, how do you represent an empty tree?
TreeNode* root = nullptr;Question 19
What is the time complexity to find a node's parent in array representation?
Question 20
When implementing a tree where parent pointers are needed, which representation is most appropriate?
Question 21
In adjacency list representation, how do you determine if a node is a leaf?
vector<vector<int>> adj;
// leaf if adj[node].empty()Question 22
What is the space complexity of pointer-based representation for a binary tree with n nodes?
Question 23
In array representation, how do you handle trees that grow beyond the array size?
Question 24
What representation is best for implementing heap data structure?
Question 25
In adjacency list representation, how do you calculate the degree of a node?
vector<vector<int>> adj;
// degree = adj[node].size()Question 26
What is the advantage of pointer-based representation for tree traversals?
Question 27
In array representation, what is the maximum height of a tree with n nodes?
Question 28
When would you choose array representation over pointer-based representation?
Question 29
In pointer-based representation, how do you implement a tree with parent pointers?
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode* parent;
};Question 30
What is the disadvantage of adjacency list representation for binary trees?
Question 31
In array representation, how do you determine if a position i represents a valid node?
Array size: n
Valid if: i < n && array[i] != nullQuestion 32
What representation is most suitable for implementing threaded binary trees?
Question 33
In adjacency list representation, how do you perform a level-order traversal?
queue<int> q;
q.push(root);
while (!q.empty()) {
int node = q.front(); q.pop();
for (int child : adj[node]) {
q.push(child);
}
}Question 34
What is the memory efficiency comparison between pointer-based and array representation for sparse trees?
Question 35
In array representation, how do you find all nodes at a specific level?
Level k: indices 2^k - 1 to min(n-1, 2^{k+1} - 2)Question 36
What representation is best for implementing a tree where nodes can have arbitrary numbers of children?
Question 37
In pointer-based representation, what is the time complexity to access a node's children?
Question 38
What is the cache performance advantage of array representation?
Question 39
In adjacency list representation, how do you implement tree reconstruction from the representation?
vector<int> indegree(n, 0);
for (auto& children : adj) {
for (int child : children) {
indegree[child]++;
}
}
// Root has indegree 0Question 40
Considering all tree representation methods, which factor most influences the choice between pointer-based, array-based, and adjacency list representations for implementing tree data structures in performance-critical applications?
