diff --git a/source_codes/arbres_AVL/avl.adb b/source_codes/arbres_AVL/avl.adb deleted file mode 100644 index b0e1e42d683bb85e5f2cf87deee91dea541231c9..0000000000000000000000000000000000000000 --- a/source_codes/arbres_AVL/avl.adb +++ /dev/null @@ -1,323 +0,0 @@ -with Ada.Text_IO; use Ada.Text_IO; - -procedure avl is - -- paquetage pour l'�criture en x-�me ligne et y-�me colonne - package Entier_ES is new Integer_IO(Integer); - use Entier_ES; - - Niv_Max : constant := 6; - - subtype T_Donnee is Integer; - - type T_Noeud; - type T_Arbre is access T_Noeud; - type T_Noeud is record - Cle : T_Donnee; - Gauche,Droite : T_Arbre; - Hg,Hd : Natural := 0; - end record; - - Cle_Presente : exception; - ------------------------------------------------------------- --- fonction qui donne le nombre max entre 2 nombres -- ------------------------------------------------------------- - function Max(H1,H2 : Integer) return Integer is - begin - if H1 > H2 then - return H1; - else - return H2; - end if; - end Max; - ------------------------------------------------------------- --- fonction qui retourne le premier noeud d�s�quilibr� et -- --- met � jour les hauteurs sur le chemin d'insertion -- ------------------------------------------------------------- - function Desequilibre(Arbre : in T_Arbre; - Donnee : in T_Donnee) return T_Arbre is - Temp : T_Arbre := Arbre; - Parent : T_Arbre; - Hd_Prec,Hg_Prec : Integer := 0; - begin - while Temp.Cle /= Donnee loop - if abs(Temp.Hd - Temp.Hg) = 2 then - Parent := Temp; - Hd_Prec := Parent.Hd; - Hg_Prec := Parent.Hg; - if Donnee <= Parent.Cle then - Parent.Hg := Parent.Hg - 1; - else - Parent.Hd := Parent.Hd - 1; - end if; - end if; - if Donnee <= Temp.Cle then - Temp := Temp.Gauche; - else - Temp := Temp.Droite; - end if; - end loop; - if Parent /= null then - Parent.Hd := Hd_Prec; - Parent.Hg := Hg_Prec; - end if; - return Parent; - end Desequilibre; - - procedure Imprimer(Arbre : in T_Arbre; N : in Positive) is - begin - if Arbre /= null then - Imprimer(Arbre.Droite,N+1); - for I in 1..N loop - Put(" "); - end loop; - Put(Arbre.Cle); - Put("(" & Integer'Image(Arbre.Hg) & "," & Integer'Image(Arbre.hd) & ")"); - New_Line; - Imprimer(Arbre.Gauche,N+1); - end if; - end Imprimer; - ------------------------------------------------------------- --- fonction qui calcule les hauteurs d'un arbre -- ------------------------------------------------------------- - function Hauteur(Noeud : in T_Arbre) return Natural is - begin - if Noeud = null then - return 0; - else - return 1+Max(Hauteur(Noeud.Gauche),Hauteur(Noeud.Droite)); - end if; - end Hauteur; - ------------------------------------------------------------- --- procedure qui remet � jour les hauteurs des noeuds -- --- apr�s le r��quilibrage -- ------------------------------------------------------------- - procedure Hauteurs(Noeud : in T_Arbre) is - begin - if Noeud.Gauche /= null then - Hauteurs(Noeud.Gauche); - Noeud.Hg := 1+Max(Noeud.Gauche.Hg,Noeud.Gauche.Hd); - else - Noeud.Hg := 0; - end if; - if Noeud.Droite /= null then - Hauteurs(Noeud.Droite); - Noeud.Hd := 1+Max(Noeud.Droite.Hg,Noeud.Droite.Hd); - else - Noeud.Hd := 0; - end if; - end Hauteurs; - ------------------------------------------------------------- --- procedure qui remet � jour les hauteurs des noeuds -- --- apr�s le r��quilibrage -- ------------------------------------------------------------- - procedure Hauteur_Noeud(Noeud : in T_Arbre) is - begin - if Noeud.Gauche /= null then - Noeud.Hg := 1+Max(Noeud.Gauche.Hg,Noeud.Gauche.Hd); - else - Noeud.Hg := 0; - end if; - if Noeud.Droite /= null then - Noeud.Hd := 1+Max(Noeud.Droite.Hg,Noeud.Droite.Hd); - else - Noeud.Hd := 0; - end if; - end Hauteur_Noeud; - ------------------------------------------------------------- --- fonction qui effectue une rotation gauche sur le -- --- noeud d�s�quilibr� -- ------------------------------------------------------------- - function Rot_G(P : T_Arbre) return T_Arbre is - Q : T_Arbre := null; - begin - if P /= null then - Q := P.Droite; - P.Droite := Q.Gauche; - Q.Gauche := P; - Hauteur_Noeud(P); - Hauteur_Noeud(Q); - end if; - return Q; - end Rot_G; - ------------------------------------------------------------- --- fonction qui effectue une rotation droite sur le -- --- noeud d�s�quilibr� -- ------------------------------------------------------------- - function Rot_D(P : T_Arbre) return T_Arbre is - Q : T_Arbre := null; - begin - if P /= null then - Q := P.Gauche; - P.Gauche := Q.Droite; - Q.Droite := P; - Hauteur_Noeud(P); - Hauteur_Noeud(Q); - end if; - return Q; - end Rot_D; - ------------------------------------------------------------- --- procedure qui effectue une double rotation gauche -- --- droite sur le noeud d�s�quilibr� -- ------------------------------------------------------------- - function Rot_GD(Noeud : T_Arbre) return T_Arbre is - begin - Noeud.Gauche := Rot_G(Noeud.Gauche); - return Rot_D(Noeud); - end Rot_GD; - ------------------------------------------------------------- --- procedure qui effectue une rotation gauche sur le -- --- noeud d�s�quilibr� -- ------------------------------------------------------------- - procedure Rotation_G(Noeud : in T_Arbre) is - New_Noeud : T_Arbre; - begin - New_Noeud := new T_Noeud'(Noeud.Cle, - Noeud.Gauche, - Noeud.Droite.Gauche, - 0,0); - Noeud.Cle := Noeud.Droite.Cle; - Noeud.Gauche := New_Noeud; - Noeud.Droite := Noeud.Droite.Droite; - Hauteur_Noeud(New_Noeud); - Hauteur_Noeud(Noeud); - end Rotation_G; - - - - - ------------------------------------------------------------- --- procedure qui effectue une double rotation droite -- --- gauche sur le noeud d�s�quilibr� -- ------------------------------------------------------------- - procedure Rot_DG(Noeud : in T_Arbre) is - begin - Noeud.Droite := Rot_D(Noeud.Droite); - return Rot_G(Noeud); - end Rot_DG; - ------------------------------------------------------------- --- procedure qui effectue une rotation droite sur le -- --- noeud d�s�quilibr� -- ------------------------------------------------------------- - procedure Rotation_D(Noeud : in T_Arbre) is - New_Noeud : T_Arbre; - begin - New_Noeud := new T_Noeud'(Noeud.Cle, - Noeud.Gauche.Droite, - Noeud.Droite, - 0,0); - Noeud.Cle := Noeud.Gauche.Cle; - Noeud.Droite := New_Noeud; - Noeud.Gauche := Noeud.Gauche.Gauche; - Hauteur_Noeud(New_Noeud); - Hauteur_Noeud(Noeud); - end Rotation_D; - ------------------------------------------------------------- --- procedure qui effectue une double rotation gauche -- --- droite sur le noeud d�s�quilibr� -- ------------------------------------------------------------- - procedure Rotation_GD(Noeud : in T_Arbre) is - begin - Rotation_G(Noeud.Gauche); - Rotation_D(Noeud); - end Rotation_GD; - ------------------------------------------------------------- --- procedure qui effectue une double rotation droite -- --- gauche sur le noeud d�s�quilibr� -- ------------------------------------------------------------- - procedure Rotation_DG(Noeud : in T_Arbre) is - begin - Rotation_D(Noeud.Droite); - Rotation_G(Noeud); - end Rotation_DG; - ------------------------------------------------------------- --- procedure qui r��quilibre l'arbre -- ------------------------------------------------------------- - procedure Reequilibre(Noeud : in T_Arbre) is - begin - case (Noeud.Hd - Noeud.Hg) is - when +2 => - case (Noeud.Droite.Hd - Noeud.Droite.Hg) is - when +1 => Rotation_G(Noeud); - when others => Rotation_DG(Noeud); - end case; - when others => - case (Noeud.Gauche.Hd - Noeud.Gauche.Hg) is - when -1 => Rotation_D(Noeud); - when others => Rotation_GD(Noeud); - end case; - end case; - end Reequilibre; - ------------------------------------------------------------- --- procedure qui ins�re une valeur dans l'arbre -- ------------------------------------------------------------- - procedure Insertion(Donnee : in T_Donnee; - Arbre : in out T_Arbre; - Deseq : in out T_Arbre) is - begin - if Arbre = null then - Arbre := new T_Noeud'(Donnee,null,null,0,0); - Deseq := null; - else - if Donnee = Arbre.Cle then - raise Cle_Presente; - elsif Donnee < Arbre.Cle then - Insertion(Donnee, Arbre.Gauche,Deseq); - if Deseq = null then - Arbre.Hg := 1 + Max(Arbre.Gauche.Hg,Arbre.Gauche.Hd); - end if; - else - Insertion(Donnee, Arbre.Droite,Deseq); - if Deseq = null then - Arbre.Hd := 1 + Max(Arbre.Droite.Hg,Arbre.Droite.Hd); - end if; - end if; - if abs(Arbre.Hd - Arbre.Hg) = 2 and Deseq = null then - Deseq := Arbre; - end if; - end if; - end Insertion; - --- Procedure principale - Mon_Arbre : T_Arbre; - Str : String(1..80) := (others => Ascii.Nul); - Taille : Natural; - Nb : Integer; - Deseq : T_Arbre; - -begin - loop - begin - Put("Nombre: "); - Get_Line(Str,Taille); - exit when Taille = 0; - Nb := Integer'Value(Str(1..Taille)); - Insertion(Nb,Mon_Arbre,Deseq); - if Deseq /= null then - Put_Line("Desequilibre!"); - Reequilibre(Deseq); - end if; - New_Line; - --Hauteurs(Mon_Arbre); - Imprimer(Mon_Arbre,1); - New_Line; - exception - when Cle_Presente => - Put_Line("Cl� d�j� pr�sente!"); - end; - end loop; -end avl; diff --git a/source_codes/arbres_binaires/.gitignore b/source_codes/arbres_binaires/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..87605a2522b98767e8215384d43a1d4012b565d8 --- /dev/null +++ b/source_codes/arbres_binaires/.gitignore @@ -0,0 +1,9 @@ +bin_tree.o +main +tree_search +bin_tree_main +arbre_binaire.o +bin_tree_main.o +bin_tree_part.o +main.o +tree_search.o diff --git a/source_codes/arbres_binaires/Makefile b/source_codes/arbres_binaires/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..7cd861be0f1453bdb49299c3f4e59bdc29d2ca78 --- /dev/null +++ b/source_codes/arbres_binaires/Makefile @@ -0,0 +1,33 @@ +CC:=gcc +# SAN:=-fsanitize=address +CFLAGS:=-Wall -Wextra -pedantic -g $(SAN) +LDFLAGS:=-lm $(SAN) +SOURCES := $(wildcard *.c) +OBJECTS := $(patsubst %.c, %.o, $(SOURCES)) + +all: main tree_search bin_tree_main $(OBJECTS) + +tree_search: tree_search.c bin_tree.o + $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) + @echo $@ >> .gitignore + +bin_tree_main: bin_tree_main.c bin_tree.o + $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) + @echo $@ >> .gitignore + +main: main.c bin_tree.o + $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) + @echo $@ >> .gitignore + +%.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ + @echo $@ >> .gitignore + +bin_tree.o: bin_tree.h +bin_tree_rec.o: bin_tree_rec.h + +.PHONY: clean all + +clean: + rm -f *.o main tree_seach bin_tree_main .gitignore + diff --git a/source_codes/arbres_binaires/arbre_binaire.adb b/source_codes/arbres_binaires/arbre_binaire.adb deleted file mode 100644 index 5142a11585283378848f5323409e9a273b40090d..0000000000000000000000000000000000000000 --- a/source_codes/arbres_binaires/arbre_binaire.adb +++ /dev/null @@ -1,84 +0,0 @@ -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 deleted file mode 100644 index c54bdf9d4334e08370f0afbda246b0ae37a0a04b..0000000000000000000000000000000000000000 --- a/source_codes/arbres_binaires/arbre_binaire.ads +++ /dev/null @@ -1,97 +0,0 @@ -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 index 920c96f45420b4f97d3c3f130b0f3d5d72035b6b..0ad59ad09fc2d9ef4a7875d55470846616145774 100644 --- a/source_codes/arbres_binaires/arbre_binaire.c +++ b/source_codes/arbres_binaires/arbre_binaire.c @@ -1,132 +1,130 @@ -#include <stdio.h> -#include <stdlib.h> +#include "arbre_binaire.h" #include <assert.h> #include <stdbool.h> -#include "arbre_binaire.h" +#include <stdio.h> +#include <stdlib.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; +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; } - crt = crt->droite; - } - } while (crt->key != cle); - } - return crt; + } 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; +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; +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); -bool arbre_delete(arbre* tree,int cle) { - node* nd = position(*tree,cle); - - if (NULL == nd || cle != nd->key) { - return false; - } + 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; - } + // 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->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; - } + 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); - } +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); +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 index 8f35bc8ca014d7cbc4af5767a7dcbe27f3d8c944..ca616c6d0803fb5d3550373cdf9dfb56e98b613f 100644 --- a/source_codes/arbres_binaires/arbre_binaire.h +++ b/source_codes/arbres_binaires/arbre_binaire.h @@ -1,38 +1,21 @@ #ifndef ARBRE_BINAIRE_H #define ARBRE_BINAIRE_H +#include <stdbool.h> + // Structure pour un arbre binaire typedef int cle; typedef struct _node { - cle key; - struct _node* gauche; - struct _node* droite; + cle key; + struct _node *gauche; + struct _node *droite; } node; -typedef node* arbre; +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 - - - - - - - - - - - - - - - - - - - +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 deleted file mode 100644 index 06572ee9a6d67b4354798ad0b03db74d85ba3b81..0000000000000000000000000000000000000000 --- a/source_codes/arbres_binaires/arbre_binaire_part.adb +++ /dev/null @@ -1,85 +0,0 @@ -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 index 9949dd84ac9ab2275bc4ec57d56868bad6cc225d..613040e8b13e783d7e36d0fc654cf5709591e44f 100644 --- a/source_codes/arbres_binaires/bin_tree.c +++ b/source_codes/arbres_binaires/bin_tree.c @@ -1,148 +1,147 @@ -#include <stdio.h> -#include <stdlib.h> +#include "bin_tree.h" #include <assert.h> -#include <stdbool.h> #include <math.h> -#include "bin_tree.h" +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.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; +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; + 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])); - } + 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]); - } + 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; +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; +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; +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_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(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; - } +bool arbre_delete_bis(arbre *tree, cle c) { + node *nd = position(*tree, c); - // 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; - } + if (NULL == nd || c != nd->key) { + return false; + } - 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; - } - } -} + // 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); - } +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 index a1ee64b64e5fb47f4bf8e2f7f854545bb7a8c739..3f7fd35fcc237bee5939ccd6016f83ad7f0b6542 100644 --- a/source_codes/arbres_binaires/bin_tree.h +++ b/source_codes/arbres_binaires/bin_tree.h @@ -1,40 +1,23 @@ #ifndef BIN_TREE_H #define BIN_TREE_H +#include <stdbool.h> + // Structure pour un arbre binaire typedef int cle; typedef struct _node { - cle key; - struct _node* child[2]; + cle key; + struct _node *child[2]; } node; -typedef node* arbre; +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); +bool arbre_search(arbre tree, cle c); +arbre 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 - - - - - - - - - - - - - - - - - - - +#endif diff --git a/source_codes/arbres_binaires/bin_tree_main.c b/source_codes/arbres_binaires/bin_tree_main.c index f9f310738c30074a22b2b2499cd2ed1840b888f5..985a318bf7cefff229ef46f9a91ccc3c139f7a98 100644 --- a/source_codes/arbres_binaires/bin_tree_main.c +++ b/source_codes/arbres_binaires/bin_tree_main.c @@ -1,24 +1,24 @@ +#include "bin_tree.h" +#include <stdbool.h> #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); + 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/bin_tree_main_rec.c b/source_codes/arbres_binaires/bin_tree_main_rec.c deleted file mode 100644 index 6cc51917b720abc14c896536309f135773042f40..0000000000000000000000000000000000000000 --- a/source_codes/arbres_binaires/bin_tree_main_rec.c +++ /dev/null @@ -1,36 +0,0 @@ -#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 index d27ee573a350da9ed2fdce4913d1ebc29977ef0d..68190dafb5363fd332bab4017787051584751635 100644 --- a/source_codes/arbres_binaires/bin_tree_part.c +++ b/source_codes/arbres_binaires/bin_tree_part.c @@ -1,109 +1,109 @@ -#include <stdio.h> -#include <stdlib.h> +#include "bin_tree_part.h" #include <assert.h> +#include <math.h> #include <stdbool.h> -#include "bin_tree.h" +#include <stdio.h> +#include <stdlib.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 *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; +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])); - } + 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; + //à 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_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; - } - } -} +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; + } -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); - } + // 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; + } + } } -bool arbre_search(arbre tree,int cle) { - // à compléter - 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.h b/source_codes/arbres_binaires/bin_tree_part.h similarity index 52% rename from source_codes/arbres_binaires/bin_tree_rec.h rename to source_codes/arbres_binaires/bin_tree_part.h index 439c006e0d788933b3de40fa9864e574eee891b1..54e846d083756d121aa5f92d22fe05eaabea3542 100644 --- a/source_codes/arbres_binaires/bin_tree_rec.h +++ b/source_codes/arbres_binaires/bin_tree_part.h @@ -1,40 +1,23 @@ #ifndef BIN_TREE_H #define BIN_TREE_H +#include <stdbool.h> + // Structure pour un arbre binaire typedef int cle; typedef struct _node { - cle key; - struct _node* child[2]; + cle key; + struct _node *child[2]; } node; -typedef node* arbre; +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); +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 - - - - - - - - - - - - - - - - - - - +#endif diff --git a/source_codes/arbres_binaires/bin_tree_rec.c b/source_codes/arbres_binaires/bin_tree_rec.c deleted file mode 100644 index a49ce6d6cb1628261385d0a5f42fdbb94b4f4349..0000000000000000000000000000000000000000 --- a/source_codes/arbres_binaires/bin_tree_rec.c +++ /dev/null @@ -1,91 +0,0 @@ -#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/main.c b/source_codes/arbres_binaires/main.c index 10ea9f04cf554ec2cd8443c6028d356ce569cbe4..6bc9e93e51b4f9bf1c599162ab07dd0a4c0e0fd8 100644 --- a/source_codes/arbres_binaires/main.c +++ b/source_codes/arbres_binaires/main.c @@ -1,24 +1,24 @@ +#include "bin_tree.h" +#include <stdbool.h> #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); + 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 deleted file mode 100644 index 2bb6f73aa7034e97463b3281c06e2ae2d4834ab2..0000000000000000000000000000000000000000 --- a/source_codes/arbres_binaires/tree_insert.adb +++ /dev/null @@ -1,38 +0,0 @@ - - -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 index 1217ed323c86039affb49c5eaf1fa109eeefd539..ac4933fbde23d88d58e8f4b54de48e7e782c8ac5 100644 --- a/source_codes/arbres_binaires/tree_search.c +++ b/source_codes/arbres_binaires/tree_search.c @@ -1,31 +1,31 @@ +#include <stdbool.h> #include <stdio.h> #include <stdlib.h> -#include <stdbool.h> typedef int cle; typedef struct _node { - cle key; - struct _node* gauche; - struct _node* droite; + cle key; + struct _node *gauche; + struct _node *droite; } node; -typedef node* arbre; +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; +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); + arbre tree; + arbre t = search(15, tree); }