Please use python code and show screenshot of functional WORKING code that corre
ID: 3800485 • Letter: P
Question
Please use python code and show screenshot of functional WORKING code that correctly answers the BINARY HEAP problem a portion of the code has been provided just work off the code there and provide the rest.
Comment areas is where code needs to be inserted
import sys
import numbergenerator
class AugmentedBinaryHeap(object):
def __init__(self):
self.heap_array = []
pass
def insert(self, key, value):
# Insert Key/Value pair
pass
def removeMin(self):
# Remove minimum key in the data structure
pass
def printMin(self):
# return the minimum key value pair in the data structure
return (-1,-1)
pass
def find(self, key):
# finding the value associated with the given key
return -1
pass
def changeKey(self, fromKey, toKey):
# Changes the key of a pair in the data structure
pass
def printHeap(self):
# returns the array of the internal heap
return self.heap_array
n,s,m=[int(x) for x in sys.stdin.readline().split()]
ng = numbergenerator.NumberGenerator(s, m)
ds = AugmentedBinaryHeap()
for i in xrange(n):
line = sys.stdin.readline().split()
if line[0] == "Insert":
rep = int(line[1])
for j in xrange(rep):
key = ng.GetNumber()
value = ng.GetNumber()
ds.insert(key, value)
elif line[0] == "RemoveMin":
rep = int(line[1])
for j in xrange(rep):
ds.removeMin()
elif line[0] == "PrintMin":
print ' '.join(str(x) for x in ds.printMin())
elif line[0] == "Find":
key = int(line[1])
print ds.find(key)
elif line[0] == "ChangeKey":
fromKey = int(line[1])
toKey = int(line[2])
ds.changeKey(int(line[1]), int(line[2]))
elif line[0] == "PrintHeap":
heapArray = ds.printHeap()
print ' '.join(str(x[0])+" "+str(x[1]) for x in heapArray)
Here is the code for the numbergenerator
import random
class NumberGenerator(object):
def __init__(self, seed, maxNumber):
self.seed = seed
self.maxNumber = maxNumber
random.seed(seed)
def GetNumber(self):
return random.randint(1,self.maxNumber)
Explanation / Answer
class BinHeap:
def __init__(self):
self.heapList = [0]
self.currentSize = 0
def percUp(self,i):
while i // 2 > 0:
if self.heapList[i] < self.heapList[i // 2]:
tmp = self.heapList[i // 2]
self.heapList[i // 2] = self.heapList[i]
self.heapList[i] = tmp
i = i // 2
def insert(self,k):
self.heapList.append(k)
self.currentSize = self.currentSize + 1
self.percUp(self.currentSize)
def percDown(self,i):
while (i * 2) <= self.currentSize:
mc = self.minChild(i)
if self.heapList[i] > self.heapList[mc]:
tmp = self.heapList[i]
self.heapList[i] = self.heapList[mc]
self.heapList[mc] = tmp
i = mc
def minChild(self,i):
if i * 2 + 1 > self.currentSize:
return i * 2
else:
if self.heapList[i*2] < self.heapList[i*2+1]:
return i * 2
else:
return i * 2 + 1
def delMin(self):
retval = self.heapList[1]
self.heapList[1] = self.heapList[self.currentSize]
self.currentSize = self.currentSize - 1
self.heapList.pop()
self.percDown(1)
return retval
def buildHeap(self,alist):
i = len(alist) // 2
self.currentSize = len(alist)
self.heapList = [0] + alist[:]
while (i > 0):
self.percDown(i)
i = i - 1
bh = BinHeap()
bh.buildHeap([9,5,6,2,3])
print(bh.delMin())
print(bh.delMin())
print(bh.delMin())
print(bh.delMin())
print(bh.delMin())