mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-04-18 19:57:35 +00:00
Fixed issue with None type for nodes
This commit is contained in:
parent
a21a624005
commit
2e35261d09
@ -28,11 +28,11 @@ class Node:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, key) -> None:
|
def __init__(self, key) -> None:
|
||||||
self.key = key
|
self.key = key or None
|
||||||
self.degree = 0
|
self.degree = 0
|
||||||
self.marked = False
|
self.marked = False
|
||||||
self.parent = None
|
self.parent = Node(None)
|
||||||
self.child = None
|
self.child = Node(None)
|
||||||
self.left = self
|
self.left = self
|
||||||
self.right = self
|
self.right = self
|
||||||
|
|
||||||
@ -70,7 +70,7 @@ class FibonacciHeap:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self.min_node = None
|
self.min_node = Node(None)
|
||||||
self.total_nodes = 0
|
self.total_nodes = 0
|
||||||
|
|
||||||
def insert(self, key) -> Node:
|
def insert(self, key) -> Node:
|
||||||
@ -127,7 +127,7 @@ class FibonacciHeap:
|
|||||||
>>> node2.left == node1
|
>>> node2.left == node1
|
||||||
True
|
True
|
||||||
"""
|
"""
|
||||||
if base_node is None:
|
if base_node.key is None:
|
||||||
return node_to_insert
|
return node_to_insert
|
||||||
|
|
||||||
node_to_insert.right = base_node.right
|
node_to_insert.right = base_node.right
|
||||||
@ -166,7 +166,7 @@ class FibonacciHeap:
|
|||||||
the Fibonacci heap properties after removal of the minimum node.
|
the Fibonacci heap properties after removal of the minimum node.
|
||||||
"""
|
"""
|
||||||
if self.min_node is None:
|
if self.min_node is None:
|
||||||
return None
|
return Node(None)
|
||||||
|
|
||||||
min_node = self.min_node
|
min_node = self.min_node
|
||||||
|
|
||||||
@ -176,7 +176,7 @@ class FibonacciHeap:
|
|||||||
while True:
|
while True:
|
||||||
next_child = current_child.right
|
next_child = current_child.right
|
||||||
self._insert_into_circular_list(self.min_node, current_child)
|
self._insert_into_circular_list(self.min_node, current_child)
|
||||||
current_child.parent = None
|
current_child.parent.key = None
|
||||||
if current_child == last_child:
|
if current_child == last_child:
|
||||||
break
|
break
|
||||||
current_child = next_child
|
current_child = next_child
|
||||||
@ -185,7 +185,7 @@ class FibonacciHeap:
|
|||||||
min_node.right.left = min_node.left
|
min_node.right.left = min_node.left
|
||||||
|
|
||||||
if min_node == min_node.right:
|
if min_node == min_node.right:
|
||||||
self.min_node = None
|
self.min_node.key = None
|
||||||
else:
|
else:
|
||||||
self.min_node = min_node.right
|
self.min_node = min_node.right
|
||||||
self._consolidate()
|
self._consolidate()
|
||||||
@ -212,7 +212,7 @@ class FibonacciHeap:
|
|||||||
called directly from outside the class.
|
called directly from outside the class.
|
||||||
"""
|
"""
|
||||||
max_degree = int(self.total_nodes**0.5) + 1
|
max_degree = int(self.total_nodes**0.5) + 1
|
||||||
degree_table = [None] * max_degree
|
degree_table = [Node(None)] * max_degree
|
||||||
|
|
||||||
roots = []
|
roots = []
|
||||||
if self.min_node:
|
if self.min_node:
|
||||||
@ -235,7 +235,7 @@ class FibonacciHeap:
|
|||||||
other_root.left.right = other_root.right
|
other_root.left.right = other_root.right
|
||||||
other_root.right.left = other_root.left
|
other_root.right.left = other_root.left
|
||||||
|
|
||||||
if root_node.child is None:
|
if root_node.child.key is None:
|
||||||
root_node.child = other_root
|
root_node.child = other_root
|
||||||
other_root.right = other_root
|
other_root.right = other_root
|
||||||
other_root.left = other_root
|
other_root.left = other_root
|
||||||
@ -246,15 +246,15 @@ class FibonacciHeap:
|
|||||||
root_node.degree += 1
|
root_node.degree += 1
|
||||||
other_root.marked = False
|
other_root.marked = False
|
||||||
|
|
||||||
degree_table[current_degree] = None
|
degree_table[current_degree] = Node(None)
|
||||||
current_degree += 1
|
current_degree += 1
|
||||||
|
|
||||||
degree_table[current_degree] = root_node
|
degree_table[current_degree] = root_node
|
||||||
|
|
||||||
self.min_node = None
|
self.min_node.key = None
|
||||||
for degree in range(max_degree):
|
for degree in range(max_degree):
|
||||||
if degree_table[degree] is not None and (
|
if degree_table[degree] is not None and (
|
||||||
self.min_node is None or (degree_table[degree].key < self.min_node.key)
|
self.min_node is None or (degree_table[degree] < self.min_node.key)
|
||||||
):
|
):
|
||||||
self.min_node = degree_table[degree]
|
self.min_node = degree_table[degree]
|
||||||
|
|
||||||
@ -271,7 +271,7 @@ class FibonacciHeap:
|
|||||||
|
|
||||||
Example:
|
Example:
|
||||||
>>> heap = FibonacciHeap()
|
>>> heap = FibonacciHeap()
|
||||||
>>> node = heap.insert(5)
|
>>> node1 = heap.insert(5)
|
||||||
>>> heap.decrease_key(node, 3)
|
>>> heap.decrease_key(node, 3)
|
||||||
>>> node.key
|
>>> node.key
|
||||||
3
|
3
|
||||||
@ -289,7 +289,7 @@ class FibonacciHeap:
|
|||||||
node.key = new_key
|
node.key = new_key
|
||||||
parent_node = node.parent
|
parent_node = node.parent
|
||||||
|
|
||||||
if parent_node is not None and node.key < parent_node.key:
|
if parent_node.key is not None and node.key < parent_node.key:
|
||||||
self._cut(node, parent_node)
|
self._cut(node, parent_node)
|
||||||
self._cascading_cut(parent_node)
|
self._cascading_cut(parent_node)
|
||||||
|
|
||||||
@ -313,7 +313,7 @@ class FibonacciHeap:
|
|||||||
outside the class.
|
outside the class.
|
||||||
"""
|
"""
|
||||||
if child_node.right == child_node:
|
if child_node.right == child_node:
|
||||||
parent_node.child = None
|
parent_node.child = Node(None)
|
||||||
else:
|
else:
|
||||||
parent_node.child = child_node.right
|
parent_node.child = child_node.right
|
||||||
child_node.right.left = child_node.left
|
child_node.right.left = child_node.left
|
||||||
@ -322,7 +322,7 @@ class FibonacciHeap:
|
|||||||
parent_node.degree -= 1
|
parent_node.degree -= 1
|
||||||
|
|
||||||
self._insert_into_circular_list(self.min_node, child_node)
|
self._insert_into_circular_list(self.min_node, child_node)
|
||||||
child_node.parent = None
|
child_node.parent = Node(None)
|
||||||
child_node.marked = False
|
child_node.marked = False
|
||||||
|
|
||||||
def _cascading_cut(self, current_node) -> None:
|
def _cascading_cut(self, current_node) -> None:
|
||||||
@ -365,7 +365,7 @@ class FibonacciHeap:
|
|||||||
self.decrease_key(node, float("-inf"))
|
self.decrease_key(node, float("-inf"))
|
||||||
self.extract_min()
|
self.extract_min()
|
||||||
|
|
||||||
def find_min(self) -> Any:
|
def find_min(self) -> float:
|
||||||
"""Return the minimum key without removing it from the heap.
|
"""Return the minimum key without removing it from the heap.
|
||||||
|
|
||||||
This operation provides quick access to the minimum key in the heap
|
This operation provides quick access to the minimum key in the heap
|
||||||
@ -382,7 +382,7 @@ class FibonacciHeap:
|
|||||||
>>> heap.find_min()
|
>>> heap.find_min()
|
||||||
3
|
3
|
||||||
"""
|
"""
|
||||||
return self.min_node.key if self.min_node else None
|
return self.min_node.key if self.min_node else Node(None)
|
||||||
|
|
||||||
def is_empty(self) -> bool:
|
def is_empty(self) -> bool:
|
||||||
"""Check if heap is empty.
|
"""Check if heap is empty.
|
||||||
@ -398,7 +398,7 @@ class FibonacciHeap:
|
|||||||
>>> heap.is_empty()
|
>>> heap.is_empty()
|
||||||
False
|
False
|
||||||
"""
|
"""
|
||||||
return self.min_node is None
|
return self.min_node.key is None
|
||||||
|
|
||||||
def merge(self, other_heap) -> None:
|
def merge(self, other_heap) -> None:
|
||||||
"""Merge another Fibonacci heap into this one.
|
"""Merge another Fibonacci heap into this one.
|
||||||
@ -421,9 +421,9 @@ class FibonacciHeap:
|
|||||||
>>> heap1.total_nodes
|
>>> heap1.total_nodes
|
||||||
2
|
2
|
||||||
"""
|
"""
|
||||||
if other_heap.min_node is None:
|
if other_heap.min_node.key is None:
|
||||||
return
|
return
|
||||||
if self.min_node is None:
|
if self.min_node.key is None:
|
||||||
self.min_node = other_heap.min_node
|
self.min_node = other_heap.min_node
|
||||||
else:
|
else:
|
||||||
self.min_node.right.left = other_heap.min_node.left
|
self.min_node.right.left = other_heap.min_node.left
|
||||||
|
Loading…
x
Reference in New Issue
Block a user