Data Structures & AlgorithmsHardGATE CS · Codeforces Div 2
Array [22, 29, 23, 18, 14, 20, 10] is turned into a min-heap in place with bottom-up heapify: for i from ⌊n/2⌋−1 down to 0, sift the element at index i down (swap with its smaller child while a child is smaller). What is the resulting array?
- A. [10, 14, 20, 18, 29, 22, 23]
- B. [22, 29, 23, 18, 14, 20, 10]
- C. [10, 14, 18, 20, 22, 23, 29]
- D. [10, 18, 14, 29, 22, 23, 20]
Correct answer: A. [10, 14, 20, 18, 29, 22, 23]
Bottom-up heapify on [22, 29, 23, 18, 14, 20, 10] produces [10, 14, 20, 18, 29, 22, 23]. Inserting one element at a time gives a different heap, and a single sift-down from the root does not fix the internal nodes.
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
- Union–find on elements 0 to 7, all starting as singletons, with no path compression. union(x, y): let rx, ry be the roots of x and y; if…
- Directed weighted graph on vertices A, B, C, D, E with edges (u→v, w): A→D (3); E→D (4); A→E (3); C→A (8); A→C (5); D→C (6); E→C (9); B→E…
- Distinct keys [26, 13, 5, 17, 19, 4] are inserted, in the order given by the sequence [19, 13, 4, 5, 17, 26], into an empty binary search…
- Undirected simple graph on vertices A, B, C, D, E, F with edges A–C (1), A–D (1), A–F (1), B–D (1), B–E (1), D–E (1), E–F (1) (weights…
- Connected undirected weighted graph on vertices A, B, C, D, E with edges A–B (1), A–D (4), A–E (4), B–C (4), B–D (1), B–E (3), D–E (2)…
- Tree with vertices 0 to 9, root 0, edges 0–1, 0–2, 2–3, 1–4, 3–5, 4–6, 5–7, 4–8, 8–9 (parent–child). Vertex weights (vertex:weight) 0:18…