Question
class PriorityQueue: def __init__(self, values=None): self.binary_heap = [0] self.size = 0 if values is not None: for num in values: self.insert(num) self.size = len(values) def
class PriorityQueue: def __init__(self, values=None): self.binary_heap = [0] self.size = 0 if values is not None: for num in values: self.insert(num) self.size = len(values)
def __str__(self): return str(self.binary_heap)
def __len__(self): return self.size
def percolate_up(self, i): while i // 2 > 0 and self.binary_heap[i]
def get_smaller_child_index(self, i): if i * 2 + 1 > self.size: return i * 2 else: if self.binary_heap[i*2]
def percolate_down(self, i): while (i * 2) self.binary_heap[min_child_index]: self.binary_heap[min_child_index], self.binary_heap[i] = self.binary_heap[i], self.binary_heap[min_child_index] i = min_child_index
def insert(self, item): self.binary_heap.append(item) self.size += 1 self.percolate_up(self.size)
def get_minimum(self): return self.binary_heap[1]
def delete_minimum(self): return_val = self.binary_heap[1] self.binary_heap[1] = self.binary_heap[self.size] self.size = self.size - 1 self.binary_heap.pop() self.percolate_down(1) return return_val
In the answer box is a class Priorityqueue as discussed in the lecture. Add a method named heap_update(self, index, value) into the PriorityQueue class. The method takes an integer index and a value as parameters, replaces the item at the list position index with value, and then ensures the order property with the minimum number of operations possible (you are allowed to use any operations from the class Priorityeue). If the index is invalid, i.e., not the index of an element currently in the heap, then the method should do nothing. For example: Answer: (penalty regime: 0,0,5,10,15,20,25,30,35,40,45,50% )Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started