二叉堆实现最大堆中的insert()和delMax()并检测

class maxheap(object):
def init(self):
self.heapList = [0]
self.currentSize = 0
def insert(self,k):
self.heapList.append(k)
self.heapList = self.currentSize - 1
self.percUp(self.currentSize)

def percUp(self,i):
    while i // 2 > 0:
        if self.heapList[i] > self.heapList[i//2]:
             telf.heapList[i//2]
             self.heapList[i//2] = self.heapList[i]
             self.heapList[i] = tmp
        i = i//2

def percDown(self,i):
    while(i*2) <= self.currentSize:
        maxc = self.maxChild(i)
        if self.heapList[i] < self.heapList[maxc]:
            tmp = self.heapList[i]
            self.heapList[i] = self.heapList[maxc]
            self.heapList[maxc] = tmp
        i = maxc

def maxChild(self,i):
    if i * 2 + 1 > self.currentSize:
        return i * 2
    else:
        if self.heapList[i*2] < self.heapList[i*2+1]:
            return i*2+1
        else:
            return i*2
def delMax(self):
    retval = self.heapList[1]
    self.heapList[1] = self.heapList[self.currentSize]
    self.currentSize = self.currentSize - 1
    self.heapList.pop()
    self.percDown(1)
    return retval

if name=='main':
a = maxheap()
a.insert(7)
a.insert(10)
a.insert(3)
a.insert(6)
a.insert(13)
print(a)

img

img


怎样解决报错,并测试insert和delMax的可行性

打印堆中的元素就能看出效果了
堆队列初始时,不要给默认元素0

self.heapList = []
class maxheap(object):
    def __init__(self):
        self.heapList = []
        self.currentSize = 0
    def insert(self,k):
        self.heapList.append(k)
        self.currentSize = self.currentSize - 1
        self.percUp(self.currentSize)
    
    def percUp(self,i):
        while i // 2 > 0:
            if self.heapList[i] > self.heapList[i//2]:
                 telf.heapList[i//2]
                 self.heapList[i//2] = self.heapList[i]
                 self.heapList[i] = tmp
            i = i//2
     
    def percDown(self,i):
        while(i*2) <= self.currentSize:
            maxc = self.maxChild(i)
            if self.heapList[i] < self.heapList[maxc]:
                tmp = self.heapList[i]
                self.heapList[i] = self.heapList[maxc]
                self.heapList[maxc] = tmp
            i = maxc
     
    def maxChild(self,i):
        if i * 2 + 1 > self.currentSize:
            return i * 2
        else:
            if self.heapList[i*2] < self.heapList[i*2+1]:
                return i*2+1
            else:
                return i*2
    def delMax(self):
        retval = self.heapList[1]
        self.currentSize = self.currentSize - 1
        self.heapList.pop()
        self.percDown(1)
        return retval
if __name__=='__main__':
    a = maxheap()
    a.insert(7)
    a.insert(10)
    a.insert(3)
    a.insert(6)
    a.insert(13)
    print(a.heapList)
    a.delMax()
    print(a.heapList)

img

是因为代码中这行:self.heapList = self.currentSize - 1
改变self.leapList为整数类型了,无法用append方法。改成:self.currentSize = self.currentSize - 1试试。

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632