forked from Firkraag/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.py
65 lines (65 loc) · 2.11 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
class max_heap(list):
def __init__(self, data):
list.__init__(self, data)
self.length = len(data)
self.heap_size = self.length
self.build_max_heap()
def left(self, i):
return 2 * i + 1
def right(self, i):
return 2 * i + 2
def parent(self, i):
return (i - 1) / 2
def max_heapify(self, i):
l = self.left(i)
r = self.right(i)
if (l <= (self.heap_size - 1)) and (self[l] > self[i]):
largest = l
else:
largest = i
if (r <= (self.heap_size - 1)) and (self[r] > self[largest]):
largest = r
if largest != i:
self[i],self[largest] = self[largest],self[i]
self.max_heapify(largest)
def build_max_heap(self):
self.heap_size = self.length
for i in range(self.length / 2 - 1, -1, -1):
self.max_heapify(i)
def heapsort(self):
self.build_max_heap()
for i in range(self.length - 1, 0, -1):
self[0],self[i] = self[i],self[0]
self.heap_size = self.heap_size - 1
self.max_heapify(0)
# print self
class min_heap(list):
def __init__(self, data):
list.__init__(self, data)
self.length = len(data)
self.heap_size = self.length
self.build_min_heap()
def __contains__(self, y):
return y in self[0:self.heap_size]
def left(self, i):
return 2 * i + 1
def right(self, i):
return 2 * i + 2
def parent(self, i):
return (i - 1) / 2
def min_heapify(self, i):
l = self.left(i)
r = self.right(i)
if (l <= (self.heap_size - 1)) and (self[l] < self[i]):
smallest = l
else:
smallest = i
if (r <= (self.heap_size - 1)) and (self[r] < self[smallest]):
smallest = r
if smallest != i:
self[i],self[smallest] = self[smallest],self[i]
self.min_heapify(smallest)
def build_min_heap(self):
self.heap_size = self.length
for i in range(self.length / 2 - 1, -1, -1):
self.min_heapify(i)