forked from msambol/dsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.py
47 lines (31 loc) · 1012 Bytes
/
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
def left(i):
return 2*i
def right(i):
return 2*i + 1
def parent(i):
return i//2
def max_heapify(a, heap_size, i):
l = left(i)
r = right(i)
largest = i
if l < heap_size and a[l] > a[i]:
largest = l
if r < heap_size and a[r] > a[largest]:
largest = r
if largest != i:
# swap elements
a[i], a[largest] = a[largest], a[i]
max_heapify(a, heap_size, largest)
def build_max_heap(a):
heap_size = len(a)
for i in range(heap_size//2, 0, -1):
max_heapify(a, heap_size, i)
def main():
# root is at index 1
# it can be at index zero but see here: https://www.quora.com/Why-do-indexes-for-heaps-start-at-1
# and: https://stackoverflow.com/questions/22900388/why-in-a-heap-implemented-by-array-the-index-0-is-left-unused
a = [None, 0, 5, 20, 6, 12, 65, 1, 4, 9, 3, 89, 22, 25, 28, 10]
build_max_heap(a)
# print heap starting with the root at index 1
print(f'Heap: {a[1:]}')
main()