2012-2013
5 vertes mures 23
2 1
noeud( panier ,
{ noeud(figues, {feuille(5)}),
noeud( bananes,
{ noeud(vertes, {feuille(2)}),
noeud(mûres, {feuille(1)})}),
noeud(noix, {feuille(23)})})
Béatrice Duval & Igor Stéphan SDA 65/ 194
Complexité des algorithmes
Méthodologie des spécifications
Structures de données
Algorithmes de recherche
...
sous-arbres
specification ARGEN0 (E )
etend BASE(E ), ENSEMBLE(ArGen(E ))
operations
Λ : ArGen(E )
noeud : E × Foret → ArGen(E )
sous arbres : ArGen(E ) → Foret
un sous arbre : ArGen(E ) → ArGen(E )
racine : ArGen(E ) → E
vide ? : ArGen(E ) → Bool
suite de ARGEN0
preconditions {a : ArGen(E )}
pre racine(a) : ¬(vide ?(a))
pre sous arbres(a) : ¬(vide ?(a))
pré un sous arbre(a) : ¬(vide ?(a))
et ¬(vide ?(sous arbres(a)))
axiomes {f : Foret; x : E }
argen(0) : sous arbres(noeud(x, f )) = f
argen(1) : un sous arbre(noeud(x, f )) = choisir (f )
argen(2) : racine(noeud(x, f )) = x
argen(3) : vide ?(Λ) = vrai
argen(4) : vide ?(noeud(x, f )) = faux
specification ARGEN1 (E )
etend ARGEN0
operations
taille : ArGen(E ) → Nat
hauteur : ArGen(E ) → Ent
suite de ARGEN1
specification AGO0 (E )
etend BASE(E ), LISTE(AGO(E ))
operations
Λ : AGO(E )
noeud : E × Foret → AGO(E )
racine : AGO(E ) → E
sous arbres : AGO(E ) → Foret
neme : AGO(E ) × Nat → AGO(E )
vide ? : AGO(E ) → Bool
suite de AGO0
Occurrence de nœud
L’occurrence de nœud est un codage qui permet une
représentation en liste d’un arbre.
A la racine de l’arbre est associé le mot vide.
Si µ est l’occurrence associée à un nœud alors l’occurrence de
son nème fils est µ.(n − 1).
n1
n2 n3 n5
n8 n4 n6 n7
n9 n10 n11
{(ǫ, n1), (0, n2), (1, n3), (2, n5), (00, n8), (10, n4),
(20, n6), (21, n7), (210, n9), (211, n10), (212, n11)}
Béatrice Duval & Igor Stéphan SDA 76/ 194
Complexité des algorithmes
Méthodologie des spécifications
Structures de données
Algorithmes de recherche
n2 n3 n4
n5 n6 n7 n8
n9 n10 n11
n2 n3 n2 n3
n4 n4
specification AB0 (E )
etend BASE(E )
sortes ArBin
operations
Λ : ArBin(E )
< , , >: E × ArBin(E ) × ArBin(E )
→ ArBin(E )
racine : ArBin(E ) → E
g : ArBin(E ) → ArBin(E )
d : ArBin(E ) → ArBin(E )
vide ? : ArBin(E ) → Bool
suite de AB0
preconditions {a : ArBin(E )}
pre g (a) : ¬(vide ?(a))
pre d(a) : ¬(vide ?(a))
pre racine(a) : ¬(vide ?(a))
axiomes {a, ag , ad : ArBin(E ); x : E }
ab(0) : g (< x, ag , ad >) = ag
ab(1) : d(< x, ag , ad >) = ad
ab(2) : racine(< x, ag , ad >) = x
ab(3) : vide ?(Λ) = vrai
ab(4) : vide ?(< x, ag , ad >) = faux
Termes et arbres
n1
n1 < , , > < , , >
n2 n3
n2 Λ Λ n3 < , , > Λ
n4
n4 Λ Λ
0 1
00 01 10 11
000 001010
Si l’arbre est parfait, i la numérotation en ordre hiérarchique
et µ l’occurrence d’un nœud alors
m = i − 2⌊log2 i⌋ , µ représentation binaire de m ;
son fils gauche (resp. droit) a pour numérotation en ordre
hiérarchique 2i (resp. 2i + 1).
Béatrice Duval & Igor Stéphan SDA 84/ 194
Complexité des algorithmes
Méthodologie des spécifications
Structures de données
Algorithmes de recherche
n2 n3 n4 n2
n5 n6 n7 n5 n3
n6 n4
n7
Spécification de la binarisation
specification BINARISATION(E )
etend AGO(E ), AB(E )
operations
binarisation : AGO(E ) → ArBin(E )
bin : Liste(AGO(E )) → ArBin(E )
axiomes {a : AGO(E ); f : Foret}
bin(0) : binarisation(a) =
si vide ?(a) alors Λ sinon bin(cons(a, [ ])) fsi
bin(1) : bin(f ) =
si vide ?(f ) alors Λ
sinon < racine(tete(f )), bin(sous arbres(tete(f ))),
bin(fin(f )) > fsi
n2 n6
n3 n4 n7 Λ
Λ Λ n5 Λ Λ n8
Λ Λ Λ Λ
n2 n6
n3 n4 n7 Λ
Λ Λ n5 Λ Λ n8
Λ Λ Λ Λ
n1
n2 n6
n3 n4 n7 Λ
(n1 n2 n3 n4 n5 n6 n7 n8)
Λ Λ n5 Λ Λ n8
Λ Λ Λ Λ
n1
n2 n6
n3 n4 n7 Λ
(n3 n2 n5 n4 n1 n7 n8 n6)
Λ Λ n5 Λ Λ n8
Λ Λ Λ Λ
n
n1
n2 n6
n3 n4 n7 Λ
(n3 n5 n4 n2 n8 n7 n6 n1)
Λ Λ n5 Λ Λ n8
Λ Λ Λ Λ