-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLAB5_ELJC_NHCG_ARBOL
97 lines (93 loc) · 2.2 KB
/
LAB5_ELJC_NHCG_ARBOL
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
#include <iostream>
#include <cstdlib>
using namespace std;
struct nodo{
int nro;
struct nodo *izq, *der;
};
typedef struct nodo *ABB;
ABB crearNodo(int x)
{
ABB nuevoNodo = new(struct nodo);
nuevoNodo->nro = x;
nuevoNodo->izq = NULL;
nuevoNodo->der = NULL;
return nuevoNodo;
}
void insertar(ABB &arbol, int x)
{
if(arbol==NULL)
{
arbol = crearNodo(x);
}
else if(x < arbol->nro)
insertar(arbol->izq, x);
else if(x > arbol->nro)
insertar(arbol->der, x);
}
void preOrden(ABB arbol)
{
if(arbol!=NULL)
{
cout << arbol->nro <<" ";
preOrden(arbol->izq);
preOrden(arbol->der);
}
}
void enOrden(ABB arbol)
{
if(arbol!=NULL)
{
enOrden(arbol->izq);
cout << arbol->nro << " ";
enOrden(arbol->der);
}
}
void postOrden(ABB arbol)
{
if(arbol!=NULL)
{
postOrden(arbol->izq);
postOrden(arbol->der);
cout << arbol->nro << " ";
}
}
void verArbol(ABB arbol, int n)
{
if(arbol==NULL)
return;
verArbol(arbol->der, n+1);
for(int i=0; i<n; i++)
cout<<" ";
cout<< arbol->nro <<endl;
verArbol(arbol->izq, n+1);
}
int main()
{
ABB arbol = NULL;
int n;
int x;
system("color e0");
cout<<"\n\n\n\t\tGRACIAS POR USAR NUESTRO PROGRAMA!!!!\n"<<endl;
cout<<"\t\tEsperamos haber sido utiles\n\n"<<endl;
cout<<"\t---JOSE CARLOS ESPINOZA LAURA---\n"<<endl;
cout<<"\t---CARLOS GABRIEL NINA HUANCA-----\n\n\n"<<endl;
cout << " INSERTE LA CANTIDAD DE NODOS PARA EL ARBOL: ";
cin >> n;
cout << endl;
for(int i=0; i<n; i++)
{
cout << " VALOS NUMERICO DEL NODO " << i+1 << ": ";
cin >> x;
insertar( arbol, x);
}
cout << "\n EJECUCION DEL DESARROLLO DEL ARBOL \n\n";
verArbol( arbol, 0);
cout << "\n RECORRIDOS REALIZADOS POR EL ARBOL";
cout << "\n\n EJECUCION REALIZADA EN EL ORDEN ASCENDENTE: "; enOrden(arbol);
cout << "\n\n EJECUCION REALIZADA EN PRE ORDEN: "; preOrden(arbol);
cout << "\n\n EJECUCION REALIZADA EN EL ORDEN DESCENDENTE : "; postOrden(arbol);
cout << endl << endl;
system("pause");
return 0;
}