Skip to content

Commit

Permalink
Modified init method and added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
atmishra committed Jun 19, 2016
1 parent 4159ea8 commit f8c485d
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions heap.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
class Heap(object):
def __init__(self):
self.hlist = ["dummy"]
self.hsize = 0

def __str__(self):
return "%s" %self.hlist[1:]
'''This is the implementation for Binary Heap'''

def inputList(self, alist):
class Heap(object):
def __init__(self, alist):

self.hlist = [0] + alist
'''initialize heap to the input list'''
self.hlist = ["dummy"]+alist #To start indexing from 1
self.hsize = len(self.hlist)-1

self.hsize = len(self.hlist) - 1
def __str__(self):
return "%s" %self.hlist[1:] #The actual heap starts from index 1

def max_heapify(self, node,size):
'''max_heapify performs perculate down operation on the heap. Node is the index
where the method is to applied annd size is the size of the list'''
left_child = 2*node
right_child = left_child + 1
largest = node
Expand All @@ -31,14 +31,16 @@ def max_heapify(self, node,size):

self.max_heapify(largest,size)

# return self.hlist[1:]


def build_max_heap(self):
'''Builds max heap for the list provided'''
for i in range(self.hsize//2,0,-1):
self.max_heapify(i,self.hsize)


def deleteMax(self):
'''Delete max element from the Heap maintaining the Heap property'''
if self.hsize < 1:
print "UnderFlow"
max = self.hlist[1]
Expand All @@ -49,13 +51,16 @@ def deleteMax(self):
return max

def perUp(self,i):
'''perform perculate Up operation on the Heap'''
while i//2>0:

if self.hlist[i]>self.hlist[i//2]:
self.hlist[i//2],self.hlist[i] = self.hlist[i],self.hlist[i//2]
i = i//2

def insert(self,value):
'''Inserts new value into the Heap maintaining the Heap property'''

self.hlist.append(value)
self.hsize += 1
self.perUp(self.hsize)
Expand All @@ -64,6 +69,7 @@ def getMax(self):
return self.hlist[1]

def heapsort(self):
'''Sorts the list provided to the Heap object using Heap Sort'''

self.max_heapify(1,self.hsize)

Expand All @@ -79,14 +85,10 @@ def heapsort(self):
if __name__ == "__main__":

alist = [30,100,3,1,20,10,15]
newHeap = Heap()
newHeap.inputList(alist)
# newHeap.max_heapify(1,)
newHeap = Heap(alist)
print(newHeap)
newHeap.insert(70)
# print(newHeap)
# newHeap.deleteMax()
# print (newHeap)
print(newHeap.getMax())
newHeap.heapsort()
print(newHeap)

Expand Down

0 comments on commit f8c485d

Please sign in to comment.