33from __future__ import annotations
44
55from collections .abc import Iterable , Iterator
6- from typing import Any , Protocol , TypeVar , runtime_checkable
6+ from typing import Any , Generic , TypeVar , Protocol
77
8-
9- @runtime_checkable
10- class SupportsLessThan (Protocol ):
8+ class Comparable (Protocol ):
119 def __lt__ (self , other : Any ) -> bool : ...
10+ def __gt__ (self , other : Any ) -> bool : ...
1211
13-
14- T = TypeVar ("T" , bound = SupportsLessThan )
12+ T = TypeVar ("T" , bound = Comparable )
1513
1614
17- class SkewNode ([T ]):
15+ class SkewNode (Generic [T ]):
1816 """
1917 One node of the skew heap. Contains the value and references to
2018 two children.
@@ -92,8 +90,7 @@ def merge(
9290
9391 return result
9492
95-
96- class SkewHeap ([T ]):
93+ class SkewHeap (Generic [T ]):
9794 """
9895 A data structure that allows inserting a new value and to pop the smallest
9996 values. Both operations take O(logN) time where N is the size of the
@@ -152,7 +149,7 @@ def __iter__(self) -> Iterator[T]:
152149 >>> list(sh)
153150 [1, 3, 3, 7]
154151 """
155- result : list [Any ] = []
152+ result : list [T ] = []
156153 while self :
157154 result .append (self .pop ())
158155
@@ -176,7 +173,7 @@ def insert(self, value: T) -> None:
176173 """
177174 self ._root = SkewNode .merge (self ._root , SkewNode (value ))
178175
179- def pop (self ) -> T | None :
176+ def pop (self ) -> T :
180177 """
181178 Pop the smallest value from the heap and return it.
182179
0 commit comments