Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

K-ary Minimum Heap please use PYTHON 2.7 for this problem just include the code that is needed in the commented out sections and provide screenshots

K-ary Minimum Heap please use PYTHON 2.7 for this problem just include the code that is needed in the commented out sections and provide screenshots of functional working code

# I will provide positive feeedback for good working code THANKS!!!

For this problem, you will be asked to implement a k-ary min-heap. All of the properties of the heap are the same as the one that you say in class except that every node has k children.

image text in transcribed

image text in transcribed

class minHeap(object): def __init__(self, k): self.heap_array = [] self.k = k

def add_num(self, x): # # TODO: Add number to the heap array # while maintaining heap property # pass

def pop_min(self): # # TODO: Remove number from heap array while # maintaining heap property # pass

def heapify_up(self, index): # # A helper function to help maintain heap property # pass

def heapify_down(self, index): # # A helper function to help maintain heap property # pass

# This will print the heap in the requested format def __str__(self): # Do not sort the heap array s = "" for i in self.heap_array: s += str(i) + " " return s.strip()

# Given Starter Code for IO. You need not modify code beneath this line

[k, c] = [int(x) for x in raw_input().split()] h = minHeap(k) for i in range(c): command = raw_input().split() if command[0] == "add": h.add_num(int(command[1])) elif command[0] == "remove": print h.pop_min() elif command[0] == "print": print h

nput The first line will contain two space separated integers k and c kis the number of children per node in the heap and c is the number of commands that follow. The next clines constitute commands that will be given to the heap. For this problem, there are three valid commands: add, remove, and print. Lines for addition will contain the command "add" followed by an integer n, where n is the number that needs to be added to the heap. Lines for removal will have the string "remove". and the minimum value will be removed from the heap. Lines for printing will contain the string "print" Output Each time a print line is requested, the user is expected to output a line of p space-delimited integers in the order of the array representation of the heap. Each time a remove is requested, print out the integer removed on its own line Example Input: 213 add 3700 print add 656 add 8375 add 2344 remove add 8365 add 6391 add 6349 remove add 7413 add 641 print Output: 3700 656 2344 641 6391 3700 8365 8375 7413 6349 nput The first line will contain two space separated integers k and c kis the number of children per node in the heap and c is the number of commands that follow. The next clines constitute commands that will be given to the heap. For this problem, there are three valid commands: add, remove, and print. Lines for addition will contain the command "add" followed by an integer n, where n is the number that needs to be added to the heap. Lines for removal will have the string "remove". and the minimum value will be removed from the heap. Lines for printing will contain the string "print" Output Each time a print line is requested, the user is expected to output a line of p space-delimited integers in the order of the array representation of the heap. Each time a remove is requested, print out the integer removed on its own line Example Input: 213 add 3700 print add 656 add 8375 add 2344 remove add 8365 add 6391 add 6349 remove add 7413 add 641 print Output: 3700 656 2344 641 6391 3700 8365 8375 7413 6349

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

Medical Image Databases

Authors: Stephen T.C. Wong

1st Edition

1461375398, 978-1461375395

More Books

Students also viewed these Databases questions

Question

Describe Balor method and give the chemical reaction.

Answered: 1 week ago