-4
내 삽입 방법이 올바르지 않아 이유를 모르겠습니다. "builtin_function 또는 object가 subscriptable이 아닌"내 min 메소드 오류 "이 힙 구현은 무엇이 문제입니까?
힙에 대한 개념적 이해가 좋다고 생각합니다. 그러나 좌절의 파쇄가 insert
에
class Heap:
def __init__(self):
self.array = []
def find_children(self, index):
""" return both the left and right children """
return index * 2 + 1, index * 2 + 2
def insert(self, value):
""" append node value to the array, then bubble up
to its rightful place
"""
self.array.append(value)
index = self.array.index(value)
while index > 0 or self.array[index] < self.array[index // 2 - 1]:
if self.array[index] < self.array[index // 2 - 1]:
temp = self.array[index]
self.array[index] = self.array[index // 2 - 1]
self.array[index // 2 - 1] = temp
index = index // 2 - 1
def min(self):
""" remove the min at the root, then bubble down to
maintain heap property
"""
minimum = self.array[0]
self.array[0] = self.array[-1]
self.array.pop()
index = self.array.index[minimum]
while index <= len(self.array):
leftChildIndex, rightChildIndex = self.find_children(index)
minChild = min(self.array[leftChildIndex], self.array[rightChildIndex])
if self.array[index] > minChild:
temp = self.array[index]
self.array[index] = minChild
minChild = temp
index = minChild
return minimum
def p(self):
return self.array
'self.array.index [최소]'에 대괄호를 잘못 사용하고 있습니다. – jonrsharpe
@jonrsharpe가 있습니다 .. 투표가 필요합니다. – MAA
아래쪽 표를 분명히하기 위해 : 오류는 정확히 있어야합니다 그 문제가 어느 라인에서 언급되었는지. 이 정보를 생략하고 다른 사람들이 작업 할 수 있도록 코드를 버리는 것은 정중하지 않습니다. – MisterMiyagi