-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryHeap.c
180 lines (162 loc) · 3.63 KB
/
BinaryHeap.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <stdlib.h>
#include <stdio.h>
#include "BinaryHeap.h"
/**
*Initializing the BinaryHeap
*
**/
BinaryHeap* initializeHeap() {
BinaryHeap *heap = malloc(sizeof(BinaryHeap));
heap->size = 0;
heap->max_length = 100;
WeightedEdge *edge;
for(int i = 0; i<heap->max_length;i++){
heap->A[i] = edge;
}
return heap;
}
///////////// Private methods
/**
* Swap values in the array
* at indexes i and j.
* Complexity: THETA(1)
*/
void swap(int i, int j,BinaryHeap* heap) {
WeightedEdge* tmp = heap->A[i];
heap->A[i] = heap->A[j];
heap->A[j] = tmp;
}
/**
* Return the number of the left
* node of node number n.
* Complexity: THETA(1)
*/
int left(int n) {
return 2*n + 1;
}
/**
* Return the number of the right
* node of node number n.
* Complexity: THETA(1)
*/
int right(int n) {
return 2*(n + 1);
}
/**
* Return the number of the parent
* node of node number n.
* Complexity: THETA(1)
*/
int parent(int n) {
return (n - 1)/2;
}
/**
* Percolate down the element à node number n
* Complexity: O(log(size))
*/
void percolateDown(int n,BinaryHeap* heap) {
int g = left(n); int d = right(n); int k = n;
if ( g < heap->size && heap->A[g]->weight < heap->A[n]->weight)
k = g;
if ( d < heap->size && heap->A[d]->weight < heap->A[k]->weight)
k = d;
if ( k != n ) {
swap(k,n,heap);
percolateDown(k,heap);
}
}
/**
* Percolate up the element à node number n
* Complexity: O(log(size))
*/
void percolateUp(int n,BinaryHeap *heap) {
WeightedEdge * e = heap->A[n];
while ( n > 0 && (*e).weight < heap->A[parent(n)] -> weight) {
heap->A[n] = heap->A[parent(n)];
n = parent(n);
}
heap->A[n] = e;
}
/**
* Arrange the elements in A such
* that it has the heap property.
* Complexity: O(size)
*/
void buildHeap(BinaryHeap *heap) {
for ( int i = parent(heap->size - 1); i >= 0; i-- ){
percolateDown(i,heap);
}
}
///////////// Public methods
/**
* Return the size of the heap
* (the number of elements in the heap).
* Complexity: THETA(1)
*/
int size(BinaryHeap *heap) {
return heap->size;
}
/**
* Return the extreme element.
* Complexity: THETA(1)
*/
WeightedEdge extreme(BinaryHeap *heap) {
if ( size != 0 )
return *(heap->A[0]);
}
/**
* Return and delete the extreme element.
* Complexity: O(log(size))
*/
WeightedEdge* deleteExtreme(BinaryHeap *heap) {
if ( heap->size != 0 ){
WeightedEdge *extreme = heap->A[0];
heap->A[0] = heap->A[--heap->size];
if ( heap->size > 0 ){
percolateDown(0,heap);
}
return extreme;
}
}
void imprimerHeap(BinaryHeap *heap){
for(int i = 0 ; i<(*heap).size;i++){
printf("%d\t %d %f\n",heap -> A[i] -> edge.origin,heap -> A[i] -> edge.destination,heap -> A[i] -> weight );
}
}
/**
* Add a new element in the heap
* Complexity: O(log(size))
*/
void addinHeap(WeightedEdge* e,BinaryHeap *heap) {
if (heap->size != heap->max_length){
heap->A[heap->size++] = e;
percolateUp(heap->size-1,heap);
}
}
///////////// Part 3: deleting in the heap
/**
* Delete the element e from the heap.
* Complexity: O(size)
*/
void delete(WeightedEdge* e,BinaryHeap *heap) {
for ( int i = 0; i < heap->size; i++ ){
if ( heap->A[i]->edge.origin == (*e).edge.origin && heap->A[i]->edge.destination == (*e).edge.destination) {
heap->A[i] = heap->A[--heap->size];
percolateUp(i,heap);
percolateDown(i,heap);
}
}
}
/**
* Delete all the elements e from the heap.
* Complexity: O(size)
*/
void deleteAll(WeightedEdge* e,BinaryHeap *heap) {
int i = 0;
while ( i < heap->size ){
if ( heap->A[i]->edge.origin == (*e).edge.origin && heap->A[i]->edge.destination == (*e).edge.destination) {
swap(i,--heap->size,heap);
}
}
buildHeap(heap);
}