Académique Documents
Professionnel Documents
Culture Documents
7.1 ARBRES BINAIRES DE RECHERCHE Dfinition Arbre binaire tel quen tout nud la cl du nud est suprieure celle de tous ses descendants de gauche et infrieure celle de tous ses descendants de droite
2
EXEMPLE
11 8 cl min 5 6 10 12 21 25
3
15 30 35 cl max
ABR:
Arbre binaire a a.g = sous-arbre de gauche de racine(a) a.d = sous-arbre de droite de racine(a)
class ABR { private int cle; private String element; private ABR gauche; private ABR droit; public ABR(int a, String s, ABR g, ABR d) { cle = a; element = s; gauche= g; droit=d; } //les mthodes
recherche de 21 15 30 35
complexit: O(h(a))7
complexit: O(h(a))
ABR ajouter (ABR a, String e, int c) b=a; si b == null alors b=new ABR (c, e, null, null ) ; sinon tant que non place_trouve faire si b.cle == c alors "erreur" //lment dj prsent si b.cle > c alors //descendre gauche si b.gauche == null alors place_trouve=vrai //fils gauche de b b.gauche=new ABR (c, e, null, null ) ; sinon b = b.gauche; finsi; sinon //descendre droite si b.droit == null alors place trouve=vrai //fils droit b.droit=new ABR (c, e, null, null ) ; sinon b =b.droit; finsi; finsi; fait; finsi; retourner b;
10
10
EXEMPLE
5
8 12
15 30
c3-2
3 2 4
11
14
c3-1
21 17 23
35
suppression d'une feuille cas 1 (11) sup. d'un nud sans fils droit cas 2 (8) sup. de nuds avec 2 fils cas 3-1 (15) puis cas 3-2 (16)
16
19
18
20
11
EXEMPLE
8 5
10 15 12 30
3 2 4
11
14 17
21 23
35
16
19
suppression d'une feuille cas 1 (11) sup. d'un nud sans fils droit cas 2 (8)
18 20
12
EXEMPLE
5
10 15 12 30
3 2 4
14 17
21 23
35
16
19
18
20
13
EXEMPLE
5
10 16 12 30
3 2 4
14 17
21 23
35
19
18
20
14
EXEMPLE
5
10 17 12 30
3 2 4
14
21 23
35
19
18
20
15
o' o a b o' o a b
cas 2
o'
C. Carrez
16
o' o a b o"
Cas 3.2
o' o" a b c d
c
C. Carrez
17
18
cas 3
a a 2 fils:
rechercher le plus petit du sous-arbre de droite f = a.d; tant que f.g faire f=f.g fait; a.element = f.element; a.cle=f.cle; l'lment de f remplace l'lment supprim f = f.d; si f n'a pas de fils gauche remonter le fils droit de f;
f.d = si f feuille
19
20
quilibre
arbre non quilibr:
a r b d
arbre quilibr:
a
r b d
h h-1
(ou+ou- 1) (en O(log(n))
23
notations des dsquilibres: = -+ ++ si h(a.g) = h(a.d) si h(a.g) = h(a.d)-1 si h(a.g) = h(a.d)-2 si h(a.g) = h(a.d)+1 si h(a.g) = h(a.d)+2
4= 6
10 = ++
17 12
14
24
1 7
exemple de rotation
3 0
++
2 1
3 5
1 9
2 3
1 8
2 0
1 7
2 1 =
R-quilibrage 1
1 8
1 9
3 0
2 0
2 3
3 5
25
h-1
h 1
C
B
h-3
h-3
h-2
h 3
dsquilibre(A)=2, et dsquilibre(A.g))=1 ou 0
o
rotation droite
o
'
A '
o
=
h 1
h -1 h -2
h 2
C h -3
D
D
C. Carrez
h -2
h -3
26
4 0 ++
17
50
12
30
45
52
14
21
35
19
23
27
17 - 12 30
+
40 14
50
21 19 23
35
45
52
4 0
2
+ +
1 7
1
50
1 2
3 0
4 5
5 2
4 0
+ +
3 0
1 4
2 1
3 5
1 7
3 5
1 9
2 3
1 2
2 1
R-quilibrage 2
1 7
=
3 0
1 4
1 9
2 3
4 0
1 2
2 1
3 5
5 0
1 4
1 9
2 3
4 5
5 2
29
rotation gauche-droite
A
o
o'
h
++
o"
h -1
E
h -3
h -2
B
h -3
C
h - 3 ( +/- 1 )
h - 3 ( + /- 1 )
A '
o h -1 h -2
C
B
D
h -3
E
h -3
30
C. Carrez
h -3
h -3
(+ / - 1 )
( + / -1 )
adjonction et suppression
mme principe que dans un arbre binaire de recherche mais en rquilibrant par rotations aprs l'opration si c'est ncessaire (si a n'est plus H quilibr) Dans les arbres AVL recherche adjonction suppression
en (log2n)
On constate exprimentalement: en moyenne 1 rotation pour 2 adjonctions en moyenne 1 rotation pour 5 suppressions
31
Exemple
71 44 37 61 83
32
Exemple
71 ++ 44 37 50 61 83
33
Exemple
71 ++ 44 37 50 71 61 44 37 50 83 61 83 Double rotation gauche puis droite
34
Exemple
71 ++ 44 37 50 71 61 44 37 37 50 50
35
61
83 puis 44
61 71 AVL 83