-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeap.py
More file actions
56 lines (46 loc) · 1.38 KB
/
Heap.py
File metadata and controls
56 lines (46 loc) · 1.38 KB
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
class Heap:
def __init__(self, S=None):
self.heapmin = []
self.node = S
#construct up to down
def heapDown(self, S, i):
E = i << 1
D = i << 1 | 1
M = i
if E < len(S) and S[E].frequence < S[M].frequence:
M = E
if D < len(S) and S[D].frequence < S[M].frequence:
M = D
if (M != i):
aux = S[i]
S[i] = S[M]
S[M] = aux
self.heapDown(S, M)
#..............................................
# construct down to up
def dad(self, i):
return i >> 1
def heapUp(self, S, i):
if i != 0 and S[self.dad(i)].frequence > S[i].frequence:
aux = S[i]
S[i] = S[self.dad(i)]
S[self.dad(i)] = aux
self.heapUp(S, self.dad(i))
# ...............................................
#construct the heap minimum
def buildHeapMin(self, S):
for i in range(len(S)-1, -1, -1):
self.heapmin.append(S[i])
self.heapUp(S, len(self.heapmin)-1)
def returnHeapMinimum(self):
self.buildHeapMin(self.node)
return self.node
#..............................................
#extract the element
def extract(self, S):
R = S[0]
S[0] = S[len(S)-1]
S[len(S)-1] = R
S.pop(len(S)-1)
self.heapDown(S, 0)
return R