def on_add_new_node(self, keys): new_heap = BinomialHeap() for key in keys: if key >= 100: self.text_output.println("WARNING: Node " + str(key) + " was not added (should be < 100)") else: new_heap.add_elem(key) self.heap.union(new_heap) self.heap_canvas.draw(self.heap)
class App: heap = BinomialHeap() def __init__(self, master): self._display_warning = True # open full screen # master.wm_attributes('-fullscreen', True) master.update() self.init_ui_elems(master) def init_ui_elems(self, master): master.grid_rowconfigure(0, weight=1) master.grid_rowconfigure(1, weight=0) master.grid_columnconfigure(0, weight=1) # Canvas canvas_cont = Frame(master) self.heap_canvas = HeapCanvas(master, canvas_cont) canvas_cont.grid(row=0, sticky="WENS") # Toolbar & text ouput container bottom_section = Frame(master, bd=5, relief=RIDGE) bottom_section.grid(row=1, sticky="WENS") bottom_section.grid_columnconfigure(0, weight=0) bottom_section.grid_columnconfigure(1, weight=3) bottom_section.grid_rowconfigure(0, weight=1) # Toolbar toolbar_cont = Frame(bottom_section) self.toolbar = Toolbar( master, toolbar_cont, on_add_new_node=self.on_add_new_node, on_sizes_click=self.on_sizes_click, on_degrees_click=self.on_degrees_click, on_remove_min_click=self.on_remove_min_click, on_min_root_click=self.on_min_root_click, on_reset_click=self.on_reset_click, ) toolbar_cont.grid(row=0, column=0, sticky="WENS") # Text output text_output_cont = Frame(bottom_section) text_output_cont.grid_columnconfigure(0, weight=1) text_output_cont.grid_columnconfigure(1, weight=0) text_output_cont.grid_rowconfigure(0, weight=0) text_output_cont.grid_rowconfigure(1, weight=1) text_output_cont.grid(row=0, column=1, sticky="WENS") self.text_output = TextOutput(text_output_cont) def on_sizes_click(self): self.text_output.println("Sizes of consecutive trees in the heap:") sizes = map(lambda tree: str(tree.size()), self.heap.trees) self.text_output.println(", ".join(sizes)) def on_degrees_click(self): self.text_output.println("Degrees of consecutive trees in the heap:") degrees = map(lambda tree: str(tree.root.degree), self.heap.trees) self.text_output.println(", ".join(degrees)) def on_min_root_click(self): min_tree = self.heap.get_min_tree() if min_tree: self.text_output.println("Minimal tree root: " + str(min_tree.root.key)) else: self.text_output.println("The heap is empty") def on_remove_min_click(self): self.heap.remove_min_node() self.heap_canvas.draw(self.heap) def on_min_click(self): self.text_output.println("Min element: " #+ str(self.heap.min) ) def on_reset_click(self): self.heap = BinomialHeap() self.heap_canvas.draw(self.heap) self.text_output.clear() def on_add_new_node(self, keys): new_heap = BinomialHeap() for key in keys: if key >= 100: self.text_output.println("WARNING: Node " + str(key) + " was not added (should be < 100)") else: new_heap.add_elem(key) self.heap.union(new_heap) self.heap_canvas.draw(self.heap)
def on_reset_click(self): self.heap = BinomialHeap() self.heap_canvas.draw(self.heap) self.text_output.clear()
def test_delete_min(): node1 = Node(34) node2 = Node(10) node3 = Node(12) node4 = Node(20) heap = BinomialHeap() heap.insert(node1) heap.insert(node2) heap.insert(node3) heap.insert(node4) assert heap.find_min() == node2 assert heap.num_elements() == 4 assert heap.delete_min() == node2 assert heap.find_min() == node3 assert heap.num_elements() == 3 assert heap.delete_min() == node3 assert heap.num_elements() == 2 assert heap.find_min() == node4 assert heap.delete_min() == node4 assert heap.find_min() == node1 assert heap.delete_min() == node1 assert heap.find_min() is None assert heap.num_elements() == 0
test(BinomialHeap, lst) print("\nBinaryHeap") test(BinaryHeap,lst) print("\nQueue") test(Queue,lst) print('\n') brh = BinaryHeap() brh_time = timer() for dst, u, v in lst: brh.add([dst, u, v]) brh_time = timer() - brh_time blh = BinomialHeap() blh_time = timer() for dst, u, v in lst: blh.add([dst, u, v]) blh_time = timer() - blh_time sq = Queue() q_time = timer() for dst, u, v in lst: sq.add([dst, u, v]) q_time = timer()-q_time print(f"Binary Heap: {brh_time}\nBinomial Heap: {blh_time}\n" + f"Simple Queue: {q_time}") # =============================================================================
def test_decrease_key(): node1 = Node(34) node2 = Node(10) node3 = Node(12) node4 = Node(20) heap = BinomialHeap() heap.insert(node1) heap.insert(node2) heap.insert(node3) heap.insert(node4) assert heap.num_elements() == 4 assert heap.find_min() == node2 pytest.raises(ValueError, heap.decrease_key, node3, 13) pytest.raises(ValueError, heap.decrease_key, None, 13) heap.decrease_key(node3, 11) assert node3.key == 11 assert heap.find_min() == node2 heap.decrease_key(node3, 8) assert heap.find_min() == node3 assert heap.find_min().key == 8
def test_make_heap(): heap = BinomialHeap.make_heap() assert isinstance(heap, BinomialHeap)
def test_find_min(): heap = BinomialHeap() assert heap.find_min() is None # min_root new_node1 = 2 new_node1 = Node(2) heap.insert(new_node1) min_root = heap.find_min() assert new_node1 == min_root new_node2 = Node(3) heap.insert(new_node2) min_root = heap.find_min() # min_root still new_node1 = 2 assert min_root == new_node1 new_node3 = Node(1) heap.insert(new_node3) min_root = heap.find_min() # min_root now new node3 = 1 assert min_root == new_node3
def test_merge(): node1 = Node(34) node2 = Node(10) node3 = Node(12) node4 = Node(20) heap1 = BinomialHeap() heap1.insert(node1) heap1.insert(node2) heap1.insert(node3) heap1.insert(node4) assert heap1.find_min() == node2 assert heap1.num_elements() == 4 node5 = Node(3) node6 = Node(11) node7 = Node(14) node8 = Node(20) heap2 = BinomialHeap() heap2.insert(node5) heap2.insert(node6) heap2.insert(node7) heap2.insert(node8) assert heap2.find_min() == node5 heap1.merge(heap2) assert heap1.find_min() == node5 assert heap1.num_elements() == 8 b = heap1.delete_min() assert b == node5 assert heap1.find_min() == node2
def test_insert(): heap = BinomialHeap() assert heap.num_elements() == 0 new_node1 = Node(2) heap.insert(new_node1) assert heap.num_elements() == 1 assert new_node1.right_sibling == new_node1 assert new_node1.right_sibling == new_node1 new_node2 = Node(3) heap.insert(new_node2) assert heap.num_elements() == 2 assert new_node1.right_sibling == new_node2 assert new_node1.left_sibling == new_node2 assert new_node2.right_sibling == new_node1 assert new_node2.left_sibling == new_node1 # min_root now new node3 = 1 new_node3 = Node(1) heap.insert(new_node3) assert heap.num_elements() == 3 assert new_node3.right_sibling == new_node2 assert new_node3.left_sibling == new_node1 assert new_node3.right_sibling == new_node2 assert new_node2.left_sibling == new_node3 assert new_node1.right_sibling == new_node3 assert new_node1.left_sibling == new_node2