Académique Documents
Professionnel Documents
Culture Documents
de données
MI2 2005/2006
Fabrice Huet
(fabrice.huet@sophia.inria.fr)
Recherche séquentielle
Recherche dichotomique
Arbres Binaires de Recherche
truc tablo[1..N];
• Sortie de boucle:
1 3 5 6 10 11 14 25 26 40 41 78 11 ≤ 78
14 25 26 40 41 78 26 ≤ 78
40 41 78 41 ≤ 78
78 78 = 78
4 opérations pour trouver le bon élément
Combien pour une recherche séquentielle?
si (tablo[indiceTest] < x)
si (tablo[indiceTest] > x)
}
Version étudiants Unice
Recherche dichotomique - Algorithme
• Invariant de boucle
(g ≤ d)
et
et
• Sortie de boucle
trouve ou ((g > d)
et
et
• Ce qui implique
• Rappels
– La hauteur d’un arbre est la profondeur maximum de ses
nœuds
– Un arbre complet de hauteur h a 2h+1-1 nœuds
• Successeur:
Le successeur de x est le plus petit y tel que y > x
• On cherche dans un ABR en utilisant une méthode
de dichotomie
Version étudiants Unice
Cherchez l’ABR
• Des arbres suivants, lesquels sont des ABR?
5 7 5
2 7 2 5 2 7
1 3 6 8 1 3 6 8 1 6 8
5 5 5
2 9 4 7
1 3 3 10 1 6 6 8
Version étudiants Unice
Parcours infixe
• Comment afficher les éléments du plus petit au plus
grand?
parcoursInfix(noeud x) {
si x ≠ NIL
parcoursInfix(x.gauche);
afficher(x);
parcoursInfix(x.droit);
}
• Complexité?
• Autres parcours?
2 7 Rechercher 3: 5 → 2 → 3
Rechercher 6: 5 → 7 → 6
1 3 6 8
5 5
Inserer(15)
2 7 2 7
Inserer(9)
1 3 6 8 1 3 6 8
15
1 3 6 8 Supprimer(7)
1 3 6 9
15
5
5
2 7
supprimer(9)
2 7
1 3 6 8
1 3 6 8
15
15
5
5
2 7
supprimer(15)
2 8
1 3 6 8
1 3 6 9
15
2 8 Supprimer(5) 2 8
1 3 6 9 1 3 7 9
7 15 15
Successeur de 5 11 11
Retourner y;
Retourner y;
Version étudiants Unice
Exemple de suppression
supprimer(arbre T, nœud z) {
si z.gauche = NIL ou z.droit = NIL
alors y ← z z
sinon y ← successeur(z); 5
si y.gauche ≠ NIL
alors x ← y.gauche; 2 8
sinon x ← y.droit;
si x ≠ NIL
alors x.parent ← y.parent 1 3 6 9
si y.parent = NIL
alors T.racine ← x
y 7 15
sinon si y = y.parent.gauche
alors y.parent.gauche ← x; x
sinon y.parent.droit ← x; 11
Si y.clé ≠ z.clé
alors copier(z,y);
Retourner y;
Retourner y;
Retourner y;
Retourner y;
T1 = n1 = 1
Th
Th = nh = nh-1 + nh-2 +1
Th-1 Th-2
• Donc
• Comme nh nombre minimal de nœuds pour un arbre
équilibré de hauteur h, tout autre arbre équilibré de
hauteur h aura n nœuds tel que
• Ce qui donne
Version étudiants Unice
Complexité des opérations
• Nous avons donc montré qu’un arbre équilibré
compose de n nœuds aura une hauteur majorée par
1.44log(n+1)