Documente Academic
Documente Profesional
Documente Cultură
Agenda
Introduccin
Conceptos bsicos
rboles Binarios
Concepto de rbol
Longitud, profundidad y altura
Relaciones entre nodos
Definicin
Propiedades
Operaciones de recorrido
Condiciones efectivas para la bsqueda
INTRODUCCIN
Modelos lineales vs. jerrquicos
INTRODUCCIN
Estructuras jerrquicas
eda
libreras
estructuras
5
aplicaciones
util
figuras
hospital
INTRODUCCIN
Estructuras jerrquicas
*
a
+
b
e
d
INTRODUCCIN
Estructuras jerrquicas
Usando un rbol
100
50
Coste lineal
25
200
75
150
cunto cuesta as ?
7
300
CONCEPTOS BSICOS
Definicin de rbol
CONCEPTOS BSICOS
Definicin de rbol
Ejemplo:
Nodo raz: A
Nodos hojas:
{D, M, N, F, J, K, L}
Nodos internos:
{A, B, E, I, C, G, H}
CONCEPTOS BASICOS
Longitud, profundidad y altura
10
La profundidad de la raz es 0
CONCEPTOS BASICOS
Relaciones entre nodos
11
u es un ascendiente de v
v es un descendiente de u
Si u != v, entonces
u es un ascendiente propio de v
v es un descendiente propio de u
RBOLES BINARIOS
Definicin
12
RBOLES BINARIOS
Propiedades
13
RBOLES BINARIOS
rboles Binarios llenos
14
H = log2N
N = 2H+1-1
RBOLES BINARIOS
rboles Binarios completos
15
2H N 2H+1 -1
RBOLES BINARIOS
Operaciones de recorrido
16
17
18
Ejemplo:
eMC = (1 * (1+0) + 2 * (1+1) +
+ 2 * (1+2) + 1 * (1+3) ) / 6 =
= (1+ 4+ 6 + 4) / 6 = 2.5
20
BST
21
BST
NodoBST
NodoBST
22
NodoBST
23
E recover(E x)
E minRecover()
Operaciones de insercin:
void
insertDuplicates(E x)
void insert(E x)
Void update(E x)
Operaciones de eliminacin:
24
void remove(E x)
void minRemove()
Consultores:
int tamanio()
Int height()
Bool isEmpty()
Double eMC()
Operaciones de recorrido:
void
void
void
void
preOrder()
postOrder()
inOrder()
levels()
// Atributos
NodoBST<E> *root;
int totComparisions;
// Constructores de un ABB vacio
public:
BST(){
root = NULL;
totComparisions = 0;
}
};
25
eMC = totComparisions
root->tamanio
return res->data;
}
template <class E> NodoBST<E>* BST<E>::recover(E x, NodoBST<E> *n){
if (n == NULL) return NULL;
else {
if (n->data < x) return recover(x, n->rigth);
else if (n->data > x) return recover(x, n->left);
else return n;
}
}
26
recover(5, 7 );
7 > 5
return recover(5, 7 ->left)
5
recover(5, 2 );
2 < 5
return recover(5,
->right)
recover(5, 5 );
5 == 5
return 5
5
27
return
->dato;
29
insert(6, 7 );
7 > 6
7 ->left = insert(6, 7 ->left)
return 7
insert(6, );
2
2 < 6
2 ->right = insert(6, 2 ->right)
return 2
2
insert(6, 5 );
5 < 6
5 ->right = insert(6, 5 ->right)
return 5
5
insert(6,NULL);
return 6
6
remove(5, 7 );
7 > 5
7 ->left = remove(5, 7 ->left)
return 7
remove(5, 2 );
2 < 5
2 ->right = remove(5, 2 ->right)
return 2
2
remove(5, 5 );
5 == 5
return 5 ->left)
3
31
remove(7, 7 );
7 == 7
7 ->dato = minRecover(
->right)->dato
->right) minRecover(
return 8
8
->right = minRemove(
return 8
8
32
minRemove( 9 );
9 ->left = minRemove( 9 ->left)
return 9
NULL
minRemove( 8 );
8 ->left == NULL
return 8 ->right
);
33
minRemove( 7 );
7 ->left = minRemove(
return 7
->left)
minRemove( 4 );
4 ->left = minRemove(
return 4
34
->left)
minRemove( 1 );
1 ->left == NULL
return 1 ->right
35