def insert(self, value): tree = BinomialTreeNode(value) tree.marked = False self.trees.append(tree) # Update the min node reference if needed if self.min_tree is None or self.min_tree.value.value > value: self.min_tree = self.trees.get_tail_node()
def insert(self, value): """ Insert a value onto the heap Merges this heap with a new heap with only one node """ # Create a heap with a single node new_heap = BinomialHeap() new_heap.trees.head.value = BinomialTreeNode(value) new_heap.min_tree = new_heap.trees.head # Merge it with the current heap self.merge(new_heap)
def decrease_min(self, node: BinomialTreeNode, value): if node.value < value: raise ValueError('Value should be smaller than this value') parent = node.parent while parent: # Remove node from parent and add it to the tree list parent.children.remove(node) node.parent = None self.trees.append(node) node.marked = False if self.min_tree is None or self.min_tree.value > node.value: self.min_tree = self.trees.get_tail_node() # Mark the parent if parent.marked: pass