# TP ALGO ARBRES E.JARRIGE INF2A # --- DEBUT CLASSE ARBRE --- # class Arbre(object): # Constructeur def __init__(self,val,g = None,d = None): self.valeur = val self.gauche = g self.droite = d # Affichage def __repr__(self): return "[%s %s %s]" % (self.valeur, self.gauche, self.droite) # --- FIN CLASSE ARBRE --- # def recherche(a,x): if(a==None): return None elif(a.valeur!=x): if(x<a.valeur): return (recherche_rec(a.gauche,x)) else: return (recherche_rec(a.droite,x)) else: return a """ def recherche_iter(a,x): test=a while(test.valeur!=x and test!=None): if(x<test.valeur): test=test.gauche elif(x>test.valeur): test=test.droite return test """ def insertion(a,x): if(a==None): a=Arbre(x) elif(a.valeur<=x): return(insertion(a.gauche,x)) else: return(insertion(a.droite,x)) return a def profondeur(a,x,p=0): if(a==None): return None if(a.valeur != x): if(x < a.valeur): profondeur(a.gauche,x,p+1) else: profondeur(a.droite,x,p+1) else: return a, p # Main a = Arbre(17,Arbre(15,Arbre(-2),Arbre(16)),Arbre(57,Arbre(23,Arbre(21),Arbre(39)),Arbre(100))) print profondeur(a,39)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question