1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
# -*- coding: utf-8 -*-
arbre = ['+', ['+', ['*', 4, ['*', 4, 2]], 2], ['*', ['*', 4, 2], 2]]
def arbre_gauche(arbre) :
print 'gauche:', arbre[1]
return arbre[1]
def arbre_droit(arbre) :
print 'droit:', arbre[2]
return arbre[2]
def est_vide(arbre) :
if arbre == [] : # if not arbre:
return True
else :
return False
#retourne le chemin pour mener à la feuille si elle a été trouvée,False sinon
def recherche(feuille, arbre, Res, chemin = []) :
if est_vide(arbre) :
return
if isinstance(arbre_gauche(arbre), list) :
recherche(feuille, arbre_gauche(arbre), Res, chemin + [1])
if isinstance(arbre_droit(arbre), list) :
recherche(feuille, arbre_droit(arbre), Res, chemin + [2])
if arbre_gauche(arbre) == feuille :
chemin += [1]
Res += [chemin]
return Res
if arbre_droit(arbre) == feuille :
chemin += [2]
Res += [chemin]
return Res
return Res
ch = recherche(2, arbre, [])
print ch |
Partager