Lecture Notes on 06 Apr 2022 * Make sure you have read the chapter on Graphs on ZyBooks * Notes on Heaps https://www.cs.utexas.edu/users/mitra/csSpring2022/cs313/notes/Heaps.pdf * Code for Heaps class Heap (object): def __init__ (self): self.heap = [] # return the size of the heap def get_size (self): return len (self.heap) # return if the heap is empty def is_empty (self): return (len(self.heap) == 0) # get index of parent def parent_idx (self, idx): new_idx = (idx - 1) // 2 return new_idx # get index of left child def lchild_idx (self, idx): new_idx = 2 * idx + 1 if (new_idx >= len(self.heap): new_idx = -1 return new_idx # get index of right child def rchild_idx (self, idx): new_idx = 2 * idx + 2 if (new_idx >= len(self.heap)): new_idx = -1 return new_idx # move a node up from the bottom to its rightful place def percolate_up (self, idx): node_val = self.heap[idx] parent = self.parent_idx (idx) parent_val = self.heap[parent] while (idx > 0) and (parent_val < node_val): self.heap[idx] = parent_val idx = parent parent = self.parent_idx (idx) parent_val = self.heap[parent] self.heap[idx] = node_val # move a node down from the top to its rightful place def percolate_down (self, idx): node_val = self.heap[idx] num_elements= self.get_size () while (idx < num_elements // 2): left_idx = self.lchild_idx (idx) right_idx = self.rchild_idx (idx) if ((right_idx > -1) and (self.heap[left_idx] < self.heap[right_idx])) larger_child = right_idx else: larger_child = left_idx if (node_val >= self.heap[larger_child]): break self.heap[idx] = self.heap[larger_child] idx = larger_child self.heap[idx] = node_val # insert an element in then ehap def insert (self, val): self.heap.append (val) new_pos = self.get_size() - 1 self.percolate_up (new_pos) # delete the root and remake the heap def remove (self): root = self.heap[0] self.heap[0] = self.heap[-1] self.heap.pop() self.percolate_down (0) return root # given an arbitrary list make a heap out of it def make_heap (self, idx): num_elements = self.get_size() if (idx > ((num_elements // 2) - 1)): return right_idx = self.rchild_idx (idx) if (right_idx > -1): self.make_heap (right_idx): left_idx = self.lchild_idx (idx) self.make_heap (left_idx) self.percolate_down (idx)