-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmin_heap.py
53 lines (41 loc) · 1.72 KB
/
min_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
def min_heapify(list, index):
# First thing that we will do is010
# find out the smallest of list[index]
# , list[2*index +1], list[2*index +2]
smallest = None
if list[index] > list[2*index +1]:
smallest = list[2*index +1]
else:
smallest = list[index]
if smallest > list[2*index +2]:
smallest = list[2*index +2]
# Now we have smallest value stored in smallest variable. Hence,
# the first thing we will do is check if smallest is equal
# to the value of node itself,(i.e. the list[index]), if it is so,
# then we have to do absolutely nothing. Hence;
if smallest == list[index]:
return
# Else, if the either of the children have the smaller values, then
# we swap the values of root/index with that particular child, and then
# run min_heapify() over that child as well. :
elif smallest == list[2*index +1]:
list[index], list[2*index +1] = \
list[2*index +1], list[index]
min_heapify(list, 2*index +1)
else:
list[index], list[2*index +2] = \
list[2*index +2], list[index]
min_heapify(list, 2*index +2)
def build_heap(list):
# To build the heap, we have a very simple logic. We'll basically
# run min_heapify() from the first parent to the root parent :
# Below denotes the first parent of the list:
i = (len(list) -1)//2
# From the first parent of the list to the last parent which is the root we have to run the min_heapify function;
while i:
min_heapify(list,i)
i = i//2
# we return the list which represents a min_heap.
return list
result = build_heap([1,4561,55, 78, 77, 898])
print(result)