From a1a4b7071081d4a1ea1a8b2951ff80e4731ceffb Mon Sep 17 00:00:00 2001 From: ialina07 Date: Fri, 12 Dec 2025 14:58:46 +0000 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=D0=B0=20=D1=80=D0=B5=D1=88=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BD=D1=82=D1=80=D0=BE=D0=BB=D1=8C=D0=BD=D0=BE=D0=B9=20?= =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/kr2/binomial_heap.py | 147 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 src/kr2/binomial_heap.py diff --git a/src/kr2/binomial_heap.py b/src/kr2/binomial_heap.py new file mode 100644 index 0000000..b39510d --- /dev/null +++ b/src/kr2/binomial_heap.py @@ -0,0 +1,147 @@ +import heapq + +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) + +class Node: + def __init__(self): + self.data = key + self.degree = 0 + self.parent = None + self.child = None + self.parent = None + self.sibling = 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 + + def adjust(_heap): + if len(_heap) <= 1: + return _heap + new_heap = [] + it1 = it2 = it3 = 0 + if len(_heap) == 2: + it2 = 1 + it3 = len(_heap) + else: + it2 = 1 + it3 = 2 + + 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 + return _heap + + def InsertATreeInHeap(_heap, tree): + temp = tree + temp = unionBinomialHeap(_heap, tree) + return adjust(_heap) + + + def removeMinFromTreeReturnBHeap(tree): + heap = [] + temp = tree.child + + # создаем кучу детей минимального элемента + while temp: + lo = temp + temp = temp.sibling + lo.sibling = None + heap.insert(0, lo) + + return heap + + + # добавляем в биномиальную кучу + def insert(_head, key): + temp = Node(key) + return insertATreeInHeap(_head, temp) + + + def getMin(_heap): + temp = _heap[0] + + # ищем минимальный элемент в куче + for node in _heap: + if node.data < temp.data: + temp = node + + return temp + + def extractMin(_heap): + new_heap = [] + lo = [] + + temp = getMin(_heap) + + # удаляем минимальный элемент кучи + for node in _heap: + if node != temp: + new_heap.append(node) + + # соединяем кучу с детьми минимального элемента + lo = removeMinFromTreeReturnBHeap(temp) + new_heap = unionBionomialHeap(new_heap, lo) + new_heap = adjust(new_heap) + + return new_heap + + + + + + + + + + +