1 / 12

ÁRBOLES

ÁRBOLES. Definición. Nodo base: raíz del árbol. Estructura de datos jerárquica (no lineal) que puede representarse como un conjunto de nodos enlazados entre sí por medio de ramas . Formalmente, un árbol es una estructura da datos que cumple una de las siguientes condiciones:

cael
Download Presentation

ÁRBOLES

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. ÁRBOLES

  2. Definición Nodo base: raíz del árbol • Estructura de datos jerárquica (no lineal) que puede representarse como un conjunto de nodos enlazados entre sí por medio de ramas. • Formalmente, un árbol es una estructura da datos que cumple una de las siguientes condiciones: • Estructura vacía • Nodo de tipo base que tiene de 0 a N subárboles disjuntos entre sí. 1 7 5 3 4 9 17 29 44 69

  3. ÁRBOLES BINARIOS

  4. Definición. • Árbol de grado 2 • Implementación física: • Estructura estática (matrices) • Estructura dinámica • Los nodos de un árbol tienen (al menos) tres componentes: • Clave (de tipo simple o estructurada) • Puntero que indique la ubicación del árbol derecho • Puntero que indique la ubicación del árbol izquierdo Además: convención para indicar que no tiene hijos (valor null)

  5. 17 9 1 7 3 4 * * * * * * * 1 7 3 4 9 17 Memoria estática arbol Memoria dinámica NodoArbol nombre raiz Arbol

  6. Definición de las clases • Clase NodoArbol public class NodoArbol { int clave; NodoArbol iz; NodoArbol de; public NodoArbol () { clave = 0; iz = null; de = null; } } public class Arbol { String nombre; NodoArbol raiz; public Arbol () { nombre = null; raiz = null; } } • Clase Arbol

  7. Recorrido en Preorden. Aplicado a objetos de la clase Arbol: // Escribe las claves del árbol binario en preorden. static void preOrden (NodoArbol arbol) { if (arbol != null) { System.out.print (arbol.clave+" ") ; preOrden (arbol.iz); preOrden (arbol.de); } } public void preorden () { preorden (raiz); } • Orden de visita de nodos: 1, 2, 4, 9, 15, 5, 3, 8 y 7. • Preferido para: • Búsquedas. arbol nombre raiz 1 2 3 8 7 4 5 9 15

  8. arbol Recorrido en Orden Central • Aplicado a objetos de la clase Arbol: // Escribe las claves del árbol binario en orden central. static void ordenCentral (NodoArbol arbol) { if (arbol != null) { ordenCentral (arbol.iz); System.out.print (arbol.clave+" "); ordenCentral (arbol.de); } } public void ordenCentral () { ordenCentral (raiz); } • Orden de visita de nodos: 9, 4, 15, 2, 5, 1, 8, 3 y 7. • Preferido para: • Recorrido de acuerdo al orden físico de los nodos. • En árboles binarios de búsqueda recupera la secuencia. nombre raiz 1 2 3 8 7 4 5 9 15

  9. Recorrido en Postorden • Aplicado a objetos de la clase Arbol:// Escribe las claves del árbol binario en postorden. static void postOrden (NodoArbol arbol) { if (arbol != null) { postOrden (arbol.iz); postOrden (arbol.de); System.out.print (arbol.clave + " ") ; } } public void postOrden () { postOrden (raiz); } • Orden de visita de nodos: 9, 15, 4, 5, 2, 8, 7, 3 y 1. • Preferido para: • Liberar memoria. • Nodos buscados en los niveles más bajos del árbol. arbol nombre raiz 1 2 3 8 7 4 5 9 15

  10. Clase NodoArbol ClaseArbol Árbol sobre Matriz: clases. public class ArbolMatriz { final int NULL = -1, N = 10; NodoArbol [ ] matriz; int numNodos; ArbolMatriz () { matriz = new NodoArbol [N]; for (int i = 0; i < N; i++) matriz [i] = new NodoArbol (); numNodos = 0; } class NodoArbol { int clave, izq, der; NodoArbol () { clave = 0; izq = -1; der = -1; } }

  11. Árbol sobre Matriz: representación.

  12. Preorden Orden central Árbol sobre Matriz: recorridos en profundidad. private void preOrden (int i) { if (i != NULL) { System.out.print (matriz [i].clave+", "); preOrden (matriz [i].izq); preOrden (matriz [i].0der); } } public void preOrden () { preOrdenAux (0); } private void ordenCentralAux (int i) { if (i != NULL) { ordenCentralAux (matriz [i].izq); System.out.print (matriz [i].clave+", "); ordenCentralAux (matriz [i].der); } } public void ordenCentral () { ordenCentralAux (0); } • Postorden private void postOrdenAux (int i) { if (i != NULL) { postOrdenAux (matriz [i].izq); postOrdenAux (matriz [i].der); System.out.print (matriz [i].clave+", "); } } public void postOrden () { postOrdenAux (0); }

More Related