Data Structures & AlgorithmsHardGATE CS · Product-company placement
A 4-ary max-heap is stored in a 0-indexed array (children of i at 4i + 1 … 4i + 4): [51, 46, 36, 39, 2, 22, 35, 14, 13, 24]. After inserting 65 (append, then sift up), what is the array?
- A. [65, 51, 36, 39, 46, 22, 35, 14, 13, 24, 2]
- B. [51, 46, 36, 39, 2, 22, 35, 14, 13, 24, 65]
- C. [65, 51, 36, 39, 2, 22, 35, 14, 13, 24, 46]
- D. [65, 46, 51, 39, 2, 22, 35, 14, 13, 24, 36]
Correct answer: D. [65, 46, 51, 39, 2, 22, 35, 14, 13, 24, 36]
In a 4-ary heap the parent of i is ⌊(i − 1)/4⌋; 65 starts at index 10 and swaps up while it beats its parent. Using the binary parent formula ⌊(i − 1)/2⌋ is the trap.
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
- How many subsets of {4, 7, 9, 10, 11, 13, 14} have sum exactly 31?
- Unit-time jobs (deadline, profit): J1 (3, 45), J2 (2, 47), J3 (1, 77), J4 (1, 75), J5 (1, 13), J6 (2, 37), J7 (3, 59). One job runs at a…
- A directed graph on A–H has edges B→A, B→C, C→F, C→H, D→C, E→C, E→H, F→G, F→H, G→C, H→A. How many strongly connected components does it…
- 0/1 knapsack with capacity 18: items (weight, value) = (2, 25), (5, 31), (8, 37), (4, 22), (4, 27). What is the maximum total value?
- Keys 78, 54, 58, 22, 55, 70, 25, 12, 39, 86 are inserted into an empty AVL tree (giving preorder 58, 25, 22, 12, 54, 39, 55, 78, 70, 86)…
- DFS runs on the directed graph with adjacency lists A: BDF; B: FG; C: ABE; D: G; E: –; F: D; G: A, starting at A, scanning lists in the…