-
Notifications
You must be signed in to change notification settings - Fork 150
/
Max_Heap_Priority_Queue.py
58 lines (42 loc) · 1.34 KB
/
Max_Heap_Priority_Queue.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
def heapify(arr, n, i):
# Find the largest among root, left child, and right child
largest = i
left_child = 2 * i + 1
right_child = 2 * i + 2
if left_child < n and arr[i] < arr[left_child]:
largest = left_child
if right_child < n and arr[largest] < arr[right_child]:
largest = right_child
# Swap and continue heapifying if root is not the largest
if largest != i:
arr[i], arr[largest] = arr[largest], arr[i]
heapify(arr, n, largest)
def insert(array, new_num):
size = len(array)
if size == 0:
array.append(new_num)
else:
array.append(new_num)
for i in range((size // 2) - 1, -1, -1):
heapify(array, size, i)
def delete_node(array, num):
size = len(array)
i = 0
for i in range(0, size):
if num == array[i]:
break
array[i], array[size - 1] = array[size - 1], array[i]
array.pop()
for i in range((len(array) // 2) - 1, -1, -1):
heapify(array, len(array), i)
store_array = []
insert(store_array, 3)
insert(store_array, 4)
insert(store_array, 9)
insert(store_array, 5)
insert(store_array, 2)
print("Max-Heap array:", store_array)
delete_node(store_array, 4)
print("After deleting an element:", store_array)
# Answer: Max-Heap array: [9, 5, 4, 3, 2]
# After deleting an element: [9, 5, 2, 3]