Descargar la presentación
La descarga está en progreso. Por favor, espere
1
Árboles Binario de Búsqueda
Universidad de Puerto Rico en Humacao Departamento de Matemáticas COMP4097: Estructuras de Datos y Algoritmos Árboles Binario de Búsqueda José O. Sotero Esteva 8 de abril de 2015 Esta obra está bajo una Licencia Creative Commons Atribución-Compartir Igual 4.0 Internacional.
2
Parte del contenido está basado en
Goodrich, Tamassia, Mount, Data Structures and Algorthms, 2nd Ed. Sección 10.1 (El material se cita aquí bajo la cláusula de uso justo de las leyes de copyright. El material de ese libro está sujeto a las restricciones de reproducción que ha establecido el editor.)
3
TDA Mapa Un mapa M contiene pares de clave-valor en el cual cada valor está asociado a una clave con los métodos: find(k): si M contiene una entrada e = (k,v), devuelve una referencia p a e. put(k,v): Si M no tiene una entrada con clave k entonces añade una (k,v). Si no reemplaza el valor v en la entrada existente con clave k. erase(k): remueve de M la entrada con clave k. Si no existe unatermina con error. size(): Cantidad de elementos en M. empty(): cierto si la M esta vacío, falso e.o.c.
4
¿Qué es una Árbol Binario de Búsqueda ?
Sea n un nodo de un arbol binario de búsqueda La clave de n es mayor que todas las etiquetas en su subárbol izquierdo TI La clave de n es menor que todas las etiquetas en su subárbol derecho TD TI y TD son árboles binarios de búsqueda
5
Ejemplo
6
Algoritmo Búsqueda
7
TreeInsert(78)
8
Remover 32 Sólo tiene un sub-árbol
9
Remover 65 sucesor de 65 Tiene dos sub-árboles
10
Análisis rendimiento ABB
Suponga que un mapa M se implanta con un ABB de altura h. Entonces la complejidad de las operaciones es: find(k): O(h) put(k,v): O(h) erase(k): O(h) size(): O(1) (supone que hay campo para ello) empty(): O(1)
11
Implatación en C++: Pares (k,v)
13
Métodos de SearchTree /* SearchTree E :: */
SearchTree() : T(), n(0) // constructor { T.addRoot(); T.expandExternal(T.root()); } // create the super root TPos root() const // get virtual root { return T.root().left(); } // left child of super root Iterator begin() { // iterator to first entry TPos v = root(); // start at virtual root while (v.isInternal()) v = v.left(); // find leftmost node return Iterator(v.parent()); } Iterator end() // iterator to end entry { return Iterator(T.root()); } // return the super root
14
Métodos de SearchTree /* SearchTree E :: */
TPos finder(const K& k, const TPos& v) { if (v.isExternal()) return v; // key not found if (k < v−>key()) return finder(k, v.left()); // search left subtree else if (v−>key() < k) return finder(k, v.right()); // search right subtree else return v; // found it here } Iterator find(const K& k) { TPos v = finder(k, root()); if (v.isInternal()) return Iterator(v); else return end();
15
Métodos de SearchTree /* SearchTree E :: */
TPos inserter(const K& k, const V& x) { // insert utility TPos v = finder(k, root()); //search from virtual root while (v.isInternal()) //key already exists? v = finder(k, v.right()); //look further T.expandExternal(v); //add new internal node v−>setKey(k); v−>setValue(x); //set entry n++; //one more entry return v; //return insert position } Iterator insert(const K& k, const V& x) { TPos v = inserter(k, x); return Iterator(v); }
16
Métodos de SearchTree /* SearchTree E :: */ TPos eraser(TPos& v) {
TPos w; if (v.left().isExternal()) w = v.left(); // remove from left else if (v.right().isExternal()) w = v.right(); // remove from right else { // both internal? w = v.right(); // go to right subtree do { w = w.left(); } while (w.isInternal()); // get leftmost node TPos u = w.parent(); v−>setKey(u−>key()); v−>setValue(u−>value()); // copy w’s parent to v } n−−; // one less entry return T.removeAboveExternal(w); // remove w and parent // remove key k entry void erase(const K& k) throw(NonexistentElement) { TPos v = finder(k, root()); // search from virtual root if (v.isExternal()) // not found? throw NonexistentElement("Erase of nonexistent"); eraser(v); // remove it
17
Métodos de SearchTree /* SearchTree E :: */ TPos eraser(TPos& v) {
TPos w; if (v.left().isExternal()) w = v.left(); // remove from left else if (v.right().isExternal()) w = v.right(); // remove from right else { // both internal? w = v.right(); // go to right subtree do { w = w.left(); } while (w.isInternal()); // get leftmost node TPos u = w.parent(); v−>setKey(u−>key()); v−>setValue(u−>value()); // copy w’s parent to v } n−−; // one less entry return T.removeAboveExternal(w); // remove w and parent void erase(const K& k) throw(NonexistentElement) {// remove key k entry TPos v = finder(k, root()); // search from virtual root if (v.isExternal()) // not found? throw NonexistentElement("Erase of nonexistent"); eraser(v); // remove it void erase(const Iterator& p) { eraser(p.v); }
Presentaciones similares
© 2025 SlidePlayer.es Inc.
All rights reserved.