B-Trees y AVL Trees
-
Upload
ulric-barnett -
Category
Documents
-
view
50 -
download
3
description
Transcript of B-Trees y AVL Trees
![Page 1: B-Trees y AVL Trees](https://reader035.fdocuments.net/reader035/viewer/2022062301/56812ff1550346895d956929/html5/thumbnails/1.jpg)
1
B-Trees y AVL Trees
![Page 2: B-Trees y AVL Trees](https://reader035.fdocuments.net/reader035/viewer/2022062301/56812ff1550346895d956929/html5/thumbnails/2.jpg)
2
AVL Trees: Topicos
• Balance en los Trees
• Chequear el Balance
• Insertar• Single y Doble Rotaciones
![Page 3: B-Trees y AVL Trees](https://reader035.fdocuments.net/reader035/viewer/2022062301/56812ff1550346895d956929/html5/thumbnails/3.jpg)
3
Evitar malos Trees?
![Page 4: B-Trees y AVL Trees](https://reader035.fdocuments.net/reader035/viewer/2022062301/56812ff1550346895d956929/html5/thumbnails/4.jpg)
4
Idea #1
• Los subarboles tienen la misma altura.
![Page 5: B-Trees y AVL Trees](https://reader035.fdocuments.net/reader035/viewer/2022062301/56812ff1550346895d956929/html5/thumbnails/5.jpg)
5
Idea #2
• Para todo nodo en el tree, los subarboles izq. y der. Tienen la misma altura.• Rigidez
![Page 6: B-Trees y AVL Trees](https://reader035.fdocuments.net/reader035/viewer/2022062301/56812ff1550346895d956929/html5/thumbnails/6.jpg)
6
AVL Trees• Binary search trees ABB, con una propiedad
adicional.• Para todo nodo en el tree, la altura de los
subarboles izq. y der. difieren en a lo más en 1 de altura.
![Page 7: B-Trees y AVL Trees](https://reader035.fdocuments.net/reader035/viewer/2022062301/56812ff1550346895d956929/html5/thumbnails/7.jpg)
7
Chequear Balance
0
0 0
00
0000
0 1 1 1
1
1
12
22
3
3
4,x
5,x
![Page 8: B-Trees y AVL Trees](https://reader035.fdocuments.net/reader035/viewer/2022062301/56812ff1550346895d956929/html5/thumbnails/8.jpg)
8
Ejemplo4
6
5 8
7 9
3
2
7
9
8
4 6
5
3
3
2
6 8
7
5
4
8
6
16 23
18
12
94 5
![Page 9: B-Trees y AVL Trees](https://reader035.fdocuments.net/reader035/viewer/2022062301/56812ff1550346895d956929/html5/thumbnails/9.jpg)
9
Computando la Height
0
0 0
00
0000
0 1 1 1
1
1
12
22
3
3
4
5
![Page 10: B-Trees y AVL Trees](https://reader035.fdocuments.net/reader035/viewer/2022062301/56812ff1550346895d956929/html5/thumbnails/10.jpg)
10
El AVL tree mas pequeño de height 7
• N(h) = # nodos en el más pequeño AVL tree de altura h.• N(h) = N(h-1) + N(h-2) + 1• N(0) = 1, N(1) = 2 • N = (2h). Equivalentemente, h = O(log N).
![Page 11: B-Trees y AVL Trees](https://reader035.fdocuments.net/reader035/viewer/2022062301/56812ff1550346895d956929/html5/thumbnails/11.jpg)
11
Operaciones
• Find, FindMin, FindMax• O(log N)
• Insertar• Necesita mantener el balance• O(log N)
• Delete• Complicado
![Page 12: B-Trees y AVL Trees](https://reader035.fdocuments.net/reader035/viewer/2022062301/56812ff1550346895d956929/html5/thumbnails/12.jpg)
12
Single Rotacion3
5
4 7
6 9
2
1
8
3
5
4
7
6
92
1
8
CB
A
y
x
C
BA
y
x
![Page 13: B-Trees y AVL Trees](https://reader035.fdocuments.net/reader035/viewer/2022062301/56812ff1550346895d956929/html5/thumbnails/13.jpg)
13
Single RotacionRaiz de Single Rotacion debe ser el nodo 3. Nodo 3 es el nodo con diferencia height = 2
3
6
4
7 10
2
1
9
CB
A
y
x
C
BA
y
x
8
5
6
3
7 10
8
2
1 4
5
x
y x
y
9
![Page 14: B-Trees y AVL Trees](https://reader035.fdocuments.net/reader035/viewer/2022062301/56812ff1550346895d956929/html5/thumbnails/14.jpg)
14
Single Rotacion que no trabaja3
5
4 8
6 9
2
1
7
3
5
4
8
6
92
1
7
CB
A
y
x
C
BA
y
x
![Page 15: B-Trees y AVL Trees](https://reader035.fdocuments.net/reader035/viewer/2022062301/56812ff1550346895d956929/html5/thumbnails/15.jpg)
15
Double Rotacion3
5
4 8
6 9
2
1
7
3
5
4
6
8
9
2
1
7
C
D
B
A z
x
y
C DBA
z
x y
![Page 16: B-Trees y AVL Trees](https://reader035.fdocuments.net/reader035/viewer/2022062301/56812ff1550346895d956929/html5/thumbnails/16.jpg)
16
Double Rotacion3
6
5 8
4
1 3
5
6
81 4
C
D
B
A z
x
y
C DBA
z
x y
![Page 17: B-Trees y AVL Trees](https://reader035.fdocuments.net/reader035/viewer/2022062301/56812ff1550346895d956929/html5/thumbnails/17.jpg)
17
Double Rotacion Ejemplo 2
4
9
6
10 11
3
2
11
8
x
y
zTree luego de insert 7:
5
7
6
9
10 11
4
11
y
C
D
B
A z
x
y
C DBA
z
x y
z
x
4
3 5 8
7
A
B
C
DDCBA
La root de rotacion es x(4), donde la diferencia de height es 2
![Page 18: B-Trees y AVL Trees](https://reader035.fdocuments.net/reader035/viewer/2022062301/56812ff1550346895d956929/html5/thumbnails/18.jpg)
18
AVL Tree Insert, Delete
• 2 tipos de rotacion
- single, doble
• Rotacion es necesaria a lo menos una vez!
• La root de rotacion es donde la diferencia de height es 2
![Page 19: B-Trees y AVL Trees](https://reader035.fdocuments.net/reader035/viewer/2022062301/56812ff1550346895d956929/html5/thumbnails/19.jpg)
19
B-Trees de orden M• Un M-ary tree. Todo nodo tiene a lo más M hijos.• Hojas
• Todos los datos está lamacenados en las leaves.• Todas las leaves estan a la misma depth.• Leaves (except root) tiene entre M/2 y M records
• Nodo Interior• M punteros a M children.• M-1 claves: la más pequeña keys se encuentra en M-1
subarbol derecho.• La root tiene entre 1 y M children.• Los nodos interiores tiene entre M/2 y M children.
• M=3: 2-3 Trees• M=4: 2-3-4 Trees
![Page 20: B-Trees y AVL Trees](https://reader035.fdocuments.net/reader035/viewer/2022062301/56812ff1550346895d956929/html5/thumbnails/20.jpg)
20
12,15 20,23 25,26,28 32,41,43 48,52,53 56,61 72,88
25:48
16:20 32:-- 56:72
16,18
Insert
12,15 20,23 25,26,28 32,41,43 48,52,50, 53 56,61 72,88
25:48
16:20 32:-- 56:72
16,18
Insert 50
12,15 20,23 25,26,28 32,41,43 48,50,52 53, 56,61 72,88
25:48
16:20 32:-- 53:72
16,18
Insert 50 (cont)53 es movido a los vecinos del nodo hermano
Original
![Page 21: B-Trees y AVL Trees](https://reader035.fdocuments.net/reader035/viewer/2022062301/56812ff1550346895d956929/html5/thumbnails/21.jpg)
21
Insert 49
12,15 20,23 25,26,28 32,41,43 48,49, 50,52 53, 56,61 72,88
25:48
16:20 32:-- 53:72
16,18
Split el nodo
Envia un hijo a los vecinos del hermano
12,15 20,23 25,26,28 32,41,43 48,49 53, 56,61 72,88
25:48
16:20 32:-- 50:53:72
16,18 50,52
12,15 20,23 25,26,28 32,41,43 48,49 53, 56,61 72,88
25:50
16:20 32:48 53:72
16,18 50,52
![Page 22: B-Trees y AVL Trees](https://reader035.fdocuments.net/reader035/viewer/2022062301/56812ff1550346895d956929/html5/thumbnails/22.jpg)
22
Insert 27
Split el nodo
Split otra vez.
12,15 20,23 25,26,27,28 32,41,43 48,49 53, 56,61 72,88
25:50
16:20 32:48 53:72
16,18 50,52
12,15 20,23 25,26 32,41,43 48,49 53, 56,61 72,88
25:50
16:20 27:32:48 53:72
16,18 50,5227,28
48,--
12,15 20,23 25,26 32,41,43 48,49 53, 56,61 72,88
25:32:50
16:20 27:-- 53:72
16,18 50,5227,28
![Page 23: B-Trees y AVL Trees](https://reader035.fdocuments.net/reader035/viewer/2022062301/56812ff1550346895d956929/html5/thumbnails/23.jpg)
23
48,--
12,15 20,23 25,26 32,41,43 48,49 53, 56,61 72,88
25:--
16:20 27:-- 53:72
16,18 50,5227,28
50:--
Split la root
32:--
Insert 27(cont’)
![Page 24: B-Trees y AVL Trees](https://reader035.fdocuments.net/reader035/viewer/2022062301/56812ff1550346895d956929/html5/thumbnails/24.jpg)
24
27,28 32,41,43 50,52 53,56,61 72,88
25:--
27:-- 53:72
48,4912,15 20,23
16:20
16,18 25,26
48:--
50:--
32:--
27,28 32,41,43 50,52 56,61 72,88
25:--
27:-- 56:72
48,4912,15 20,23
16:20
16,18 25,26
48:--
50:--
32:--
Delete
![Page 25: B-Trees y AVL Trees](https://reader035.fdocuments.net/reader035/viewer/2022062301/56812ff1550346895d956929/html5/thumbnails/25.jpg)
25
27,28 32,41 50,52 56,61 72,88
25:--
27:-- 56:72
43,4912,15 20,23
16:20
16,18 25,26
43:--
50:--
32:--
27,28 32,41,49 50,52 56,61 72,88
25:--
27:-- 56:72
12,15 20,23
16:20
16,18 25,26
43:--
50:--
32:--
![Page 26: B-Trees y AVL Trees](https://reader035.fdocuments.net/reader035/viewer/2022062301/56812ff1550346895d956929/html5/thumbnails/26.jpg)
26
27,28 32,41,49 50,52 56,61 72,88
25:--
27:-- 72:--
12,15 20,23
16:20
16,18 25,26
50:--
56:--
32:--
27,28 32,41,49 50,52 56,72,88
25:--
27:-- 72:--
12,15 20,23
16:20
16,18 25,26
50:--
56:--
32:--
![Page 27: B-Trees y AVL Trees](https://reader035.fdocuments.net/reader035/viewer/2022062301/56812ff1550346895d956929/html5/thumbnails/27.jpg)
27
27,28 32,41,49 50,52 56,72,88
25:--
27:--
12,15 20,23
16:20
16,18 25,26
50:56
56:--
32:--
27,28 32,41,49 50,52 56,72,88
25:32
27:--
12,15 20,23
16:20
16,18 25,26
50:56
32:--
![Page 28: B-Trees y AVL Trees](https://reader035.fdocuments.net/reader035/viewer/2022062301/56812ff1550346895d956929/html5/thumbnails/28.jpg)
28
27,28 32,41,49 50,52 56,72,88
25:32
27:--
12,15 20,23
16:20
16,18 25,26
50:56
32,41,49 50,52 56,72,88
25:32
27:--
12,15 20,23
16:20
16,18 25,26,28
50:56
32,41,49 50,52 56,72,88
20:32
25:--
12,15 20,23
16:--
16,18 25,26,28
50:56
![Page 29: B-Trees y AVL Trees](https://reader035.fdocuments.net/reader035/viewer/2022062301/56812ff1550346895d956929/html5/thumbnails/29.jpg)
29
Operaciones sobre un B-Trees
• Find:• Atravesar el tree
• Insert• Hallar e insertar• Dar un camino o Split
• Delete• Hallar y borrar• Combinar
32,41,43 50, 53 54,56,61 72,88
54:72
48,49
48:--
50:--
![Page 30: B-Trees y AVL Trees](https://reader035.fdocuments.net/reader035/viewer/2022062301/56812ff1550346895d956929/html5/thumbnails/30.jpg)
30
Topicos
• Depth de un B-Trees
• Tiempo de Find, Insert, Delete
![Page 31: B-Trees y AVL Trees](https://reader035.fdocuments.net/reader035/viewer/2022062301/56812ff1550346895d956929/html5/thumbnails/31.jpg)
31
Depth de B-Trees
20,23
20:--
16,18 27,28
25:--
27:--
25,26 32,41 50,52 72,88
72:--
48,49
48:--
50:--
32:--
)(log
)log100(
loglog
log
1log
log
2/log
log
log
2
10099
2/
NOd
Nd
MM
N
M
N
M
N
Nd
NM
M
M
M
d
![Page 32: B-Trees y AVL Trees](https://reader035.fdocuments.net/reader035/viewer/2022062301/56812ff1550346895d956929/html5/thumbnails/32.jpg)
32
27,28 32,41,43 50,52 53,56,61 72,88
25:--
27:-- 53:72
48,4912,15 20,23
16:20
16,18 25,26
48:--
50:--
32:--
12:34:38:42:55:64:69:75:--:--:--
O(M)O(log M)
O(logMN)
)(log
log
loglog
)))(log((log
NO
M
NMO
NMO M
Find
![Page 33: B-Trees y AVL Trees](https://reader035.fdocuments.net/reader035/viewer/2022062301/56812ff1550346895d956929/html5/thumbnails/33.jpg)
33
27,28 32,41,43 50,52 53,56
25:--
27:-- 53:72
48,4920,23
20:--
16,18 25,26
48:--
50:--
32:--
12:34:38:42:55:64:69:75:--:--:--
O(M): inserta un elemento en la lista
O(logMN)
NM
MO
M
NMO
NMO M
loglog
log
log
)log(
Insert y Delete
O(log N)
![Page 34: B-Trees y AVL Trees](https://reader035.fdocuments.net/reader035/viewer/2022062301/56812ff1550346895d956929/html5/thumbnails/34.jpg)
34
Running Time
• Find• Cada nodo, O(log M) para determinar la rama a
considerar (binary search)
• O(logMN) nodos para visitar
• Total O(log N)
• Insert y Delete• require O(M) para fijar lo que se desea.
• O(M logMN) = O((M/log M) log N)
![Page 35: B-Trees y AVL Trees](https://reader035.fdocuments.net/reader035/viewer/2022062301/56812ff1550346895d956929/html5/thumbnails/35.jpg)
35
B-Trees en Memoria
• Nodo Interior• M punteros y M-1 keys
• Nodo Leaf• M’ datos (o M’ punteros a datos)
12 15 17 18 23 25 26 28 32 41 43 48 53 56 61 72 88
18 32 56 72
25 48
![Page 36: B-Trees y AVL Trees](https://reader035.fdocuments.net/reader035/viewer/2022062301/56812ff1550346895d956929/html5/thumbnails/36.jpg)
36
12:34:38:42:55:64:69:--:--
O(t2 + t1 log M)
27,28 32,41,43 50,52 53,56,61 72,88
25:--
27:-- 53:72
48,4912,15 20,23
16:20
16,18 25,26
48:--
50:--
32:--O(logMN)
)(log
)loglog(
)logloglog(
)))(loglog((
12
12
12
NO
NtNtO
NMtNtO
NMttO
M
M
MM
M
Find
t1: main memory tiempo de aceso para todo
byte.t2: todo disco acceso de un block
![Page 37: B-Trees y AVL Trees](https://reader035.fdocuments.net/reader035/viewer/2022062301/56812ff1550346895d956929/html5/thumbnails/37.jpg)
37
27,28 32,41,43 50,52 53,56
25:--
27:-- 53:72
48,4920,23
20:--
16,18 25,26
48:--
50:--
32:--
O(logMN)
Insert y Delete
O(t2 + t1 M)
)(log
)loglog
log(
)loglog(
)))(log((
12
12
12
NO
NM
MtNtO
NMtNtO
NMttO
M
M
MM
M
12:34:38:42:55:64:69:--:--