Skip to content

Commit 2e35261

Browse files
committed
Fixed issue with None type for nodes
1 parent a21a624 commit 2e35261

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

data_structures/heap/fibonacci_heap.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ class Node:
2828
"""
2929

3030
def __init__(self, key) -> None:
31-
self.key = key
31+
self.key = key or None
3232
self.degree = 0
3333
self.marked = False
34-
self.parent = None
35-
self.child = None
34+
self.parent = Node(None)
35+
self.child = Node(None)
3636
self.left = self
3737
self.right = self
3838

@@ -70,7 +70,7 @@ class FibonacciHeap:
7070
"""
7171

7272
def __init__(self) -> None:
73-
self.min_node = None
73+
self.min_node = Node(None)
7474
self.total_nodes = 0
7575

7676
def insert(self, key) -> Node:
@@ -127,7 +127,7 @@ def _insert_into_circular_list(self, base_node, node_to_insert) -> Node:
127127
>>> node2.left == node1
128128
True
129129
"""
130-
if base_node is None:
130+
if base_node.key is None:
131131
return node_to_insert
132132

133133
node_to_insert.right = base_node.right
@@ -166,7 +166,7 @@ def extract_min(self) -> Node:
166166
the Fibonacci heap properties after removal of the minimum node.
167167
"""
168168
if self.min_node is None:
169-
return None
169+
return Node(None)
170170

171171
min_node = self.min_node
172172

@@ -176,7 +176,7 @@ def extract_min(self) -> Node:
176176
while True:
177177
next_child = current_child.right
178178
self._insert_into_circular_list(self.min_node, current_child)
179-
current_child.parent = None
179+
current_child.parent.key = None
180180
if current_child == last_child:
181181
break
182182
current_child = next_child
@@ -185,7 +185,7 @@ def extract_min(self) -> Node:
185185
min_node.right.left = min_node.left
186186

187187
if min_node == min_node.right:
188-
self.min_node = None
188+
self.min_node.key = None
189189
else:
190190
self.min_node = min_node.right
191191
self._consolidate()
@@ -212,7 +212,7 @@ def _consolidate(self):
212212
called directly from outside the class.
213213
"""
214214
max_degree = int(self.total_nodes**0.5) + 1
215-
degree_table = [None] * max_degree
215+
degree_table = [Node(None)] * max_degree
216216

217217
roots = []
218218
if self.min_node:
@@ -235,7 +235,7 @@ def _consolidate(self):
235235
other_root.left.right = other_root.right
236236
other_root.right.left = other_root.left
237237

238-
if root_node.child is None:
238+
if root_node.child.key is None:
239239
root_node.child = other_root
240240
other_root.right = other_root
241241
other_root.left = other_root
@@ -246,15 +246,15 @@ def _consolidate(self):
246246
root_node.degree += 1
247247
other_root.marked = False
248248

249-
degree_table[current_degree] = None
249+
degree_table[current_degree] = Node(None)
250250
current_degree += 1
251251

252252
degree_table[current_degree] = root_node
253253

254-
self.min_node = None
254+
self.min_node.key = None
255255
for degree in range(max_degree):
256256
if degree_table[degree] is not None and (
257-
self.min_node is None or (degree_table[degree].key < self.min_node.key)
257+
self.min_node is None or (degree_table[degree] < self.min_node.key)
258258
):
259259
self.min_node = degree_table[degree]
260260

@@ -271,7 +271,7 @@ def decrease_key(self, node, new_key):
271271
272272
Example:
273273
>>> heap = FibonacciHeap()
274-
>>> node = heap.insert(5)
274+
>>> node1 = heap.insert(5)
275275
>>> heap.decrease_key(node, 3)
276276
>>> node.key
277277
3
@@ -289,7 +289,7 @@ def decrease_key(self, node, new_key):
289289
node.key = new_key
290290
parent_node = node.parent
291291

292-
if parent_node is not None and node.key < parent_node.key:
292+
if parent_node.key is not None and node.key < parent_node.key:
293293
self._cut(node, parent_node)
294294
self._cascading_cut(parent_node)
295295

@@ -313,7 +313,7 @@ def _cut(self, child_node, parent_node):
313313
outside the class.
314314
"""
315315
if child_node.right == child_node:
316-
parent_node.child = None
316+
parent_node.child = Node(None)
317317
else:
318318
parent_node.child = child_node.right
319319
child_node.right.left = child_node.left
@@ -322,7 +322,7 @@ def _cut(self, child_node, parent_node):
322322
parent_node.degree -= 1
323323

324324
self._insert_into_circular_list(self.min_node, child_node)
325-
child_node.parent = None
325+
child_node.parent = Node(None)
326326
child_node.marked = False
327327

328328
def _cascading_cut(self, current_node) -> None:
@@ -365,7 +365,7 @@ def delete(self, node) -> None:
365365
self.decrease_key(node, float("-inf"))
366366
self.extract_min()
367367

368-
def find_min(self) -> Any:
368+
def find_min(self) -> float:
369369
"""Return the minimum key without removing it from the heap.
370370
371371
This operation provides quick access to the minimum key in the heap
@@ -382,7 +382,7 @@ def find_min(self) -> Any:
382382
>>> heap.find_min()
383383
3
384384
"""
385-
return self.min_node.key if self.min_node else None
385+
return self.min_node.key if self.min_node else Node(None)
386386

387387
def is_empty(self) -> bool:
388388
"""Check if heap is empty.
@@ -398,7 +398,7 @@ def is_empty(self) -> bool:
398398
>>> heap.is_empty()
399399
False
400400
"""
401-
return self.min_node is None
401+
return self.min_node.key is None
402402

403403
def merge(self, other_heap) -> None:
404404
"""Merge another Fibonacci heap into this one.
@@ -421,9 +421,9 @@ def merge(self, other_heap) -> None:
421421
>>> heap1.total_nodes
422422
2
423423
"""
424-
if other_heap.min_node is None:
424+
if other_heap.min_node.key is None:
425425
return
426-
if self.min_node is None:
426+
if self.min_node.key is None:
427427
self.min_node = other_heap.min_node
428428
else:
429429
self.min_node.right.left = other_heap.min_node.left

0 commit comments

Comments
 (0)