-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.py
109 lines (87 loc) · 2.43 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
import unittest
class Heap:
def __init__(self):
self._heap = [None] # don't use self._heap[0], makes arithmetic easier
def __len__(self):
return len(self._heap) - 1
def __getitem__(self, index):
return self._heap[index+1]
def insert(self, key):
self._heap.append(key)
self._swim(len(self))
def pop(self):
if not len(self): return None
h = self._heap
maxV = h[1]
h[1], h[-1] = h[-1], h[1]
self._heap = self._heap[:-1] # chop last value
self._sink(1)
return maxV
def _sink(self, i):
h = self._heap
N = len(self)
while (2*i <= N):
j = 2*i
if (j < N and not self.compare(j, j+1)):
j += 1
if self.compare(i, j):
break
h[i], h[j] = h[j], h[i]
i = j
def _swim(self, i):
h = self._heap
while (i > 1):
j = self._parent(i)
if self.compare(i, j):
h[j], h[i] = h[i], h[j]
i = j
def _parent(self, i):
return i // 2
def _leftchild(self, i):
return 2*i
def _rightchild(self, i):
return 2*i + 1
def compare(self, a, b):
# by arbitrary default, Heap is a min priority queue
return self._heap[a] < self._heap[b]
def __iter__(self):
while len(self):
yield self.pop()
class TestHeap(unittest.TestCase):
def test_length(self):
h = Heap()
self.assertEqual(len(h), 0)
h.insert("A")
self.assertEqual(len(h), 1)
def test_swim(self):
h = Heap()
h.insert("D")
h.insert("A")
self.assertEqual(h[0], "A")
def test_pop(self):
h = Heap()
h.insert("D")
h.insert("A")
h.insert("C")
h.insert("B")
a = h.pop()
self.assertEqual(a, "A")
def test_generator(self):
import random
h = Heap()
input = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
# push onto heap in shuffled order
shuf = list(input)
random.shuffle(shuf)
self.assertNotEqual(input, ''.join(shuf))
for c in shuf:
h.insert(c)
# should pop off head in sorted order
res = []
for c in h:
res.append(c)
self.assertEqual(input, ''.join(res))
def main():
unittest.main()
if __name__ == '__main__':
main()