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?

  1. A. [65, 51, 36, 39, 46, 22, 35, 14, 13, 24, 2]
  2. B. [51, 46, 36, 39, 2, 22, 35, 14, 13, 24, 65]
  3. C. [65, 51, 36, 39, 2, 22, 35, 14, 13, 24, 46]
  4. 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.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic