Skip to content
Snippets Groups Projects
Commit 4527a199 authored by Romain Vuillemot's avatar Romain Vuillemot
Browse files

maj code arbres

parent 4894b43d
No related branches found
No related tags found
No related merge requests found
import sys
class Noeud:
def __init__(self, v, c = []):
self.v = v
......@@ -11,6 +13,10 @@ def parcours_affiche(arbre):
def parcours_recherche(arbre, valeur):
if arbre is None:
return False
stack = arbre.c
if valeur > arbre.v:
......@@ -19,15 +25,12 @@ def parcours_recherche(arbre, valeur):
if valeur == arbre.v:
return True
prev = stack[0].v
prev = sys.maxsize
while len(stack) > 0:
children = stack.pop(0)
if valeur == children.v:
return True
if valeur > prev and valeur < children.v:
stack = children.c
prev = stack[0].v
......@@ -36,8 +39,18 @@ def parcours_recherche(arbre, valeur):
return False
if __name__=="__main__":
racine = Noeud(13, [Noeud(4, [Noeud(1), Noeud(2), Noeud(3)]), Noeud(8, \
[Noeud(5), Noeud(6), Noeud(7)]), Noeud(12, [Noeud(9), Noeud(10), Noeud(11)])])
print(parcours_affiche(racine))
# renvoie False pour un arbre vide
parcours_recherche(None, -1)
racine = Noeud(13, [Noeud(4, [Noeud(1), Noeud(2), Noeud(3)]), Noeud(8, [Noeud(5), Noeud(6), Noeud(7)]), Noeud(12, [Noeud(9), Noeud(10), Noeud(11)])])
# 11 est dans l'arbre
assert parcours_recherche(racine, 11) == True
print(parcours_affiche(racine))
print(parcours_recherche(racine, 11))
\ No newline at end of file
# 12 n'est pas dans l'arbre
assert parcours_recherche(racine, 12) == False
\ No newline at end of file
class Noeud:
def __init__(self, v, c = []):
self.v = v
self.c = c
def parcours(n):
stack = []
stack.append(n)
r = []
while len(stack) > 0:
current = stack.pop(0)
r.append(current.v)
for v in current.c:
stack.append(v)
return r
if __name__=="__main__":
print(parcours(Noeud(1, \
[ \
Noeud(2, [ \
Noeud(4), Noeud(5) \
]), \
Noeud(3) \
] \
) \
))
# [1, 2, 3, 4, 5]
print(parcours(Noeud(5, [Noeud(4, [Noeud(2), Noeud(1)]), Noeud(3)])))
# [5, 4, 3, 1, 2]
print(parcours(Noeud(5, [Noeud(1), Noeud(2), Noeud(3), Noeud(4)])))
# [5, 1, 2, 3, 4]
print(parcours(Noeud(1, [Noeud(5), Noeud(4), Noeud(3), Noeud(2)])))
# [1, 5, 4, 3, 2]
import sys
class Noeud:
def __init__(self, v, c = []):
self.v = v
self.c = c
def parcours_affiche(arbre):
for c in arbre.c:
parcours_affiche(c)
print(arbre.v)
def parcours_recherche(arbre, valeur):
if arbre is None:
return False
stack = arbre.c
if valeur > arbre.v:
return False
if valeur == arbre.v:
return True
prev = sys.maxsize
while len(stack) > 0:
children = stack.pop(0)
if valeur == children.v:
return True
if valeur > prev and valeur < children.v:
stack = children.c
prev = stack[0].v
else:
prev = children.v
return False
if __name__=="__main__":
racine = Noeud(13, [Noeud(4, [Noeud(1), Noeud(2), Noeud(3)]), Noeud(8, \
[Noeud(5), Noeud(6), Noeud(7)]), Noeud(12, [Noeud(9), Noeud(10), Noeud(11)])])
print(parcours_affiche(racine))
# renvoie False pour un arbre vide
parcours_recherche(None, -1)
# 11 est dans l'arbre
assert parcours_recherche(racine, 11) == True
# 12 n'est pas dans l'arbre
assert parcours_recherche(racine, 12) == False
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment