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)
怎样解决报错,并测试insert和delMax的可行性