Skip to content

Commit

Permalink
Update heap.py (TheAlgorithms#726)
Browse files Browse the repository at this point in the history
Added comments for the better understanding of heap.
  • Loading branch information
AkashAli506 authored and poyea committed Mar 4, 2019
1 parent 88b6caa commit 6f65106
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions data_structures/heap/heap.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
except NameError:
raw_input = input # Python 3

#This heap class start from here.
class Heap:
def __init__(self):
def __init__(self): #Default constructor of heap class.
self.h = []
self.currsize = 0

Expand Down Expand Up @@ -37,13 +38,13 @@ def maxHeapify(self,node):
self.h[m] = temp
self.maxHeapify(m)

def buildHeap(self,a):
def buildHeap(self,a): #This function is used to build the heap from the data container 'a'.
self.currsize = len(a)
self.h = list(a)
for i in range(self.currsize//2,-1,-1):
self.maxHeapify(i)

def getMax(self):
def getMax(self): #This function is used to get maximum value from the heap.
if self.currsize >= 1:
me = self.h[0]
temp = self.h[0]
Expand All @@ -54,7 +55,7 @@ def getMax(self):
return me
return None

def heapSort(self):
def heapSort(self): #This function is used to sort the heap.
size = self.currsize
while self.currsize-1 >= 0:
temp = self.h[0]
Expand All @@ -64,7 +65,7 @@ def heapSort(self):
self.maxHeapify(0)
self.currsize = size

def insert(self,data):
def insert(self,data): #This function is used to insert data in the heap.
self.h.append(data)
curr = self.currsize
self.currsize+=1
Expand All @@ -74,7 +75,7 @@ def insert(self,data):
self.h[curr] = temp
curr = curr/2

def display(self):
def display(self): #This function is used to print the heap.
print(self.h)

def main():
Expand Down

0 comments on commit 6f65106

Please sign in to comment.