Vous êtes sur la page 1sur 7

CPGE AGADIR MP-PSI

TD : ARBRES
CORRECTION

#Exercice 1 : Arbre binaire


#Les fonctions de base sur la manipulation des arbres binaires.

def vide(a): def val(a):


return a==[] if a!=[]: return a[0]

def filsGauche(a): def filsDroit(a):


if not vide(a): if not vide(a):
return a[1] return a[2]
else: else:
return [] return []

#Question 1

def isbinary(tree):
if vide(tree): return (True)
if len(tree)!=3: return (False)
else: return isbinary(filsGauche(tree)) and isbinary(filsDroit(tree))

#Question 2

def checkmul(tree) :
if vide(tree): return True
if not estFeuille(tree):
if val(tree)!=val(filsGauche(tree))*val(filsDroit(tree)):
return (False)
return checkmul(filsGauche(tree)) and checkmul(filsDroit(tree))

#Question 3

def path_exists(tree,path) :
if path=="" : return True
if vide(tree) : return False
if val(tree)== path[0] :
return path_exists(filsGauche(tree),path[1:]) or
path_exists(filsDroit(tree),path[1:])
else:
return path_exists(filsGauche(tree),path) or
path_exists(filsDroit(tree),path)

TD-ARBRES 1/7 M.GUEROIHI


#Exercice 2 : Expression arithmétique
##1
def parcours(A):
if vide(A): return ""
elif estFeuille(A): return(str(val(A)))
else:
return '('+parcours(filsGauche(A))+str(val(A))+parcours(filsDroit(A))+')'
##2
def valExpression(a):
if vide(a): return 0
if val(a)=='+':
return valExpression(filsGauche(a))+valExpression(filsDroit(a))
elif val(a)=='*':
return valExpression(filsGauche(a))*valExpression(filsDroit(a))
elif val(a)=='-':
return valExpression(filsGauche(a))-valExpression(filsDroit(a))
elif val(a)=='/':
return valExpression(filsGauche(a))/valExpression(filsDroit(a))
else:
return val(a)

#Exercice 3 : Arbre binaire de recherche


##1
def AfficherDecRec(A):
if not vide(A):
AfficherDecRec(filsDroit(A))
print (val(A),end=' ')
AfficherDecRec(filsGauche(A))
##2
def MaximumItr(A):
while not vide(filsDroit(A)):
A=filsDroit(A)
return val(A)

##3
def MaximumRec(A):
if vide(filsDroit(A)): return val(A)
return MaximumRec(filsDroit(A))

TD-ARBRES 2/7 M.GUEROIHI


##4
def Recherche(v,A):
if vide (A): return (False)
elif val(A)==v : return True
elif v>val(A) : return Recherche(v,filsDroit(A))
else : return Recherche(v,filsGauche(A))
##5
def Inserer(v,A):

if vide(A):
A+= [v,[],[]]
else:
if v<=val(A):
Inserer(v,filsGauche(A))
else:
Inserer(v,filsDroit(A))

#Exercice 4 : Arbre n-aire


def vide(a):
return (a==[])

def val(a):
if not vide(a): return a[0]
else: return (None)

def fils(a):
if not vide(a): return a[1]
else: return []

#parcourir Préfixe d’un arbre n-aire

def prefixe(a): def prefixe2(a):


if not vide(a): if vide(a): return []
print(val(a),end=' ') else:
for e in fils(a): L=[val(a)]
prefixe(e) for e in fils(a):
L+=prefixe2(e)
return L

TD-ARBRES 3/7 M.GUEROIHI


#parcourir Postfixe d’un arbre n-aire

def postfixe(a): def Postfixe2(a):


if not vide(a): if vide(a) : return (a)
for e in fils(a): L=[]
postfixe(e) for f in fils(a):
print(val(a),end=' ') L+=Postfixe2(f)
return L+[val(a)]

#parcourir un arbre en largeur d’un arbre n-aire

def parcourLargeur(a):
L=[]
F=[] #File FIFO
F.append(a) #enfiler a dans la file F
while not vide(F):
n=F.pop(0) #défiler F
L+=[val(n)]
for f in fils(n):
if not vide(f): F.append(f)
return(L)

def nombreNoeuds(a):
if vide(a):
return 0
else:
n=1
for e in fils(a):
n+=nombreNoeuds(e)
return (n)

def estFeuille(a):
if vide(a):return false
return vide(fils(a))

def nombreFeuilles(a):
if vide(a): return 0
elif estFeuille(a): return 1
else:
n=0
for f in fils(a):
n+= nombreFeuilles(f)
return n

TD-ARBRES 4/7 M.GUEROIHI


def hauteur(a):
if vide(a) or estFeuille(a):
return 0
else:
L=[]
for f in fils(a):
L+=[hauteur(f)]
return 1 + max (L)

#Exercice 5: arbre binaire par dictionnaire


def vide(a):
return a=={}

def val(a):
if not vide(a):
return a['r']
else:
return None

def filsGauche(a): def filsDroit(a):


if not vide(a): return a['g'] if not vide(a): return a['d']
else: return {} else: return {}

def Prefixe(a):
if vide(a):
return ([])
else:
return [val(a)] + Prefixe(filsGauche(a)) + Prefixe(filsDroit(a))

def Postfixe(a):
if vide(a):
return ([])
else:
return Postfixe(filsGauche(a)) + Postfixe(filsDroit(a))+ [val(a)]

def Infixe(a):
if vide(a):return ([])
return Infixe(filsGauche(a))+ [val(a)] + Infixe(filsDroit(a))

TD-ARBRES 5/7 M.GUEROIHI


def PrefixeIter(a):
"""
retourne une liste des valeurs des noeuds
parcourus en profondeur préfixe
"""
L=[]
P=[]
P.append(a)
while P!=[]:
n=P.pop()
if not vide(n):
L+=[val(n)]
P.append(filsDroit(n))
P.append(filsGauche(n))
return (L)

#parcourir un arbre en largeur

def parcourLargeur2(a):
F=[] #File FIFO
F.append(a) #enfiler a dans la file F
while F!=[]:
n=F.pop(0) #défiler F
print(val(n),end=' ')
if not vide(filsGauche(n)): F.append(filsGauche(n))
if not vide(filsDroit(n)) : F.append(filsDroit(n))
print()
#Algorithmes de base sur les arbres binaires

def hauteur(a):
if vide(a) or estFeuille(a):
return 0
else:
return 1 + max (hauteur(filsGauche(a)) , hauteur(filsDroit(a)))

def nombreNoeuds(a):
if vide(a):
return 0
else:
return 1 + nombreNoeuds(filsGauche(a)) + nombreNoeuds(filsDroit(a))

TD-ARBRES 6/7 M.GUEROIHI


def estFeuille(a):
if vide(a) : return False
return vide(filsGauche(a)) and vide(filsDroit(a))

def nombreFeuilles(a):
if vide(a):
return 0
elif estFeuille(a):
return 1
else:
return nombreFeuilles(filsGauche(a)) + nombreFeuilles(filsDroit(a))

def estNoeudInterne(a):
if a==[] : return False
return not estFeuille(a)

def nombreNoeudInternes(a):
if vide(a):
return 0
elif estFeuille(a):
return 0
else:
return 1+ nombreNoeudInternes(filsGauche(a)) +
nombreNoeudInternes(filsDroit(a))

#fonction de recherche dans un arbre binaire quelconque

def existe(a, x):


if vide(a):
return False
else:
if val(a) ==x:
return True
else:
return existe(filsGauche(a),x ) or existe(filsDroit(a), x)

TD-ARBRES 7/7 M.GUEROIHI

Vous aimerez peut-être aussi