Professional Documents
Culture Documents
ESTRUCTURAS DE DATOS
CONCEPTOS
125
80 15 20 30 28 25
75
72
UTILIDAD DE UN HEAP
El heap se utiliza
IMPLEMENTACION
2
150
5 6
125
75
80
20
9
30
28
10
25
72
En el orden de llenado
i
1 2 3 4 5 6
i/2
i z q ( i ) d e r ( i ) p a d r e ( i )
2 4 6 8 1 0 -
Podemos conocer los indices de los hijos y el padre de un nodo Ejemplo: Del nodo 4, hijos 8 y 9, padre 2
i*2
3 5 7 9 -
i*2+1
1 1 2 2 3
CONTANDO DESDE 0
150
4 5
125
75
80 15
7
30 20
8
25
72
Izq(i) = i*2
Der(i) = i*2+1 Padre(i) = i/2
0 1
28
9
150 125 75
2
80
3
30
4
25
5
72
6
15
7
20
8
28
9
REGLAS
Si 2i+1 < n, V[2i+1] es el hijo izq Si 2i+2 < n, V[2i+2] es el hijo der Si i != 0, v[(i-1)/2] es el padre
Si es heap
i : 1 i n : v [ i 1 /2 ] v [ i ]
Tipo de Dato
Necesitamos un arreglo Llevar el tamao efectivo Llevar el mximo del arreglo typedef struct{ Generico *Elementos; int nefectivo; int max; Tipo_Orden tipo; }Heap;
Desencolar y Encolar
Desencolar: Extraer el elemento mas grande/pequeo del heap(la raiz) Encolar: Insertar un valor al heap y ubicarlo en la posicion correcta Dado un Heap permite generar un arreglo/lista ordenado Ajustar
Reestablece la propiedad de orden de un subheap hacia abajo No a todo el arbol!
HeapSort
Operaciones Adicionales
Construir_heap
Dado un arreglo que no representa un heap Arregla el arreglo y lo convierte en un Heap
MAS OPERACIONES
AJUSTAR
Se compararan los tres para ver quien tiene el mayor Si el mayor lo tiene algun hijo
Intercambia Al hacer esto, el sub-heap afectado puede perder su propiedad de Orden. Ajustar el sub-heap afectado
AJUSTAR: EJEMPLO
0
Pos
3
150
4 5
Por un nodo
80 60
75
Ejemplo: Ejemplo:
Pos PosMayor
80 60 70 20
7
30
70 70 60
8
25
72
En el el nodo nodo 1 no no se se cumple cumple No En se puede 3 ajustar un nodo hoja Ajustar Ajustar
El mayor mayor es es el el nodo nodo 3 3 El Intercambiar Intercambiar Ajustar Ajustar Otra desde nodo Vez intercambiado(3)
28
9
PosMayor 25
5
150 80 60
0 1
75
2
80 70
3
30
4
72
6
20
7
70 60
8
28
9
AJUSTAR: IMPLEMENTACION
static void Heap_Ajustar(Heap *H, int posnodo, Generico_fnComparar comocomparar){ int pos_mayor, izq, der; pos_mayor = posnodo; izq = IdxIzquierdo(*H, posnodo); der = IdxDerecho(*H, posnodo); if(izq>=0 && Heap_CompararxTipo(H->tipo_orden,H->Elementos[izq], H->Elementos[posnodo],comocomparar) ) pos_mayor = izq; if(der>=0 && Heap_CompararxTipo(H->tipo_orden,H->Elementos[der], H->Elementos[pos_mayor], comocomparar)) pos_mayor = der; if(pos_mayor != posnodo){ Generico_Intercambiar(&(H->Elementos[pos_mayor]), &(H->Elementos[posnodo])); Heap_Ajustar(H,pos_mayor,comocomparar); } }
CONSTRUIR UN HEAP
La estructura de un heap
Ejemplo:
Hay que arreglar la propiedad de orden
1
1
28
2
35
3
10
4
5
5
0
8
6
21
7
50
8
42
9
15
4 5
28
35 50
8
10 42
9
15 50
0
42 1
1
28
2
35
3
10 15
4
5
5
0
8
6
21
7
50 1
8
42 10
9
50 15
4 5
50 42 15 1
28
50 35 1 50 35 1
8
10 42 15 42 10
9
int i;
for(i = H->n/2-1; i >= 0; i--){ Heap_Ajustar(H,i,comocomparar);
}
}
DESENCOLAR
Es el mayor/menor elemento del mismo Sobre el resto de elementos no estamos seguros Pero de la raiz, es la mayor de todos
Que valor se ubica en la nueva raiz? El ultimo reemplaza al primero El tamao efectivo del heap cambia
DESENCOLAR: EJEMPLO
0
Intercambiar valores
150 125 28
4
5
80 28 125
75
28 80 20
8
30 28
9
25
72
80 28
3
30
4
25
5
72
6
15
7
20
8
28
9
DESENCOLAR: IMPLEMENTACION
Generico Heap_DesEnColar(Heap *H, Generico_fnComparar comocomparar){ Generico gmax; if(!Heap_EstaVacio(*H)){ gmax = H->Elementos[0]; Generico_Intercambiar(&(H->Elementos[0]), &(H->Elementos[H->nefectiva-1])); H->nefectiva --; Heap_Ajustar(H, 0, comocomparar); return gmax; } return NULL; }
HEAPSORT
HEAPSORT: EJEMPLO
1
125 28 15 20 75 72 30 25 28 80 150
4 5
125 30 28 20 15 80
75 72 25 15
28 15 80
20
8
30 20
28
9
25 15
72 15
80 28 15
3
30 20
4
25 15
5
72 15
6
15
7
20
8
28
9
15
0
20
1
25
2
28
3
30
4
72
5
75
6
80 125 150
7 8 9
HEAPSORT: IMPLEMENTACION
LSE *HeapSort(LSE *Desordenada, Generico_fncomparar comocomparar){
Heap H; LSE_nodo *p; p = LSE_NodoPrimero(Desordenada); } while(Heap_EstaVacio(H)){ LSE_InsertarInicio(Desordenada, Heap_Desencolar(H, comocomparar); } return Desordenada; } while(!LSE_EstaVacia(Desordenada)) { Heap_Encolar(&H,LSE_SacarPrimerNodo(Desordenada),comocomparar)
ENCOLAR
ENCOLAR: EJEMPLO
0 1
15
7
150
4 5
125 130
75
80 20
8
30 130 125 28
9
25 30 130
72
150 125 75
0 1 2
80
3
30
4
25
5
72
6
15
7
20
8
28 130
9 10
ENCOLAR: IMPLEMENTACION
void Heap_EnColar(Heap *H, Generico G, Generico_fnComparar comocomparar){ int padre, i; if(H->nefectiva < H->max){ H->Elementos[H->nefectiva] = G; H->nefectiva++; i = H->nefectiva-1; padre = IdxPadre(*H,i); while((i>=0 && padre>=0) && Heap_CompararxTipo(H->tipo_orden, H->Elementos[i], H->Elementos[padre], comocomparar)){ Generico_Intercambiar(&(H->Elementos[i]), &(H->Elementos[padre])); i = padre; padre = IdxPadre(*H,i); } } }