Skip to content

Commit 072a3d6

Browse files
authored
fix skew_heap.py
1 parent bf93372 commit 072a3d6

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

data_structures/heap/skew_heap.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
from __future__ import annotations
44

5-
from collections.abc import Iterable, Iterator
6-
from typing import Any, Callable, Optional
5+
from collections.abc import Iterable, Iterator, Callable
6+
from typing import Any
77

88

99
class SkewNode:
@@ -14,8 +14,8 @@ class SkewNode:
1414

1515
def __init__(self, value: Any) -> None:
1616
self._value: Any = value
17-
self.left: Optional[SkewNode] = None
18-
self.right: Optional[SkewNode] = None
17+
self.left: SkewNode | None = None
18+
self.right: SkewNode | None = None
1919

2020
@property
2121
def value(self) -> Any:
@@ -37,10 +37,10 @@ def value(self) -> Any:
3737

3838
@staticmethod
3939
def merge(
40-
root1: Optional[SkewNode],
41-
root2: Optional[SkewNode],
42-
comp: Callable[[Any, Any], bool],
43-
) -> Optional[SkewNode]:
40+
root1: SkewNode | None,
41+
root2: SkewNode | None,
42+
comp: Callable[[Any, Any], bool]
43+
) -> SkewNode | None:
4444
"""
4545
Merge two nodes together.
4646
>>> def comp(a, b): return a < b
@@ -75,7 +75,6 @@ def merge(
7575
result.left = SkewNode.merge(root1, temp, comp)
7676
return result
7777

78-
7978
class SkewHeap:
8079
"""
8180
A data structure that allows inserting a new value and popping the smallest
@@ -102,21 +101,21 @@ class SkewHeap:
102101
def __init__(
103102
self,
104103
data: Iterable[Any] | None = None,
105-
comp: Callable[[Any, Any], bool] = lambda a, b: a < b,
104+
comp: Callable[[Any, Any], bool] = lambda a, b: a < b
106105
) -> None:
107106
"""
108107
Initialize the skew heap with optional data and comparison function
109-
108+
110109
>>> sh = SkewHeap([3, 1, 3, 7])
111110
>>> list(sh)
112111
[1, 3, 3, 7]
113-
112+
114113
# Max-heap example
115114
>>> max_heap = SkewHeap([3, 1, 3, 7], comp=lambda a, b: a > b)
116115
>>> list(max_heap)
117116
[7, 3, 3, 1]
118117
"""
119-
self._root: Optional[SkewNode] = None
118+
self._root: SkewNode | None = None
120119
self._comp = comp
121120
if data:
122121
for item in data:
@@ -159,7 +158,6 @@ def __iter__(self) -> Iterator[Any]:
159158
# Restore the heap state
160159
self._root = temp_heap._root
161160
return iter(result)
162-
163161
def insert(self, value: Any) -> None:
164162
"""
165163
Insert a new value into the heap
@@ -172,7 +170,11 @@ def insert(self, value: Any) -> None:
172170
>>> list(sh)
173171
[1, 3, 3, 7]
174172
"""
175-
self._root = SkewNode.merge(self._root, SkewNode(value), self._comp)
173+
self._root = SkewNode.merge(
174+
self._root,
175+
SkewNode(value),
176+
self._comp
177+
)
176178

177179
def pop(self) -> Any:
178180
"""
@@ -194,7 +196,11 @@ def pop(self) -> Any:
194196
"""
195197
result = self.top()
196198
if self._root:
197-
self._root = SkewNode.merge(self._root.left, self._root.right, self._comp)
199+
self._root = SkewNode.merge(
200+
self._root.left,
201+
self._root.right,
202+
self._comp
203+
)
198204
return result
199205

200206
def top(self) -> Any:

0 commit comments

Comments
 (0)