📜  门| GATE-CS-2014-(Set-2) |问题 22

📅  最后修改于: 2021-09-26 03:53:58             🧑  作者: Mango

优先级队列实现为最大堆。最初,它有 5 个元素。堆的层序遍历是:10, 8, 5, 3, 2。两个新元素1和7按这个顺序插入到堆中。元素插入后堆的层序遍历为:

(一) 10, 8, 7, 3, 2, 1, 5
(B) 10, 8, 7, 2, 3, 1, 5
(C) 10, 8, 7, 1, 2, 3, 5
(D) 10, 8, 7, 5, 3, 2, 1答案:(一)
解释:

Initially heap has 10, 8, 5, 3, 2
    10
   /  \ 
  8    5
 / \
3   2

After insertion of 1
     10
   /   \ 
  8     5
 / \   /
3   2 1 
No need to heapify as 5 is greater than 1.


After insertion of 7
     10
   /   \ 
  8     5
 / \   / \
3   2 1   7
Heapify 5 as 7 is greater than 5
     10
   /   \ 
  8     7
 / \   / \
3   2 1   5
No need to heapify any further as 10 is
greater than 7 

这个问题的测验