Skip to content

Commit 6f65106

Browse files
AkashAli506poyea
authored andcommitted
Update heap.py (#726)
Added comments for the better understanding of heap.
1 parent 88b6caa commit 6f65106

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

data_structures/heap/heap.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
except NameError:
88
raw_input = input # Python 3
99

10+
#This heap class start from here.
1011
class Heap:
11-
def __init__(self):
12+
def __init__(self): #Default constructor of heap class.
1213
self.h = []
1314
self.currsize = 0
1415

@@ -37,13 +38,13 @@ def maxHeapify(self,node):
3738
self.h[m] = temp
3839
self.maxHeapify(m)
3940

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

46-
def getMax(self):
47+
def getMax(self): #This function is used to get maximum value from the heap.
4748
if self.currsize >= 1:
4849
me = self.h[0]
4950
temp = self.h[0]
@@ -54,7 +55,7 @@ def getMax(self):
5455
return me
5556
return None
5657

57-
def heapSort(self):
58+
def heapSort(self): #This function is used to sort the heap.
5859
size = self.currsize
5960
while self.currsize-1 >= 0:
6061
temp = self.h[0]
@@ -64,7 +65,7 @@ def heapSort(self):
6465
self.maxHeapify(0)
6566
self.currsize = size
6667

67-
def insert(self,data):
68+
def insert(self,data): #This function is used to insert data in the heap.
6869
self.h.append(data)
6970
curr = self.currsize
7071
self.currsize+=1
@@ -74,7 +75,7 @@ def insert(self,data):
7475
self.h[curr] = temp
7576
curr = curr/2
7677

77-
def display(self):
78+
def display(self): #This function is used to print the heap.
7879
print(self.h)
7980

8081
def main():

0 commit comments

Comments
 (0)