-
Lenguaje
Pascal
-
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
142
143
144
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
142
143
144
program prog_arbol;
uses crt;
type Arbol = ^TDAArbol;
TDAArbol = record
izq, der : Arbol;
entero : integer;
end;
function arbol_insertar (nodo : Arbol; entero : integer) : Arbol;
begin
if nodo = nil then
begin
new (nodo);
nodo^.izq := nil;
nodo^.der := nil;
nodo^.entero := entero;
end
else
if entero < nodo^.entero then
nodo^.izq := arbol_insertar (nodo^.izq, entero)
else
nodo^.der := arbol_insertar (nodo^.der, entero);
arbol_insertar := nodo;
end;
function arbol_quitar (nodo : Arbol; entero : integer) : Arbol;
var pivote : Arbol;
begin
if nodo <> nil then
begin
if nodo^.entero = entero then
begin
if nodo^.izq = nil then
pivote := nodo^.der
else if nodo^.der = nil then
pivote := nodo^.izq
else
begin
pivote := nodo^.izq;
while pivote^.der <> nil do
pivote := pivote^.der;
pivote^.der := nodo^.der;
pivote := nodo^.izq;
end;
dispose (nodo);
nodo := pivote;
end
else if entero < nodo^.entero then
nodo^.izq := arbol_quitar (nodo^.izq, entero)
else
nodo^.der := arbol_quitar (nodo^.der, entero);
end;
arbol_quitar := nodo;
end;
procedure arbol_preorden (nodo : Arbol);
begin
if nodo <> nil then
begin
writeln (nodo^.entero);
arbol_preorden (nodo^.izq);
arbol_preorden (nodo^.der);
end;
end;
procedure arbol_inorden (nodo : Arbol);
begin
if nodo <> nil then
begin
arbol_inorden (nodo^.izq);
writeln (nodo^.entero);
arbol_inorden (nodo^.der);
end;
end;
procedure arbol_postorden (nodo : Arbol);
begin
if nodo <> nil then
begin
arbol_postorden (nodo^.izq);
arbol_postorden (nodo^.der);
writeln (nodo^.entero);
end;
end;
var raiz : Arbol;
var opcion, tecla : char;
var entero, i : integer;
begin
raiz := nil;
randomize;
repeat
clrscr;
writeln ('MEN'#233);
writeln ('1.- Insertar entero');
writeln ('2.- Insertar n'#163'meros aleatorios');
writeln ('3.- Quitar entero');
writeln ('4.- Listado en preorden');
writeln ('5.- Listado en inorden');
writeln ('6.- Listado en postorden');
writeln ('7.- Salir'#10#13);
write ('Seleccione una opci'#162'n: ');
repeat
opcion := readkey;
until (opcion >= '1') and (opcion <= '7');
writeln (opcion, #10#13);
if (raiz = nil) and (opcion <> '1') and (opcion <> '2') and (opcion <> '7') then
writeln ('El '#160'rbol est'#160' vac'#161'o.')
else case opcion of
'1':
begin
write ('Ingrese el entero a insertar: ');
readln (entero);
raiz := arbol_insertar (raiz, entero);
writeln (#10#13'Entero agregado correctamente.');
end;
'2':
begin
write ('Ingrese la cantidad de n'#163'meros a insertar: ');
readln (entero);
for i:=1 to entero do
raiz := arbol_insertar (raiz, random(2000) - 1000);
writeln (#10#13'N'#163'meros agregados correctamente.');
end;
'3':
begin
write ('Ingrese el entero a quitar: ');
readln (entero);
raiz := arbol_quitar (raiz, entero);
writeln (#10#13'Entero borrado correctamente.');
end;
'4': arbol_preorden (raiz);
'5': arbol_inorden (raiz);
'6': arbol_postorden (raiz);
end;
if opcion <> '7' then
begin
write (#10#13'Presione una tecla para continuar . . . ');
tecla := readkey;
end
until opcion = '7';
end.
uses crt;
type Arbol = ^TDAArbol;
TDAArbol = record
izq, der : Arbol;
entero : integer;
end;
function arbol_insertar (nodo : Arbol; entero : integer) : Arbol;
begin
if nodo = nil then
begin
new (nodo);
nodo^.izq := nil;
nodo^.der := nil;
nodo^.entero := entero;
end
else
if entero < nodo^.entero then
nodo^.izq := arbol_insertar (nodo^.izq, entero)
else
nodo^.der := arbol_insertar (nodo^.der, entero);
arbol_insertar := nodo;
end;
function arbol_quitar (nodo : Arbol; entero : integer) : Arbol;
var pivote : Arbol;
begin
if nodo <> nil then
begin
if nodo^.entero = entero then
begin
if nodo^.izq = nil then
pivote := nodo^.der
else if nodo^.der = nil then
pivote := nodo^.izq
else
begin
pivote := nodo^.izq;
while pivote^.der <> nil do
pivote := pivote^.der;
pivote^.der := nodo^.der;
pivote := nodo^.izq;
end;
dispose (nodo);
nodo := pivote;
end
else if entero < nodo^.entero then
nodo^.izq := arbol_quitar (nodo^.izq, entero)
else
nodo^.der := arbol_quitar (nodo^.der, entero);
end;
arbol_quitar := nodo;
end;
procedure arbol_preorden (nodo : Arbol);
begin
if nodo <> nil then
begin
writeln (nodo^.entero);
arbol_preorden (nodo^.izq);
arbol_preorden (nodo^.der);
end;
end;
procedure arbol_inorden (nodo : Arbol);
begin
if nodo <> nil then
begin
arbol_inorden (nodo^.izq);
writeln (nodo^.entero);
arbol_inorden (nodo^.der);
end;
end;
procedure arbol_postorden (nodo : Arbol);
begin
if nodo <> nil then
begin
arbol_postorden (nodo^.izq);
arbol_postorden (nodo^.der);
writeln (nodo^.entero);
end;
end;
var raiz : Arbol;
var opcion, tecla : char;
var entero, i : integer;
begin
raiz := nil;
randomize;
repeat
clrscr;
writeln ('MEN'#233);
writeln ('1.- Insertar entero');
writeln ('2.- Insertar n'#163'meros aleatorios');
writeln ('3.- Quitar entero');
writeln ('4.- Listado en preorden');
writeln ('5.- Listado en inorden');
writeln ('6.- Listado en postorden');
writeln ('7.- Salir'#10#13);
write ('Seleccione una opci'#162'n: ');
repeat
opcion := readkey;
until (opcion >= '1') and (opcion <= '7');
writeln (opcion, #10#13);
if (raiz = nil) and (opcion <> '1') and (opcion <> '2') and (opcion <> '7') then
writeln ('El '#160'rbol est'#160' vac'#161'o.')
else case opcion of
'1':
begin
write ('Ingrese el entero a insertar: ');
readln (entero);
raiz := arbol_insertar (raiz, entero);
writeln (#10#13'Entero agregado correctamente.');
end;
'2':
begin
write ('Ingrese la cantidad de n'#163'meros a insertar: ');
readln (entero);
for i:=1 to entero do
raiz := arbol_insertar (raiz, random(2000) - 1000);
writeln (#10#13'N'#163'meros agregados correctamente.');
end;
'3':
begin
write ('Ingrese el entero a quitar: ');
readln (entero);
raiz := arbol_quitar (raiz, entero);
writeln (#10#13'Entero borrado correctamente.');
end;
'4': arbol_preorden (raiz);
'5': arbol_inorden (raiz);
'6': arbol_postorden (raiz);
end;
if opcion <> '7' then
begin
write (#10#13'Presione una tecla para continuar . . . ');
tecla := readkey;
end
until opcion = '7';
end.