• Lenguaje

    Java

  • Descripción

    Implementa la estructura de datos de tipo árbol binario.
    Inserta y elimina números enteros al árbol.
    Muestra el recorrido en preorden, inorden y postorden.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.Random;
import java.util.Scanner;

public class Arbol {

    private Arbol izq = null, der = null;
    private int entero;

    public static PrintStream out;

    private Arbol() {}
   
    private Arbol(int entero) {
        this.entero = entero;
    }

    public static Arbol insertar(Arbol nodo, int entero) {
        if (nodo == null)
            nodo = new Arbol(entero);
        else if (entero < nodo.entero)
            nodo.izq = insertar(nodo.izq, entero);
        else
            nodo.der = insertar(nodo.der, entero);
        return nodo;
    }

    public static Arbol quitar(Arbol nodo, int entero) {
        if (nodo != null) {
            if (entero == nodo.entero) {
                Arbol pivote;
                if (nodo.izq == null)
                    pivote = nodo.der;
                else if (nodo.der == null)
                    pivote = nodo.izq;
                else {
                    for (pivote = nodo.izq; pivote.der != null; pivote = pivote.der);
                    pivote.der = nodo.der;
                    pivote = nodo.izq;
                }
                nodo = pivote;
            } else if (entero < nodo.entero)
                nodo.izq = quitar(nodo.izq, entero);
            else
                nodo.der = quitar(nodo.der, entero);
        }
        return nodo;
    }

    public static void preorden(Arbol nodo) {
        if (nodo != null) {
            out.println(nodo.entero);
            preorden(nodo.izq);
            preorden(nodo.der);
        }
    }

    public static void inorden(Arbol nodo) {
        if (nodo != null) {
            inorden(nodo.izq);
            out.println(nodo.entero);
            inorden(nodo.der);
        }
    }

    public static void postorden(Arbol nodo) {
        if (nodo != null) {
            postorden(nodo.izq);
            postorden(nodo.der);
            out.println(nodo.entero);
        }
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        Arbol raiz = null;
        char opcion;
        int entero, i;
        String linea;
        Random rand = new Random();
        Scanner teclado;
        if(System.getProperties().get("os.name").equals("Linux") || System.console()==null) {
            teclado = new Scanner(System.in);
            out = System.out;
        } else {
            teclado = new Scanner(System.in, "CP850");
            out =  new PrintStream(System.out, true, "CP850");
        }
        do {
            out.print(
                "MEN\u00DA\n" +
                "1.- Insertar entero\n" +
                "2.- Insertar n\u00FAmeros aleatorios\n" +
                "3.- Quitar entero\n" +
                "4.- Listado en preorden\n" +
                "5.- Listado en inorden\n" +
                "6.- Listado en postorden\n" +
                "7.- Salir\n\n" +
                "Seleccione una opci\u00F3n: ");
            do {
                linea = teclado.nextLine();
            } while (linea.length() != 1 || linea.charAt(0) < '1' || linea.charAt(0) > '7');
            opcion = linea.charAt(0);
            out.println();
            if (raiz == null && opcion != '1' && opcion != '2' && opcion != '7')
                out.println("El \u00E1rbol est\u00E1 vac\u00EDo.");
            else
                switch (opcion) {
                    case '1':
                        out.print("Ingrese el entero a insertar: ");
                        entero = Integer.parseInt(teclado.nextLine());
                        raiz = Arbol.insertar(raiz, entero);
                        out.println("\nEntero agregado correctamente.");
                        break;
                    case '2':
                        out.print("Ingrese la cantidad de n\u00FAmeros a insertar: ");
                        entero = Integer.parseInt(teclado.nextLine());
                        for (i=0; i<entero; i++)
                            raiz = Arbol.insertar (raiz, rand.nextInt(2000) - 1000);
                        out.println("\nN\u00FAmeros agregados correctamente.");
                        break;
                    case '3':
                        out.print("Ingrese el entero a quitar: ");
                        entero = Integer.parseInt(teclado.nextLine());
                        raiz = Arbol.quitar(raiz, entero);
                        out.println("\nEntero borrado correctamente.");
                        break;
                    case '4': Arbol.preorden (raiz); break;
                    case '5': Arbol.inorden  (raiz); break;
                    case '6': Arbol.postorden(raiz); break;
                }
            if (opcion != '7') {
                out.print("\nPresione <ENTER> para continuar . . . ");
                teclado.nextLine();
                out.println("\n\n\n\n\n\n\n\n");
            }
        } while (opcion != '7');
    }

}