-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriorite.cpp
More file actions
executable file
·79 lines (67 loc) · 1.98 KB
/
priorite.cpp
File metadata and controls
executable file
·79 lines (67 loc) · 1.98 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
#include "priorite.h"
#include <iostream>
using namespace std;
// Ajoute un element
void FilePriorite::push(PointDist d) {
int i = v.size();
v.push_back(d);
// On replace le point où il faut, selon si ses parents sont plus petits et ceux jusqu'au début:
while (v[i / 2] < v[i] && i / 2 > 0) {
swap(v[i / 2], v[i]);
i = i / 2;
}
}
// Retire un element
PointDist FilePriorite::pop() {
PointDist p = v[1];
v[1] = v.back();
v.pop_back();
if (!empty()) {
int i = 1;
// On continue s'il y a deux enfants, s'il y en a un plus grand, et s'il y a qu'un enfant, s'il est plus grand.
while (((2 * i + 1 < v.size()) && (v[i] < v[2 * i] || v[i] < v[2 * i + 1])) ||
((2 * i < v.size()) && (v[i] < v[2 * i]))) {
// On vérifie s'il y a deux enfants :
if (2 * i + 1 < v.size()) {
// Dans le cas ou les deux sont plus grands :
if (v[i] < v[2 * i] && v[i] < v[2 * i + 1]) {
// on cherche le plus grand des deux.
if (v[2 * i] < v[2 * i + 1]) {
swap(v[i], v[2 * i + 1]);
i = 2 * i + 1;
}
else {
swap(v[i], v[2 * i]);
i = 2 * i;
}
}
else {
if (v[i] < v[2 * i]) {
swap(v[i], v[2 * i]);
i = 2 * i;
}
else {
swap(v[i], v[2 * i + 1]);
i = 2 * i + 1;
}
}
}
else {
swap(v[i], v[2 * i]);
}
}
}
return p;
}
FilePriorite::FilePriorite() {
PointDist v(0, 0, 0);
push(v);
};
bool FilePriorite::empty() {
if (v.size() > 0) {
return (false);
}
else {
return (true);
}
}