Ci-dessous, les différences entre deux révisions de la page.
| Prochaine révision | Révision précédente | ||
| les_programmes_a_connaitre:algorithmique_term:cle_arbre [2021/01/19 10:53] mc created | les_programmes_a_connaitre:algorithmique_term:cle_arbre [2022/04/29 11:57] (Version actuelle) lt [Insérer une clé] | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| - | ====== Rechercher une clé (noeud) | + | ====== Rechercher | 
| + | ====== | ||
| + | ====Rechercher une clé==== | ||
| + | <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(15) | ||
| + | racine.insert_gauche(6) | ||
| + | racine.insert_droit(18) | ||
| + | |||
| + | b_node = racine.get_gauche() | ||
| + | b_node.insert_gauche(3) | ||
| + | b_node.insert_droit(7) | ||
| + | |||
| + | i_node = racine.get_droit() | ||
| + | i_node.insert_gauche(17) | ||
| + | i_node.insert_droit(20) | ||
| + | |||
| + | c_node = b_node.get_gauche() | ||
| + | c_node.insert_gauche(2) | ||
| + | c_node.insert_droit(4) | ||
| + | |||
| + | d_node = b_node.get_droit() | ||
| + | d_node.insert_droit(13) | ||
| + | |||
| + | g_node = d_node.get_droit() | ||
| + | g_node.insert_gauche(9) | ||
| + | # | ||
| + | T = racine | ||
| + | |||
| + | """ | ||
| + | Objectif : Rechercher un noeud k dans un arbre binaire de recherche | ||
| + | (complexité en O(n) dans le pire des cas) | ||
| + | Entrée : T->noeud racine, k->noeud recherché | ||
| + | Sortie: | ||
| + | """ | ||
| + | def arbre_recherche(T, | ||
| + | if T == None: | ||
| + | return False | ||
| + | if k == T.get_valeur(): | ||
| + | return True | ||
| + | if k< | ||
| + | return arbre_recherche(T.get_gauche(), | ||
| + | else: | ||
| + | return arbre_recherche(T.get_droit(), | ||
| + | </ | ||
| + | |||
| + | ====Insérer une clé==== | ||
| + | <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(15) | ||
| + | racine.insert_gauche(6) | ||
| + | racine.insert_droit(18) | ||
| + | |||
| + | b_node = racine.get_gauche() | ||
| + | b_node.insert_gauche(3) | ||
| + | b_node.insert_droit(7) | ||
| + | |||
| + | i_node = racine.get_droit() | ||
| + | i_node.insert_gauche(17) | ||
| + | i_node.insert_droit(20) | ||
| + | |||
| + | c_node = b_node.get_gauche() | ||
| + | c_node.insert_gauche(2) | ||
| + | c_node.insert_droit(4) | ||
| + | |||
| + | d_node = b_node.get_droit() | ||
| + | d_node.insert_droit(13) | ||
| + | |||
| + | g_node = d_node.get_droit() | ||
| + | g_node.insert_gauche(9) | ||
| + | # | ||
| + | T = racine | ||
| + | |||
| + | """ | ||
| + | Objectif : Insérer un noeud y dans un arbre de recherche | ||
| + | (complexité en O(n) dans le pire des cas) | ||
| + | Entrée : T->noeud racine, | ||
| + | Sortie : affiche(racine) | ||
| + | """ | ||
| + | def arbre_insertion(T, | ||
| + | while T.get_droit()!=None or T.get_gauche()!=None: | ||
| + | if y< | ||
| + | T = T.get_gauche() | ||
| + | else: | ||
| + | T = T.get_droit() | ||
| + | if y< | ||
| + | y_node = T | ||
| + | y_node.insert_gauche(y) | ||
| + | else: | ||
| + | y_node = T | ||
| + | y_node.insert_droit(y) | ||
| + | </ | ||
| + | |||
| + | ===En lien avec cette page : === | ||
| + | [[les_fiches_revisions: | ||
| + | |||
| + | [[les_fiches_revisions: | ||