Está en la página 1de 8

/*

* 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(){

int menu_val=0;

do {
System.out.println("");
System.out.println("");
System.out.println("-----------MENU------------");
System.out.println("1 - Ingreso Secuencial");
System.out.println("2 - Imprimir ARBOL Completo");
System.out.println("3 - Imprimir Pre-Orden");
System.out.println("4 - Imprimir In-Orden");
System.out.println("5 - Imprimir Post-Orden");
System.out.println("6 - Imprimir las 3 formas");
System.out.println("7 - INGRESO Pre-Orden");
System.out.println("8 - INGRESO In-Orden");
System.out.println("9 - INGRESO Post-Orden");
System.out.println("10 - Salir");
Scanner sc = new Scanner (System.in);

menu_val= sc.nextInt();

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;

}
public void imp_preorden(){
// for ( int i = 0; i < 15; i++) {
System.out.println("");
System.out.print("Pre-Orden: ");
System.out.print(arbol[0]+",");
System.out.print(arbol[1]+",");
System.out.print(arbol[3]+",");
System.out.print(arbol[7]+",");
System.out.print(arbol[8]+",");
System.out.print(arbol[4]+",");
System.out.print(arbol[9]+",");
System.out.print(arbol[10]+",");
System.out.print(arbol[2]+",");
System.out.print(arbol[5]+",");
System.out.print(arbol[11]+",");
System.out.print(arbol[12]+",");
System.out.print(arbol[6]+",");
System.out.print(arbol[13]+",");
System.out.print(arbol[14]);
// }
}
public void limpiar_pantalla(){
for(int i=0;i<100; i++ )
System.out.println("");
}
public void imp_inorden(){
System.out.println("");
System.out.print("in-Orden: ");
System.out.print(arbol[7]+",");
System.out.print(arbol[3]+",");
System.out.print(arbol[8]+",");
System.out.print(arbol[1]+",");
System.out.print(arbol[9]+",");
System.out.print(arbol[4]+",");
System.out.print(arbol[10]+",");
System.out.print(arbol[0]+",");
System.out.print(arbol[11]+",");
System.out.print(arbol[5]+",");
System.out.print(arbol[12]+",");
System.out.print(arbol[2]+",");
System.out.print(arbol[13]+",");
System.out.print(arbol[6]+",");
System.out.print(arbol[14]);
}
public void imp_postonder(){
System.out.println("");
System.out.print("Post-Orden: ");
System.out.print(arbol[7]+",");
System.out.print(arbol[8]+",");
System.out.print(arbol[3]+",");
System.out.print(arbol[9]+",");
System.out.print(arbol[10]+",");
System.out.print(arbol[4]+",");
System.out.print(arbol[1]+",");
System.out.print(arbol[11]+",");
System.out.print(arbol[12]+",");
System.out.print(arbol[5]+",");
System.out.print(arbol[13]+",");
System.out.print(arbol[14]+",");
System.out.print(arbol[6]+",");
System.out.print(arbol[2]+",");
System.out.print(arbol[0]);
}
public void imprimir(){
limpiar_pantalla();
int indice=0;
System.out.println(" "+arbol[0]);
System.out.println("");
System.out.println(" "+arbol[1]+"
"+arbol[2]);
System.out.println("");
System.out.println(" "+arbol[3]+" "+arbol[4]+"
"+arbol[5]+" "+arbol[6]);
System.out.println("");
System.out.println(" "+arbol[7]+" "+arbol[8]+" "+arbo
l[9]+" "+arbol[10]+" "+arbol[11]+" "+arbol[12]+" "+arbol[13]
+" "+arbol[14]);
}
}

También podría gustarte