Advertisement
santiagol26

DIFERENTES ABOLES EL QUE SUBI

Oct 22nd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.32 KB | None | 0 0
  1. import java.util.Scanner;
  2. // Titulo: comparar cuantos arboles iguales hay.
  3. public class TreeBS{
  4.     NodoArbol raiz;
  5.     String palabras="";
  6.     public TreeBS(){
  7.         raiz=null;
  8.     }
  9.     public class NodoArbol{
  10.         int dato;  
  11.         NodoArbol izquierdo,derecho;
  12.         public  NodoArbol (int dat){
  13.         this.dato=dat; 
  14.         }  
  15.     }  
  16.      public void insertar(int dato){
  17.          NodoArbol nuevo= new NodoArbol(dato);  
  18.          if (raiz==null){
  19.              raiz=nuevo;
  20.          } else {
  21.              NodoArbol auxiliar=raiz;
  22.              NodoArbol padre;
  23.              while(auxiliar!=null){
  24.                 padre=auxiliar; //el padre del nodo actual va ser el nodo temporal
  25.                 if (dato<auxiliar.dato ){
  26.                     auxiliar=auxiliar.izquierdo;
  27.                     if (auxiliar==null){
  28.                         padre.izquierdo=nuevo;
  29.                         return;
  30.                         }
  31.                 }else{
  32.                      auxiliar=auxiliar.derecho;
  33.                      if (auxiliar==null ){
  34.                      padre.derecho=nuevo;
  35.                      return;}
  36.                  }
  37.              }
  38.           }
  39.      }
  40.         public void recorridopreorden(NodoArbol x){  //recorrido Preorder
  41.             if (x==null){
  42.             palabras=palabras.concat("n");
  43.             }
  44.             else{
  45.             recorridopreorden(x.izquierdo);
  46.             recorridopreorden(x.derecho);
  47.             palabras=palabras.concat("d");
  48.             }
  49.         }
  50.         public String devPalabra(){
  51.             return palabras;
  52.         }
  53.     public static void main(String[] args){
  54.         String palabra="";
  55.         int count=0;
  56.         Scanner tec=new Scanner (System.in);
  57.         //System.out.println("Digite el numero de arboles y su numero de nodos: ");
  58.          int arboles=tec.nextInt();
  59.          int nodos=tec.nextInt();
  60.          String arryP[]=new String[arboles];
  61.          for (int i=0;i<arboles; i++){
  62.              TreeBS arbol=new TreeBS();
  63.              for (int j=0;j<nodos; j++){
  64.               int x=tec.nextInt();
  65.               arbol.insertar(x);
  66.              }
  67.              arbol.recorridopreorden(arbol.raiz);
  68.              palabra=palabra.concat(arbol.devPalabra());
  69.              palabra=palabra.concat(" ");
  70.          }
  71.          arryP=palabra.split(" ");
  72.          //System.out.println(arryP[1]+"-"+arryP[5]);
  73.          for (int i=0;i<arboles;i++){
  74.              for (int j=i+1;j<arboles;j++){
  75.                  if(arryP[i].equals(arryP[j])) {
  76.                      count=count+1;
  77.                      //System.out.print(" hay ");
  78.                  }
  79.                 // System.out.print(j+" ");
  80.              }
  81.              //System.out.print(i+" i ");
  82.          }  
  83.          System.out.println((arboles-count));
  84.          tec.close();
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement