Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
les_programmes_a_connaitre:algorithmique_term:parcours_arbre [2021/01/19 11:29] mc |
les_programmes_a_connaitre:algorithmique_term:parcours_arbre [2022/05/13 11:18] (Version actuelle) bh [Parcours en largeur d'abord] |
||
---|---|---|---|
Ligne 73: | Ligne 73: | ||
préfixe(T.get_droit()) | préfixe(T.get_droit()) | ||
</ | </ | ||
+ | |||
==== Parcours suffixe ==== | ==== Parcours suffixe ==== | ||
Ligne 219: | Ligne 220: | ||
</ | </ | ||
// | // | ||
+ | |||
+ | ==== Parcours en largeur d' | ||
+ | |||
+ | <code python> | ||
+ | #---Classe permettant l' | ||
+ | class ArbreBinaire: | ||
+ | def __init__(self, | ||
+ | self.valeur = valeur | ||
+ | self.enfant_gauche = None | ||
+ | self.enfant_droit = None | ||
+ | |||
+ | def insert_gauche(self, | ||
+ | if self.enfant_gauche == None: | ||
+ | | ||
+ | else: | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | def insert_droit(self, | ||
+ | if self.enfant_droit == None: | ||
+ | | ||
+ | else: | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | def get_valeur(self): | ||
+ | return self.valeur | ||
+ | |||
+ | def get_gauche(self): | ||
+ | return self.enfant_gauche | ||
+ | |||
+ | def get_droit(self): | ||
+ | return self.enfant_droit | ||
+ | |||
+ | # | ||
+ | |||
+ | # | ||
+ | racine = ArbreBinaire(' | ||
+ | racine.insert_gauche(' | ||
+ | racine.insert_droit(' | ||
+ | |||
+ | b_node = racine.get_gauche() | ||
+ | b_node.insert_gauche(' | ||
+ | b_node.insert_droit(' | ||
+ | |||
+ | f_node = racine.get_droit() | ||
+ | f_node.insert_gauche(' | ||
+ | f_node.insert_droit(' | ||
+ | |||
+ | c_node = b_node.get_gauche() | ||
+ | c_node.insert_droit(' | ||
+ | |||
+ | g_node = f_node.get_gauche() | ||
+ | g_node.insert_gauche(' | ||
+ | |||
+ | h_node = f_node.get_droit() | ||
+ | h_node.insert_droit(' | ||
+ | # | ||
+ | T = racine | ||
+ | |||
+ | """ | ||
+ | Objectif : Parcourir l' | ||
+ | Entrée : T->noeud racine | ||
+ | Sortie: - | ||
+ | """ | ||
+ | f = [] | ||
+ | |||
+ | def parcours_largeur(T): | ||
+ | f.append(T) | ||
+ | while len(f) !=0: | ||
+ | x=f.pop(0) | ||
+ | return x | ||
+ | if T.get_gauche != None: | ||
+ | T.get_gauche.append(x.gauche) | ||
+ | f.append(T.get_gauche) | ||
+ | if T.get_droit !=None: | ||
+ | T.get_droit.append(x.droit) | ||
+ | f.append(T.get_droit) | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
===En lien avec cette page : === | ===En lien avec cette page : === |