-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.py
94 lines (74 loc) · 2.58 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
class Heap:
def __init__(self, list_):
self.container = list_
self.size = len(self.container)
def get_parent(self, index):
return (index - 1) // 2
def get_left_child(self, index):
return 2 * index + 1
def get_right_child(self, index):
return (2 * index) + 2
def get_size(self):
return self.size
def get_item(self, index):
return self.container[index]
def get_max(self):
if self.size == 0:
return None
return self.container[0]
def extract_max(self):
if self.size == 0:
return None
largest = self.get_max()
self.container[0] = self.container[-1]
del self.container[-1]
self.max_heapify(0)
return largest
def max_heapify(self, index):
l = self.get_left_child(index)
r = self.get_right_child(index)
if l <= self.size - 1 and self.container[l] > self.container[index]:
largest = l
else:
largest = index
if r <= self.size - 1 and self.container[r] > self.container[largest]:
largest = r
if largest != index:
self.swap(largest, index)
self.max_heapify(largest)
def build_max_heap(self):
self.size = len(self.container)
for i in range((len(self.container) // 2), -1, -1):
self.max_heapify(i)
def swap(self, i, j):
self.container[i], self.container[j] = self.container[j], self.container[i]
def insert(self, key):
index = self.size
self.container.append(key)
while index != 0:
p = self.get_parent(index)
if self.get_item(p) < self.get_item(index):
self.swap(p, index)
index = p
def min_heapify(self, index):
l = self.get_left_child(index)
r = self.get_right_child(index)
if l <= self.size and self.container[l] < self.container[index]:
minimun = l
else:
minimun = index
if r <= self.size and self.container[r] < self.container[index]:
minimun = r
if minimun != index:
self.swap(minimun, index)
self.min_heapify(minimun)
def build_min_heap(self):
self.size = len(self.container)
for i in range((len(self.container) // 2), 0):
self.min_heapify(self.container, i)
def heap_sort(self):
self.build_max_heap()
for i in range(len(self.container) - 1, -1, -1):
self.swap(0, i)
self.size = self.size - 1
self.max_heapify(0)