class Heaptree():def __init__(self):self.heap = []self.size = 0def data_down (self,i):while (i * 2 + 2) <= self.size:mi = self.get_min_index(i)if self.heap[i] > self.heap[mi]:self.heap[i], self.heap[mi] = self.heap[mi], self.heap[i]i = midef get_min_index(self,i):if i *2 + 2 >= self.size:return i * 2 + 1else:if self.heap[i*2+1] < self.heap[i*2+2]:return i * 2 + 1else:return i * 2 + 2def build_heap(self, mylist):i = (len(mylist) // 2) - 1self.size = len(mylist)self.heap = mylistwhile (i >= 0):self.data_down(i)i = i - 1h = [10, 21, 5, 9, 13, 28, 3]print("Before run   = ", h)obj = Heaptree()obj.build_heap(h)print("After run = ", obj.heap)
Result

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store