• Domingo 22 de Septiembre de 2024, 09:48

Autor Tema:  Insercion En Un Arbol De Datos  (Leído 1117 veces)

Drackzer

  • Miembro activo
  • **
  • Mensajes: 40
  • Nacionalidad: mx
    • Ver Perfil
Insercion En Un Arbol De Datos
« en: Sábado 4 de Noviembre de 2006, 23:03 »
0
Código: Text
  1. //Daniel Reyes -  Estructura de datos - Arbol de busqueda Binario
  2. import java.io.*;
  3.  
  4. class Arbol_B
  5. {
  6.   boolean flag;
  7.   private Nodo OTHER,NUDE;
  8.   int data;
  9.   BufferedReader t = new BufferedReader(new InputStreamReader(System.in));
  10.  
  11.   private class Nodo
  12.   {
  13.     private int INFO;
  14.     private Nodo left,right;
  15.   }
  16.  
  17.   Arbol_B()
  18.   {
  19.     //OTHER=null; NUDE=null;
  20.   }
  21.   // METODO: CARGADO DEL PRIMER NODO(PADRE DEL ARBOL)
  22.   public void LOAD_FATHER(int data)
  23.   {
  24.     Nodo NUDE = new Nodo();
  25.     NUDE.INFO = data;
  26.     NUDE.left = null;
  27.     NUDE.right = null;
  28.     System.out.println(NUDE.INFO);
  29.     flag=true;
  30.   }
  31.   // METODO: LLENADO POR INSERCION DEL ARBOL-BINARIA(COMPARACION HACIA LOS LADOS)
  32.   public void INSERTION(Nodo NUDE,int data)
  33.   {
  34.     if((NUDE!=null)&&(flag=true)){
  35.     if(data<NUDE.INFO)
  36.     {
  37.       if(NUDE.left == null)
  38.       {
  39.         Nodo OTHER = new Nodo();
  40.         OTHER.left = null;
  41.         OTHER.right = null;
  42.         OTHER.INFO = data;
  43.         NUDE.left = OTHER;
  44.         System.out.println(OTHER.INFO);
  45.       }
  46.       else
  47.       {
  48.         INSERTION(NUDE.left,data);
  49.       }
  50.     }
  51.     else
  52.     {
  53.       if(data>NUDE.INFO)
  54.       {
  55.         if(NUDE.right == null)
  56.         {
  57.           Nodo OTHER = new Nodo();
  58.           OTHER.left = null;
  59.           OTHER.right = null;
  60.           OTHER.INFO = data;
  61.           NUDE.right = OTHER;
  62.         }
  63.         else
  64.         {
  65.           INSERTION(NUDE.right,data);
  66.         }
  67.       }
  68.       else
  69.       {
  70.         System.out.println("\n\tEl nodo ya se encuentra en el arbol.!!!");
  71.       }
  72.     }
  73.     }
  74.     else {System.out.println("No hay raiz");}
  75.    
  76.    
  77.   }
  78.  
  79.   public static void main(String args[])
  80.   {
  81.     Nodo NUDE=null;
  82.     int data,opc;
  83.     BufferedReader t = new BufferedReader(new InputStreamReader(System.in));
  84.     Arbol_B exe = new Arbol_B();
  85.     /****************************** INICIO DEL NODO PADRE DEL ARBOL **********************************/
  86.     try
  87.     {
  88.       System.out.println("\tBienvenido a Estructura Arbol-BINARIO DR@CKZER\n\tpor favor inserta el nodo padre"+
  89.       "\n\tpara iniciar el llenado de datos en el arbol..!!");
  90.       data=Integer.parseInt(t.readLine());
  91.       exe.LOAD_FATHER(data);
  92.     }
  93.     catch(NumberFormatException N)
  94.     {
  95.       System.out.println("\n\tPor favor, solo inserte valores numericos enteros\n\t\tpara las opciones..!!");
  96.     }
  97.     catch(IOException io){System.out.println("Error de captura");}
  98.     /********************************* PARTE DEL LLENADO DEL ARBOL *************************************/
  99.     try
  100.     {
  101.      
  102.       do
  103.       {
  104.         System.out.println("\tBienvenido a Estructura Arbol DR@CKZER\npor favor elije una opcion"+
  105.         "\n1.-\t INSERCION EN EL ARBOL DE BUSQUEDA BINARIA\n2.-\t <<< SALIDA DEL PROGRAMA >>>");
  106.         opc=Integer.parseInt(t.readLine());
  107.        
  108.         switch(opc)
  109.         {
  110.           case 1: try
  111.             {
  112.                System.out.println("Introduce un valor para el dato a guardar :");
  113.                data=Integer.parseInt(t.readLine());
  114.                exe.INSERTION(NUDE,data);
  115.             }
  116.             catch(NumberFormatException N)
  117.             {
  118.               System.out.println("Por favor, solo inserte valores numericos enteros..!!");
  119.             }
  120.           break;
  121.          
  122.           case 2: System.out.println("\nHaz elegido la opcion de salida\n\nGracias Por utilizar Dra@ckzer software");
  123.               System.exit(0);
  124.           break;
  125.           default: System.out.println("Opcion incorrecta!!!");
  126.         }
  127.       }while(opc!=2);  
  128.     }
  129.     catch(IOException io){System.out.println("Error de captura");}
  130.   }
  131. }
  132.  


: Quisiera que me ayudaran a encontrar el error en el programa, en donde no hace la insercion del dato que se supone tiene que seguirle al nodo..!! (tendra algo ke ver la inicializacion del Nodo en el main?)/...!!

Gracias, y espero su resopuesta colegas!! Happy CODE!! :kicking:
INGENIERIA EN SISTEMAS - Prox. Sitio Web
"BETTA GLOBAL SYSTEMS"


silverfox

  • Miembro MUY activo
  • ***
  • Mensajes: 280
    • Ver Perfil
Re: Insercion En Un Arbol De Datos
« Respuesta #1 en: Lunes 6 de Noviembre de 2006, 09:51 »
0
Hola...

Repasa y depura tu programa.

El error está en el método LOAD_FATHER. Como comprobarás cuando depures, en ningún momento se inserta el nodo en el árbol (ni siquiera temporalmente).


Echale un vistazo, observando el ámbito de las variables.


Un saludo.




Silverfox