1 / 37

B-Trees y AVL Trees

B-Trees y AVL Trees. AVL Trees: Topicos. Balance en los Trees Chequear el Balance Insertar Single y Doble Rotaciones. Evitar malos Trees?. Idea #1. Los subarboles tienen la misma altura. Idea #2. Para todo nodo en el tree, los subarboles izq. y der. Tienen la misma altura. Rigidez.

Download Presentation

B-Trees y AVL Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. B-Trees y AVL Trees

  2. AVL Trees: Topicos Balance en los Trees Chequear el Balance Insertar Single y Doble Rotaciones

  3. Evitar malos Trees?

  4. Idea #1 • Los subarboles tienen la misma altura.

  5. Idea #2 • Para todo nodo en el tree, los subarboles izq. y der. Tienen la misma altura. • Rigidez

  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.

  7. 5,x 3 4,x 1 2 2 3 1 1 0 0 2 0 1 1 0 0 0 1 0 0 0 0 Chequear Balance

  8. 7 4 3 9 2 6 5 8 3 5 8 4 6 7 9 3 8 2 5 6 12 4 7 4 5 9 18 6 8 16 23 Ejemplo

  9. 5 3 4 1 2 2 3 1 1 0 0 2 0 1 1 0 0 0 1 0 0 0 0 Computando la Height

  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).

  11. Operaciones • Find, FindMin, FindMax • O(log N) • Insertar • Necesita mantener el balance • O(log N) • Delete • Complicado

  12. 3 3 1 5 1 7 2 4 7 5 9 2 6 9 4 6 8 x y 8 y x A C B C A B Single Rotacion

  13. x y 9 9 y x A C B C A B Single RotacionRaiz de Single Rotacion debe ser el nodo 3. Nodo 3 es el nodo con diferencia height = 2 y x 6 3 x 3 8 y 1 6 10 8 1 4 7 2 4 2 5 5 7 10

  14. 3 3 1 5 1 8 2 4 8 5 9 2 6 9 4 6 x y 7 7 y x A C B C A B Single Rotacion que no trabaja

  15. 3 1 5 2 4 8 z x y 6 9 A B C D 7 x y A z D B C 3 Double Rotacion 1 6 5 8 2 4 7 9

  16. z x y 4 A B C D x y A z D B C Double Rotacion 3 5 1 6 3 6 5 8 1 4 8

  17. z x y A B C D x y A z D B C Double Rotacion Ejemplo 2 Tree luego de insert 7: z La root de rotacion es x(4), donde la diferencia de height es 2 x 6 4 y y x 9 9 4 2 A z 11 6 11 3 5 3 8 A B C D 10 11 B 5 8 10 11 D 4 7 C 7

  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

  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

  20. 25:48 16:20 32:-- 56:72 12,15 16,18 20,23 25,26,28 32,41,43 48,52,53 56,61 72,88 Insert Original Insert 50 25:48 16:20 32:-- 56:72 12,15 16,18 20,23 25,26,28 32,41,43 48,52,50, 53 56,61 72,88 Insert 50 (cont) 53 es movido a los vecinos del nodo hermano 25:48 16:20 32:-- 53:72 12,15 16,18 20,23 25,26,28 32,41,43 48,50,52 53, 56,61 72,88

  21. 25:48 16:20 32:-- 53:72 12,15 16,18 20,23 25,26,28 32,41,43 48,49, 50,52 53, 56,61 72,88 25:48 16:20 32:-- 50:53:72 12,15 16,18 20,23 25,26,28 32,41,43 48,49 50,52 53, 56,61 72,88 25:50 16:20 32:48 53:72 12,15 16,18 20,23 25,26,28 32,41,43 48,49 50,52 53, 56,61 72,88 Insert 49 Split el nodo Envia un hijo a los vecinos del hermano

  22. Insert 27 25:50 16:20 32:48 53:72 12,15 16,18 20,23 25,26,27,28 32,41,43 48,49 50,52 53, 56,61 72,88 Split el nodo 25:50 16:20 27:32:48 53:72 12,15 16,18 20,23 25,26 27,28 32,41,43 48,49 50,52 53, 56,61 72,88 25:32:50 Split otra vez. 16:20 27:-- 48,-- 53:72 12,15 16,18 20,23 25,26 27,28 32,41,43 48,49 50,52 53, 56,61 72,88

  23. Insert 27 (cont’) Split la root 32:-- 25:-- 50:-- 16:20 27:-- 48,-- 53:72 12,15 16,18 20,23 25,26 27,28 32,41,43 48,49 50,52 53, 56,61 72,88

  24. Delete 32:-- 25:-- 50:-- 16:20 27:-- 48:-- 53:72 12,15 16,18 20,23 25,26 27,28 32,41,43 48,49 50,52 53,56,61 72,88 32:-- 25:-- 50:-- 16:20 27:-- 48:-- 56:72 12,15 16,18 20,23 25,26 27,28 32,41,43 48,49 50,52 56,61 72,88

  25. 32:-- 25:-- 50:-- 16:20 27:-- 43:-- 56:72 12,15 16,18 20,23 25,26 27,28 32,41 43,49 50,52 56,61 72,88 32:-- 25:-- 50:-- 16:20 27:-- 43:-- 56:72 12,15 16,18 20,23 25,26 27,28 32,41,49 50,52 56,61 72,88

  26. 32:-- 25:-- 56:-- 16:20 27:-- 50:-- 72:-- 12,15 16,18 20,23 25,26 27,28 32,41,49 50,52 56,61 72,88 32:-- 25:-- 56:-- 16:20 27:-- 50:-- 72:-- 12,15 16,18 20,23 25,26 27,28 32,41,49 50,52 56,72,88

  27. 32:-- 25:-- 56:-- 16:20 27:-- 50:56 12,15 16,18 20,23 25,26 27,28 32,41,49 50,52 56,72,88 32:-- 25:32 16:20 27:-- 50:56 12,15 16,18 20,23 25,26 27,28 32,41,49 50,52 56,72,88

  28. 25:32 16:20 27:-- 50:56 12,15 16,18 20,23 25,26 27,28 32,41,49 50,52 56,72,88 25:32 16:20 27:-- 50:56 12,15 16,18 20,23 25,26,28 32,41,49 50,52 56,72,88 20:32 16:-- 25:-- 50:56 12,15 16,18 20,23 25,26,28 32,41,49 50,52 56,72,88

  29. 50:-- 48:-- 54:72 32,41,43 48,49 50, 53 54,56,61 72,88 Operaciones sobre un B-Trees • Find: • Atravesar el tree • Insert • Hallar e insertar • Dar un camino o Split • Delete • Hallar y borrar • Combinar

  30. Topicos • Depth de un B-Trees • Tiempo de Find, Insert, Delete

  31. Depth de B-Trees 32:-- 25:-- 50:-- 20:-- 27:-- 48:-- 72:-- 16,18 20,23 25,26 27,28 32,41 48,49 50,52 72,88

  32. Find 12:34:38:42:55:64:69:75:--:--:-- 32:-- O(logMN) 25:-- 50:-- 16:20 27:-- 48:-- 53:72 12,15 16,18 20,23 25,26 27,28 32,41,43 48,49 50,52 53,56,61 72,88 O(M) O(log M)

  33. Insert y Delete 12:34:38:42:55:64:69:75:--:--:-- 32:-- 25:-- 50:-- O(logMN) 20:-- 27:-- 48:-- 53:72 O(log N) 16,18 20,23 25,26 27,28 32,41,43 48,49 50,52 53,56 O(M): inserta un elemento en la lista

  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)

  35. 18 32 56 72 25 48 12 15 17 18 23 25 26 28 32 41 43 48 53 56 61 72 88 B-Trees en Memoria • Nodo Interior • M punteros y M-1 keys • Nodo Leaf • M’ datos (o M’ punteros a datos)

  36. Find 32:-- O(logMN) 25:-- 50:-- 16:20 27:-- 48:-- 53:72 12,15 16,18 20,23 25,26 27,28 32,41,43 48,49 50,52 53,56,61 72,88 12:34:38:42:55:64:69:--:-- t1: main memory tiempo de aceso para todo byte.t2: todo disco acceso de un block O(t2 + t1 log M)

  37. Insert y Delete 32:-- 25:-- 50:-- 20:-- 27:-- 48:-- 53:72 16,18 20,23 25,26 27,28 32,41,43 48,49 50,52 53,56 12:34:38:42:55:64:69:--:-- O(logMN) O(t2 + t1 M)

More Related