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

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Exploring subqueries in PostgreSQL — Column names and expressions.

Getting started with Couchbase in Mac OS Sierra using Docker

Trumpworld’s artificial scholarly con artists are helping him degrade popular government with…

Android App performance tips for developers

Container-Diff: Change Management for Containers

Microservices Design Patterns

Simple scale your application with Kubernets in Google Cloud Platform

A Day in the Life: Greg Ryzhov (CTO, Urban Sports Club)

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
Che Kai LIANG

Che Kai LIANG

More from Medium

Python While — Loop

Implementing Boids in Python

Learning Programming with simple language: Python, Part 1