Conversation
Godrik0
left a comment
There was a problem hiding this comment.
4 балла за наличие хоть какой-то структуры, функции extractMin и insert.
Итого 4/30
| def __init__(self): | ||
| self.data = key |
There was a problem hiding this comment.
Вы используете переменную key, которую не передаете в функцию.
Соответственно все вызовы вида temp = Node(key) не будут работать.
| self.parent = None | ||
| self.child = None | ||
| self.parent = None |
| def mergeBinomialTrees(b1, b2): | ||
| if b1.data > b2.data: | ||
| b1, b2 = b2, b1 | ||
| b2.parent = b1 | ||
| b2.sibling = b1.child | ||
| b1.child = b2 | ||
| b1.degree += 1 | ||
|
|
||
| return b1 | ||
|
|
||
| def unionBinomialHeap(l1, l2): | ||
| _new = [] | ||
| it = ot = 0 | ||
| while (it < len(l1)) and (ot < len(l2)): | ||
| if l1[it].degree <= l2[ot].degree: | ||
| _new.append(l1[it]) | ||
| it+=1 | ||
| else: | ||
| _new.append(l2[ot]) | ||
| ot += 1 | ||
|
|
||
| while it < len(l1): | ||
| _new.append(l1[it]) | ||
| it += 1 | ||
| while ot < len(l2): | ||
| _new.append(l2[ot]) | ||
| ot += 1 | ||
|
|
||
| return _new |
There was a problem hiding this comment.
Почему эти функции определены внутри функции __init__?
| # добавляем в биномиальную кучу | ||
| def insert(_head, key): | ||
| temp = Node(key) | ||
| return insertATreeInHeap(_head, temp) |
There was a problem hiding this comment.
У Вас метод называется InsertATreeInHeap
|
|
||
| elif _heap[it1].degree == _heap[it2].degree: | ||
| _heap[it1] = mergeBinomialTrees(_heap[it1], _heap[it2]) | ||
| del _heap[it2] |
There was a problem hiding this comment.
Вы итерируетесь по этому циклу при помощи индексов и удаляете элементы из него, это приведет к IndexError.
| while it1 < len(_heap): | ||
| if it2 == len(_heap): | ||
| it1 += 1 | ||
|
|
||
| elif _heap[it1].degree < _heap[it2].degree: | ||
| it1 += 1 | ||
| it2 += 1 | ||
| if it3 < len(_heap): | ||
| it3 += 1 | ||
|
|
||
| elif it3 < len(_heap) and _heap[it1].degree == _heap[it2].degree == _heap[it3].degree: | ||
| it1 += 1 | ||
| it2 += 1 | ||
| it3 += 1 | ||
|
|
||
| elif _heap[it1].degree == _heap[it2].degree: | ||
| _heap[it1] = mergeBinomialTrees(_heap[it1], _heap[it2]) | ||
| del _heap[it2] | ||
| if it3 < len(_heap): | ||
| it3 += 1 |
There was a problem hiding this comment.
Есть подозрение, что в текущей реализации это может зависнуть, например если сделать так:
def test():
node_deg1 = Node(10)
node_deg1.degree = 1
node_deg0 = Node(20)
node_deg0.degree = 0
broken_heap = [node_deg1, node_deg0]
Node.adjust(broken_heap)|
|
||
| # соединение двух биномиальных куч | ||
| def mergeBinomialTrees(b1, b2): | ||
| if b1.data > b2.data: |
There was a problem hiding this comment.
А почему не обрабатываются остальные случаи? Если b1.data <= b2.data функция вернет None.
| priority_queue = [] | ||
| heapq.heappush(priority_queue, (2, "One")) | ||
| heapq.heappush(priority_queue, (1, "Two")) | ||
| heapq.heappush(priority_queue, (3, "Three")) | ||
| while priority_queue: | ||
| priority, task = heapq.heappop(priority_queue) |
|
|
||
| # соединяем кучу с детьми минимального элемента | ||
| lo = removeMinFromTreeReturnBHeap(temp) | ||
| new_heap = unionBionomialHeap(new_heap, lo) |
No description provided.