MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / BinomialHeap

Class BinomialHeap

data_structures/heap/binomial_heap.py:48–394  ·  view source on GitHub ↗

r""" Min-oriented priority queue implemented with the Binomial Heap data structure implemented with the BinomialHeap class. It supports: - Insert element in a heap with n elements: Guaranteed logn, amoratized 1 - Merge (meld) heaps of size m and n: O(logn + logm)

Source from the content-addressed store, hash-verified

46
47
48class BinomialHeap:
49 r"""
50 Min-oriented priority queue implemented with the Binomial Heap data
51 structure implemented with the BinomialHeap class. It supports:
52 - Insert element in a heap with n elements: Guaranteed logn, amoratized 1
53 - Merge (meld) heaps of size m and n: O(logn + logm)
54 - Delete Min: O(logn)
55 - Peek (return min without deleting it): O(1)
56
57 Example:
58
59 Create a random permutation of 30 integers to be inserted and 19 of them deleted
60 >>> import numpy as np
61 >>> permutation = np.random.permutation(list(range(30)))
62
63 Create a Heap and insert the 30 integers
64 __init__() test
65 >>> first_heap = BinomialHeap()
66
67 30 inserts - insert() test
68 >>> for number in permutation:
69 ... first_heap.insert(number)
70
71 Size test
72 >>> first_heap.size
73 30
74
75 Deleting - delete() test
76 >>> [int(first_heap.delete_min()) for _ in range(20)]
77 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
78
79 Create a new Heap
80 >>> second_heap = BinomialHeap()
81 >>> vals = [17, 20, 31, 34]
82 >>> for value in vals:
83 ... second_heap.insert(value)
84
85
86 The heap should have the following structure:
87
88 17
89 / \
90 # 31
91 / \
92 20 34
93 / \ / \
94 # # # #
95
96 preOrder() test
97 >>> " ".join(str(x) for x in second_heap.pre_order())
98 "(17, 0) ('#', 1) (31, 1) (20, 2) ('#', 3) ('#', 3) (34, 2) ('#', 3) ('#', 3)"
99
100 printing Heap - __str__() test
101 >>> print(second_heap)
102 17
103 -#
104 -31
105 --20

Callers 1

delete_minMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected