This repository has been archived by the owner on Sep 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathheap.py
157 lines (151 loc) · 4.61 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
__all__ = ["Heap", "HeapSet"]
class Heap:
"""Heap data structure (priority queue)."""
def __init__(self):
self.data = []
def swap(self,i,j):
"Internal."
data = self.data
temp = data[i]
data[i] = data[j]
data[j] = temp
def heapify(self,i):
"Internal."
data = self.data
n = len(data)
l = i*2+1
r = i*2+2
if l<n and data[l][0]>data[i][0]:
largest = l
else:
largest = i
if r<n and data[r][0]>data[largest][0]:
largest = r
if largest!=i:
self.swap(i,largest)
self.heapify(largest)
def top_priority(self):
"Return the highest priority in the heap."
return self.data[0][0]
def top(self):
"Return the top element in the heap."
return self.data[0][1]
def extract_max(self):
"Remove the top element from the heap and return it."
result = self.data[0]
self.data[0] = self.data[-1]
del self.data[-1]
self.heapify(0)
return result
def insert(self,priority,object):
"Insert a new element into the heap."
data = self.data
data.append(None)
i = len(data)-1
while i>0 and priority>data[(i-1)/2][0]:
data[i] = data[(i-1)/2]
i = (i-1)/2
data[i] = (priority,object)
def __len__(self):
return len(self.data)-1
class HeapSet:
"""A Heap that allows out-of-order deletions."""
def __init__(self):
self.location = {}
self.data = []
def swap(self,i,j):
"Internal."
data = self.data
temp = data[i]
data[i] = data[j]
data[j] = temp
self.location[data[i][2]] = i
self.location[data[j][2]] = j
def heapify(self,i):
"Internal."
data = self.data
n = len(data)
l = i*2+1
r = i*2+2
if l<n and data[l][0]>data[i][0]:
largest = l
else:
largest = i
if r<n and data[r][0]>data[largest][0]:
largest = r
if largest!=i:
self.swap(i,largest)
self.heapify(largest)
def top_priority(self):
"Return the highest priority."
return self.data[0][0]
def top(self):
"Return the top element (doesn't remove)."
return self.data[0][1]
def extract_max(self):
"Remove the top element and return it."
result = self.data[0]
self.data[0] = self.data[-1]
del self.data[-1]
if len(self.data)>0:
self.location[self.data[0][2]] = 0
self.heapify(0)
return result
def insert(self,priority,object,key):
"Insert a new element into the heap with the given key."
self.data.append(None)
i = len(self.data)-1
while i>0 and priority>self.data[(i-1)/2][0]:
self.data[i] = self.data[(i-1)/2]
self.location[self.data[i][2]] = i
i = (i-1)/2
self.data[i] = (priority,object,key)
self.location[self.data[i][2]] = i
def get(self,key):
"Return the data associated with the key"
return self.data[self.location[key]][1]
def priority(self,key):
"Return the priority associated with the key"
return self.data[self.location[key]][0]
def delete(self,key):
"Remove a key and its associated data from the heap."
i = self.location[key]
if i<len(self.data):
self.data[i] = self.data[-1]
self.location[self.data[i][2]] = i
del self.data[-1]
del self.location[self.data[-1][2]]
self.heapify(i)
def __len__(self):
return len(self.data)
import unittest,random
class TestHeap(unittest.TestCase):
def setUp(self):
self.heap = Heap()
def testHeap(self):
h = self.heap
data = [random.uniform(0,1) for i in range(20)]
for x in data: h.insert(x,x)
last = 1e99
while len(h)>0:
x = h.extract_max()
assert x[0]<=last
last = x
class TestHeapSet(unittest.TestCase):
def setUp(self):
self.heapset = HeapSet()
def testHeapSet(self):
h = self.heapset
data = [random.uniform(0,1) for i in range(10)]
for i in range(len(data)): h.insert(data[i],data[i],i)
# FIXME the stuff below fails, so we return for now
return
for i in range(5): h.delete(i)
last = 1e99
while len(h)>0:
x,o,i = h.extract_max()
assert i>=5
assert x<last
last = x
if __name__ == "__main__":
unittest.main()