Skip to content

Иванова Алина. решение контрольной работы 2#12

Open
ialina07 wants to merge 1 commit intomainfrom
kr2
Open

Иванова Алина. решение контрольной работы 2#12
ialina07 wants to merge 1 commit intomainfrom
kr2

Conversation

@ialina07
Copy link
Owner

No description provided.

Copy link

@Godrik0 Godrik0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4 балла за наличие хоть какой-то структуры, функции extractMin и insert.

Итого 4/30

Comment on lines +11 to +12
def __init__(self):
self.data = key
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вы используете переменную key, которую не передаете в функцию.
Соответственно все вызовы вида temp = Node(key) не будут работать.

Comment on lines +14 to +16
self.parent = None
self.child = None
self.parent = None
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.parent инициализируется дважды

Comment on lines +20 to +48
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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Почему эти функции определены внутри функции __init__?

# добавляем в биномиальную кучу
def insert(_head, key):
temp = Node(key)
return insertATreeInHeap(_head, temp)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

У Вас метод называется InsertATreeInHeap


elif _heap[it1].degree == _heap[it2].degree:
_heap[it1] = mergeBinomialTrees(_heap[it1], _heap[it2])
del _heap[it2]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вы итерируетесь по этому циклу при помощи индексов и удаляете элементы из него, это приведет к IndexError.

Comment on lines +62 to +81
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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Есть подозрение, что в текущей реализации это может зависнуть, например если сделать так:

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:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А почему не обрабатываются остальные случаи? Если b1.data <= b2.data функция вернет None.

Comment on lines +3 to +8
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)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?


# соединяем кучу с детьми минимального элемента
lo = removeMinFromTreeReturnBHeap(temp)
new_heap = unionBionomialHeap(new_heap, lo)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unionBinomialHeap

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants