# ######################################################################### # # Implémentation d'un arbre binaire en Programmation Orientée Objet (POO) # # Programme arbre_poo Version 3 # # ######################################################################### # # ##################################################################### # Classe Arbre (qui crée 1 noeud) : class Arbre: def __init__(self, val): self.valeur = val self.gauche = None self.droit = None def ajout_gauche(self, val): self.gauche = Arbre(val) def ajout_droit(self, val): self.droit = Arbre(val) def taille(self): tg = self.gauche.taille() if self.gauche else 0 td = self.droit.taille() if self.droit else 0 return 1 + tg + td def hauteur(self): hg = self.gauche.hauteur() if self.gauche else 0 hd = self.droit.hauteur() if self.droit else 0 return 1 + max(hg, hd) # ##################################################################### # Programme principal : # Création en mémoire de l'arbre de la page 1 : b = Arbre('A') b.ajout_gauche('B') b.ajout_droit('C') b.gauche.ajout_gauche('D') b.gauche.ajout_droit('E') b.gauche.gauche.ajout_droit('G') b.droit.ajout_gauche('F') # Affichage de la taille et de la hauteur de l'arbre : print('Taille : %d' % b.taille()) print('Hauteur : %d' % b.hauteur())