lonsomehell

Untitled

May 20th, 2013
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.75 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. typedef struct noeud arbre;
  5. struct noeud
  6. {
  7.     int data;
  8.     arbre* gauche;
  9.     arbre* droit;
  10. };
  11.  
  12. arbre* creer(){
  13.     arbre* abr;
  14.     abr=malloc(sizeof(arbre));
  15.     abr->gauche=NULL;
  16.     abr->droit=NULL;
  17.     return abr;
  18. }
  19. arbre* recherche(arbre *abr,int n){
  20.     arbre* courant;
  21.     if(!abr)
  22.         return NULL;
  23.     if(abr->data==n)
  24.         return abr;
  25.     if(abr->data<n)
  26.         return (recherche(abr->gauche,n));
  27.     if(abr->data>n)
  28.         return(recherche(abr->droit,n));
  29. }
  30. int max(arbre* abr)
  31. {
  32.     if(!abr)
  33.         return -32000;
  34.     if(!abr->droit)
  35.         return abr->data;
  36.     return max(abr->droit);
  37. }
  38. int min(arbre* abr)
  39. {
  40.     if(!abr)
  41.         return -32000;
  42.     if(!abr->gauche)
  43.         return abr->data;
  44.     return min(abr->gauche);
  45. }
  46. int ABRTrue(arbre* abr)
  47. {
  48.     if(!abr)
  49.         return 1;
  50.     if((abr->data>=min(abr))&&(abr->data<=max(abr)))
  51.         return ABRTrue(abr->gauche)*ABRTrue(abr->droit);
  52.     else
  53.         return 0;
  54. }
  55. arbre* ajout(arbre *abr,int n)
  56. {
  57.  
  58.     if(!abr){
  59.         arbre * temp=creer();
  60.         temp->data=n;
  61.         return temp;
  62.     }
  63.     else if(abr->data==n){
  64.         printf("l'entier existe deja\n");
  65.         return abr;
  66.     }
  67.     else if(abr->data<n){
  68.         abr->droit=ajout(abr->droit,n);
  69.     }
  70.     else{
  71.         abr->gauche=ajout(abr->gauche,n);
  72.     }
  73.     return abr;
  74. }
  75. arbre* conc(arbre* abr,arbre* Ag,arbre* Ad)
  76. {
  77.     abr->droit=Ad;
  78.     abr->gauche=Ag;
  79.     return abr;
  80. }
  81. arbre* concat(arbre* abr,arbre* Ag,arbre* Ad)
  82. {
  83.     if((abr->data<min(Ad))&&(abr->data>max(Ag))&&(ABRTrue(Ad))&&(ABRTrue(Ag)))
  84.     {
  85.         return conc(abr,Ag,Ad);
  86.     }
  87.     return NULL;
  88. }
  89. arbre* supp_racine(arbre *abr){
  90.     if(abr){
  91.         arbre *temp =abr;
  92.         if((!abr->droit)&&(!abr->gauche)){
  93.             free(abr);
  94.             return NULL;
  95.         }
  96.         else if((abr->droit)&&(!abr->gauche)){
  97.             abr=abr->droit;
  98.             free(temp);
  99.             return abr;
  100.         }
  101.         else if((!abr->droit)&&(abr->gauche)){
  102.             abr =abr->gauche;
  103.             free(temp);
  104.             return abr;
  105.         }
  106.         else if(!abr->droit->gauche){
  107.             temp =abr;
  108.             abr=abr->droit;
  109.             abr->gauche=temp->gauche;
  110.             free (temp);
  111.             return abr;
  112.         }
  113.         else{
  114.             arbre *courant=abr->droit;
  115.             while(courant->gauche->gauche)
  116.                 courant=courant->gauche;
  117.             temp=courant->gauche;
  118.             courant->gauche=courant->gauche->droit;
  119.             temp->gauche=abr->gauche;
  120.             temp->droit=abr->droit;
  121.             free(abr);
  122.             return temp;
  123.         }
  124.     }
  125. }
  126. arbre* supp_Val(arbre *abr,int n){//n existes.
  127.     if(abr){
  128.         if(abr->data==n)
  129.             abr=supp_racine(abr);
  130.         else if(n<abr->data){
  131.             abr->gauche=supp_Val(abr->gauche,n);
  132.         }
  133.         else{
  134.             abr->droit=supp_Val(abr->droit,n);
  135.         }
  136.         return abr;
  137.     }
  138. }
  139. void affiche(arbre* abr)
  140. {
  141.     if(abr){
  142.         affiche(abr->gauche);
  143.         printf("%d\n",abr->data);
  144.         affiche(abr->droit);
  145.     }
  146. }
  147. int main(){
  148.     arbre * abr;
  149.     abr=ajout(abr,0);
  150.     abr=ajout(abr,-1);
  151.     abr=ajout(abr,2);
  152.     abr=ajout(abr,0);
  153.     abr=supp_Val(abr,2);
  154.     printf("%d\n",ABRTrue(abr));
  155.     affiche(abr);  
  156. }
Advertisement
Add Comment
Please, Sign In to add comment