-
Notifications
You must be signed in to change notification settings - Fork 3
/
heap.py
67 lines (53 loc) · 1.55 KB
/
heap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
__author__ = 'V Manikantan'
documentation = '''Implementation of a MaxHeap which can store objects of a single type.
Comparison must be defined for the object in order to store based on priority.
1) Insert inserts given value into the max heap
2) GetMax returns the value of root node of heap (not deleted)
3) DeleteMax deletes root node and returns its value
NOTE: Tested functionality with Heap Sort algorithm.
'''
class MaxHeap:
def __init__(self):
self.nodes = []
self.size = 0
def Insert(self,val):
self.nodes.append(val)
self.size+=1
n = self.size-1
self.__sift_up__(self.nodes,n)
def GetMax(self):
if(self.nodes):
return self.nodes[0]
else:
return None
def DeleteMax(self):
n = self.size
if(n):
res = self.nodes[0]
v = self.nodes[n-1]
self.nodes.pop(n-1)
n,self.size = n-1,self.size-1
if(self.nodes):
self.nodes[0] = v
self.__sift_down__(self.nodes,0,self.size)
return res
else:
return None
def __sift_up__(self,a,index):
while(index>0):
if(a[index]>a[(index-1)/2]):
a[index],a[(index-1)/2] = a[(index-1)/2],a[index]
index = (index-1)/2
else:
break
def __sift_down__(self,a,index,size):
while(index<size):
if((2*index+1>=size or a[index]>=a[2*index+1]) and (2*index+2>=size or a[index]>=a[2*index+2])):
break
elif(2*index+1<size and a[2*index+1]>a[index] and a[2*index+1]>a[2*index+2]):
a[index],a[2*index+1] = a[2*index+1],a[index]
index = 2*index+1
else:
if(2*index+2<size):
a[index],a[2*index+2] = a[2*index+2],a[index]
index = 2*index+2