Data Structures & AlgorithmsModerateCognizant · Capgemini pattern
What is printed? (AVL: which rotation is needed after inserting 30, 10, 20 into an empty AVL tree?)
What is printed? (AVL: which rotation is needed after inserting 30, 10, 20 into an empty AVL tree?)
def balance_case(order):
a, b, c = order
first = 'L' if b < a else 'R'
second = 'L' if c < b else 'R'
return first + second
print(balance_case((30, 10, 20)), balance_case((10, 20, 30)), balance_case((30, 20, 10)))- A. LL RR LR
- B. LR RL LL
- C. RL RR LL
- D. LR RR LL
Correct answer: D. LR RR LL
30 → 10 goes left (L), then 20 goes right of 10 (R): an LR case, fixed by a left rotation at 10 then a right rotation at 30 (double rotation), making 20 the root. 10, 20, 30 is RR (single left rotation); 30, 20, 10 is LL (single right rotation).
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
- What is the minimum number of nodes in an AVL tree of height h (height of a single node = 0)? python def nmin(h): a, b = 1, 2 # N(0), N(1)…
- 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…