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
| #include <stdio.h>
#include <stdlib.h>
typedef struct noeud *arbre;
typedef struct noeud
{
char lettre;
int ng;
struct noeud *fg;
struct noeud *fd;
} Noeud;
int EstVide (arbre A);
void InsererDansArbre (arbre *A, int ng, char lettre);
arbre Supprimer (arbre A, char lettre);
arbre SupprimerNoeud (arbre A);
arbre Predecesseur (arbre A);
void Affiche_pref (arbre A);
int main (void)
{
arbre A = NULL;
char lettre;
InsererDansArbre (&A, 8, 'p');
InsererDansArbre (&A, 3, 'g');
InsererDansArbre (&A, 1, 'd');
InsererDansArbre (&A, 0, 'a');
InsererDansArbre (&A, 0, 'f');
InsererDansArbre (&A, 0, 'h');
InsererDansArbre (&A, 0, 'l');
InsererDansArbre (&A, 3, 'n');
InsererDansArbre (&A, 1, 'j');
InsererDansArbre (&A, 0, 'r');
InsererDansArbre (&A, 1, 'v');
InsererDansArbre (&A, 0, 'u');
InsererDansArbre (&A, 0, 'x');
Affiche_pref (A);
printf ("Quelle lettre voulez-vous supprimer ?: ");
scanf ("%c", &lettre);
Supprimer (A, lettre);
Affiche_pref (A);
return 0;
}
void Affiche_pref (arbre A)
{
if (!EstVide (A))
{
printf ("lettre: %c et nb fg: ", A->lettre);
printf ("%d\n", A->ng);
Affiche_pref (A->fg);
Affiche_pref (A->fd);
}
return ;
}
int EstVide (arbre A)
{
if (A == NULL)
{
return 1;
}
return 0;
}
void InsererDansArbre (arbre *A, int ng, char lettre)
{
if (EstVide (*A))
{
*A = malloc (sizeof (Noeud));
if (A == NULL)
{
fprintf (stderr, "Impossible d'allouer le noeud");
exit (0);
}
(*A)->lettre = lettre;
(*A)->ng = ng;
(*A)->fg = NULL;
(*A)->fd = NULL;
}
else
{
if (lettre < (*A)->lettre)
{
InsererDansArbre (&(*A)->fg, ng, lettre);
}
else
{
InsererDansArbre (&(*A)->fd, ng, lettre);
}
}
return ;
}
arbre Supprimer (arbre A, char lettre)
{
if (EstVide (A))
{
return NULL;
}
else if (A->lettre == lettre)
{
return SupprimerNoeud (A);
}
else if (A->lettre > lettre)
{
(A->ng)--;
return Supprimer (A->fg, lettre);
}
else
{
return Supprimer (A->fd, lettre);
}
}
arbre SupprimerNoeud (arbre A)
{
if ((A->fg == NULL) && (A->fd == NULL))
{
return NULL;
}
else if (A->fg == NULL)
{
return A->fd;
}
else if (A->fd == NULL)
{
return A->fg;
}
else
{
arbre f = Predecesseur (A->fg);
A->lettre = f->lettre;
A->fg = Supprimer (A->fg, f->lettre);
}
return A;
}
arbre Predecesseur (arbre A)
{
if (A->fd == NULL)
{
return A;
}
return Predecesseur (A->fd);
} |
Partager