-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.c
153 lines (120 loc) · 3.31 KB
/
heap.c
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
145
146
147
148
149
150
151
152
153
// MAX-MIN HEAP
typedef struct heapNode {
int priority;
int data;
} heapNode;
typedef char boolean;
int parent(int i) {return (i - 1) / 2;}
int left_child(int i) {return 2*i + 1;}
int right_child(int i) {return 2*i + 2;}
void swap(heapNode* x, heapNode* y) {
heapNode temp = *x;
*x = *y;
*y = temp;
}
void max_heap_insert(heapNode heap[], heapNode node, int* n, int max_dim) {
if (max_dim == *n) {
if (heap[*n].priority < node.priority)
heap[*n] = node;
} else {
heap[*n] = node;
}
int i = *n;
while(i != 0 && heap[parent(i)].priority < heap[i].priority) {
swap(&heap[parent(i)], &heap[i]);
i = parent(i);
}
if (max_dim != *n)
*n += 1;
}
void min_heap_insert(heapNode heap[], heapNode node, int* n) {
// if(node.data == 399)
// printf("\n\n INSERISCO 399 \n");
heap[*n] = node;
int i = *n;
while(i != 0 && heap[parent(i)].priority > heap[i].priority) {
swap(&heap[parent(i)], &heap[i]);
i = parent(i);
}
*n += 1;
}
void max_heapify(heapNode heap[], int i, int n) {
int left = left_child(i);
int right = right_child(i);
int largest = i;
if(left <= n && heap[left].priority > heap[largest].priority)
largest = left;
if(right <= n && heap[right].priority > heap[largest].priority)
largest = right;
if (largest != i) {
heapNode temp = heap[i];
heap[i] = heap[largest];
heap[largest] = temp;
max_heapify(heap, largest, n);
}
}
void min_heapify(heapNode heap[], int i, int n) {
int left = left_child(i);
int right = right_child(i);
int lower = i;
if(left < n && heap[left].priority < heap[lower].priority)
lower = left;
if(right < n && heap[right].priority < heap[lower].priority)
lower = right;
if (lower != i) {
heapNode temp = heap[i];
heap[i] = heap[lower];
heap[lower] = temp;
min_heapify(heap, lower, n);
}
}
void build_max_heap(heapNode heap[], int n) {
for (int i = n/2; i >= 0; i--) {
max_heapify(heap, i, n);
}
}
void build_min_heap(heapNode heap[], int n) {
for (int i = n/2; i >= 0; i--) {
min_heapify(heap, i, n);
}
}
heapNode get_maxmin(heapNode heap[]) {
return heap[0];
}
heapNode delete_max(heapNode heap[], int* n) {
heapNode max = heap[0];
heap[0] = heap[*n - 1];
*n -= 1;
max_heapify(heap, 0, *n);
return max;
}
heapNode delete_min(heapNode heap[], int* n) {
heapNode min = heap[0];
heap[0] = heap[*n - 1];
*n -= 1;
min_heapify(heap, 0, *n);
return min;
}
void updatePriority(heapNode h[], int n, int i, int new_priority) {
if (new_priority == h[i].priority)
return;
if(new_priority > h[i].priority) {
h[i].priority = new_priority;
min_heapify(h, i, n);
} else {
h[i].priority = new_priority;
if (i == 0)
return;
while (parent(i) >= 0 && h[i].priority < h[parent(i)].priority) {
swap(&h[parent(i)], &h[i]);
i = parent(i);
}
}
}
void print_heap(heapNode heap[], int n) {
printf("\n------ heap ------");
for (int i = 0; i < n; i++) {
printf("\nPri: %d, Data: %d", heap[i].priority, heap[i].data);
}
printf("\n-------------------");
}