diff --git a/source_codes/arbres_binaires/arbre_binaire.adb b/source_codes/arbres_binaires/arbre_binaire.adb new file mode 100644 index 0000000000000000000000000000000000000000..5142a11585283378848f5323409e9a273b40090d --- /dev/null +++ b/source_codes/arbres_binaires/arbre_binaire.adb @@ -0,0 +1,84 @@ +with Ada.Text_Io; use Ada.Text_Io; +with Ada.Unchecked_Deallocation; + +package body Arbre_Binaire is + + procedure Liberer is new Ada.Unchecked_Deallocation(T_Noeud,T_Arbre); + + procedure Position(A : in T_Arbre; + Cle : in Integer; + Nd,Parent : in out T_Arbre) is + begin + if A = null then + raise ARBRE_VIDE; + end if; + while Nd.Cle /= Cle loop + if Cle < Nd.Cle then + exit when Nd.Gauche = null; + Parent := Nd; + Nd := Nd.Gauche; + else + exit when Nd.Droite = null; + Parent := Nd; + Nd := Nd.Droite; + end if; + end loop; + end Position; + + procedure Insert(A : in out T_Arbre; Cle : in Integer) is + Parent : T_Arbre := null; + Nd : T_Arbre := A; + begin + Position(A,Cle,Nd,Parent); + if Cle < Nd.Cle then + Nd.Gauche := new T_Noeud'(Cle,null,null); + elsif Cle > Nd.Cle then + Nd.Droite := new T_Noeud'(Cle,null,null); + else + raise CLE_PRESENTE; + end if; + exception + when ARBRE_VIDE => A := new T_Noeud'(Cle,null,null); + end Insert; + + procedure Delete(A : in out T_Arbre; Cle : in Integer) is + Parent, Tmp : T_Arbre := null; + Nd : T_Arbre := A; + begin + Position(A,Cle,Nd,Parent); + if Cle /= Nd.Cle then + raise CLE_ABSENTE; + end if; + if Nd.Gauche /= null then + Parent := Nd; + Tmp := Nd.Gauche; + Position(Nd.Gauche,Cle,Tmp,Parent); + elsif Nd.Droite /= null then + Parent := Nd; + Tmp := Nd.Droite; + Position(Nd.Droite,Cle,Tmp,Parent); + end if; + Nd.Cle := Tmp.Cle; + if Parent = null then + A := null; + elsif Parent.Gauche = Tmp then + Parent.Gauche := Tmp.Gauche; + else + Parent.Droite := Tmp.Droite; + end if; + Liberer(Tmp); + end Delete; + + function Search(A : T_Arbre; Cle : Integer) return T_Arbre is + Parent : T_Arbre := null; + Nd : T_Arbre := A; + begin + Position(A,Cle,Nd,Parent); + if Cle /= Nd.Cle then + raise CLE_ABSENTE; + end if; + return Nd; + end Search; + +end Arbre_Binaire; + diff --git a/source_codes/arbres_binaires/arbre_binaire.ads b/source_codes/arbres_binaires/arbre_binaire.ads new file mode 100644 index 0000000000000000000000000000000000000000..c54bdf9d4334e08370f0afbda246b0ae37a0a04b --- /dev/null +++ b/source_codes/arbres_binaires/arbre_binaire.ads @@ -0,0 +1,97 @@ +package Arbre_Binaire is + type T_Arbre is private; + -- Fonctionnalités d'un arbre binaire ordonné (fonctions et procédures) + procedure Insert(A : in out T_Arbre; Cle : in Integer); + procedure Delete(A : in out T_Arbre; Cle : in Integer); + function Search(A : T_Arbre; Cle : Integer) return T_Arbre; + -- Exceptions + ARBRE_VIDE : exception; + CLE_ABSENTE : exception; + CLE_PRESENTE : exception; +private + -- Implémentation de la structure d'arbre binaire + type T_Noeud; + type T_Arbre is access T_Noeud; + type T_Noeud is + record + Cle : T_Cle; + Gauche : T_Arbre := null; + Droite : T_Arbre := null; + end record; +end Arbre_Binaire; + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +package Arbre_Binaire is + type T_Arbre is private; + -- Fonctionnalités d'un arbre binaire ordonné (fonctions et procédures) + procedure Insert(A : in out T_Arbre; Cle : in Integer); + procedure Delete(A : in out T_Arbre; Cle : in Integer); + function Search(A : T_Arbre; Cle : Integer) return T_Arbre; + -- Exceptions + ARBRE_VIDE : exception; + CLE_ABSENTE : exception; + CLE_PRESENTE : exception; +private + -- Implémentation de la structure d'arbre binaire + type T_Noeud; + type T_Arbre is access T_Noeud; + type T_Noeud is + record + Cle : T_Cle; + Gauche : T_Arbre := null; + Droite : T_Arbre := null; + end record; +end Arbre_Binaire; + + + + + + + + + + diff --git a/source_codes/arbres_binaires/arbre_binaire.c b/source_codes/arbres_binaires/arbre_binaire.c new file mode 100644 index 0000000000000000000000000000000000000000..920c96f45420b4f97d3c3f130b0f3d5d72035b6b --- /dev/null +++ b/source_codes/arbres_binaires/arbre_binaire.c @@ -0,0 +1,132 @@ +#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); +} + + diff --git a/source_codes/arbres_binaires/arbre_binaire.h b/source_codes/arbres_binaires/arbre_binaire.h new file mode 100644 index 0000000000000000000000000000000000000000..8f35bc8ca014d7cbc4af5767a7dcbe27f3d8c944 --- /dev/null +++ b/source_codes/arbres_binaires/arbre_binaire.h @@ -0,0 +1,38 @@ +#ifndef ARBRE_BINAIRE_H +#define ARBRE_BINAIRE_H + +// Structure pour un arbre binaire +typedef int cle; +typedef struct _node { + cle key; + struct _node* gauche; + struct _node* droite; +} node; +typedef node* arbre; + +// Fonctionnalités pour un arbre binaire ordonné +bool arbre_search(arbre tree,int cle); +bool arbre_insert(arbre* tree,int cle); +bool arbre_delete(arbre* tree,int cle); +void arbre_print(arbre tree,int N); +#endif + + + + + + + + + + + + + + + + + + + + diff --git a/source_codes/arbres_binaires/arbre_binaire_part.adb b/source_codes/arbres_binaires/arbre_binaire_part.adb new file mode 100644 index 0000000000000000000000000000000000000000..06572ee9a6d67b4354798ad0b03db74d85ba3b81 --- /dev/null +++ b/source_codes/arbres_binaires/arbre_binaire_part.adb @@ -0,0 +1,85 @@ +with Ada.Text_Io; use Ada.Text_Io; +with Ada.Unchecked_Deallocation; + +package body Arbre_Binaire is + + procedure Liberer is new Ada.Unchecked_Deallocation(T_Noeud,T_Arbre); + + procedure Position(A : in T_Arbre; + Cle : in Integer; + Nd,Parent : in out T_Arbre) is + begin + if A = null then + raise ARBRE_VIDE; + end if; + while Nd.Cle /= Cle loop + if Cle < Nd.Cle then + exit when Nd.Gauche = null; + Parent := Nd; + Nd := Nd.Gauche; + else + exit when Nd.Droite = null; + Parent := Nd; + Nd := Nd.Droite; + end if; + end loop; + end Position; + + procedure Insert(A : in out T_Arbre; Cle : in Integer) is + Parent : T_Arbre := null; + Nd : T_Arbre := A; + begin + Position(A,Cle,Nd,Parent); + if Cle < Nd.Cle then + Nd.Gauche := new T_Noeud'(Cle,null,null); + elsif Cle > Nd.Cle then + Nd.Droite := new T_Noeud'(Cle,null,null); + else + raise CLE_PRESENTE; + end if; + exception + when ARBRE_VIDE => A := new T_Noeud'(Cle,null,null); + end Insert; + + procedure Delete(A : in out T_Arbre; Cle : in Integer) is + Parent, Tmp : T_Arbre := null; + Nd : T_Arbre := A; + begin + Position(A,Cle,Nd,Parent); + if Cle /= Nd.Cle then + raise CLE_ABSENTE; + end if; + if Nd.Gauche /= null then + -- à compléter + null; + elsif Nd.Droite /= null then + -- à compléter + null; + end if; + Nd.Cle := Tmp.Cle; + if Parent = null then + -- à compléter + null; + elsif Parent.Gauche = Tmp then + -- à compléter + null; + else + -- à compléter + null; + end if; + Liberer(Tmp); + end Delete; + + function Search(A : T_Arbre; Cle : Integer) return T_Arbre is + Parent : T_Arbre := null; + Nd : T_Arbre := A; + begin + Position(A,Cle,Nd,Parent); + if Cle /= Nd.Cle then + raise CLE_ABSENTE; + end if; + return Nd; + end Search; + +end Arbre_Binaire; + diff --git a/source_codes/arbres_binaires/bin_tree.c b/source_codes/arbres_binaires/bin_tree.c new file mode 100644 index 0000000000000000000000000000000000000000..9949dd84ac9ab2275bc4ec57d56868bad6cc225d --- /dev/null +++ b/source_codes/arbres_binaires/bin_tree.c @@ -0,0 +1,148 @@ +#include <stdio.h> +#include <stdlib.h> +#include <assert.h> +#include <stdbool.h> +#include <math.h> +#include "bin_tree.h" + +static node* position(arbre tree,cle c) { + node* crt = tree; + if (NULL != crt) { + while (c != crt->key + && NULL != crt->child[(c > crt->key)]) { + crt = crt->child[(c > crt->key)]; + } + } + return crt; +} + +bool arbre_is_empty(arbre tree) { + return NULL == tree; +} + +int arbre_depth(arbre tree) { + if (arbre_is_empty(tree)) { + return 0; + } else { + return 1+fmax(arbre_depth(tree->child[0]),arbre_depth(tree->child[1])); + } +} + +int arbre_size(arbre tree) { + if (arbre_is_empty(tree)) { + return 0; + } else { + return 1+arbre_size(tree->child[0])+arbre_size(tree->child[1]); + } +} + +static node* create_node(int val) { + node* nd = calloc(1,sizeof(node)); + nd->key = val; + return nd; +} + +arbre arbre_insert(arbre tree,cle c) { + if (arbre_is_empty(tree)) { + tree = create_node(c); + } else { + node* nd = position(tree,c); + nd->child[(c > nd->key)] = create_node(c); + } + return tree; +} + +static node* parent(arbre tree,node* nd) { + assert(!arbre_is_empty(tree) && NULL != nd); + node* parent = NULL; + if (nd != tree) { + node* crt = tree; + cle c = nd->key; + do { + parent = crt; + if (c != crt->key) { + crt = crt->child[(c > crt->key)]; + } + } while (crt != nd); + } + return parent; +} + +bool arbre_search(arbre tree,cle c) { + node* nd = position(tree,c); + return (NULL != nd && c == nd->key); +} + +bool arbre_delete(arbre* tree,cle c) { + node* nd = position(*tree,c); + if (NULL == nd || c != nd->key) { + return false; + } + // noeud terminal ou avec 1 enfant + if (arbre_is_empty(nd->child[0]) || arbre_is_empty(nd->child[1])) { + node* nd_parent = parent(*tree,nd); + if (NULL == nd_parent) { // nd est la racine + *tree = nd->child[(NULL != nd->child[1])]; + } else { + nd_parent->child[(nd == nd_parent->child[1])] + = nd->child[(NULL != nd->child[1])]; + } + free(nd); + } else { // noeud interne (2 enfants) + node* next = position(nd->child[1],c); //next a 0 ou 1 enfant + cle tmp = next->key; + bool res = arbre_delete(tree,tmp); + nd->key = tmp; + } + return true; +} + +bool arbre_delete_bis(arbre* tree,cle c) { + node* nd = position(*tree,c); + + if (NULL == nd || c != nd->key) { + return false; + } + + // terminal node + if (arbre_is_empty(nd->child[0]) && arbre_is_empty(nd->child[1])) { + node* nd_parent = parent(*tree,nd); + if (NULL == nd_parent) { // single node tree + *tree = NULL; + } else { + nd_parent->child[(nd == nd_parent->child[1])] = NULL; + } + free(nd); + return true; + } + + for (int ind=0;ind<2;ind++) { + if (!arbre_is_empty(nd->child[ind])) { + node* next = position(nd->child[ind],c); + int val = next->key; + if (NULL == nd->child[ind]->child[ind^1]) { + nd->child[ind] = next->child[ind]; + free(next); + } else { + bool res = arbre_delete(tree,next->key); + } + nd->key = val; + return true; + } + } +} + + +void arbre_print(arbre tree,int N) { + if (N <= 0) { + N = 1; + } + if (NULL != tree) { + arbre_print(tree->child[1],N+1); + for (int i=0;i<N;i++) { + printf(" "); + } + printf("%d\n",tree->key); + arbre_print(tree->child[0],N+1); + } +} diff --git a/source_codes/arbres_binaires/bin_tree.h b/source_codes/arbres_binaires/bin_tree.h new file mode 100644 index 0000000000000000000000000000000000000000..a1ee64b64e5fb47f4bf8e2f7f854545bb7a8c739 --- /dev/null +++ b/source_codes/arbres_binaires/bin_tree.h @@ -0,0 +1,40 @@ +#ifndef BIN_TREE_H +#define BIN_TREE_H + +// Structure pour un arbre binaire +typedef int cle; +typedef struct _node { + cle key; + struct _node* child[2]; +} node; +typedef node* arbre; + +// Fonctionnalités pour un arbre binaire ordonné +bool arbre_is_empty(arbre tree); +bool arbre_search(arbre tree,cle c); +bool arbre_insert(arbre* tree,cle c); +bool arbre_delete(arbre* tree,cle c); +void arbre_print(arbre tree,int N); +int arbre_depth(arbre tree); +int arbre_size(arbre tree); +#endif + + + + + + + + + + + + + + + + + + + + diff --git a/source_codes/arbres_binaires/bin_tree_main.c b/source_codes/arbres_binaires/bin_tree_main.c new file mode 100644 index 0000000000000000000000000000000000000000..f9f310738c30074a22b2b2499cd2ed1840b888f5 --- /dev/null +++ b/source_codes/arbres_binaires/bin_tree_main.c @@ -0,0 +1,24 @@ +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include "bin_tree.h" + +void main() { + bool b; + int val; + arbre tree = NULL; + arbre_print(tree,1); + do { + printf("insert val = "); + scanf("%d",&val); + b = arbre_insert_rec(&tree,val); + arbre_print(tree,1); + } while (b); + node* nd; + do { + printf("delete val = "); + scanf("%d",&val); + b = arbre_delete(&tree,val); + arbre_print(tree,1); + } while (NULL != tree); +} diff --git a/source_codes/arbres_binaires/bin_tree_main_rec.c b/source_codes/arbres_binaires/bin_tree_main_rec.c new file mode 100644 index 0000000000000000000000000000000000000000..6cc51917b720abc14c896536309f135773042f40 --- /dev/null +++ b/source_codes/arbres_binaires/bin_tree_main_rec.c @@ -0,0 +1,36 @@ +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include "bin_tree_rec.h" + +void main() { + int val; + arbre tree = NULL; + arbre_print(tree,1); + do { + printf("insert val = "); + scanf("%d",&val); + if (arbre_search(tree,val)) break; + tree = arbre_insert(tree,val); + arbre_print(tree,1); + } while (true); + + do { + printf("find val = "); + scanf("%d",&val); + if (!arbre_search(tree,val)) { + printf("not found\n"); + break; + } + printf("found\n"); + arbre_print(tree,1); + } while (true); + + node* nd; + do { + printf("delete val = "); + scanf("%d",&val); + tree = arbre_delete(tree,val); + arbre_print(tree,1); + } while (!arbre_is_empty(tree)); +} diff --git a/source_codes/arbres_binaires/bin_tree_part.c b/source_codes/arbres_binaires/bin_tree_part.c new file mode 100644 index 0000000000000000000000000000000000000000..d27ee573a350da9ed2fdce4913d1ebc29977ef0d --- /dev/null +++ b/source_codes/arbres_binaires/bin_tree_part.c @@ -0,0 +1,109 @@ +#include <stdio.h> +#include <stdlib.h> +#include <assert.h> +#include <stdbool.h> +#include "bin_tree.h" + +static node* position(arbre tree,int cle) { + node* crt = tree; + if (NULL != crt) { + while (cle != crt->key + && NULL != crt->child[(cle > crt->key)]) { + crt = crt->child[(cle > crt->key)]; + } + } + return crt; +} + +static node* parent(arbre tree,node* nd) { + assert(NULL != tree && NULL != nd); + node* parent = NULL; + int cle = nd->key; + if (nd != tree) { + node* crt = tree; + do { + //à compléter + } while (crt != nd); + } + return parent; +} + +int arbre_depth(arbre tree) { + if (NULL == tree) { + return 0; + } else { + return 1+fmax(arbre_depth(tree->child[0]),arbre_depth(tree->child[1])); + } +} + +int arbre_size(arbre tree) { + //à compléter + return 0; +} + +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) { + //à compléter + } else { + return false; + } + } + return true; +} + +bool arbre_delete(arbre* tree,int cle) { + node* nd = position(*tree,cle); + if (NULL == nd || cle != nd->key) { + return false; + } + // noeud terminal + if (NULL == nd->child[0] && NULL == nd->child[1]) { + node* nd_parent = parent(*tree,nd); + // à compléter: + // considérer les cas de l'arbre à un seul/plusieurs noeuds + free(nd); + return true; + } + + // noeud interne et récursion + for (int ind=0;ind<2;ind++) { + if (NULL != nd->child[ind]) { + node* next = position(nd->child[ind],cle); + int val = next->key; + if (NULL == nd->child[ind]->child[ind^1]) { + nd->child[ind] = next->child[ind]; + free(next); + } else { + bool res = arbre_delete(tree,next->key); + } + nd->key = val; + return true; + } + } +} + +void arbre_print(arbre tree,int N) { + if (N <= 0) { + N = 1; + } + if (NULL != tree) { + arbre_print(tree->child[1],N+1); + for (int i=0;i<N;i++) { + printf(" "); + } + printf("%d\n",tree->key); + arbre_print(tree->child[0],N+1); + } +} + +bool arbre_search(arbre tree,int cle) { + // à compléter + return true; +} + + diff --git a/source_codes/arbres_binaires/bin_tree_rec.c b/source_codes/arbres_binaires/bin_tree_rec.c new file mode 100644 index 0000000000000000000000000000000000000000..a49ce6d6cb1628261385d0a5f42fdbb94b4f4349 --- /dev/null +++ b/source_codes/arbres_binaires/bin_tree_rec.c @@ -0,0 +1,91 @@ +#include <stdio.h> +#include <stdlib.h> +#include <assert.h> +#include <stdbool.h> +#include <math.h> +#include "bin_tree_rec.h" + +static node* create_node(int val); +static node* position(arbre tree,cle c); + +bool arbre_is_empty(arbre tree) { + return NULL == tree; +} + +int arbre_depth(arbre tree) { + if (arbre_is_empty(tree)) { + return 0; + } else { + return 1+fmax(arbre_depth(tree->child[0]),arbre_depth(tree->child[1])); + } +} + +int arbre_size(arbre tree) { + if (arbre_is_empty(tree)) { + return 0; + } else { + return 1+arbre_size(tree->child[0])+arbre_size(tree->child[1]); + } +} + +arbre arbre_insert(arbre tree,cle c) { + if (arbre_is_empty(tree)) { + return create_node(c); + } + node* nd = position(tree,c); + nd->child[(c > nd->key)] = create_node(c); + return tree; +} + +bool arbre_search(arbre tree,cle c) { + node* nd = position(tree,c); + return (NULL != nd && c == nd->key); +} + +arbre arbre_delete(arbre tree,cle c) { + if (!arbre_is_empty(tree)) { + if (c != tree->key) { + tree->child[(c > tree->key)] = arbre_delete(tree->child[(c > tree->key)],c); + } else if (!arbre_is_empty(tree->child[0]) + && !arbre_is_empty(tree->child[1])) { // noeud interne (2 enfants) + node* next = position(tree->child[1],c); + tree->key = next->key; + tree->child[1] = arbre_delete(tree->child[1],next->key); + } else { + node* nd = tree; + tree = tree->child[!arbre_is_empty(tree->child[1])]; + free(nd); + } + } + return tree; +} + +void arbre_print(arbre tree,int N) { + if (N <= 0) { + N = 1; + } + if (NULL != tree) { + arbre_print(tree->child[1],N+1); + for (int i=0;i<N;i++) { + printf(" "); + } + printf("%d\n",tree->key); + arbre_print(tree->child[0],N+1); + } +} + +static node* create_node(int val) { + node* nd = calloc(1,sizeof(node)); + nd->key = val; + return nd; +} + +static node* position(arbre tree,cle c) { + if (!arbre_is_empty(tree)) { + if (c != tree->key && !arbre_is_empty(tree->child[(c > tree->key)])) { + return position(tree->child[(c > tree->key)],c); + } + } + return tree; +} + diff --git a/source_codes/arbres_binaires/bin_tree_rec.h b/source_codes/arbres_binaires/bin_tree_rec.h new file mode 100644 index 0000000000000000000000000000000000000000..439c006e0d788933b3de40fa9864e574eee891b1 --- /dev/null +++ b/source_codes/arbres_binaires/bin_tree_rec.h @@ -0,0 +1,40 @@ +#ifndef BIN_TREE_H +#define BIN_TREE_H + +// Structure pour un arbre binaire +typedef int cle; +typedef struct _node { + cle key; + struct _node* child[2]; +} node; +typedef node* arbre; + +// Fonctionnalités pour un arbre binaire ordonné +bool arbre_is_empty(arbre tree); +bool arbre_search(arbre tree,cle c); +arbre arbre_insert(arbre tree,cle c); +arbre arbre_delete(arbre tree,cle c); +void arbre_print(arbre tree,int N); +int arbre_depth(arbre tree); +int arbre_size(arbre tree); +#endif + + + + + + + + + + + + + + + + + + + + diff --git a/source_codes/arbres_binaires/main.c b/source_codes/arbres_binaires/main.c new file mode 100644 index 0000000000000000000000000000000000000000..10ea9f04cf554ec2cd8443c6028d356ce569cbe4 --- /dev/null +++ b/source_codes/arbres_binaires/main.c @@ -0,0 +1,24 @@ +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include "bin_tree.h" + +void main() { + bool b; + int val; + arbre tree = NULL; + arbre_print(tree,1); + do { + printf("insert val = "); + scanf("%d",&val); + b = arbre_insert(&tree,val); + arbre_print(tree,1); + } while (b); + node* nd; + do { + printf("delete val = "); + scanf("%d",&val); + b = arbre_delete(&tree,val); + arbre_print(tree,1); + } while (NULL != tree); +} diff --git a/source_codes/arbres_binaires/tree_insert.adb b/source_codes/arbres_binaires/tree_insert.adb new file mode 100644 index 0000000000000000000000000000000000000000..2bb6f73aa7034e97463b3281c06e2ae2d4834ab2 --- /dev/null +++ b/source_codes/arbres_binaires/tree_insert.adb @@ -0,0 +1,38 @@ + + +procedure Tree_Insert is + + type T_Noeud; + type T_Arbre is access T_Noeud; + type T_Noeud is record + Info : Integer; + Sag : T_Arbre; + Sad : T_Arbre; + end record; + + procedure Insert(A : in out T_Arbre; + I : in Integer) is + Crt : T_Arbre := A; + begin + if A = null then + A := new T_Noeud'(I,null,null); + else + while I /= Crt.Info loop + if I > Crt.Info then + if Crt.Sag = null then + Crt.Sag := new T_Noeud'(I,null,null); + end if; + Crt := Crt.Sag; + elsif I < Crt.Info then + if Crt.Sad = null then + Crt.Sad := new T_Noeud'(I,null,null); + end if; + Crt := Crt.Sad; + end if; + end loop; + end if; + end Insert; + +begin + null; +end Tree_Insert; diff --git a/source_codes/arbres_binaires/tree_search.c b/source_codes/arbres_binaires/tree_search.c new file mode 100644 index 0000000000000000000000000000000000000000..1217ed323c86039affb49c5eaf1fa109eeefd539 --- /dev/null +++ b/source_codes/arbres_binaires/tree_search.c @@ -0,0 +1,31 @@ +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> + +typedef int cle; +typedef struct _node { + cle key; + struct _node* gauche; + struct _node* droite; +} node; +typedef node* arbre; + +arbre search(cle X,arbre tree) { + bool success = false; + arbre courant = tree; + while (NULL != courant && !success) { + if (courant->key > X) { + courant = courant->gauche; + } else if (courant->key < X){ + courant = courant->droite; + } else { + success = true; + } + } + return courant; +} + +void main() { + arbre tree; + arbre t = search(15,tree); +} diff --git a/source_codes/sorting/quicksort/Makefile b/source_codes/sorting/quicksort/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..927fce8eaec2e3f81c8804eb06c2e92709dbbf86 --- /dev/null +++ b/source_codes/sorting/quicksort/Makefile @@ -0,0 +1,21 @@ +CC:=gcc +# SAN:=-fsanitize=address +CFLAGS:=-Wall -Wextra -pedantic -g $(SAN) +LDFLAGS:=-lm $(SAN) + +all: quicksort quicksort_part + +quicksort: quicksort.c + $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) + @echo $@ >> .gitignore + +quicksort_part: quicksort_part.c + $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) + @echo $@ >> .gitignore + + +.PHONY: clean all + +clean: + rm -f *.o quicksort_part quicksort .gitignore + diff --git a/source_codes/sorting/quicksort/quicksort.c b/source_codes/sorting/quicksort/quicksort.c new file mode 100644 index 0000000000000000000000000000000000000000..af95cb590c2c039a5612fabeb7ef7e5df081f3d2 --- /dev/null +++ b/source_codes/sorting/quicksort/quicksort.c @@ -0,0 +1,83 @@ +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <assert.h> + +void print(int size,int tab[size]) { + for (int i=0;i<size;i++) { + printf("%d ",tab[i]); + } +} + +void random_tab(int size,int tab[size],int inf,int sup) { + assert(sup > inf); + for (int i=0;i<size;i++) { + tab[i] = inf+rand()%(sup-inf); + } +} + +void swap(int* p_a,int* p_b) { + int tmp = *p_a; + *p_a = *p_b; + *p_b = tmp; +} + +int partition(int size,int array[size],int first,int last) { + int pivot = array[last]; + int i = first-1,j = last; + do { + do { + i++; + } while (array[i] < pivot && i<j); + do { + j--; + } while(array[j] > pivot && i<j); + if (j>i) { + swap(&array[i],&array[j]); + } + } while (j > i); + swap(&array[i],&array[last]); + return i; +} + +void quicksort(int size,int array[size],int first,int last) { + if (first < last) { + int midpoint = partition(size,array,first,last); + if (first < midpoint-1) { + quicksort(size,array,first,midpoint-1); + } + if (midpoint+1 < last) { + quicksort(size,array,midpoint+1,last); + } + } +} + +void test_ordre(int size,int array[size]) { + for (int i=0;i<size-1;i++) { + if (array[i] > array[i+1]) { + printf("erreur"); + return; + } + } + printf("ok"); +} + +int main(int argc,char** argv) { + if (2 != argc) { + return 1; + } + int size = atoi(argv[1]); + int seed = atoi(argv[2]); + srand(seed); + int* res = malloc(size*sizeof(int)); + for (int k=0;k<20;k++) { + random_tab(size,res,0,100); + print(size,res); + printf("\n"); + quicksort(size,res,0,size-1); + print(size,res); + test_ordre(size,res); + printf("\n================\n"); + } +} + diff --git a/source_codes/sorting/quicksort/quicksort_part.c b/source_codes/sorting/quicksort/quicksort_part.c new file mode 100644 index 0000000000000000000000000000000000000000..67b79b7b790de4e0f71f99465cfc445891c7d621 --- /dev/null +++ b/source_codes/sorting/quicksort/quicksort_part.c @@ -0,0 +1,97 @@ +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> + +void print(int size,int tab[size]) { + for (int i=0;i<size;i++) { + printf("%d ",tab[i]); + } + printf("\n"); +} + +void random_tab(int size,int tab[size],int inf,int sup) { + for (int i=0;i<size;i++) { + tab[i] = inf+rand()%(sup-inf); + } +} + +void swap(int* p_a,int* p_b) { + int tmp = *p_a; + *p_a = *p_b; + *p_b = tmp; +} + +// Partition du tableau <array> autour d'une valeur pivot: +// compléter le code +int partition(int size,int array[size],int first,int last) { + int pivot = array[last]; + int i = first-1,j = last; + do { + // à compléter pour <i>: do {...} while (...); + // à compléter pour <j>: do {...} while (...); + // à compléter: échanger cases <i> et <j> du tableau <array> + } while (j > i); + // à compléter: échanger cases <i> et <last> du tableau <array> + return i; +} + +// Tri rapide récursif +void quicksort(int size,int array[size],int first,int last) { + if (first < last) { + int midpoint = partition(size,array,first,last); + if (first < midpoint-1) { + quicksort(size,array,first,midpoint-1); + } + if (midpoint+1 < last) { + quicksort(size,array,midpoint+1,last); + } + } +} + +// Test si le tableau <array> est ordonné croissant +void test_ordre(int size,int array[size]) { + for (int i=0;i<size-1;i++) { + if (array[i] > array[i+1]) { + printf("erreur"); + return; + } + } + printf("ok"); +} + +int main(int argc,char** argv) { + if (2 != argc) { + return 1; + } + int size = atoi(argv[1]); + int seed = atoi(argv[2]); + srand(seed); + int* res = (int*)malloc(size*sizeof(int)); + for (int k=0;k<20;k++) { + random_tab(size,res,0,100); + print(size,res); + quicksort(size,res,0,size-1); + print(size,res); + test_ordre(size,res); + printf("\n================\n"); + } +} + + + + + + + + + + + + + + + + + + +