Python 为什么我的build\u heap方法不会停止运行?
我为Python 为什么我的build\u heap方法不会停止运行?,python,heap,interrupt,Python,Heap,Interrupt,我为MinHeap类编写了一个类,并创建了build\u heap方法。每当我调用build\u heap函数时,程序将继续运行,除非我用键盘中断它。当我中断函数调用时,堆看起来是构建的,但是我很好奇为什么函数看起来是无限运行的 MinHeap类: class MinHeap: def __init__(self): self.heap_list = [0] self.current_size = 0 def perc_up(self, inde
MinHeap
类编写了一个类,并创建了build\u heap
方法。每当我调用build\u heap
函数时,程序将继续运行,除非我用键盘中断它。当我中断函数调用时,堆看起来是构建的,但是我很好奇为什么函数看起来是无限运行的
MinHeap类:
class MinHeap:
def __init__(self):
self.heap_list = [0]
self.current_size = 0
def perc_up(self, index):
while index // 2 > 0:
if self.heap_list[index] < self.heap_list[index // 2]:
temp = self.heap_list[index // 2]
self.heap_list[index // 2] = self.heap_list[index]
self.heap_list[index] = temp
index = index // 2
def perc_down(self, index):
while (index * 2) <= self.current_size:
mc = self.min_child(index)
if self.heap_list[index] > self.heap_list[mc]:
temp = self.heap_list[index]
self.heap_list[index] = self.heap_list[mc]
self.heap_list[mc] = temp
i = mc
def min_child(self, index):
if (index * 2 + 1) > self.current_size:
return index * 2
else:
if self.heap_list[index * 2] < self.heap_list[index * 2 + 1]:
return index * 2
else:
return index * 2 + 1
def insert(self, value):
self.heap_list.append(value)
self.current_size += 1
self.perc_up(self.current_size)
def peek(self):
return self.heap_list[1]
def del_min(self):
ret_val = self.heap_list[1]
self.heap_list[1] = self.heap_list[self.current_size]
self.heap_list.pop()
self.perc_down(1)
return ret_val
def is_empty(self):
if self.current_size == 0:
return True
else:
return False
def size(self):
return self.current_size
def build_heap(self, a_list):
i = len(a_list) // 2
self.current_size = len(a_list)
self.heap_list = [0] + a_list[:]
while (i > 0):
self.perc_down(i)
i = i - 1
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
heap.build_堆(lyst)
文件“C:/Users/frost\u 000/Documents/Python Files/MinHeap.py”,第62行,在build\u heap中
自我监督(一)
文件“C:/Users/frost\u 000/Documents/Python Files/MinHeap.py”,第16行,perc\u down
while(索引*2)>>heap.heap\u列表
>>> [0, 1, 3, 2, 19, 13, 4, 6]
问题是perc\u down()
在调用build\u help()时永远不会返回。这是因为条件(索引*2)不是答案,而是删除perc_down中的'i=mc'行。我在该范围内的其他任何地方都没有使用,所以充其量它是一条冗余线路。您可能会被困在perc_down()中。我看不到任何地方你在减少self.current\u size,所以你将停留在while循环中。while(index*2)current\u size
在这个循环->无限循环中不更新。I=mc
什么是I
?你是说index=mc
?正如其他人指出的那样,问题是你需要修改循环中某个地方的index
值。那些评论您需要修改该循环中当前大小的人是错误的。但是,在del_min
功能中,删除项目后确实需要减小current_size
。这是错误的。谢谢你的帮助!花了太长时间盯着它,忽略了那个打字错误可能有一百次。不客气。不要忽略也添加到del_min()
方法中的行-即使这不是无限循环的原因。
>>> heap = MinHeap()
>>> lyst = [ 1, 3, 6, 19, 13, 4, 2]
>>> heap.build_heap(lyst)
class MinHeap:
def __init__(self):
self.heap_list = [0]
self.current_size = 0
def perc_up(self, index):
while index // 2 > 0:
if self.heap_list[index] < self.heap_list[index // 2]:
temp = self.heap_list[index // 2]
self.heap_list[index // 2] = self.heap_list[index]
self.heap_list[index] = temp
index = index // 2
def perc_down(self, index):
while (index * 2) <= self.current_size:
mc = self.min_child(index)
if self.heap_list[index] > self.heap_list[mc]:
temp = self.heap_list[index]
self.heap_list[index] = self.heap_list[mc]
self.heap_list[mc] = temp
index = mc #### changed
def min_child(self, index):
if (index * 2 + 1) > self.current_size:
return index * 2
else:
if self.heap_list[index * 2] < self.heap_list[index * 2 + 1]:
return index * 2
else:
return index * 2 + 1
def insert(self, value):
self.heap_list.append(value)
self.current_size += 1
self.perc_up(self.current_size)
def peek(self):
return self.heap_list[1]
def del_min(self):
ret_val = self.heap_list[1]
self.heap_list[1] = self.heap_list[self.current_size]
self.heap_list.pop()
self.current_size -= 1 #### added
self.perc_down(1)
return ret_val
def is_empty(self):
if self.current_size == 0:
return True
else:
return False
def size(self):
return self.current_size
def build_heap(self, a_list):
i = len(a_list) // 2
self.current_size = len(a_list)
self.heap_list = [0] + a_list[:]
while (i > 0):
print('i: {}'.format(i))
self.perc_down(i)
i = i - 1
heap = MinHeap()
lyst = [ 1, 3, 6, 19, 13, 4, 2]
heap.build_heap(lyst)