-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmax_heap.py
83 lines (58 loc) · 2.13 KB
/
max_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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import sys
class MaxHeap:
def __init__(self, maxsize):
self.maxsize = maxsize
self.size = 0
self.Heap = [0] * (self.maxsize + 1)
self.Heap[0] = (None, 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 (self.size // 2) < 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 max_heapify(self, pos):
if not self.isLeaf(pos):
if (self.Heap[pos][1] < self.Heap[self.leftChild(pos)][1] or
self.rightChild(pos) <= self.size and self.Heap[pos][1] < self.Heap[self.rightChild(pos)][1]):
if (self.Heap[self.leftChild(pos)][1] >
self.Heap[self.rightChild(pos)][1]):
self.swap(pos, self.leftChild(pos))
self.max_heapify(self.leftChild(pos))
else:
self.swap(pos, self.rightChild(pos))
self.max_heapify(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][1] >
self.Heap[self.parent(current)][1]):
self.swap(current, self.parent(current))
current = self.parent(current)
def extract_max(self):
popped = self.Heap[self.FRONT]
self.Heap[self.FRONT] = self.Heap[self.size]
self.size -= 1
if self.size > 0:
self.max_heapify(self.FRONT)
return popped
def select_top(self, results, top):
for res in results:
self.insert(res)
top_results = []
for i in range(top):
if self.size == 0:
break
top_results.append(self.extract_max())
return top_results