Skip to content

Commit b33f0b0

Browse files
authored
Update skew_heap.py
1 parent cf707b5 commit b33f0b0

1 file changed

Lines changed: 33 additions & 32 deletions

File tree

data_structures/heap/skew_heap.py

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import annotations
44

55
from collections.abc import Iterable, Iterator
6-
from typing import Protocol, TypeVar
6+
from typing import Protocol, TypeVar, Any
77

88

99
class Comparable(Protocol):
@@ -61,11 +61,9 @@ def value(self) -> T:
6161
return self._value
6262

6363
@staticmethod
64-
def merge(
65-
root1: SkewNode[T] | None, root2: SkewNode[T] | None
66-
) -> SkewNode[T] | None:
64+
def merge(root1: SkewNode[T] | None, root2: SkewNode[T] | None) -> SkewNode[T] | None:
6765
"""
68-
Merge 2 nodes together.
66+
Merge two nodes together.
6967
>>> SkewNode.merge(SkewNode(10), SkewNode(-10.5)).value
7068
-10.5
7169
>>> SkewNode.merge(SkewNode(10), SkewNode(10.5)).value
@@ -75,35 +73,39 @@ def merge(
7573
>>> SkewNode.merge(SkewNode(-100), SkewNode(-10.5)).value
7674
-100
7775
"""
76+
# Handle empty nodes
7877
if not root1:
7978
return root2
80-
8179
if not root2:
8280
return root1
81+
82+
# Compare values using explicit __lt__ method
8383
try:
84-
if root1.value < root2.value:
84+
# Check if root1 is smaller than root2
85+
if root1.value.__lt__(root2.value):
86+
# root1 is smaller, make it the new root
8587
result = root1
8688
temp = root1.right
8789
result.right = root1.left
8890
result.left = SkewNode.merge(temp, root2)
8991
return result
90-
except TypeError:
91-
# 回退到值比较
92+
except (TypeError, AttributeError):
93+
# Fallback if __lt__ comparison fails
9294
pass
93-
94-
# 如果比较失败或 root2 更小
95+
96+
# If root2 is smaller or comparison failed, use root2 as new root
9597
result = root2
9698
temp = root2.right
9799
result.right = root2.left
98100
result.left = SkewNode.merge(root1, temp)
99101
return result
100102

101103

104+
102105
class SkewHeap[T]:
103106
"""
104-
A data structure that allows inserting a new value and to pop the smallest
105-
values. Both operations take O(logN) time where N is the size of the
106-
structure.
107+
A data structure that allows inserting a new value and popping the smallest
108+
values. Both operations take O(logN) time where N is the size of the heap.
107109
Wiki: https://en.wikipedia.org/wiki/Skew_heap
108110
Visualization: https://www.cs.usfca.edu/~galles/visualization/SkewHeap.html
109111
@@ -125,6 +127,8 @@ class SkewHeap[T]:
125127

126128
def __init__(self, data: Iterable[T] | None = ()) -> None:
127129
"""
130+
Initialize the skew heap with optional data
131+
128132
>>> sh = SkewHeap([3, 1, 3, 7])
129133
>>> list(sh)
130134
[1, 3, 3, 7]
@@ -136,8 +140,8 @@ def __init__(self, data: Iterable[T] | None = ()) -> None:
136140

137141
def __bool__(self) -> bool:
138142
"""
139-
Check if the heap is not empty.
140-
143+
Check if the heap is not empty
144+
141145
>>> sh = SkewHeap()
142146
>>> bool(sh)
143147
False
@@ -152,26 +156,26 @@ def __bool__(self) -> bool:
152156

153157
def __iter__(self) -> Iterator[T]:
154158
"""
155-
Returns sorted list containing all the values in the heap.
156-
159+
Iterate through all values in sorted order
160+
157161
>>> sh = SkewHeap([3, 1, 3, 7])
158162
>>> list(sh)
159163
[1, 3, 3, 7]
160164
"""
161165
result: list[T] = []
162166
while self:
163167
result.append(self.pop())
164-
165-
# Pushing items back to the heap not to clear it.
168+
169+
# Restore the heap state
166170
for item in result:
167171
self.insert(item)
168-
172+
169173
return iter(result)
170174

171175
def insert(self, value: T) -> None:
172176
"""
173-
Insert the value into the heap.
174-
177+
Insert a new value into the heap
178+
175179
>>> sh = SkewHeap()
176180
>>> sh.insert(3)
177181
>>> sh.insert(1)
@@ -184,8 +188,8 @@ def insert(self, value: T) -> None:
184188

185189
def pop(self) -> T:
186190
"""
187-
Pop the smallest value from the heap and return it.
188-
191+
Remove and return the smallest value from the heap
192+
189193
>>> sh = SkewHeap([3, 1, 3, 7])
190194
>>> sh.pop()
191195
1
@@ -203,15 +207,12 @@ def pop(self) -> T:
203207
result = self.top()
204208
if self._root:
205209
self._root = SkewNode.merge(self._root.left, self._root.right)
206-
else:
207-
self._root = None
208-
209210
return result
210211

211212
def top(self) -> T:
212213
"""
213-
Return the smallest value from the heap.
214-
214+
Return the smallest value without removing it
215+
215216
>>> sh = SkewHeap()
216217
>>> sh.insert(3)
217218
>>> sh.top()
@@ -232,8 +233,8 @@ def top(self) -> T:
232233

233234
def clear(self) -> None:
234235
"""
235-
Clear the heap.
236-
236+
Clear all elements from the heap
237+
237238
>>> sh = SkewHeap([3, 1, 3, 7])
238239
>>> sh.clear()
239240
>>> sh.pop()

0 commit comments

Comments
 (0)