Data Structures & AlgorithmsHardTCS NQT · Infosys pattern
What is the minimum number of nodes in an AVL tree of height h (height of a single node = 0)?
What is the minimum number of nodes in an AVL tree of height h (height of a single node = 0)?
def n_min(h):
a, b = 1, 2 # N(0), N(1)
if h == 0: return 1
for _ in range(2, h + 1): a, b = b, a + b + 1
return b
print(n_min(3), n_min(5), n_min(1))- A. 7 20 2
- B. 7 12 2
- C. 8 20 2
- D. 7 20 3
Correct answer: A. 7 20 2
A sparsest AVL tree of height h has a root, one subtree of height h − 1 and one of height h − 2: N(h) = N(h − 1) + N(h − 2) + 1 with N(0) = 1, N(1) = 2 → 1, 2, 4, 7, 12, 20. So height grows at most about 1.44 log₂ n, which keeps AVL operations O(log n).
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- BFS from vertex 0 visits neighbours in increasing order. What is the BFS order and the DFS (recursive) order? python g = {0: [1, 2], 1…
- Run Dijkstra from A on the weighted directed graph. In what order are vertices finalised (popped), and what is dist[E]? python import…
- Prim's algorithm from vertex 0 on the undirected graph below. In what order are vertices added and what is the MST weight? python import…
- Kahn's topological sort with a FIFO queue (initial zero-indegree vertices in increasing order). What is printed? python from collections…
- Kruskal's algorithm: which edges are accepted, in order, for the graph below? python edges = sorted([(7, 'A', 'B'), (5, 'A', 'D'), (8…
- How many times is the base case reached when computing C(5, 2) with the plain recursion C(n, k) = C(n−1, k−1) + C(n−1, k)? python base = 0…