From 0f915d0b4e931330391a8a8acddfedbc2398181f Mon Sep 17 00:00:00 2001 From: Spectre Date: Mon, 24 Mar 2025 09:50:22 +0100 Subject: [PATCH] Auto urgent commit. --- trees/AB/arbre.py | 44 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/trees/AB/arbre.py b/trees/AB/arbre.py index 74b5e20..279e484 100644 --- a/trees/AB/arbre.py +++ b/trees/AB/arbre.py @@ -23,17 +23,41 @@ class Arbre_Binaire(object): profondeur_gauche = self.ss_arbre_gauche.hauteur() if self.ss_arbre_gauche else 0 profondeur_droite = self.ss_arbre_droit.hauteur() if self.ss_arbre_droit else 0 return 1+max(profondeur_droite,profondeur_gauche) -def affichage(arbre): - pass - if arbre.est_vide(): - return ([]) + + def parcours_largeur(self): + if self.est_vide(): + return None + file = [(self, 0)] + niveaux = {} + while file: + noeud, niveau = file.pop(0) + if niveau not in niveaux: + niveaux[niveau] = [] + niveaux[niveau].append(noeud.racine) + if noeud.ss_arbre_gauche: + file.append((noeud.ss_arbre_gauche, niveau + 1)) + if noeud.ss_arbre_droit: + file.append((noeud.ss_arbre_droit, niveau + 1)) + return niveaux +def affiche(arbre, traitement): + if arbre: + sag,racine,sad = arbre + if traitement == 'prefixe': + print(racine) + affiche(sag,"prefixe") + if traitement == "infixe": + print(racine) + affiche(sad,"infixe") + if traitement == "postfixe": + print(racine) +def list_to_btree(liste): + if not liste: + return Arbre_Binaire() else: - racine = arbre.racine - prefixe = arbre.ss_arbre_gauche - suffixe = arbre.ss_arbre_droit - #todo - - + racine = Arbre_Binaire(racine=liste[0]) + for elt in liste[1:]: + racine.insertion(elt) # besoin de insertion + return racine if __name__ == "__main__": arbre = Arbre_Binaire(1, Arbre_Binaire(2), Arbre_Binaire(3)) print("taille arbre :", arbre.taille())