Implement the class MaxHeapPriorityQueue as a heap with the following operations: • MaxHeapPriorityQueue() creates a new heap that is empty. It needs no parameters and returns nothing. Note that as discussed in the video lecture, the index range of the array implementation of a heap is 1:n, NOT 0:n-1 • parent(index) returns the value of the parent of heap[index]. index is between 1 and the size of the heap. If index<=1 or index>size of the heap, it returns None • leftChild(index) returns the value of the left child of heap[index], returns None if there is no value • rightChild(index) returns the value of the right child of heap[index], return None if there is no value • len(heap_object) returns the number of items in the heap. It needs no parameters and returns an integer. • insert(item) adds the item into the heap satisfying the max heap property. It modifies self.heap and returns nothing. You can use the swap function provided in the starter code • deleteMax() removes the max element of the heap (the root node). It needs no parameters and returns the value removed from the heap. The heap satisfies the max heap property after deletion. (If you think on a recursive approach, you are allowed to create another method to help this method restore the max heap property) starter code: class MaxHeapPriorityQueue: '''>>>h=MaxHeapPriorityQueue()>>> h.insert(10) >>> h.insert(5) >>> h.heap[10, 5] >>> h.insert(14) >>> h.heap[14, 5, 10] >>> h.insert(9) >>> h.heap[14, 9, 10, 5] >>> h.insert(2) >>> h.heap[14, 9, 10, 5, 2] >>> h.insert(11) >>> h.heap[14, 9, 11, 5, 2, 10] >>> h.insert(6) >>> h.heap[14, 9, 11, 5, 2, 10, 6] >>> h.parent(2) 14 >>> h.leftChild(1) 9 >>> h.rightChild(1) 11 >>> h.deleteMax() 14 >>> h.heap[11, 9, 10, 5, 2, 6] >>> h.deleteMax() 11 >>> h.heap[10, 9, 6, 5, 2]''' def __init__(self): self.heap=[] self.size = 0 def __len__(self): #write your code here def parent(self,index): #write your code here def leftChild(self,index): #write your code here def rightChild(self,index): #write your code here def swap(self, index1, index2): self.heap[index1-1], self.heap[index2-1] = self.heap[index2-1],self.heap[index1-1] def insert(self,x): #write your code here def deleteMax(self): if self.size<=0: return None elif self.size==1: self.size=0 return self.heap[0] #write your code here
# class that represents a minHeap
class MaxHeapPriorityQueue:
# algorithm:
# we will store the nodes of the heap in a list
# starting from 1st index.
# a node at index i, will have its children at index 2*i + 1
# and 2*i + 2 indices.
# constructor
def __init__(self):
self.heap = [0]
self.currentSize = 0
def parent(self, index):
if index <= 1 or index > self.currentSize:
return None
return self.heap[index // 2]
def leftChild(self, index):
l = index * 2
if l <= 1 or l >= self.currentSize:
return None
return self.heap[l]
def rightChild(self, index):
r = 1 + index * 2
if r <= 1 or r >= self.currentSize:
return None
return self.heap[r]
def __len__(self):
return self.currentSize
def swap(self, index1, index2):
self.heap[index1], self.heap[index2] = self.heap[index2], self.heap[index1]
def deleteMax(self):
if self.currentSize<=0:
return None
else:
# remove the root node.
retval = self.heap[1]
self.heap[1] = self.heap[self.currentSize]
self.currentSize = self.currentSize - 1
self.heap.pop()
self.percDown(1)
return retval
# this method bubblesUp the heap starting from a index till root
def percUp(self, i):
while self.parent(i) is not None:
if self.heap[i] > self.heap[i // 2]:
self.swap(i//2, i)
else:
break
i = i // 2
# add a new entry to the heap
def insert(self, k):
self.heap.append(k)
self.currentSize = self.currentSize + 1
self.percUp(self.currentSize)
# this method bubblesDown the heap starting from a index till leaf
def percDown(self, i):
while (i * 2) <= self.currentSize:
mc = self.maxChildIdx(i)
if self.heap[i] < self.heap[mc]:
self.swap(mc, i)
else:
break
i = mc
# this method finds the index of the child having more values.
def maxChildIdx(self, i):
if self.rightChild(i) is None:
return i * 2
else:
if self.leftChild(i) > self.rightChild(i):
return i * 2
else:
return i * 2 + 1
# return number of entries in heap
def len(self):
return self.currentSize
hp = MaxHeapPriorityQueue()
for x in [10,7,12,6,3,8,17]:
hp.insert(x)
while len(hp) != 0:
print(hp.deleteMax())
![main.py history https://WhitesmokeDualAccess.anjaligupta000.repl Python 3.6.1 (default, Dec 2015, 13:05:11) [GCC 4.8.2] on Li](http://img.homeworklib.com/questions/0fcb7a00-d6c1-11ea-a37f-9dd03d7e90eb.png?x-oss-process=image/resize,w_560)
Implement the class MaxHeapPriorityQueue as a heap with the following operations: • MaxHeapPriorityQueue() creates a new...
Implement the class MaxHeapPriorityQueue as a heap with the following operations using python: - MaxHeapPriorityQueue() creates a new heap that is empty. It needs no parameters and returns nothing. - parent(index) returns the value of the parent of heap[index]. index is between 1 and the size of the heap. If index<=1 or index>size of the heap, it returns None - leftChild(index) returns the value of the left child of heap[index], returns None if there is no value - rightChild(index) returns...
add/ remove any methods to the program. please post new code
and output
Min Heap:
public class MinHeap {
private int[] Heap; private int size; private int
maxsize;
private static final int FRONT = 1;
public MinHeap(int maxsize) {
this.maxsize = maxsize;
this.size = 0;
Heap = new int[this.maxsize + 1]; Heap[0] =
Integer.MIN_VALUE;
}
private int parent(int pos) {
return pos / 2; }
private int leftChild(int pos) {
return (2 * pos); }
private int rightChild(int pos) {...
Min heap class implementation in Python.
Implement a min-using an array. Your min-heap class will have one private attribute, an array of integers. Implement the following methods for the min-heap class You may not use the built-in min function. init - Constructor makes an empty heap str - Prints the heap out in any way you want for debugging only) makenull(self) - Makes the heap empty insert(self,x) - Insert element x into the heap parent(self,i) - Returns the index of...
Write a method called removeDuplicates that accepts a PriorityQueue of integers as a parameter and modifies the queue’s state so that any element that is equal to another element in the queue is removed. For example, if the queue stores [7, 7, 8, 8, 8, 10, 45, 45], your method should modify the queue to store [7, 8, 10, 45]. You may use one stack or queue as auxiliary storage. Please also create a Main Program to test the code....
1. Code a breadth First traversal. 2. Code a depth First traversal. Note, your traversals should return a string listing the nodes added to the min spanning tree and the order they are added. Using Python. Starter code: from collections import deque class Edge: def __init__(self, endIndex, next = None): self.endIndex = endIndex self.next = next class Node: def __init__(self, name): self.name = name self.visited = False self.connects = None class Graph: def __init__(self): self.nodeList = [] self.size = 20...
Exercise 1: BSTree operations For this exercise you'll implement three additional methods in the binary search tree data structure completed in class, so that you have an opportunity to practice both using the recursive pattern covered in class and navigating the binary tree structure. The methods you'll implement are: count_less_than: takes an argument x, and returns the number of elements in the tree with values less than x successor: takes an argument x, and returns the smallest value from the...
#ifndef HEAP_H_ #define HEAP_H_ namespace cse { template<typename dtype> class heap { public: /** TO DO:: default constructor perform new to reserve memory of size INITIAL_CAPACITY. set num_items = 0; */ heap() { // write your code here }; /** Create a heap from a given array */ heap(dtype *other, size_t len) { // write your code here }; /** copy constructor copy another heap to this heap. */ heap(const heap<dtype> &other) { //write your code here }; /** Accessor...
Write a C program insertion and deletion functions for a max heap represented as a linked binary tree. Assume that each node has a parent field as well as the usual left child, right child, and key fields. -Condition : Do not use Array representation. Use the following structure. typedef struct node *treePointer; typedef struct node { int key; treePointer parent; treePointer leftChild, rightChild; }; - INPUT i k : Insert the node with the key value of k in...
Write a C program insertion and deletion functions for a max heap represented as a linked binary tree. Assume that each node has a parent field as well as the usual left child, right child, and key fields. -Condition : Do not use Array representation. Use the following structure. typedef struct node *treePointer; typedef struct node { int key; treePointer parent; treePointer leftChild, rightChild; }; - INPUT i k : Insert the node with the key value of k in...
Write a second constructor that could be added to the HeapPriorityQueue class. This constructor accepts an array of elements as a parameter and uses that array as the heap rather than creating a new array. Of course, the array passed in is probably not in proper heap ordering, so you must rearrange it until it is. There's a neat trick for achieving this: If you just "bubble down" all of the non-leaf nodes of the heap, starting from the last...