N—E—E 2022-01-14 15:18 采纳率: 59.5%
浏览 55
已结题

这个最小堆代码执行后为什么没有变化

问题遇到的现象和发生背景

无论是使用列表构造最小堆还是一个一个元素插入构成最小堆都是未排序的,不知道到是不是percdown那里错了?

问题相关代码,请勿粘贴截图

```python
class MinHeap:
    def __init__(self):
        self.list = [0]
        self.currentsize = 0
    def insert(self,value):
        self.list.append(value)
        self.currentsize += 1
        i = self.currentsize
        while (i // 2 > 0 and value < self.list[i]):
            self.list[i] = self.list[i//2]
            i = i // 2
        self.list[i] = value
    def PercDown(self,i):
        tmp = self.list[i]
        while 2*i <= self.currentsize:
            '''找到孩子中的较小者'''
            if 2*i == self.currentsize:
                MinChild = 2*i
            else:
                MinChild = 2*i if self.list[2*i] < self.list[2*i+1] else 2*i+1
            '''移动'''
            if tmp < self.list[MinChild]:
                break
            self.list[i] = self.list[MinChild]
            i = MinChild
        self.list[i] = tmp
    def DelMin(self):
        self.list[1] = self.list[self.currentsize]
        self.currentsize -= 1
        self.list.pop()
        self.PercDown(1)
    def Construct(self,alist):
        it = iter(alist)
        try:
            while True:
                self.list.append(next(it))
        except StopIteration:
            pass
        for i in range(1,self.currentsize//2 + 1):
            self.PercDown(i)
    def __str__(self):
        return str(self.list)
    
if __name__ == '__main__':
    H = MinHeap()
    H.insert(5)
    H.insert(4)
    H.insert(3)
    H.insert(2)
    H.insert(1)
    print(H)

            





###### 运行结果及报错内容 

![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/427266441246139.png "#left")
  • 写回答

4条回答 默认 最新

  • 慷仔 2022-01-14 16:56
    关注

    单从insert中看,就有两个问题:
    1.循环判断条件出错:不应该是while (i // 2 > 0 and value < self.list[i]),应该是while (i > 0 and self.list[i] < self.list[i//2])。
    2.循环内部没有数据交换swap。
    我改编了你的代码,具体代码如下所示:

    import os
    class MinHeap:
        def __init__(self):
            self.list = []
            self.currentsize = 0
    
        def insert(self, value):
            self.list.append(value)
            self.currentsize += 1
            i = self.currentsize - 1
            while (i > 0 and self.list[i] < self.list[i//2]):
                reg = self.list[i//2]
                self.list[i//2] = self.list[i]
                self.list[i] = reg
                i = i // 2
            # self.list[i] = value
    
        def PercDown(self, i):
            tmp = self.list[i]
            while 2 * i <= self.currentsize:
                '''找到孩子中的较小者'''
                if 2 * i == self.currentsize:
                    MinChild = 2 * i
                else:
                    MinChild = 2 * i if self.list[2 * i] < self.list[2 * i + 1] else 2 * i + 1
                '''移动'''
                if tmp < self.list[MinChild]:
                    break
                self.list[i] = self.list[MinChild]
                i = MinChild
            self.list[i] = tmp
    
        def DelMin(self):
            self.list[1] = self.list[self.currentsize]
            self.currentsize -= 1
            self.list.pop()
            self.PercDown(1)
    
        def Construct(self, alist):
            it = iter(alist)
            try:
                while True:
                    self.list.append(next(it))
            except StopIteration:
                pass
            for i in range(1, self.currentsize // 2 + 1):
                self.PercDown(i)
    
        def __str__(self):
            return str(self.list)
    
    
    if __name__ == '__main__':
        H = MinHeap()
        H.insert(5)
        H.insert(4)
        H.insert(3)
        H.insert(2)
        H.insert(1)
        print(H)
    

    有如下输出:
    [1, 2, 3,4, 5]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 1月22日
  • 已采纳回答 1月14日
  • 创建了问题 1月14日

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像