-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtas.c
More file actions
45 lines (40 loc) · 700 Bytes
/
tas.c
File metadata and controls
45 lines (40 loc) · 700 Bytes
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
#include "tas.h"
#include "util.h"
int parent(int i)
{
++i;
i >>= 1;
return --i;
}
int droite(int i)
{
return (i <<= 1) + 2;
}
int gauche(int i) /* En C, gauche les indices sont impaires */
{
return (i <<= 1) + 1;
}
void entasserMax(tas_t * t, int i)
{
int g = gauche(i), d = droite(i), max;
if ((g < t->taille) && (t->tab[g].poids > t->tab[i].poids))
max = g;
else
max = i;
if ((d < t->taille) && (t->tab[d].poids > t->tab[max].poids))
max = d;
if (max != i)
{
echanger(&(t->tab[max]), &(t->tab[i]));
entasserMax(t, max);
}
}
void construireTasMax(tas_t * t)
{
int i;
t->taille = t->longueur;
for (i = parent(t->longueur - 1); i >= 0; --i)
{
entasserMax(t, i);
}
}