Vous êtes sur la page 1sur 14

ALUMNO :Cristian Mostacero Sauceda Prob 1

#include <iostream> #include <stdlib.h> using namespace std; struct nodo { int nro; struct nodo *izq,*der; }; typedef nodo *ABB; ABB crearNodo(int p); void verArbol(ABB arbol, int n); void insertar(ABB &a,int p); void preOrden(ABB a); void postOrden(ABB a); void enOrden(ABB a); int main() { ABB a=NULL; int m,opc,op=1; int p; do { cout<<"------MENU--------"<<endl<<endl;

cout<<"Opciones:"<<endl; cout<<"1.insertar:"<<endl; cout<<"2.mostrar:"<<endl; cout<<"3.mortrar nodos enorden "<<endl; cout<<"4.mortrar nodos preorden "<<endl; cout<<"5.mortrar nodos postorden "<<endl; cin>>opc; switch (opc) { case 1:cout<<"INGRESAR CANTIDAD DE NUMEROS: "<<endl; cin>>m; for(int i=0;i<m;i++) { cout<<"INGRESA EL NUMERO: "<<i<<" : "<<endl; cin>>p; insertar(a,p); }

break; case 2: verArbol(a,0); break;

case 3: enOrden(a); break;

case 4: preOrden(a); break; case 5: postOrden(a); break; default: cout<<"Opcion incorrecta"<<endl; break; } cout<<"Para realizar otra accion presiona 1, de lo contrario cualquier tecla"<<endl; cin>>op; system("cls"); }while(op==1); return 0; } ABB crearNodo(int p) { ABB nuevoNodo = new(struct nodo); nuevoNodo->nro = p; nuevoNodo->izq = NULL; nuevoNodo->der = NULL;

return nuevoNodo; } void insertar(ABB &arbol, int p)

{ if(arbol==NULL) { arbol = crearNodo(p); } else if(p < arbol->nro) insertar(arbol->izq, p); else if(p > arbol->nro) insertar(arbol->der, p); }

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); }

Prob 2
#include <iostream> #include <stdlib.h> using namespace std; struct nodo { char nom[100]; struct nodo *izq,*der; }; typedef nodo *ABB;

void eliminahojas(ABB a); void verarbol(ABB arbol, int n); void preorden(ABB arbol); int main () { ABB a,b; a=NULL; char no[7][100]={"Juan","Ana","Maria","Jorge","Alicia","Andres","Luis"}; for(int i=0;i<7;i++) { b=new(struct nodo); b->nom=no[i];

b->izq=NULL; b->der=NULL; a=b; } verarbol(a,0); system("pause"); system("cls"); cout<<"El nuevo arbol es: "<<endl<<endl; eliminahojas(a); verarbol(a,0); cout<<endl<<endl; system("pause"); } void preorden(ABB arbol) { if(arbol!=NULL) { preorden(arbol->izq); preorden(arbol->der); } } void eliminahojas(ABB a) { if(a->izq==NULL && a->der==NULL && a!=NULL) delete (a); else if(a!=NULL)

{ preorden(a->izq); preorden(a->der); } } 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->nom <<endl;

verarbol(arbol->izq, n+1); }

Prob 3
#include<iostream> using namespace std; struct nodo { int nro;

struct nodo *izq,*der; }; typedef nodo *ABB; ABB crearNodo(int x); void insertar(ABB &arbol, int x); void eliminar(ABB &abb); void eliminanodo(ABB d); void verarbol(ABB arbol, int n); void preorden(ABB arbol); void eliminahojas(ABB a); void enorden(ABB arbol); ABB q; int main() { ABB a=NULL; int n,opc,op=1; int x; cout<<"Ingrese cantidad de numeros"<<endl;cin>>n; for(int i=0;i<n;i++) { cout<<"Ingrese numero "<<i+1<<" : "<<endl; cin>>x; insertar(a,x); } verarbol(a,0); system("pause");

system("cls"); cout<<"El nuevo arbol es: "<<endl<<endl; eliminahojas(a); verarbol(a,0); system("pause"); } 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); } 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 enorden(ABB arbol) { if(arbol!=NULL) { enorden(arbol->izq); cout << arbol->nro << " "; enorden(arbol->der); } } void eliminahojas(ABB c) { if(c->izq==NULL && c->der==NULL) delete (c);

else if(c!=NULL) { enorden(c->izq); if(c->izq==NULL && c->der==NULL) delete (c); enorden(c->der); } }

Prob 4
#include <iostream> #include <stdlib.h> using namespace std; struct nodo { char nom[100]; struct nodo *izq,*der; }; typedef nodo *ABB;

void eliminahojas(ABB a); void verarbol(ABB arbol, int n); void preorden(ABB arbol); int main () { ABB a,b,c;

a=NULL; char no[7][100]={"Juan","Ana","Maria","Jorge","Alicia","Andres","Luis"}; for(int i=0;i<7;i++) { b=new(struct nodo); b->nom=no[i]; b->izq=NULL; b->der=NULL; a=b; } verarbol(a,0); system("pause"); system("cls"); cout<<"El nuevo arbol es: "; cin>>c; busacarahojas(c); verarbol(a,0); cout<<endl<<endl; system("pause"); } void preorden(ABB arbol) { if(arbol!=NULL) { preorden(arbol->izq); preorden(arbol->der);

} }

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->nom <<endl;

verarbol(arbol->izq, n+1); } void buscararbol(ABB c) { if(a->izq==NULL && a->der==NULL && a!=NULL) delete (a); else if(a!=NULL) { preorden(a->izq); preorden(a->der); } }

Vous aimerez peut-être aussi