@@ -37,9 +37,7 @@ def value(self) -> Any:
3737
3838 @staticmethod
3939 def merge (
40- root1 : SkewNode | None ,
41- root2 : SkewNode | None ,
42- comp : Callable [[Any , Any ], bool ]
40+ root1 : SkewNode | None , root2 : SkewNode | None , comp : Callable [[Any , Any ], bool ]
4341 ) -> SkewNode | None :
4442 """
4543 Merge two nodes together.
@@ -75,6 +73,7 @@ def merge(
7573 result .left = SkewNode .merge (root1 , temp , comp )
7674 return result
7775
76+
7877class SkewHeap :
7978 """
8079 A data structure that allows inserting a new value and popping the smallest
@@ -101,15 +100,15 @@ class SkewHeap:
101100 def __init__ (
102101 self ,
103102 data : Iterable [Any ] | None = None ,
104- comp : Callable [[Any , Any ], bool ] = lambda a , b : a < b
103+ comp : Callable [[Any , Any ], bool ] = lambda a , b : a < b ,
105104 ) -> None :
106105 """
107106 Initialize the skew heap with optional data and comparison function
108-
107+
109108 >>> sh = SkewHeap([3, 1, 3, 7])
110109 >>> list(sh)
111110 [1, 3, 3, 7]
112-
111+
113112 # Max-heap example
114113 >>> max_heap = SkewHeap([3, 1, 3, 7], comp=lambda a, b: a > b)
115114 >>> list(max_heap)
@@ -158,6 +157,7 @@ def __iter__(self) -> Iterator[Any]:
158157 # Restore the heap state
159158 self ._root = temp_heap ._root
160159 return iter (result )
160+
161161 def insert (self , value : Any ) -> None :
162162 """
163163 Insert a new value into the heap
@@ -170,11 +170,7 @@ def insert(self, value: Any) -> None:
170170 >>> list(sh)
171171 [1, 3, 3, 7]
172172 """
173- self ._root = SkewNode .merge (
174- self ._root ,
175- SkewNode (value ),
176- self ._comp
177- )
173+ self ._root = SkewNode .merge (self ._root , SkewNode (value ), self ._comp )
178174
179175 def pop (self ) -> Any :
180176 """
@@ -196,11 +192,7 @@ def pop(self) -> Any:
196192 """
197193 result = self .top ()
198194 if self ._root :
199- self ._root = SkewNode .merge (
200- self ._root .left ,
201- self ._root .right ,
202- self ._comp
203- )
195+ self ._root = SkewNode .merge (self ._root .left , self ._root .right , self ._comp )
204196 return result
205197
206198 def top (self ) -> Any :
0 commit comments