-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbheap.py
85 lines (73 loc) · 2.14 KB
/
bheap.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
class Bin_tree:
def __init__(self, key):
self.key = key
self.children = []
self.order = 0
def add(self, tree):
self.children.append(tree)
self.order += 1
class Bin_heap:
def __init__(self):
self.trees = []
def enqueue(self, key): # make singleton heap add tree merge
singleton = Bin_heap()
singleton.trees.append(Bin_tree(key))
self.merge(singleton)
def merge(self, other):
self.trees.extend(other.trees)
self.trees.sort(key=lambda tree: tree.order)
i = 0
while i < len(self.trees)-1:
if self.trees[i].order == self.trees[i+1].order:
if self.trees[i].key < self.trees[i+1].key:
self.trees[i].add(self.trees[i+1])
self.trees.pop(i+1)
i -= 1
elif self.trees[i].key > self.trees[i+1].key:
self.trees[i+1].add(self.trees[i])
self.trees.pop(i)
i -= 1
i += 1
def peek(self):
min = self.trees[0].key
for i in range(len(self.trees)):
if min > self.trees[i].key:
min = self.trees[i].key
print(min)
def print_heap(self):
print("head", end="")
for i in self.trees:
print("<-", i.key, [j.key for j in i.children],
f"({i.order})", end="")
print()
def extract_min(self):
min = self.trees[0].key
mintree = self.trees[0]
for i in range(len(self.trees)):
if min > self.trees[i].key:
min = self.trees[i].key
mintree = self.trees[i]
self.trees.remove(mintree)
temp = Bin_heap()
temp.trees = mintree.children
self.merge(temp)
def main():
m = Bin_heap()
m.enqueue(13)
m.print_heap()
m.enqueue(12)
m.print_heap()
m.enqueue(19)
m.print_heap()
m.enqueue(6)
m.print_heap()
m.enqueue(5)
m.print_heap()
m.enqueue(10)
m.print_heap()
m.peek()
m.extract_min()
m.print_heap()
m.extract_min()
m.print_heap()
main()