-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpriorityQ.cpp
More file actions
144 lines (120 loc) · 2.85 KB
/
priorityQ.cpp
File metadata and controls
144 lines (120 loc) · 2.85 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <stdio.h>
int tree_array_size = 20;
int heap_size = 0;
const int INF = 100000;
void swap( int *a, int *b ) {
int t;
t = *a;
*a = *b;
*b = t;
}
//function to get right child of a node of a tree
int get_right_child(int A[], int index) {
if((((2*index)+1) < tree_array_size) && (index >= 1))
return (2*index)+1;
return -1;
}
//function to get left child of a node of a tree
int get_left_child(int A[], int index) {
if(((2*index) < tree_array_size) && (index >= 1))
return 2*index;
return -1;
}
//function to get the parent of a node of a tree
int get_parent(int A[], int index) {
if ((index > 1) && (index < tree_array_size)) {
return index/2;
}
return -1;
}
void max_heapify(int A[], int index) {
int left_child_index = get_left_child(A, index);
int right_child_index = get_right_child(A, index);
// finding largest among index, left child and right child
int largest = index;
if ((left_child_index <= heap_size) && (left_child_index>0)) {
if (A[left_child_index] > A[largest]) {
largest = left_child_index;
}
}
if ((right_child_index <= heap_size && (right_child_index>0))) {
if (A[right_child_index] > A[largest]) {
largest = right_child_index;
}
}
// largest is not the node, node is not a heap
if (largest != index) {
swap(&A[index], &A[largest]);
max_heapify(A, largest);
}
}
void build_max_heap(int A[]) {
int i;
for(i=heap_size/2; i>=1; i--) {
max_heapify(A, i);
}
}
int maximum(int A[]) {
return A[1];
}
int extract_max(int A[]) {
int maxm = A[1];
A[1] = A[heap_size];
heap_size--;
max_heapify(A, 1);
return maxm;
}
void increase_key(int A[], int index, int key) {
A[index] = key;
while((index>1) && (A[get_parent(A, index)] < A[index])) {
swap(&A[index], &A[get_parent(A, index)]);
index = get_parent(A, index);
}
}
void decrease_key(int A[], int index, int key) {
A[index] = key;
max_heapify(A, index);
}
void insert(int A[], int key) {
heap_size++;
A[heap_size] = -1*INF;
increase_key(A, heap_size, key);
}
void print_heap(int A[]) {
int i;
for(i=1; i<=heap_size; i++) {
printf("%d\n",A[i]);
}
printf("\n");
}
int main() {
int A[tree_array_size];
insert(A, 20);
insert(A, 15);
insert(A, 8);
insert(A, 10);
insert(A, 5);
insert(A, 7);
insert(A, 6);
insert(A, 2);
insert(A, 9);
insert(A, 1);
print_heap(A);
increase_key(A, 5, 22);
print_heap(A);
decrease_key(A, 1, 13);
print_heap(A);
printf("%d\n\n", maximum(A));
printf("%d\n\n", extract_max(A));
print_heap(A);
printf("%d\n", extract_max(A));
printf("%d\n", extract_max(A));
printf("%d\n", extract_max(A));
printf("%d\n", extract_max(A));
printf("%d\n", extract_max(A));
printf("%d\n", extract_max(A));
printf("%d\n", extract_max(A));
printf("%d\n", extract_max(A));
printf("%d\n", extract_max(A));
return 0;
}