Programación General > Java

 Insercion En Un Arbol De Datos

(1/1)

Drackzer:

--- Código: Text ---//Daniel Reyes -  Estructura de datos - Arbol de busqueda Binarioimport java.io.*; class Arbol_B{  boolean flag;  private Nodo OTHER,NUDE;  int data;  BufferedReader t = new BufferedReader(new InputStreamReader(System.in));   private class Nodo  {    private int INFO;    private Nodo left,right;  }    Arbol_B()  {    //OTHER=null; NUDE=null;  }  // METODO: CARGADO DEL PRIMER NODO(PADRE DEL ARBOL)  public void LOAD_FATHER(int data)  {    Nodo NUDE = new Nodo();    NUDE.INFO = data;    NUDE.left = null;    NUDE.right = null;    System.out.println(NUDE.INFO);    flag=true;  }  // METODO: LLENADO POR INSERCION DEL ARBOL-BINARIA(COMPARACION HACIA LOS LADOS)  public void INSERTION(Nodo NUDE,int data)  {    if((NUDE!=null)&&(flag=true)){    if(data<NUDE.INFO)    {      if(NUDE.left == null)      {        Nodo OTHER = new Nodo();        OTHER.left = null;        OTHER.right = null;        OTHER.INFO = data;        NUDE.left = OTHER;        System.out.println(OTHER.INFO);      }      else      {        INSERTION(NUDE.left,data);      }    }    else    {      if(data>NUDE.INFO)      {        if(NUDE.right == null)        {          Nodo OTHER = new Nodo();          OTHER.left = null;          OTHER.right = null;          OTHER.INFO = data;          NUDE.right = OTHER;        }        else        {          INSERTION(NUDE.right,data);        }      }      else      {        System.out.println("\n\tEl nodo ya se encuentra en el arbol.!!!");      }    }    }    else {System.out.println("No hay raiz");}          }    public static void main(String args[])  {    Nodo NUDE=null;    int data,opc;    BufferedReader t = new BufferedReader(new InputStreamReader(System.in));    Arbol_B exe = new Arbol_B();    /****************************** INICIO DEL NODO PADRE DEL ARBOL **********************************/    try    {      System.out.println("\tBienvenido a Estructura Arbol-BINARIO DR@CKZER\n\tpor favor inserta el nodo padre"+      "\n\tpara iniciar el llenado de datos en el arbol..!!");      data=Integer.parseInt(t.readLine());      exe.LOAD_FATHER(data);    }    catch(NumberFormatException N)    {      System.out.println("\n\tPor favor, solo inserte valores numericos enteros\n\t\tpara las opciones..!!");    }    catch(IOException io){System.out.println("Error de captura");}    /********************************* PARTE DEL LLENADO DEL ARBOL *************************************/    try    {            do      {        System.out.println("\tBienvenido a Estructura Arbol DR@CKZER\npor favor elije una opcion"+        "\n1.-\t INSERCION EN EL ARBOL DE BUSQUEDA BINARIA\n2.-\t <<< SALIDA DEL PROGRAMA >>>");        opc=Integer.parseInt(t.readLine());                switch(opc)        {          case 1: try            {               System.out.println("Introduce un valor para el dato a guardar :");               data=Integer.parseInt(t.readLine());               exe.INSERTION(NUDE,data);            }            catch(NumberFormatException N)            {              System.out.println("Por favor, solo inserte valores numericos enteros..!!");            }          break;                    case 2: System.out.println("\nHaz elegido la opcion de salida\n\nGracias Por utilizar Dra@ckzer software");              System.exit(0);          break;          default: System.out.println("Opcion incorrecta!!!");        }      }while(opc!=2);      }    catch(IOException io){System.out.println("Error de captura");}  }} 

: 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:

silverfox:
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

Navegación

[0] Índice de Mensajes

Ir a la versión completa