/* Arbres binaires tout simplement */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
struct noeud {
int num;
float val;
struct noeud * sg;
struct noeud * sd;
} noeud_t;
typedef struct noeud * arbre_t;
|
void ajout (arbre_t * a, int v, float x) {
arbre_t new;
int rande;
if (! (*a)) {
new = (arbre_t) malloc (sizeof (noeud_t));
new->num = v;
new->val = x;
new->sg = (arbre_t) 0;
new->sd = (arbre_t) 0;
*a = new;
return;
}
rande = rand ();
if (rande & 0x01) {
/* on le met à gauche*/
if (! ((*a)->sg)) {
/* s'il ny a pas de fils gauche */
(*a)->sg = (arbre_t) malloc (sizeof (noeud_t));
(*a)->sg->num = v;
(*a)->sg->val = x;
(*a)->sg->sg = (arbre_t) 0;
(*a)->sg->sd = (arbre_t) 0;
return;
}
ajout(&((*a)->sg), v, x);
return;
}
/* on le met donc a droite*/
if (! ((*a)->sd)) {
/* s'il ny a pas de fils droit */
(*a)->sd = (arbre_t) malloc (sizeof (noeud_t));
(*a)->sd->num = v;
(*a)->sd->val = x;
(*a)->sd->sg = (arbre_t) 0;
(*a)->sd->sd = (arbre_t) 0;
return;
}
ajout(&((*a)->sd), v, x);
return;
}
/*
To create the tree, you add, one after the other, the values in the
tree.*/
arbre_t creation (int n) {
arbre_t a;
float x, y;
x = 1.5;
y = 0.21;
a = (arbre_t) 0;
srand(time(NULL));
while (n--) {
ajout (&a, n, x);
x += y;
y += 0.06;
if (x > 3.0 || x < -2.0) {
y = - y;
}
}
return a;
}
|
void afficharbre (arbre_t a) {
if (a) {
afficharbre (a->sg);
printf("%d %3.2f ", a->num, a->val);
afficharbre (a->sd);
}
}
|
int appartient (int nu, arbre_t a) {
if (! a)
return 0;
if (a->num == nu)
return 1;
if (appartient (nu, a->sd))
return 1;
return appartient (nu, a->sg);
}
|
Arbres binaires de recherche
Voici une réécriture des fonctions ajout et création.
void ajout (arbre_t * a, int v, float x, float y) {
arbre_t new;
int pivot;
if (! (*a)) {
new = (arbre_t) malloc (sizeof (noeud_t));
assert(new);
new->num = v;
new->coef = y;
new->unit = x;
new->sg = (arbre_t) 0;
new->sd = (arbre_t) 0;
(*a) = new;
return;
}
pivot = (*a)->num;
/*
printf(" insere %d en %d avec %2d\n", v, a->num, rande %100);
*/
if (v < pivot) {
/* on le met à gauche*/
if (! ((*a)->sg)) {
/* s‘il ny a pas de fils gauche */
(*a)->sg = (arbre_t) malloc (sizeof (noeud_t));
assert((*a)->sg);
(*a)->sg->num = v;
(*a)->sg->coef = y;
(*a)->sg->unit = x;
(*a)->sg->sg = (arbre_t) 0;
(*a)->sg->sd = (arbre_t) 0;
return;
}
ajout(&((*a)->sg), v, x, y);
return;
}
/* on le met donc a droite*/
if (! ((*a)->sd)) {
/* s‘il ny a pas de fils droit */
(*a)->sd = (arbre_t) malloc (sizeof (noeud_t));
assert((*a)->sd);
(*a)->sd->num = v;
(*a)->sd->coef = y;
(*a)->sd->unit = x;
(*a)->sd->sg = (arbre_t) 0;
(*a)->sd->sd = (arbre_t) 0;
return;
}
ajout(&((*a)->sd), v, x, y);
return ;
}
arbre_t creation (int n) {
arbre_t a;
float x, y;
x = 1.5;
y = 0.21;
a = (arbre) 0;
while (n--) {
a = ajout (a, n, x);
x += y;
y += 0.06;
if (x > 3.0 || x < -2.0) {
y = - y;
}
}
return a;
}
|
int appartient (arbre_t a, float x) {
if (! a)
return 0;
if (a->val == x)
return 1;
if (a->val > x)
return appartient (a->sg, x);
return appartient (a->sd, x);
}
|