* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates * and open the template in the editor. */ package arboles_arreglo; /** * * */ public class inicio { public static void main(String[] args) { Arboles_arreglo b =new Arboles_arreglo(); b.menu();
} } -------------------------------------------------------------------------------- ----- /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package arboles_arreglo; import java.util.Scanner; /** * * */ public class Arboles_arreglo { int arbol[]=new int[15]; int contador=0; int control_ingreso=0; public void menu(){
switch (menu_val) { case 1: control_ingreso=1; //INGRESO SECUENCIAL ingreso(); break; case 2: imprimir(); break; case 3: imp_preorden(); break; case 4: imp_inorden(); break; case 5: imp_postonder(); break; case 6: recorrer_todos(); break; case 7: control_ingreso=2; //INGRESO PRE-ORDEN ingreso(); break; case 8: control_ingreso=3; //INGRESO IN-ORDEN ingreso(); break; case 9: control_ingreso=4; //INGRESO POST-ORDEN ingreso(); break; case 10: break;
} } while((menu_val) != 10); } public void recorrer_todos(){ imp_preorden(); imp_inorden(); imp_postonder(); } public void ingreso(){ int ingreso=0; if (contador==15) { System.out.println("ADVERTENCIA.... ARBOL LLENO !!"); } else { for ( int i = 0; i < 15; i++){ if (control_ingreso==1) { if(i==0){ System.out.println("INGRESE LA RAIZ PRINCIPA L:"); } else{ if(i%2==0){ System.out.println("RAIZ "+buscar_ra iz(i)+"-- Nodo HIJO DERECHO"); } else{ System.out.println("RAIZ "+buscar_ra iz(i)+"-- Nodo HIJO IZQUIERDO"); } } } else{ System.out.println("INGRESE EL VALOR DEL NODO:"); } Scanner sc = new Scanner (System.in); ingreso= sc.nextInt(); if (control_ingreso==1){//INGRESO SECUENCIAL arbol[contador]=ingreso; }
if (control_ingreso==2){ //INGRESO PRE-ORDEN arbol[insertar_preorden(i)]=ingreso; }
if (control_ingreso==3){//INGRESO IN-ORDEN arbol[insertar_inorden(i)]=ingreso; } if (control_ingreso==4){//INGRESO POST-ORDEN arbol[insertar_postorden(i)]=ingreso; } contador++; } } } public int buscar_raiz(int indice){ int valor=0; if (indice==1){ valor= arbol[0]; } if (indice==2){ valor= arbol[0]; } if (indice==3){ valor= arbol[1]; } if (indice==4){ valor= arbol[1]; } if (indice==5){ valor= arbol[2]; } if (indice==6){ valor= arbol[2]; } if (indice==7){ valor= arbol[3]; } if (indice==8){ valor= arbol[3]; } if (indice==9){ valor= arbol[4]; } if (indice==10){ valor= arbol[4]; } if (indice==11){ valor= arbol[5]; } if (indice==12){ valor= arbol[5]; } if (indice==13){ valor= arbol[6]; } if (indice==14){ valor= arbol[6]; } return valor; } public int insertar_preorden(int indice){ int valor=0; if (indice==0){ valor= 0; } if (indice==1){ valor= 1; } if (indice==2){ valor= 3; } if (indice==3){ valor= 7; } if (indice==4){ valor= 8; } if (indice==5){ valor= 4; } if (indice==6){ valor= 9; } if (indice==7){ valor= 10; } if (indice==8){ valor= 2; } if (indice==9){ valor= 5; } if (indice==10){ valor= 11; } if (indice==11){ valor= 12; } if (indice==12){ valor= 6; } if (indice==13){ valor= 13; } if (indice==14){ valor= 14; } return valor; } public int insertar_inorden(int indice){ int valor=0; if (indice==0){ valor= 7; } if (indice==1){ valor= 3; } if (indice==2){ valor= 8; } if (indice==3){ valor= 1; } if (indice==4){ valor= 9; } if (indice==5){ valor=4; } if (indice==6){ valor= 10; } if (indice==7){ valor= 0; } if (indice==8){ valor= 11; } if (indice==9){ valor= 5; } if (indice==10){ valor= 12; } if (indice==11){ valor=2; } if (indice==12){ valor=13; } if (indice==13){ valor= 6; } if (indice==14){ valor= 14; } return valor; } public int insertar_postorden(int indice){ int valor=0; if (indice==0){ valor= 7; } if (indice==1){ valor= 8; } if (indice==2){ valor= 3; } if (indice==3){ valor= 9; } if (indice==4){ valor= 10; } if (indice==5){ valor=4; } if (indice==6){ valor= 1; } if (indice==7){ valor= 11; } if (indice==8){ valor= 12; } if (indice==9){ valor= 5; } if (indice==10){ valor= 13; } if (indice==11){ valor=14; } if (indice==12){ valor=6; } if (indice==13){ valor= 2; } if (indice==14){ valor=0; } return valor;