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): [58, 57, 54, 33, 11, 34, 22, 42, 30, 19]. After inserting 62 (append, then sift up), what is the array?

  1. A. [62, 58, 54, 33, 57, 34, 22, 42, 30, 19, 11]
  2. B. [58, 57, 54, 33, 11, 34, 22, 42, 30, 19, 62]
  3. C. [62, 58, 54, 33, 11, 34, 22, 42, 30, 19, 57]
  4. D. [62, 57, 58, 33, 11, 34, 22, 42, 30, 19, 54]

Correct answer: D. [62, 57, 58, 33, 11, 34, 22, 42, 30, 19, 54]

In a 4-ary heap the parent of i is ⌊(i − 1)/4⌋; 62 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