VaultBy ENSAMIENParrainage
C100 lines

ex2-arbre-binaire-recherche.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
/*
 * Examen TP DSA, exercice 2 : arbre binaire de recherche
 *
 *   typedef struct ABR {
 *       int val;
 *       struct ABR *FD; // fils droit
 *       struct ABR *FG; // fils gauche
 *   } arbre;
 *
 * Écrire les fonctions qui :
 * 1. retournent la taille d'un arbre ;
 * 2. retournent la valeur maximale d'un arbre ;
 * 3. calculent la somme des valeurs des nœuds ;
 * 4. calculent la hauteur de la branche la plus courte ;
 * 5. retournent le nombre de feuilles.
 */

#include <stdio.h>
#include <stdlib.h>

typedef struct ABR {
    int val;
    struct ABR *FD;
    struct ABR *FG;
} arbre;

/* 1. Taille */
int taille(arbre *root) {
    if (root == NULL) return 0;
    return 1 + taille(root->FD) + taille(root->FG);
}

/* 2. Maximum : dans un ABR c'est le nœud le plus à droite.
 * L'arbre ne doit pas être vide. */
int max(arbre *root) {
    while (root->FD != NULL)
        root = root->FD;
    return root->val;
}

/* 3. Somme */
int somme(arbre *root) {
    if (root == NULL) return 0;
    return root->val + somme(root->FD) + somme(root->FG);
}

/* 4. Hauteur de la branche la plus courte (jusqu'à une feuille) */
int min_hauteur(arbre *root) {
    if (root == NULL) return 0;
    if (root->FD == NULL) return 1 + min_hauteur(root->FG);
    if (root->FG == NULL) return 1 + min_hauteur(root->FD);
    int d = min_hauteur(root->FD);
    int g = min_hauteur(root->FG);
    return 1 + (d < g ? d : g);
}

/* 5. Nombre de feuilles */
int nb_feuilles(arbre *root) {
    if (root == NULL) return 0;
    if (root->FD == NULL && root->FG == NULL) return 1;
    return nb_feuilles(root->FD) + nb_feuilles(root->FG);
}

/* Outils pour tester */
arbre *inserer(arbre *root, int val) {
    if (root == NULL) {
        arbre *n = malloc(sizeof(arbre));
        n->val = val;
        n->FD = n->FG = NULL;
        return n;
    }
    if (val < root->val)
        root->FG = inserer(root->FG, val);
    else
        root->FD = inserer(root->FD, val);
    return root;
}

void liberer(arbre *root) {
    if (root == NULL) return;
    liberer(root->FG);
    liberer(root->FD);
    free(root);
}

int main(void) {
    int valeurs[] = {8, 3, 10, 1, 6, 14, 4, 7, 13};
    arbre *root = NULL;
    for (int i = 0; i < 9; i++)
        root = inserer(root, valeurs[i]);

    printf("taille      : %d\n", taille(root));      /* 9 */
    printf("max         : %d\n", max(root));         /* 14 */
    printf("somme       : %d\n", somme(root));       /* 66 */
    printf("min hauteur : %d\n", min_hauteur(root)); /* 3 */
    printf("feuilles    : %d\n", nb_feuilles(root)); /* 4 */

    liberer(root);
    return 0;
}

    Type at least 2 letters. ↑ ↓ to move, Enter to open.