-
Notifications
You must be signed in to change notification settings - Fork 1
/
binomial_heap.py
189 lines (169 loc) · 6.23 KB
/
binomial_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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import os, sys
class BinomialHeap():
class Node():
def __init__(self, k):
self.key=k
self.par=None
self.sib=None
self.child=None
self.deg=0
def __str__(self):
return '[key={}, deg={}]'.format(self.key, self.deg)
def __init__(self, data=None, condition=None):
self.heap=None
self.condition = lambda a,b: a<b
if condition is not None:
self.condition=condition
if data is not None:
for val in data:
self.insert(val)
#insert: O(log(n))
def insert(self, val):
self.merge([self.Node(k=val)])
def remove(self):
i=1
idx=0
removed = self.heap[idx]
n=len(self.heap)
while i<n:
if self.condition(self.heap[i].key, removed.key): #if condition satisfy then stored that node
idx=i
removed = self.heap[idx]
i+=1
del self.heap[idx]
rm_childs = []
p = removed.child
while p is not None:
t=p
p=p.sib
t.sib=None
t.par=None
rm_childs.append(t)
self.merge( rm_childs )
return removed.key
#update: O(n) bcz finding the key tooks atmost O(n) time after than heapify it took O(log(n))
def update(self, old_key, new_key):
match_node = None
for node in self.heap:
match_node= self._find(node, old_key)
if match_node is not None:
break
if match_node is not None:
match_node.key = new_key
parent = match_node.par
child = match_node
while parent is not None and child is not None and not self.condition(parent.key, child.key):
parent.key, child.key = child.key, parent.key
child, parent = parent, parent.par
parent = match_node
child = parent.child
while parent is not None and child is not None and not self.condition(parent.key, child.key):
while child.sib is not None and not self.condition(child.key, child.sib.key):
child = child.sib
parent.key, child.key = child.key, parent.key
parent, child = child, child.child
else:
print('Key not found!')
def delete(self, key):
# need to be generalize
# work for only integer list and for max heap only
self.update( old_key=key, new_key=2**31 )
self.remove( )
def _find(self, node, key, msg=None):
if node is not None:
if node.key == key:
return node
m = self._find(node.child, key, msg='child')
if m is not None:
return m
return self._find(node.sib, key, 'sib')
return None
def insert_by_order(self, target):
temp = []
j=i=0
n = len(self.heap) if self.heap else 0
m = len(target) if target else 0
while i<n and j<m:
if self.heap[i].deg <= target[j].deg:
temp.append(self.heap[i])
i+=1
else:
temp.append(target[j])
j+=1
while i<n:
temp.append(self.heap[i])
i+=1
while j<m:
temp.append(target[j])
j+=1
self.heap=temp
def merge(self, target):
if isinstance(target, BinomialHeap):
target = target.heap
self.insert_by_order(target)
f, s, t = 0, 1, 2 #pointing to 1st smallest binomial tree, pointing to 2nd smallest binomial tree, pointing to 3rd smallest binomial tree
while f < len(self.heap):
if s >= len(self.heap):
break
elif self.heap[f].deg != self.heap[s].deg:
f, s, t = f+1, s+1, t+1
elif t<len(self.heap) and self.heap[f].deg == self.heap[s].deg and self.heap[f].deg == self.heap[t].deg:
f, s, t =f+1, s+1, t+1
elif self.heap[f].deg == self.heap[s].deg:
if not self.condition(self.heap[f].key, self.heap[s].key): # if condition voilates then swap it to make it as a child(first is always parents of second)
q = self.heap[f]
self.heap[f] = self.heap[s]
self.heap[s] = q
self.heap[s].par = self.heap[f]
self.heap[s].sib = self.heap[f].child
self.heap[f].child = self.heap[s]
self.heap[f].deg += 1
del self.heap[s] #O(log(n)) <<- length of self.heap array
def exist(self):
return len(self.heap)>0
def print(self):
def _print(node):
print(node)
if node.sib is not None:
print('sibling of', node, end=' ')
_print(node.sib)
if node.child is not None:
print('child of', node, end=' ')
_print(node.child)
for node in self.heap:
_print(node)
if __name__=='__main__':
bh = BinomialHeap(condition=lambda a, b: a < b)
for i in range(50, 0, -1):
bh.insert(i)
bh.update(49, -99)
bh.update(23, 99)
bh.update(1, -10)
while bh.exist():
print(bh.remove(), end=' ')
print('\n')
more = True
if more:
#priority example
bh2 = BinomialHeap(data=[(12, 124),
(41, 121),
(23, 532),
(123, 214),
(213, 14532),
(4312, 35),
(1, 2),
(21, 14),
(124, 12)], condition=lambda a, b: a[1] > b[1])
bh2.insert((14124, 124125))
bh2.insert((23, 532))
bh2.insert((523, 574))
bh2.update((23, 532), (1240, 120))
while bh2.exist():
print(bh2.remove(), end=' ')
#MERGE TWO BH
print('\n')
b1 = BinomialHeap(data=list(range(1, 1001, 3)))
b2 = BinomialHeap(data=list(range(101, 2001, 2)))
b2.merge(b1)
while b2.exist():
print(b2.remove(), end=' ')