forked from nirala96/open_source_start
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaxHeap.py
130 lines (102 loc) · 3.02 KB
/
MaxHeap.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import sys
class MaxHeap:
def __init__(self, maxsize):
self.maxsize = maxsize
self.size = 0
self.Heap = [0]*(self.maxsize+1 )
self.Heap[0] = sys.maxsize
self.FRONT = 1
def parent(self, pos):
return pos//2
def leftChild(self, pos):
return 2 * pos
def rightChild(self, pos):
return (2 * pos) + 1
def isLeaf(self, pos):
if pos >= (self.size//2) and pos <= self.size:
return True
return False
def swap(self, fpos, spos):
self.Heap[fpos], self.Heap[spos] = self.Heap[spos], self.Heap[fpos]
def maxHeapify(self, pos):
if not self.isLeaf(pos):
if (self.Heap[pos] < self.Heap[self.leftChild(pos)] or
self.Heap[pos] < self.Heap[self.rightChild(pos)]):
if self.Heap[self.leftChild(pos)] > self.Heap[self.rightChild(pos)]:
self.swap(pos, self.leftChild(pos))
self.maxHeapify(self.leftChild(pos))
else:
self.swap(pos, self.rightChild(pos))
self.maxHeapify(self.rightChild(pos))
def insert(self, element):
if self.size >= self.maxsize :
return
self.size+= 1
self.Heap[self.size] = element
current = self.size
while self.Heap[current] > self.Heap[self.parent(current)]:
self.swap(current, self.parent(current))
current = self.parent(current)
def Print(self):
for i in range(1, (self.size//2)+1):
print(" PARENT : "+str(self.Heap[i])+" LEFT CHILD : "+
str(self.Heap[2 * i])+" RIGHT CHILD : "+
str(self.Heap[2 * i + 1]))
def extractMax(self):
popped = self.Heap[self.FRONT]
self.Heap[self.FRONT] = self.Heap[self.size]
self.size-= 1
self.maxHeapify(self.FRONT)
return popped
def heapify(self, n, i):
largest = i
l = 2 * i + 1
r = 2 * i + 2
if l < n and self.Heap[i] < self.Heap[l]:
largest = l
if r < n and self.Heap[largest] < self.Heap[r]:
largest = r
if largest != i:
self.Heap[i],self.Heap[largest] = self.Heap[largest],self.Heap[i]
self.heapify( n, largest)
def heapify(self,arr, n, i):
largest = i
l = 2 * i + 1
r = 2 * i + 2
if l < n and arr[i] < arr[l]:
largest = l
if r < n and arr[largest] < arr[r]:
largest = r
if largest != i:
arr[i],arr[largest] = arr[largest],arr[i] # swap
self.heapify(arr, n, largest)
def heapSort(self,arr):
n = len(arr)
for i in range(n//2 - 1, -1, -1):
self.heapify(arr, n, i)
for i in range(n-1, 0, -1):
arr[i], arr[0] = arr[0], arr[i]
self.heapify(arr, i, 0)
if __name__ == "__main__":
print('The maxHeap is ')
m=[5,3,17,10,84,19,6,22,9]
n=len(m)
minHeap = MaxHeap(n)
minHeap.insert(5)
minHeap.insert(3)
minHeap.insert(17)
minHeap.insert(10)
minHeap.insert(84)
minHeap.insert(19)
minHeap.insert(6)
minHeap.insert(22)
minHeap.insert(9)
minHeap.Print()
w=minHeap.extractMax()
print("The Max val is " + str(w))
m.remove(w)
minHeap.heapSort(m)
n=len(m)
print ("Sorted array is")
for i in range(n):
print ("%d" %m[i]),