-
Notifications
You must be signed in to change notification settings - Fork 0
/
binaryheap.py
107 lines (89 loc) · 2.95 KB
/
binaryheap.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
from operator import gt, lt
class BinaryHeap:
"""
Binary Heap implementation
Examples:
>>> data = [1,8,5,-5]
>>> min_heap = BinaryHeap.heapify_slow(data,lt)
>>> min_heap.storage
[None, -5, 1, 5, 8]
>>> max_heap = BinaryHeap.heapify_slow(data,gt)
>>> max_heap.storage
[None, 8, 1, 5, -5]
>>> max_heap.insert(12)
>>> max_heap.storage
[None, 12, 8, 5, -5, 1]
>>> print(max_heap.dominant())
12
>>> max_heap.del_dominant()
12
>>> max_heap.storage
[None, 8, 1, 5, -5]
"""
def __init__(self):
self.storage=[None]
self.upto=0
self.comp_op = None
@classmethod
def heapify_slow(cls, it, compare_op):
"""Method to create heap out of data and comparison operator
it: data used to populate heap
compare_op: binary operation used to produce heap
"""
inst = cls()
inst.comp_op = compare_op
for i in it:
inst.insert(i)
return inst
def insert(self, value):
"""Method to insert given value into heap
value: value to add into heap
"""
self.storage.append(value)
self.upto += 1
self.sift_up(self.upto)
def sift_up(self, i):
"""Helper function for insert method
i: index of current node
"""
parent = i // 2
if parent > 0 and self.comp_op(self.storage[i],self.storage[parent]):
self.storage[i], self.storage[parent] = self.storage[parent], self.storage[i]
self.sift_up(parent)
def _min_child(self, i):
"""Helper function for sift_down functon
i: index of current node
"""
if 2*i + 1 > self.upto:
return 2*i
else:
if self.comp_op(self.storage[2*i], self.storage[2*i+1]):
return 2*i
return 2*i + 1
def sift_down(self, i):
"""Helper function for del_dominant
Sifts values down the heap
i: index of current node
"""
if 2*i <= self.upto:
child = self._min_child(i)
if not self.comp_op(self.storage[i],self.storage[child]):
self.storage[child], self.storage[i] = self.storage[i], self.storage[child]
self.sift_down(child)
def dominant(self):
"""Method to find dominant element in heap-
dominant is the largest in a max heap and smallest
in a min heap
"""
return self.storage[1]
def del_dominant(self):
"""Method to delete dominant element in heap-
dominant is the largest in a max heap and smallest
in a min heap
"""
dom_val = self.storage[1]
self.storage[1], self.storage[self.upto] = self.storage[self.upto], self.storage[1]
self.storage.pop()
self.upto -= 1
self.sift_down(1)
return dom_val