Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions data_structures/heap/binomial_heap.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
self.size = self.size + other.size

# Update min.node
if self.min_node.val > other.min_node.val:

Check failure on line 149 in data_structures/heap/binomial_heap.py

View workflow job for this annotation

GitHub Actions / ty

ty (missing-union-attribute)

data_structures/heap/binomial_heap.py:149:12: missing-union-attribute: Attribute `val` is not defined on `None` in union `Unknown | None | Node`
self.min_node = other.min_node
# Merge

Expand Down Expand Up @@ -179,7 +179,11 @@
next_node = i.parent.parent

# Merging trees
pair = (i, i.parent)
i = i.merge_trees(i.parent)
# With equal values the old min_node can lose its place as a root
if self.min_node in pair:
self.min_node = i

# Updating links
i.left = previous_node
Expand Down Expand Up @@ -219,7 +223,7 @@
self.size += 1

# update min_node
if val < self.min_node.val:

Check failure on line 226 in data_structures/heap/binomial_heap.py

View workflow job for this annotation

GitHub Actions / ty

ty (missing-union-attribute)

data_structures/heap/binomial_heap.py:226:22: missing-union-attribute: Attribute `val` is not defined on `None` in union `Unknown | None | Node`
self.min_node = new_node
# Put new_node as a bottom_root in heap
assert self.bottom_root is not None
Expand All @@ -237,7 +241,11 @@
next_node = self.bottom_root.parent.parent

# Merge
pair = (self.bottom_root, self.bottom_root.parent)
self.bottom_root = self.bottom_root.merge_trees(self.bottom_root.parent)
# With equal values the old min_node can lose its place as a root
if self.min_node in pair:
self.min_node = self.bottom_root

# Update Links
self.bottom_root.parent = next_node
Expand All @@ -249,7 +257,7 @@
"""
return min element without deleting it
"""
return self.min_node.val

Check failure on line 260 in data_structures/heap/binomial_heap.py

View workflow job for this annotation

GitHub Actions / ty

ty (missing-union-attribute)

data_structures/heap/binomial_heap.py:260:16: missing-union-attribute: Attribute `val` is not defined on `None` in union `Unknown | None | Node`

def is_empty(self) -> bool:
return self.size == 0
Expand All @@ -257,11 +265,19 @@
def delete_min(self):
"""
delete min element and return it

>>> heap = BinomialHeap()
>>> for value in [4, 1, 8, 2, 2]:
... heap.insert(value)
>>> heap.delete_min(), heap.delete_min(), heap.peek()
(1, 2, 2)
>>> heap.delete_min(), heap.delete_min(), heap.delete_min()
(2, 4, 8)
"""
# assert not self.isEmpty(), "Empty Heap"

# Save minimal value
min_value = self.min_node.val

Check failure on line 280 in data_structures/heap/binomial_heap.py

View workflow job for this annotation

GitHub Actions / ty

ty (missing-union-attribute)

data_structures/heap/binomial_heap.py:280:21: missing-union-attribute: Attribute `val` is not defined on `None` in union `Unknown | None | Node`

# Last element in heap corner case
if self.size == 1:
Expand All @@ -278,12 +294,12 @@
# 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
if self.min_node.right is None:

Check failure on line 297 in data_structures/heap/binomial_heap.py

View workflow job for this annotation

GitHub Actions / ty

ty (missing-union-attribute)

data_structures/heap/binomial_heap.py:297:12: missing-union-attribute: Attribute `right` is not defined on `None` in union `Unknown | None | Node`
# Update size
self.size -= 1

# Update bottom root
self.bottom_root = self.bottom_root.parent

Check failure on line 302 in data_structures/heap/binomial_heap.py

View workflow job for this annotation

GitHub Actions / ty

ty (missing-union-attribute)

data_structures/heap/binomial_heap.py:302:32: missing-union-attribute: Attribute `parent` is not defined on `None` in union `Unknown | None | Node`
assert self.bottom_root is not None
self.bottom_root.left = None

Expand Down
Loading