7. El TDA Diccionario

65
7. El TDA Diccionario

description

7. El TDA Diccionario. ¿Qué es un Diccionario ? . Dado un conjunto de elementos {X 1 , X 2 , ..., X N } , todos distintos entre sí, se desea almacenarlos en una estructura de datos que permita la implementación eficiente de las operaciones: - PowerPoint PPT Presentation

Transcript of 7. El TDA Diccionario

Page 1: 7. El TDA Diccionario

7.  El TDA Diccionario

Page 2: 7. El TDA Diccionario

¿Qué es un Diccionario ? • Dado un conjunto de elementos {X1, X2, ..., XN}, todos distintos

entre sí, se desea almacenarlos en una estructura de datos que permita la implementación eficiente de las operaciones:

• búsqueda(X): dado un elemento X, conocido como llave de búsqueda, encontrarlo dentro del conjunto o decir que no está.

• inserción(X): agregar un nuevo elemento X al conjunto. • eliminación(X): eliminar el elemento X del conjunto. • Estas operaciones describen al TDA diccionario. En el presente

capítulo se verán distintas implementaciones de este TDA y se estudiarán las consideraciones de eficiencia de cada una de dichas implementaciones.

Page 3: 7. El TDA Diccionario

Implementaciones sencillas• Una lista enlazada, con la inserción de nuevos

elementos al comienzo. – búsqueda: O(n) (búsqueda secuencial). – inserción: O(1) (insertando siempre al comienzo de la

lista). – eliminación: O(n) (búsqueda + O(1)).

• Arreglo ordenado: inserción y eliminación ineficientes, puesto ("correr" los elementos)

• Sin embargo, la ventaja que tiene mantener el orden es que es posible realizar una búsqueda binaria para encontrar el elemento buscado.

Page 4: 7. El TDA Diccionario

Programacion de la Búsqueda binariaInvariante

Inicialmente: i = 0 y j = n-1.

En cada iteración: Si el conjunto es vacío (j-i < 0), o sea si j < i, entonces el elemento x no está en el conjunto (búsqueda infructuosa). En caso contrario, m = (i+j)/2. Si x = a[m], el elemento fue encontrado (búsqueda exitosa). Si x < a[m] se modifica j = m-1, sino se modifica i = m+1 y se sigue iterando.

Page 5: 7. El TDA Diccionario

Programacion de la Búsqueda binariapublic int busquedaBinaria(int []a, int x) { int i=0, j=a.length-1; while (i<=j) { int m=(i+j)/2; if (x==a[m]) return m; else if (x<a[m]) j=m-1; else i=m+1; } return NO_ENCONTRADO; // NO_ENCONTRADO se define como -1 }

Page 6: 7. El TDA Diccionario

Eficiencia de la Búsqueda binaria• Todo algoritmo de búsqueda basado en comparaciones

corresponde a algún árbol de decisión. • Cada nodo de dicho árbol corresponde al conjunto de

elementos candidatos en donde se encuentra el elemento buscado, y que es consistente con las comparaciones realizadas entre los elementos. Los arcos del árbol corresponden a los resultados de las comparaciones, que en este caso pueden ser mayor que o menor que el elemento buscado, es decir, es un árbol de decisión binario.

• El número de comparaciones realizadas por el algoritmo de búsqueda es igual a la altura del árbol de decisión (profundidad de la hoja más profunda).

Page 7: 7. El TDA Diccionario

Eficiencia de la Búsqueda binariaLema: sea D un árbol binario de altura h. D tiene a lo más 2h hojas. Demostración: por inducción.

Lema: un árbol binario con H hojas debe tener una profundidad de al menos Demostración: directo del lema anterior.

Si n es el número de nodos de elementos del conjunto, el número de respuestas posibles (hojas del árbol de decisión) es de n+1, el lema anterior implica que el costo en el peor caso es mayor o igual que el logaritmo del número de respuestas posibles. Corolario: cualquier algoritmo de búsqueda mediante comparaciones se demora al menos preguntas en el peor caso. Por lo tanto, la búsqueda binaria es óptima.

Page 8: 7. El TDA Diccionario

Métodos auto-organizantes•Idea: cada vez que se accede a un elemento Xk se modifica la

lista para que los accesos futuros a Xk sean más eficientes. Algunas políticas de modificación de la lista son:

•TR (transpose): se intercambia de posición Xk con Xk-1 (siempre que k>1).

•MTF (move-to-front): se mueve el elemento Xk al principio de la lista.

•Se puede demostrar que Costooptimo<=CostoTR<=CostoMTF<=2Costooptimo.

Page 9: 7. El TDA Diccionario

¿árbol binario de búsqueda o árbol de búsqueda binaria(ABB)? Estructura de datos (recursiva) que está vacía , o contiene un nodo raíz con un valor y referencias a dos ABBs

(izquierdo y derecho) los valores contenidos en el subárbol izquierdo son menores que el

valor en la raíz los valores contenidos en el subárbol derecho son mayores que el

valor en la raíz Ejemplo: raíz D izq valor der B F A C E G

Page 10: 7. El TDA Diccionario

Diccionario: implementación con ABB tiempo de todas las operaciones proporcional a altura del árbol si está balanceado, altura=log2n raíz E E’ izq pal sig der C C’ G G’ B B’ D D’ H H’ class Nodo{ public Comparable palabra; public Object significado; public Nodo izq, der; public Nodo( Comparable x,Object y,Nodo z,Nodo w){

palabra=x; significado=y; izq=z; der=w; } }

Page 11: 7. El TDA Diccionario

class Diccionario{protected Nodo raiz;

public Diccionario(){raiz=null;

}public Object buscar(Comparable x){

Nodo r=referencia(x,raiz);return r==null ? null : r.significado;

}public boolean cambiar(Comparable x,Object y){

Nodo r=referencia(x,raiz);if(r==null) return false;r.significado = y;return true;

}

Page 12: 7. El TDA Diccionario

//búsqueda iterativa en un ABBprotected Nodo referencia(Comparable x,Nodo r){

while(r!=null){int c=x.compareTo(r.palabra);if(c==0) return r;r = c<0 ? r.izq : r.der;

}return null;

}//búsqueda recursiva en un ABBprotected Nodo referencia(Comparable x,Nodo r){

if(r==null) return null;int c=x.compareTo(r.palabra);if(c==0) return r;return referencia(x, c<0 ? r.izq : r.der);

}

Page 13: 7. El TDA Diccionario

public boolean agregar(Comparable x,Object y)throws DiccLleno{

if(referencia(x,raiz)!=null) return false;raiz=agregar(x,y,raiz);return true;

}protected Nodo agregar(Comparable x,Object y,Nodo r)throws DiccLleno{

if(r==null) return new Nodo(x,y,null,null);if(x.compareTo(r.palabra) < 0)

r.izq=agregar(x,y,r.izq);else

r.der=agregar(x,y,r.der);return r;

}

Page 14: 7. El TDA Diccionario

Solución 2: en un sólo recorrido no recursivo del ABBpublic boolean agregar(Comparable x,Object y)throws DiccLleno{

Nodo q=new Nodo(x,y,null,null);if(raiz==null){raiz=q; return true;}Nodo r=raiz;while(true){

int c=x.compareTo(r.palabra);if(c==0) return false; //ya existeif(c<0)

if(r.izq==null){r.izq=q; break;}else r=r.izq;

elseif(r.der==null){r.der=q; break;}else r=r.der;

}return true;

}

Page 15: 7. El TDA Diccionario

public boolean borrar(Comparable x){Nodo r=referencia(x, raiz);if(r==null) return false;raiz=borrar(x,raiz);return true;

}//borrar Nodo con x y devolver raíz del ABBprotected Nodo borrar(Comparable x,Nodo r){ if(r==null) return null; int c=x.compareTo(r.palabra);

if(c==0) return borrar(r);if(c<0)

r.izq=borrar(x,r.izq);else

r.der=borrar(x,r.der);return r;

}

Page 16: 7. El TDA Diccionario

Caso 1 y 2: borrar hoja izquierda o derecha(ej:“H”) raíz E E’ izq pal sig der r C C’ G G’ null B B’ D D’ H H’ Caso 3: borrar raíz reemplazando por mayor de árbol izquierdo Caso 3.1: borrar “C” (mayor en raíz de árbol izquierdo) raíz E E’ izq pal sig der r null B B’ G G’ B B’ D D’ H H’ Caso 3: borrar “E” (mayor en el extremo derecho de árbol izquierdo) raíz D D’ r izq pal sig der C C’ null G G’ B B’ D D’ H H’

Page 17: 7. El TDA Diccionario

protected Nodo borrar(Nodo r){//caso 1: sólo árbol derechoif(r.izq==null) return r.der;

//Caso 2:solo árbol izquierdoif(r.der==null) return r.izq;

//Caso 3:reemplazar por mayor de arbol izq

//caso 3.1: mayor en raíz de árbol izquierdoif(r.izq.der==null){

r.palabra = r.izq.palabra;r.significado = r.izq.significado;r.izq = r.izq.izq; //enlazar hijos menores

}

Page 18: 7. El TDA Diccionario

//caso 3.2: mayor a la derecha de árbol izqelse{

//buscar ref de antecesor de mayorNodo rAnt=r.izq;while(rAnt.der.der!=null)

rAnt=rAnt.der;

//reemplazar raiz por mayorr.palabra = rAnt.der.palabra;r.significado = rAnt.der.significado;rAnt.der = rAnt.der.izq;//enlazar menores

}return r;}Propuesto: borrar reemplazando por menor de árbol derecho

Page 19: 7. El TDA Diccionario

Solución 2: en un recorrido no recursivopublic boolean borrar(Comparable x){//buscar ref r de x, recordando ref antecesorNodo r=raiz, rAnt=null;

while(r!=null){ int c=x.compareTo(r.palabra); if(c==0) break; rAnt=r; r=c<0 ? r.izq: r.der;}if(r==null) return false;//no está

Page 20: 7. El TDA Diccionario

//borrar nodo r, actualizando antecesorif(r==raiz)

raiz=borrar(r);

else if(r==rAnt.izq)

rAnt.izq=borrar(r);

else

rAnt.der=borrar(r);

return true;}

Page 21: 7. El TDA Diccionario

21

AVL-Trees (Adelson-Velskii & Landis, 1962)

In normal search trees, the complexity of find, insert and delete operations in search trees is in the worst case: (n).

Can be better! Idea: Balanced trees.Definition: An AVL-tree is a binary search tree such that

for each sub-tree T ' = < L, x, R > | h(L) - h(R) | 1 holds

(balanced sub-trees is a characteristic of AVL-trees).The balance factor or height is often annotated at each

node h(.)+1.

Page 22: 7. El TDA Diccionario

22

|Height(I) – hight(D)| < = 1

This is an AVL tree

Page 23: 7. El TDA Diccionario

23

This is NOT an AVL tree (node * does not hold the required condition)

Page 24: 7. El TDA Diccionario

24

Goals

1. How can the AVL-characteristics be kept when inserting and deleting nodes?

2. We will see that for AVL-trees the complexity of the operations is in the worst case

= O(height of the AVL-tree)= O(log n)

Page 25: 7. El TDA Diccionario

25

Preservation of the AVL-characteristics

After inserting and deleting nodes from a tree we must procure that new tree preserves the characteristics of an AVL-tree:

Re-balancing. How ?: simple and double rotations

Page 26: 7. El TDA Diccionario

Only 2 cases (an their mirrors)

• Let’s analyze the case of insertion

– The new element is inserted at the right (left) sub-tree of the right (left) child which was already higher than the left (right) sub-tree by 1

– The new element is inserted at the left (right) sub-tree of the right (left) child which was already higher than the left (right) sub-tree by 1

26

Page 27: 7. El TDA Diccionario

27

Rotation  (for the case when the right sub-tree grows too high after an insertion)

Is transformed into

Page 28: 7. El TDA Diccionario

28

Double rotation (for the case that the right sub-tree grows too high after an insertion at its left sub-tree)

Is transformed into

Double rotation

Page 29: 7. El TDA Diccionario

29

W

Z

a

b

cx

y

W Z

a

b

c

x y

new

new

First rotation

Second rotation

Page 30: 7. El TDA Diccionario

30

Re-balancing after insertion: After an insertion the tree might be still balanced or:

theorem: After an insertion we need only one rotation of double-rotation at the first node that got unbalanced * in order to re-establish the balance properties of the AVL tree.

(* : on the way from the inserted node to the root).

Because: after a rotation or double rotation the resulting tree will have the original size of the tree!

Page 31: 7. El TDA Diccionario

The same applies for deleting

• Only 2 cases (an their mirrors)– The element is deleted at the right (left) sub-tree of

which was already smaller than the left (right) sub-tree by 1

– The new element is inserted at the left (right) sub-tree of the right (left) child which was already higher that the left (right) sub-tree by 1

31

Page 32: 7. El TDA Diccionario

The cases

32

1

1

1

Deleted node

Page 33: 7. El TDA Diccionario

33

Re-balancing after deleting:

After deleting a node the tree might be still balanced or:

Theorem: after deleting we can restore the AVL balance properties of the sub-tree having as root the first* node that got unbalanced with just only one simple rotation or a double rotation.

(* : on the way from the deleted note to the root). However: the height of the resulting sub-tree might be

shortened by 1, this means more rotations might be (recursively) necessary at the parent nodes, which can affect up to the root of the entire tree.

Page 34: 7. El TDA Diccionario

34

About Implementation While searching for unbalanced sub-tree after an operation

it is only necessary to check the parent´s sub-tree only when the son´s sub-tree has changed it height.

In order make the checking for unbalanced sub-trees more efficient, it is recommended to put some more information on the nodes, for example: the height of the sub-tree or the balance factor (height(left sub-tree) – height(right sub-tree)) This information must be updated after each operation

It is necessary to have an operation that returns the parent of a certain node (for example, by adding a pointer to the parent).

Page 35: 7. El TDA Diccionario

35

Complexity analysis– worst case

Be h the height of the AVL-tree.

Searching: as in the normal binary search tree O(h).

Insert: the insertion is the same as the binary search tree (O(h)) but we must add the cost of one simple or double rotation, which is constant : also O(h).

delete: delete as in the binary search tree(O(h)) but we must add the cost of (possibly) one rotation at each node on the way from the deleted node to the root, which is at most the height of the tree: O(h).

All operations are O(h).

Page 36: 7. El TDA Diccionario

36

Calculating the height of an AVL tree Be N(h) the minimal number of nodes In an AVL-tree having height h.

N(0)=1, N(1)=2,N(h) = 1 + N(h-1) + N(h-2) for h 2.N(3)=4, N(4)=7 remember: Fibonacci-numbersfibo(0)=0, fibo(1)=1, fibo(n) = fibo(n-1) + fibo(n-2)fib(3)=1, fib(4)=2, fib(5)=3, fib(6)=5, fib(7)=8By calculating we can state:N(h) = fibo(h+3) - 1

0

12 3

Principle of construction

Page 37: 7. El TDA Diccionario

37

Be n the number of nodes of an AVL-tree of height h. Then it holds that:n N(h) ,Remember fn =(1 /sqrt(5)) (Ф1

n - Ф2n)

with Ф1= (1+ sqrt(5))/2 ≈ 1.618 Ф2= (1- sqrt(5))/2 ≈ 0.618

we can now writen fibo(h+3)-1 = (Ф1

h+3 – Ф2h+3 ) / sqrt(5) – 1

(Ф1 h+3/sqrt(5)) – 3/2,thush+3+log Ф1(1/sqrt(5)) log Ф1(n+3/2),thus there is a constant c withh log Ф1(n) + c = log Ф1(2) • log2(n) + c = 1.44… • log2(n) + c = O(log n).

Page 38: 7. El TDA Diccionario

38

 Arboles B (External Search)• The algorithms we have seen so far are good when all

data are stored in primary storage device (RAM). Its access is fast(er)

• Big data sets are frequently stored in secondary storage devices (hard disk). Slow(er) access (about 100-1000 times slower)

Access: always to a complete block (page) of data (4096 bytes), which is stored in the RAM

For efficiency: keep the number of accesses to the pages low!

Page 39: 7. El TDA Diccionario

39

 Arboles 2-3• Los nodos internos pueden contener hasta 2

elementos • por lo tanto un nodo interno puede tener 2 o 3 hijos,

dependiendo de cuántos elementos posea el nodo.

Page 40: 7. El TDA Diccionario

40

 Propiedad• todas las hojas están a la misma profundidad, es

decir, los árboles 2-3 son árboles perfectamente balanceados

• La altura está acotada por

Page 41: 7. El TDA Diccionario

41

 Inserción• se realiza una búsqueda infructuosa y se inserta

dicho elemento en el último nodo visitado durante la búsqueda,

• implica manejar dos casos distintos:

Page 42: 7. El TDA Diccionario

42

 Ejemplos

Page 43: 7. El TDA Diccionario

43

 Eliminación• Físicamente se debe eliminar un nodo del último

nivel• Si el elemento a borrar está en un nodo interno el

valor se reemplaza por el inmediatamente anterior/posterior

• Estos necesariamente están en último nivel

Page 44: 7. El TDA Diccionario

44

 Caso simple• El nodo donde se encuentra Z contiene dos

elementos. En este caso se elimina Z y el nodo queda con un solo elemento.

Page 45: 7. El TDA Diccionario

45

 Caso complejo 1• El nodo donde se encuentra Z contiene un solo

elemento. En este caso al eliminar el elemento Z el nodo queda sin elementos (underflow). Si el nodo hermano posee dos elementos, se le quita uno y se inserta en el nodo con underflow.

Page 46: 7. El TDA Diccionario

46

 Caso complejo 2• Si el nodo hermano contiene solo una llave, se le quita un

elemento al padre y se inserta en el nodo con underflow. • Si esta operación produce underflow en el nodo padre, se

repite el procedimiento anterior un nivel más arriba. Finalmente, si la raíz queda vacía, ésta se elimina.

• Costo de las operaciones de búsqueda, inserción y eliminación en el peor caso: Θ (log(n))

Page 47: 7. El TDA Diccionario

47

For external search: a variant of search trees:1 node = 1 page

Multiple way search trees!

Page 48: 7. El TDA Diccionario

48

Multiple way-search trees Definición: An empty tree is a multiple way search tree with an

empty set of keys {} .

Be T0, ..., Tn multiple way-search trees with keys taken from a common key set S, and be k1,...,kn a sequence of keys with k1 < ...< kn. Then is the sequence:

T0 k1 T1 k2 T2 k3 .... kn Tn

a multiple way-search trees only when:

• for all keys x from T0 x < k1 • for i=1,...,n-1, for all keys x in Ti, ki < x < ki+1 • for all keys x from Tn kn < x

Page 49: 7. El TDA Diccionario

49

B-Tree

Definition

A B-Tree of Order m is a multiple way tree with the following characteristics

• 1 #(keys in the root) 2m and m #(keys in the nodes) 2m for all other nodes.• All paths from the root to a leaf are equally long. • Each internal node (not leaf) which has s keys has exactly s+1

children. • 2-3 Trees is a particular case for m=1

Page 50: 7. El TDA Diccionario

50

Example: a B-tree of order 2:

Page 51: 7. El TDA Diccionario

51

Assessment of B-treesThe minimal possible number of nodes in a B-tree of order m

and height h:• Number of nodes in each sub-tree

1 + (m+1) + (m+1)2 + .... + (m+1)h-1

= ( (m+1)h – 1) / m.

The root of the minimal tree has only one key and two children, all other nodes have m keys.

Altogether: number of keys n in a B-tree of height h: n 2 (m+1)h – 1

Thus the following holds for each B-tree of height h with n keys:h logm+1 ((n+1)/2) .

Page 52: 7. El TDA Diccionario

52

ExampleThe following holds for each B-tree of height h with n keys:

h logm+1 ((n+1)/2).

Example: for• Page size: 1 KByte and • each entry plus pointer: 8 bytes, If we chose m=63, and for an ammount of data of n= 1 000 000 We have

h log 64 500 000.5 < 4 and with that hmax = 3.

Page 53: 7. El TDA Diccionario

53

Key searching algorithm  in a B-tree 

Algorithm search(r, x) //search for key x in the tree having as root node r; //global variable p = pointer to last node visited in r, search for the first key y >= x or until no more keys if y == x {stop search, p = r, found} else if r a leaf {stop search, p = r, not found} else if not past last key search(pointer to node before y, x) else search(last pointer, x)

Page 54: 7. El TDA Diccionario

54

Inserting and deleting of keys

Algorithm insert (r, x) //insert key x in the tree having root r search for x in tree having root r; if x was not found { be p the leaf where the search stopped; insert x in the right position; if p now has 2m+1 keys {overflow(p)} }

Page 55: 7. El TDA Diccionario

55

Algorithm overflow (p) = split (p)

Algorithm split (p) first case: p has a parent q.

Divide the overflowed node. The key of the middle goes to the parent.

remark: the splitting may go up until the root, in which case the height of the tree is incremented by one.

Algorithm Split (1)

Page 56: 7. El TDA Diccionario

56

Algorithm split (p) second case: p is the

root.

Divide overflowed node. Open a new level above containing a new root with the key of the middle (root has one key).

Algorithm Split (2)

Page 57: 7. El TDA Diccionario

57

//delete key x from tree having root r search for x in the tree with root r; if x found { if x is in an internal node { exchange x with the next bigger key x' in the tree // if x is in an internal node then there must // be at least one bigger number in the tree //this number is in a leaf ! } be p the leaf, containing x; erase x from p; if p is not in the root r { if p has m-1 keys {underflow (p)} } }

Algorithm delete (r,x)

Page 58: 7. El TDA Diccionario

58

Algorithm underflow (p)

if p has a neighboring node with s>m nodes { balance (p,p') }else // because p cannot be the root, p must have a neighbor with

m keys { be p' the neighbor with m keys; merge (p,p')}

Page 59: 7. El TDA Diccionario

59

Algorithm balance (p, p') // balance node p with its neighbor p'

(s > m , r = (m+s)/2 -m )

Page 60: 7. El TDA Diccionario

60

Algorithm merge (p,p') // merge node p with its neighbor perform the following operation:

afterwards:if( q <> root) and (q

has m-1 keys) underflow (q)

else (if(q= root) and (q empty)) {free q let root point to p^}

Page 61: 7. El TDA Diccionario

61

Recursion

If when performing underflow we have to perform merge, we might have to perform underflow again one level up

This process might be repeated until the root.

Page 62: 7. El TDA Diccionario

62

Example:B-Tree of order 2 (m = 2)

Page 63: 7. El TDA Diccionario

63

Cost

Be m the order of the B-tree, n the number of keys.

Costs for search , insert and delete: O(h) = O(logm+1 ((n+1)/2) ) = O(logm+1(n)).

Page 64: 7. El TDA Diccionario

64

Remark:

B-trees can also be used as internal storage structure:

Especially: B-trees of order 1 (then only one or 2 keys in each node – no elaborate search inside the nodes).

Cost of search, insert, delete: O(log n).

Page 65: 7. El TDA Diccionario

65

Remark: use of storage memory Over 50%reason: the condition:

1/2•k #(keys in the node) k For nodes root

(k=2m)