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:taille_arbre [2021/01/19 10:42] mc |
les_programmes_a_connaitre:algorithmique_term:taille_arbre [2023/01/04 11:44] (Version actuelle) hk [Calculer la hauteur et la taille d'un arbre] |
||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | ====== Calculer la hauteur et la taille d'un arbre ====== | + | ====== Calculer la hauteur et la taille d'un arbre ====== |
====Hauteur d'un arbre :==== | ====Hauteur d'un arbre :==== | ||
//rappel :// profondeur maximale d'un arbre. | //rappel :// profondeur maximale d'un arbre. | ||
- | Algorithme | + | Programme |
<code python> | <code python> | ||
- | VARIABLE | + | #---Classe permettant l' |
- | T : arbre | + | class ArbreBinaire: |
- | x : noeud | + | def __init__(self, |
+ | self.valeur = valeur | ||
+ | self.enfant_gauche = None | ||
+ | self.enfant_droit = None | ||
- | DEBUT | + | def insert_gauche(self, |
- | HAUTEUR(T) : | + | if self.enfant_gauche == None: |
- | | + | |
- | x ← T.racine | + | else: |
- | | + | new_node = ArbreBinaire(valeur) |
- | | + | |
- | | + | |
- | fin si | + | |
- | FIN | + | 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 | ||
+ | Entrée : T-> | ||
+ | Sortie: hauteur de l' | ||
+ | """ | ||
+ | def hauteur(T): | ||
+ | | ||
+ | return | ||
+ | else: | ||
+ | | ||
</ | </ | ||
Ligne 32: | Ligne 87: | ||
DEBUT | DEBUT | ||
- | TAILLE(T) : | + | #---Classe permettant l' |
- | | + | class ArbreBinaire: |
- | x ← T.racine | + | def __init__(self, valeur): |
- | | + | |
- | | + | self.enfant_gauche = None |
- | | + | self.enfant_droit = None |
- | fin si | + | |
- | FIN | + | 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 | ||
+ | Entrée : T-> | ||
+ | Sortie: taille de l' | ||
+ | """ | ||
+ | def taille(T): | ||
+ | | ||
+ | return | ||
+ | else: | ||
+ | | ||
</ | </ | ||
+ | ===En lien avec cette page : === | ||
+ | [[les_fiches_revisions: | ||
+ | |||
+ | [[les_fiches_revisions: |