-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.py
63 lines (55 loc) · 1.74 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
#!/usr/bin/python
class Heap(object):
def __init__(self):
self.root = 1
self.keys = [0] # 0th element unused
def add(self, key):
self.keys.append(key)
self.siftup()
def siftup(self):
newest = len(self.keys) - 1
if newest > 1:
parent = newest//2
while parent > 0 and self.keys[parent] > self.keys[newest]:
# swap
self.keys[parent], self.keys[newest] = self.keys[newest], self.keys[parent]
# move up a level
newest = parent
parent = parent//2
def remove_min_key(self):
retval, self.keys[self.root] = self.keys[self.root], self.keys[-1]
# fix the heap
self.keys = self.keys[:-1]
self.siftdown()
return retval
def siftdown(self):
top = self.root
while top < len(self.keys):
# check left child
child = top*2
rchild = child + 1
if (child >= len(self.keys)):
# done
break
if (rchild < len(self.keys)):
if self.keys[child] > self.keys[rchild]:
child = rchild
if self.keys[top] > self.keys[child]:
# swap
self.keys[top], self.keys[child] = self.keys[child], self.keys[top]
top = child
def debug(self):
print(self.keys)
if __name__ == '__main__':
testHeap = Heap()
testHeap.add('p')
testHeap.add('o')
testHeap.add('l')
testHeap.add('y')
testHeap.add('c')
testHeap.add('o')
testHeap.add('p')
testHeap.add('t')
testHeap.add('e')
testHeap.add('r')
testHeap.debug()