📜  门| GATE-CS-2009 |第60章

📅  最后修改于: 2021-06-28 22:04:11             🧑  作者: Mango

考虑以上问题中给出的数据。在对上一个问题的正确答案进行两次删除操作后,数组的内容是什么?
(A) 14,13,12,10,8
(B) 14,12,13,8,10
(C) 14,13,8,12,10
(D) 14,13,12,8,10答案: (D)
说明:对于堆树,删除节点包括以下两个操作。

1)将根替换为最后一级的最后一个元素。
2)从根开始,从上到下堆积整个树。

Let us delete the two nodes one by one:
1) Deletion of 25:
Replace 25 with 12

          12
        /    \
      /       \
    14        16
   / \         /
 /     \      /
13     10    8
Since heap property is violated for root (16 is greater than 12),
make 16 as root of the tree.

           16
        /     \
      /        \
    14         12
   / \         /
  /   \       /
13     10    8
2) Deletion of 16:
Replace 16 with 8

           8
        /    \
       /      \
    14         12
   / \
  /   \
 13     10
Heapify from root to bottom.

           14
         /    \
       /       \
     8         12
    / \
   /   \
 13     10
            14
         /     \
        /       \
     13         12
    / \
   /   \
  8    10

这个问题的测验