Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update heap.py #726

Merged
merged 1 commit into from
Mar 4, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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