Advertisement
Mitoeap

ArbolBinB

Jun 23rd, 2020
2,239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.13 KB | None | 0 0
  1. package arbolbb;
  2.  
  3. public class ArbolBinB {
  4.     NodoArbol raiz;
  5.     int cant;
  6.     int altura;
  7.    
  8.     public ArbolBinB(){
  9.         this.raiz=null;
  10.     }
  11.    
  12.     public void agregarNodo(int dat){
  13.         NodoArbol nuevo = new NodoArbol(dat);
  14.         if (this.raiz==null) {
  15.             this.raiz=nuevo;
  16.         }else{
  17.             NodoArbol aux = this.raiz;
  18.             NodoArbol padre;
  19.             while(true){
  20.                 padre=aux;
  21.                 if (dat<aux.Dato) {
  22.                     aux=aux.HijoIzq;
  23.                     if (aux==null) {
  24.                         padre.HijoIzq=nuevo;
  25.                         return;
  26.                     }
  27.                 }else{
  28.                     aux=aux.HijoDer;
  29.                     if (aux==null) {
  30.                         padre.HijoDer=nuevo;
  31.                         return;
  32.                     }
  33.                 }
  34.             }
  35.         }
  36.     }
  37.    
  38.     public boolean estaVacio(){
  39.         return this.raiz==null;
  40.     }
  41.    
  42.    
  43.     public void inOrder(NodoArbol r){
  44.         if(r != null){
  45.             inOrder(r.HijoIzq);
  46.             System.out.print(r.Dato+" ");
  47.             inOrder(r.HijoDer);
  48.         }
  49.     }
  50.    
  51.     public void preOrder(NodoArbol r){
  52.         if(r != null){
  53.             System.out.print(r.Dato+" ");
  54.             preOrder(r.HijoIzq);
  55.             preOrder(r.HijoDer);
  56.         }
  57.     }
  58.    
  59.     public void posOrder(NodoArbol r){
  60.         if(r != null){
  61.             posOrder(r.HijoIzq);
  62.             posOrder(r.HijoDer);
  63.             System.out.print(r.Dato+" ");
  64.         }
  65.     }
  66.    
  67.     public void descendente(NodoArbol r){
  68.         if(r != null){
  69.             descendente(r.HijoDer);
  70.             System.out.print(r.Dato+" ");
  71.             descendente(r.HijoIzq);
  72.         }
  73.     }
  74.    
  75.     public NodoArbol buscarNodo(int dat){
  76.         NodoArbol aux = this.raiz;
  77.         while(aux.Dato != dat){
  78.             if (dat < aux.Dato) {
  79.                 aux = aux.HijoIzq;
  80.             }else{
  81.                 aux = aux.HijoDer;
  82.             }
  83.             if (aux == null) {
  84.                 return null;
  85.             }
  86.         }
  87.         return aux;
  88.     }
  89.    
  90.     public int numNodos(NodoArbol r){
  91.         if (r == null) {
  92.             return 0;
  93.         }else{
  94.             return 1+ numNodos(r.HijoDer)+numNodos(r.HijoIzq);
  95.         }
  96.     }
  97.    
  98.     public void cantNodosHoja(NodoArbol r) {
  99.             if (r != null) {
  100.                 if (r.HijoIzq == null && r.HijoDer == null) {
  101.                     cant++;
  102.                 }
  103.             cantNodosHoja(r.HijoIzq);
  104.             cantNodosHoja(r.HijoDer);
  105.         }
  106.     }
  107.    
  108.     public int cantidadNodosHoja(NodoArbol r) {
  109.         cant=0;
  110.         cantNodosHoja(r);
  111.         return cant;
  112.     }
  113.    
  114.     private void retornarAltura (NodoArbol r,int nivel)    {
  115.         if (r != null) {    
  116.             retornarAltura (r.HijoIzq,nivel+1);
  117.             if (nivel>altura)
  118.                 altura=nivel;
  119.             retornarAltura (r.HijoDer,nivel+1);
  120.         }
  121.     }
  122.  
  123.     public  int retornarAltura () {
  124.         altura=0;
  125.         retornarAltura (raiz,1);
  126.         return altura;
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement