Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Oracle8I Building Efficient Databases

Authors: Jonathan Lewis

1st Edition

0201715848, 978-0201715842

More Books

Students also viewed these Databases questions

Question

9. Repeat Example 1b when the balls are selected with replacement.

Answered: 1 week ago