@@ -15,10 +15,10 @@ class FibonacciTree:
15
15
16
16
def __init__ (self , key : int ) -> None :
17
17
self .key = key
18
- self .children : list [" FibonacciTree" ] = []
18
+ self .children : list [' FibonacciTree' ] = []
19
19
self .order = 0
20
20
21
- def add_at_end (self , child_node : " FibonacciTree" ) -> None :
21
+ def add_at_end (self , child_node : ' FibonacciTree' ) -> None :
22
22
"""
23
23
Adds a child node 'child_node' to the end of the children list.
24
24
@@ -53,8 +53,8 @@ class FibonacciHeap:
53
53
"""
54
54
55
55
def __init__ (self ) -> None :
56
- self .trees : list [" FibonacciTree" ] = []
57
- self .least : " FibonacciTree" | None = None
56
+ self .trees : list [' FibonacciTree' ] = []
57
+ self .least : ' FibonacciTree' | None = None
58
58
self .count = 0
59
59
60
60
def insert (self , key : int ) -> None :
@@ -66,7 +66,7 @@ def insert(self, key: int) -> None:
66
66
"""
67
67
new_tree = FibonacciTree (key )
68
68
self .trees .append (new_tree )
69
- if self .least is None or key < self .least .key :
69
+ if ( self .least is None or key < self .least .key ) :
70
70
self .least = new_tree
71
71
self .count = self .count + 1
72
72
@@ -88,7 +88,8 @@ def extract_min(self) -> int | None:
88
88
Returns:
89
89
int: The minimum key.
90
90
"""
91
- if (smallest := self .least ) is not None :
91
+ smallest = self .least
92
+ if smallest is not None :
92
93
for child in smallest .children :
93
94
if child is not None :
94
95
self .trees .append (child )
@@ -110,6 +111,9 @@ def consolidate(self) -> None:
110
111
111
112
while self .trees :
112
113
x = self .trees [0 ]
114
+ if x is None :
115
+ self .trees .pop (0 ) # Remove the None value
116
+ continue # Skip processing None values
113
117
order = x .order
114
118
self .trees .remove (x )
115
119
while aux [order ] is not None :
@@ -125,10 +129,9 @@ def consolidate(self) -> None:
125
129
for k in aux :
126
130
if k is not None :
127
131
self .trees .append (k )
128
- if self .least is None or k .key < self .least .key :
132
+ if ( self .least is None or k .key < self .least .key ) :
129
133
self .least = k
130
-
131
-
134
+
132
135
def floor_log2 (x : int ) -> int :
133
136
"""
134
137
Computes the floor of the base-2 logarithm of 'x'.
@@ -141,14 +144,11 @@ def floor_log2(x: int) -> int:
141
144
"""
142
145
return math .floor (math .log2 (x )) if x > 0 else 0
143
146
144
-
145
147
# Doctest for floor_log2
146
148
if __name__ == "__main__" :
147
149
import doctest
148
-
149
150
doctest .testmod ()
150
151
151
152
if __name__ == "__main__" :
152
153
import doctest
153
-
154
154
doctest .testmod ()
0 commit comments