2019-09-05 05:58:38 +00:00
|
|
|
"""
|
2019-10-29 22:54:31 +00:00
|
|
|
Binomial Heap
|
|
|
|
Reference: Advanced Data Structures, Peter Brass
|
2019-09-05 05:58:38 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class Node:
|
|
|
|
"""
|
|
|
|
Node in a doubly-linked binomial tree, containing:
|
|
|
|
- value
|
|
|
|
- size of left subtree
|
2019-10-29 22:54:31 +00:00
|
|
|
- link to left, right and parent nodes
|
2019-09-05 05:58:38 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, val):
|
|
|
|
self.val = val
|
|
|
|
# Number of nodes in left subtree
|
|
|
|
self.left_tree_size = 0
|
|
|
|
self.left = None
|
|
|
|
self.right = None
|
|
|
|
self.parent = None
|
|
|
|
|
2022-10-29 20:45:21 +00:00
|
|
|
def merge_trees(self, other):
|
2019-09-05 05:58:38 +00:00
|
|
|
"""
|
2019-10-29 22:54:31 +00:00
|
|
|
In-place merge of two binomial trees of equal size.
|
|
|
|
Returns the root of the resulting tree
|
2019-09-05 05:58:38 +00:00
|
|
|
"""
|
2019-10-05 05:14:13 +00:00
|
|
|
assert self.left_tree_size == other.left_tree_size, "Unequal Sizes of Blocks"
|
2019-09-05 05:58:38 +00:00
|
|
|
|
|
|
|
if self.val < other.val:
|
|
|
|
other.left = self.right
|
|
|
|
other.parent = None
|
|
|
|
if self.right:
|
|
|
|
self.right.parent = other
|
|
|
|
self.right = other
|
2019-10-05 05:14:13 +00:00
|
|
|
self.left_tree_size = self.left_tree_size * 2 + 1
|
2019-09-05 05:58:38 +00:00
|
|
|
return self
|
|
|
|
else:
|
|
|
|
self.left = other.right
|
|
|
|
self.parent = None
|
|
|
|
if other.right:
|
|
|
|
other.right.parent = self
|
|
|
|
other.right = self
|
2019-10-05 05:14:13 +00:00
|
|
|
other.left_tree_size = other.left_tree_size * 2 + 1
|
2019-09-05 05:58:38 +00:00
|
|
|
return other
|
|
|
|
|
|
|
|
|
|
|
|
class BinomialHeap:
|
2019-10-29 22:54:31 +00:00
|
|
|
r"""
|
|
|
|
Min-oriented priority queue implemented with the Binomial Heap data
|
|
|
|
structure implemented with the BinomialHeap class. It supports:
|
2020-01-18 12:24:33 +00:00
|
|
|
- Insert element in a heap with n elements: Guaranteed logn, amoratized 1
|
2019-09-05 05:58:38 +00:00
|
|
|
- Merge (meld) heaps of size m and n: O(logn + logm)
|
2019-10-29 22:54:31 +00:00
|
|
|
- Delete Min: O(logn)
|
2019-09-05 05:58:38 +00:00
|
|
|
- Peek (return min without deleting it): O(1)
|
2019-10-29 22:54:31 +00:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
Create a random permutation of 30 integers to be inserted and 19 of them deleted
|
|
|
|
>>> import numpy as np
|
|
|
|
>>> permutation = np.random.permutation(list(range(30)))
|
|
|
|
|
|
|
|
Create a Heap and insert the 30 integers
|
|
|
|
__init__() test
|
|
|
|
>>> first_heap = BinomialHeap()
|
|
|
|
|
|
|
|
30 inserts - insert() test
|
|
|
|
>>> for number in permutation:
|
|
|
|
... first_heap.insert(number)
|
|
|
|
|
|
|
|
Size test
|
2022-10-27 20:52:00 +00:00
|
|
|
>>> first_heap.size
|
2019-10-29 22:54:31 +00:00
|
|
|
30
|
|
|
|
|
|
|
|
Deleting - delete() test
|
2024-09-30 21:01:15 +00:00
|
|
|
>>> [int(first_heap.delete_min()) for _ in range(20)]
|
2022-10-29 20:45:21 +00:00
|
|
|
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
|
2019-10-29 22:54:31 +00:00
|
|
|
|
|
|
|
Create a new Heap
|
|
|
|
>>> second_heap = BinomialHeap()
|
|
|
|
>>> vals = [17, 20, 31, 34]
|
|
|
|
>>> for value in vals:
|
|
|
|
... second_heap.insert(value)
|
|
|
|
|
|
|
|
|
|
|
|
The heap should have the following structure:
|
|
|
|
|
|
|
|
17
|
|
|
|
/ \
|
|
|
|
# 31
|
|
|
|
/ \
|
|
|
|
20 34
|
|
|
|
/ \ / \
|
|
|
|
# # # #
|
|
|
|
|
|
|
|
preOrder() test
|
2022-10-29 20:45:21 +00:00
|
|
|
>>> " ".join(str(x) for x in second_heap.pre_order())
|
|
|
|
"(17, 0) ('#', 1) (31, 1) (20, 2) ('#', 3) ('#', 3) (34, 2) ('#', 3) ('#', 3)"
|
2019-10-29 22:54:31 +00:00
|
|
|
|
|
|
|
printing Heap - __str__() test
|
|
|
|
>>> print(second_heap)
|
|
|
|
17
|
|
|
|
-#
|
|
|
|
-31
|
|
|
|
--20
|
|
|
|
---#
|
|
|
|
---#
|
|
|
|
--34
|
|
|
|
---#
|
|
|
|
---#
|
|
|
|
|
|
|
|
mergeHeaps() test
|
2022-10-29 20:45:21 +00:00
|
|
|
>>>
|
|
|
|
>>> merged = second_heap.merge_heaps(first_heap)
|
2019-10-29 22:54:31 +00:00
|
|
|
>>> merged.peek()
|
|
|
|
17
|
|
|
|
|
|
|
|
values in merged heap; (merge is inplace)
|
2022-10-29 20:45:21 +00:00
|
|
|
>>> results = []
|
|
|
|
>>> while not first_heap.is_empty():
|
2024-09-30 21:01:15 +00:00
|
|
|
... results.append(int(first_heap.delete_min()))
|
2022-10-29 20:45:21 +00:00
|
|
|
>>> results
|
|
|
|
[17, 20, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 31, 34]
|
2019-09-05 05:58:38 +00:00
|
|
|
"""
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
def __init__(self, bottom_root=None, min_node=None, heap_size=0):
|
2019-09-05 05:58:38 +00:00
|
|
|
self.size = heap_size
|
|
|
|
self.bottom_root = bottom_root
|
|
|
|
self.min_node = min_node
|
|
|
|
|
2022-10-29 20:45:21 +00:00
|
|
|
def merge_heaps(self, other):
|
2019-09-05 05:58:38 +00:00
|
|
|
"""
|
2019-10-29 22:54:31 +00:00
|
|
|
In-place merge of two binomial heaps.
|
|
|
|
Both of them become the resulting merged heap
|
2019-09-05 05:58:38 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
# Empty heaps corner cases
|
|
|
|
if other.size == 0:
|
2023-03-01 16:23:33 +00:00
|
|
|
return None
|
2019-09-05 05:58:38 +00:00
|
|
|
if self.size == 0:
|
|
|
|
self.size = other.size
|
|
|
|
self.bottom_root = other.bottom_root
|
|
|
|
self.min_node = other.min_node
|
2023-03-01 16:23:33 +00:00
|
|
|
return None
|
2019-09-05 05:58:38 +00:00
|
|
|
# Update size
|
|
|
|
self.size = self.size + other.size
|
|
|
|
|
|
|
|
# Update min.node
|
|
|
|
if self.min_node.val > other.min_node.val:
|
|
|
|
self.min_node = other.min_node
|
|
|
|
# Merge
|
|
|
|
|
|
|
|
# Order roots by left_subtree_size
|
|
|
|
combined_roots_list = []
|
|
|
|
i, j = self.bottom_root, other.bottom_root
|
|
|
|
while i or j:
|
2019-10-05 05:14:13 +00:00
|
|
|
if i and ((not j) or i.left_tree_size < j.left_tree_size):
|
2019-09-05 05:58:38 +00:00
|
|
|
combined_roots_list.append((i, True))
|
|
|
|
i = i.parent
|
|
|
|
else:
|
|
|
|
combined_roots_list.append((j, False))
|
|
|
|
j = j.parent
|
|
|
|
# Insert links between them
|
|
|
|
for i in range(len(combined_roots_list) - 1):
|
2019-10-05 05:14:13 +00:00
|
|
|
if combined_roots_list[i][1] != combined_roots_list[i + 1][1]:
|
|
|
|
combined_roots_list[i][0].parent = combined_roots_list[i + 1][0]
|
|
|
|
combined_roots_list[i + 1][0].left = combined_roots_list[i][0]
|
2019-09-05 05:58:38 +00:00
|
|
|
# Consecutively merge roots with same left_tree_size
|
|
|
|
i = combined_roots_list[0][0]
|
|
|
|
while i.parent:
|
|
|
|
if (
|
2019-10-05 05:14:13 +00:00
|
|
|
(i.left_tree_size == i.parent.left_tree_size) and (not i.parent.parent)
|
2019-09-05 05:58:38 +00:00
|
|
|
) or (
|
|
|
|
i.left_tree_size == i.parent.left_tree_size
|
2019-10-05 05:14:13 +00:00
|
|
|
and i.left_tree_size != i.parent.parent.left_tree_size
|
2019-09-05 05:58:38 +00:00
|
|
|
):
|
|
|
|
# Neighbouring Nodes
|
|
|
|
previous_node = i.left
|
|
|
|
next_node = i.parent.parent
|
|
|
|
|
|
|
|
# Merging trees
|
2022-10-29 20:45:21 +00:00
|
|
|
i = i.merge_trees(i.parent)
|
2019-09-05 05:58:38 +00:00
|
|
|
|
|
|
|
# Updating links
|
|
|
|
i.left = previous_node
|
|
|
|
i.parent = next_node
|
|
|
|
if previous_node:
|
|
|
|
previous_node.parent = i
|
|
|
|
if next_node:
|
|
|
|
next_node.left = i
|
|
|
|
else:
|
|
|
|
i = i.parent
|
|
|
|
# Updating self.bottom_root
|
|
|
|
while i.left:
|
|
|
|
i = i.left
|
|
|
|
self.bottom_root = i
|
|
|
|
|
|
|
|
# Update other
|
|
|
|
other.size = self.size
|
|
|
|
other.bottom_root = self.bottom_root
|
|
|
|
other.min_node = self.min_node
|
|
|
|
|
|
|
|
# Return the merged heap
|
|
|
|
return self
|
|
|
|
|
|
|
|
def insert(self, val):
|
|
|
|
"""
|
2019-10-29 22:54:31 +00:00
|
|
|
insert a value in the heap
|
2019-09-05 05:58:38 +00:00
|
|
|
"""
|
|
|
|
if self.size == 0:
|
|
|
|
self.bottom_root = Node(val)
|
|
|
|
self.size = 1
|
|
|
|
self.min_node = self.bottom_root
|
|
|
|
else:
|
|
|
|
# Create new node
|
|
|
|
new_node = Node(val)
|
|
|
|
|
|
|
|
# Update size
|
|
|
|
self.size += 1
|
|
|
|
|
|
|
|
# update min_node
|
|
|
|
if val < self.min_node.val:
|
|
|
|
self.min_node = new_node
|
|
|
|
# Put new_node as a bottom_root in heap
|
|
|
|
self.bottom_root.left = new_node
|
|
|
|
new_node.parent = self.bottom_root
|
|
|
|
self.bottom_root = new_node
|
|
|
|
|
|
|
|
# Consecutively merge roots with same left_tree_size
|
|
|
|
while (
|
|
|
|
self.bottom_root.parent
|
|
|
|
and self.bottom_root.left_tree_size
|
|
|
|
== self.bottom_root.parent.left_tree_size
|
|
|
|
):
|
|
|
|
# Next node
|
|
|
|
next_node = self.bottom_root.parent.parent
|
|
|
|
|
|
|
|
# Merge
|
2022-10-29 20:45:21 +00:00
|
|
|
self.bottom_root = self.bottom_root.merge_trees(self.bottom_root.parent)
|
2019-09-05 05:58:38 +00:00
|
|
|
|
|
|
|
# Update Links
|
|
|
|
self.bottom_root.parent = next_node
|
|
|
|
self.bottom_root.left = None
|
|
|
|
if next_node:
|
|
|
|
next_node.left = self.bottom_root
|
|
|
|
|
|
|
|
def peek(self):
|
|
|
|
"""
|
2019-10-29 22:54:31 +00:00
|
|
|
return min element without deleting it
|
2019-09-05 05:58:38 +00:00
|
|
|
"""
|
|
|
|
return self.min_node.val
|
|
|
|
|
2022-10-29 20:45:21 +00:00
|
|
|
def is_empty(self):
|
2019-09-05 05:58:38 +00:00
|
|
|
return self.size == 0
|
|
|
|
|
2022-10-29 20:45:21 +00:00
|
|
|
def delete_min(self):
|
2019-09-05 05:58:38 +00:00
|
|
|
"""
|
2019-10-29 22:54:31 +00:00
|
|
|
delete min element and return it
|
2019-09-05 05:58:38 +00:00
|
|
|
"""
|
|
|
|
# assert not self.isEmpty(), "Empty Heap"
|
|
|
|
|
|
|
|
# Save minimal value
|
|
|
|
min_value = self.min_node.val
|
|
|
|
|
|
|
|
# Last element in heap corner case
|
|
|
|
if self.size == 1:
|
|
|
|
# Update size
|
|
|
|
self.size = 0
|
|
|
|
|
|
|
|
# Update bottom root
|
|
|
|
self.bottom_root = None
|
|
|
|
|
|
|
|
# Update min_node
|
|
|
|
self.min_node = None
|
|
|
|
|
|
|
|
return min_value
|
|
|
|
# No right subtree corner case
|
|
|
|
# The structure of the tree implies that this should be the bottom root
|
|
|
|
# and there is at least one other root
|
2019-09-21 14:23:34 +00:00
|
|
|
if self.min_node.right is None:
|
2019-09-05 05:58:38 +00:00
|
|
|
# Update size
|
|
|
|
self.size -= 1
|
|
|
|
|
|
|
|
# Update bottom root
|
|
|
|
self.bottom_root = self.bottom_root.parent
|
|
|
|
self.bottom_root.left = None
|
|
|
|
|
|
|
|
# Update min_node
|
|
|
|
self.min_node = self.bottom_root
|
|
|
|
i = self.bottom_root.parent
|
|
|
|
while i:
|
|
|
|
if i.val < self.min_node.val:
|
|
|
|
self.min_node = i
|
|
|
|
i = i.parent
|
|
|
|
return min_value
|
|
|
|
# General case
|
|
|
|
# Find the BinomialHeap of the right subtree of min_node
|
|
|
|
bottom_of_new = self.min_node.right
|
|
|
|
bottom_of_new.parent = None
|
|
|
|
min_of_new = bottom_of_new
|
|
|
|
size_of_new = 1
|
|
|
|
|
|
|
|
# Size, min_node and bottom_root
|
|
|
|
while bottom_of_new.left:
|
|
|
|
size_of_new = size_of_new * 2 + 1
|
|
|
|
bottom_of_new = bottom_of_new.left
|
|
|
|
if bottom_of_new.val < min_of_new.val:
|
|
|
|
min_of_new = bottom_of_new
|
|
|
|
# Corner case of single root on top left path
|
2019-10-05 05:14:13 +00:00
|
|
|
if (not self.min_node.left) and (not self.min_node.parent):
|
2019-09-05 05:58:38 +00:00
|
|
|
self.size = size_of_new
|
|
|
|
self.bottom_root = bottom_of_new
|
|
|
|
self.min_node = min_of_new
|
|
|
|
# print("Single root, multiple nodes case")
|
|
|
|
return min_value
|
|
|
|
# Remaining cases
|
|
|
|
# Construct heap of right subtree
|
2022-10-29 20:45:21 +00:00
|
|
|
new_heap = BinomialHeap(
|
2019-10-05 05:14:13 +00:00
|
|
|
bottom_root=bottom_of_new, min_node=min_of_new, heap_size=size_of_new
|
2019-09-05 05:58:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Update size
|
|
|
|
self.size = self.size - 1 - size_of_new
|
|
|
|
|
|
|
|
# Neighbour nodes
|
|
|
|
previous_node = self.min_node.left
|
|
|
|
next_node = self.min_node.parent
|
|
|
|
|
|
|
|
# Initialize new bottom_root and min_node
|
|
|
|
self.min_node = previous_node or next_node
|
|
|
|
self.bottom_root = next_node
|
|
|
|
|
|
|
|
# Update links of previous_node and search below for new min_node and
|
|
|
|
# bottom_root
|
|
|
|
if previous_node:
|
|
|
|
previous_node.parent = next_node
|
|
|
|
|
|
|
|
# Update bottom_root and search for min_node below
|
|
|
|
self.bottom_root = previous_node
|
|
|
|
self.min_node = previous_node
|
|
|
|
while self.bottom_root.left:
|
|
|
|
self.bottom_root = self.bottom_root.left
|
|
|
|
if self.bottom_root.val < self.min_node.val:
|
|
|
|
self.min_node = self.bottom_root
|
|
|
|
if next_node:
|
|
|
|
next_node.left = previous_node
|
|
|
|
|
|
|
|
# Search for new min_node above min_node
|
|
|
|
i = next_node
|
|
|
|
while i:
|
|
|
|
if i.val < self.min_node.val:
|
|
|
|
self.min_node = i
|
|
|
|
i = i.parent
|
|
|
|
# Merge heaps
|
2022-10-29 20:45:21 +00:00
|
|
|
self.merge_heaps(new_heap)
|
2019-09-05 05:58:38 +00:00
|
|
|
|
2024-09-30 21:01:15 +00:00
|
|
|
return int(min_value)
|
2019-09-05 05:58:38 +00:00
|
|
|
|
2022-10-29 20:45:21 +00:00
|
|
|
def pre_order(self):
|
2019-09-05 05:58:38 +00:00
|
|
|
"""
|
2019-10-29 22:54:31 +00:00
|
|
|
Returns the Pre-order representation of the heap including
|
|
|
|
values of nodes plus their level distance from the root;
|
|
|
|
Empty nodes appear as #
|
2019-09-05 05:58:38 +00:00
|
|
|
"""
|
|
|
|
# Find top root
|
|
|
|
top_root = self.bottom_root
|
|
|
|
while top_root.parent:
|
|
|
|
top_root = top_root.parent
|
|
|
|
# preorder
|
2022-10-29 20:45:21 +00:00
|
|
|
heap_pre_order = []
|
|
|
|
self.__traversal(top_root, heap_pre_order)
|
|
|
|
return heap_pre_order
|
2019-09-05 05:58:38 +00:00
|
|
|
|
|
|
|
def __traversal(self, curr_node, preorder, level=0):
|
|
|
|
"""
|
2019-10-29 22:54:31 +00:00
|
|
|
Pre-order traversal of nodes
|
2019-09-05 05:58:38 +00:00
|
|
|
"""
|
|
|
|
if curr_node:
|
|
|
|
preorder.append((curr_node.val, level))
|
2019-10-05 05:14:13 +00:00
|
|
|
self.__traversal(curr_node.left, preorder, level + 1)
|
|
|
|
self.__traversal(curr_node.right, preorder, level + 1)
|
2019-09-05 05:58:38 +00:00
|
|
|
else:
|
|
|
|
preorder.append(("#", level))
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
"""
|
2019-10-29 22:54:31 +00:00
|
|
|
Overwriting str for a pre-order print of nodes in heap;
|
|
|
|
Performance is poor, so use only for small examples
|
2019-09-05 05:58:38 +00:00
|
|
|
"""
|
2022-10-29 20:45:21 +00:00
|
|
|
if self.is_empty():
|
2019-09-05 05:58:38 +00:00
|
|
|
return ""
|
2022-10-29 20:45:21 +00:00
|
|
|
preorder_heap = self.pre_order()
|
2019-09-05 05:58:38 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
return "\n".join(("-" * level + str(value)) for value, level in preorder_heap)
|
2019-09-05 05:58:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Unit Tests
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import doctest
|
|
|
|
|
|
|
|
doctest.testmod()
|