Skip to content
Snippets Groups Projects
Select Git revision
  • 2d64b984b792c274a86d89f42fcedb27b68bc1ee
  • master default protected
2 results

package.json

Blame
  • Forked from Développement Web Avancé / 2019_TP2
    Source project has a limited visibility.
    This project manages its dependencies using npm. Learn more
    arbre_binaire.c 2.95 KiB
    #include <stdio.h>
    #include <stdlib.h>
    #include <assert.h>
    #include <stdbool.h>
    #include "arbre_binaire.h"
    
    static node* position(arbre tree,int cle) {
       node* crt = tree;
       if (NULL != tree) {
          do {
             if (cle < crt->key) {
                if (NULL == crt->gauche) {
                   break;
                }
                crt = crt->gauche;
             } else if (cle > crt->key) {
                if (NULL == crt->droite) {
                   break;
                }
                crt = crt->droite;
             }
          } while (crt->key != cle);
       }
       return crt;
    }
    
    bool arbre_insert(arbre* tree,int cle) {
       if (NULL == *tree) {
          *tree = calloc(1,sizeof(node));
          (*tree)->key = cle;
       } else {
          node* nd = position(*tree,cle);
          if (cle < nd->key) {
             nd->gauche = calloc(1,sizeof(node));
             nd->gauche->key = cle;
          } else if (cle > nd->key) {
             nd->droite = calloc(1,sizeof(node));
             nd->droite->key = cle;
          } else {
             return false;
          }
       }
       return true;
    }
    
    static node* parent(arbre tree,node* nd) {
       assert(NULL != tree && NULL != nd);
       node* parent = NULL;
       if (nd != tree) {
          node* crt = tree;
          int cle = nd->key;
          do  {
             parent = crt;
             if (cle < crt->key) {
                crt = crt->gauche;
             } else if (cle > crt->key) {
                crt = crt->droite;
             } 
          } while (crt != nd);
       }
       return parent;
    }
    
    
    bool arbre_delete(arbre* tree,int cle) {
       node* nd = position(*tree,cle);
       
       if (NULL == nd || cle != nd->key) {
          return false;
       }
    
       // terminal node
       if (NULL == nd->gauche && NULL == nd->droite) {
          node* nd_parent = parent(*tree,nd);
          if (NULL == nd_parent) { // single node tree
             *tree = NULL;
          } else if (nd == nd_parent->gauche) {
             nd_parent->gauche = NULL;
          } else if (nd == nd_parent->droite) {
             nd_parent->droite = NULL;
          }
          free(nd);
          return true;
       }
    
       if (NULL != nd->gauche) {
          node* child = position(nd->gauche,cle);
          int val = child->key;
          if (NULL == nd->gauche->droite) {
             nd->gauche = child->gauche;
             free(child);
          } else {
             bool res = arbre_delete(tree,child->key);
          }
          nd->key = val;
          return true;
       }
    
       if (NULL != nd->droite) {
          node* child = position(nd->droite,cle);
          int val = child->key;
          if (NULL == nd->droite->gauche) {
             nd->droite = child->droite;
             free(child);
          } else {
             bool res = arbre_delete(tree,child->key);
          }
          nd->key = val;
          return true;
       }
    }
    
    void arbre_print(arbre tree,int N) {
       if (N <= 0) {
          N = 1;
       }
       if (NULL != tree) {
          arbre_print(tree->droite,N+1);
          for (int i=0;i<N;i++) {
             printf("    ");
          }
          printf("%d\n",tree->key);            
          arbre_print(tree->gauche,N+1);
       } 
    }
    
    bool arbre_search(arbre tree,int cle) {
       node* nd = position(tree,cle);
       return (NULL != nd && cle == nd->key);
    }