Answer to Question #301303 in Python for erica

Question #301303

Create a min-heap . write function to insert a new number in min-heap . write function to delete its minimum element . write function to delete a given element from min-heap . write function to sort it in descending order .


1
Expert's answer
2022-02-23T00:10:32-0500
class MinHeap:
	    def __init__(self):
	        self.heap = []
	
	    def peek(self):
	        if not self.heap:
	            return False
	        return self.heap[0]

	    def push(self, data):
	        self.heap.append(data)
	        self.heapify_up()
	
        def heapify_up(self):
	        child = len(self.heap) - 1
	        parent = (child - 1) // 2
	        while self.heap[child] < self.heap[parent] and child != 0:
	            self.heap[child], self.heap[parent] = self.heap[parent], self.heap[child]
	            child = parent
	            parent = (child - 1) // 2
	
	    def pop(self):
	        if not self.heap:
	            return False
	        self.heap[0], self.heap[-1] = self.heap[-1], self.heap[0]
	        data = self.heap.pop()
	        self.heapify_down()
	        return data
	
	    def heapify_down(self):
	        i = 0
	        while (2*i+1) < len(self.heap):
	            smaller_idx = 2*i+1
	            if (2*i+2) < len(self.heap) and self.heap[2*i+2] < self.heap[2*i+1]:
	                smaller_idx = 2*i+2
	            if self.heap[smaller_idx] > self.heap[i]:
	                return
	            self.heap[smaller_idx], self.heap[i] = self.heap[i], self.heap[smaller_idx]
	            i = smaller_idx

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS