Advertisement
LaCaraDeLaVerga

Pasar de AB a ABB

Jun 7th, 2016
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.05 KB | None | 0 0
  1. // todo dentro de la clase ABB
  2.  
  3.     public void insertarABB(int valor) {
  4.         if (this.raiz == null) {
  5.             this.raiz = new Nodo(valor);
  6.         } else {
  7.             agregarABB(this.raiz, valor);
  8.         }
  9.     }
  10.  
  11.     private void agregarABB(Nodo nodo, int valor) {
  12.         if (nodo.dato > valor) {
  13.             if (nodo.izq == null) {
  14.                 nodo.izq = new Nodo(valor);
  15.             } else {
  16.                 agregarABB(nodo.izq, valor);
  17.             }
  18.         } else if (nodo.dato < valor) {
  19.             if (nodo.der == null) {
  20.                 nodo.der = new Nodo(valor);
  21.             } else {
  22.                 agregarABB(nodo.der, valor);
  23.             }
  24.         } else {
  25.             throw new RuntimeException("El dato ya existe");
  26.         }
  27.     }
  28.     private void ingresar (Nodo nodo){      // recorre el ab y los agrega ordenados
  29.         if (nodo.izq != null){
  30.             this.insertarABB(nodo.izq.dato);
  31.             ingresar(nodo.izq);
  32.         }
  33.        
  34.         if (nodo.der != null){
  35.             this.insertarABB(nodo.der.dato);
  36.             ingresar(nodo.der);
  37.         }
  38.     }
  39.        
  40.     public void toABB(AB arbol){               
  41.         if (arbol.raiz != null){
  42.             this.insertarABB(arbol.raiz.dato);
  43.         }
  44.         this.ingresar(arbol.raiz);          //le pasas la raiz para q empiece a recorrer
  45.        
  46.        
  47.        
  48.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement