iteratort c = n.lchild();
while (c!=T.end()) {
preorder(T,c,L);
c = c.right();
}
}
void preorder(tree &T,list<int> &L) {
if (T.begin()==T.end()) return;
preorder(T,T.begin(),L);
}
//---:---<*>---:---<*>---:---<*>---:---<*>
void postorder(tree &T,iteratort n,list<int> &L) {
iteratort c = n.lchild();
while (c!=T.end()) {
postorder(T,c,L);
c = c.right();
}
L.insert(L.end(),T.retrieve(n));
}
void postorder(tree &T,list<int> &L) {
if (T.begin()==T.end()) return;
postorder(T,T.begin(),L);
}
//---:---<*>---:---<*>---:---<*>---:---<*>
void lispprint(tree &T,iteratort n) {
iteratort c = n.lchild();
if (c==T.end()) cout << T.retrieve(n);
else {
cout << "(" << T.retrieve(n);
while (c!=T.end()) {
cout << " ";
lispprint(T,c);
c = c.right();
}
cout << ")";
}
}
void lispprint(tree &T) {
if (T.begin()!=T.end()) lispprint(T,T.begin());
}
//---:---<*>---:---<*>---:---<*>---:---<*>
iteratort treecopy(tree &T,iteratort nt,
tree &Q,iteratort nq) {
nq = Q.insert(nq,T.retrieve(nt));
iteratort
ct = nt.lchild(),
cq = nq.lchild();
while (ct!=T.end()) {
cq = treecopy(T,ct,Q,cq);
ct = ct.right();
cq = cq.right();
}
return nq;
}
//---:---<*>---:---<*>---:---<*>---:---<*>8
iteratort mirrorcopy(tree &T,iteratort nt,
tree &Q,iteratort nq) {
nq = Q.insert(nq,T.retrieve(nt));
iteratort
ct = nt.lchild(),
cq = nq.lchild();
while (ct != T.end()) {
cq = mirrorcopy(T,ct,Q,cq);
ct = ct.right();
}
return nq;
}
//---:---<*>---:---<*>---:---<*>---:---<*>
iteratort pruneodd(tree &T,iteratort n) {
if (T.retrieve(n) % 2) n = T.erase(n);
else {
iteratort c = n.lchild();
while (c != T.end()) c = pruneodd(T,c);
n = n.right();
}
return n;
}
struct Nodo{
int dato;
Nodo *der;
Nodo *izq;
Nodo *padre;
};
return nuevo_nodo;
}
int main(){
int dato,opcion;
int contador=0;
Nodo *arbol=NULL;
do{
printf("\tMENU\n");
printf("1/Insertar nuevo Nodo\n");
printf("2/Mostrar nuevo Nodo\n");
printf("3/Buscar nuevo Nodo\n");
printf("4/Recorrido en Pre Orden\n");
printf("5/Recorrido en In Orden\n");
printf("6/Recorrido en Post Orden\n");
printf("7/Eliminar Nodo\n");
printf("8/Salir\n");
printf("Ingrese opcion\n");
cin>>opcion;
switch(opcion){
case 1:
cout<<"\nDigite un numero"<<endl;
cin>>dato;
insertarNodo(arbol,dato,NULL);
system("pause");
break;
case 2:
cout<<"Mostrando los datos"<<endl;
mostrarArbol(arbol,contador);
system("pause");
break;
case 3:
cout<<"Digite el elemento a buscar"<<endl;
cin>>dato;
if(busqueda(arbol,dato)){
cout<<"el elmento :"<<dato<<"fue enncontrado"<<endl;
}else{
cout<<"el elmento :"<<dato<<" NO fue enncontrado"<<endl;
}
system("pause");
break;
case 4:
cout<<"Recorrido en Pre orden"<<endl;
recorridoPreorden(arbol);
system("pause");
break;
case 5:
cout<<"Recorrido en In orden"<<endl;
recorridoInorden(arbol);
system("pause");
break;
case 6:
cout<<"Recorrido en Post orden"<<endl;
recorridoPostorden(arbol);
system("pause");
break;
case 7:
cout<<"ingrese nodo a eliminar"<<endl;
cin>>dato;
eliminar(arbol,dato);
system("pause");
break;
}
system("cls");
}while(opcion!=8);
getch();
return 0;
}
16 y 17)
#include
<iostream>
#include <stdlib.h>
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);
cout<<"\n\t Insertado..!"<<endl<<endl;
}
else if(x < arbol->nro)
insertar(arbol->izq, x);
else if(x > arbol->nro)
insertar(arbol->der, x);
}
numNodos++;
cout<< arbol->nro <<endl;
verArbol(arbol->izq, n+1);
}
if(arbol==NULL)
return r;
if(dato<arbol->nro)
r = busquedaRec(arbol->izq, dato);
else
r = 1; // son iguales, lo encontre
return r;
}
if(x<arbol->nro)
elimina(arbol->izq, x);
else if(x>arbol->nro)
elimina(arbol->der, x);
else
{
ABB p = arbol;
arbol = unirABB(arbol->izq, arbol->der);
delete p;
}
}
if(arbol==NULL)
return -1;
else
{
AltIzq = alturaAB(arbol->izq);
AltDer = alturaAB(arbol->der);
if(AltIzq>AltDer)
return AltIzq+1;
else
return AltDer+1;
}
}
if(arbol!=NULL)
{
encola(q, arbol);
while(q.delante!=NULL)
{
arbol = desencola(q);
cout << arbol->nro << ' ';
if(arbol->izq!=NULL)
encola(q, arbol->izq);
if(arbol->der!=NULL)
encola(q, arbol->der);
}
}
}
if(arbol!=NULL)
{
temp = arbol->izq;
arbol->izq = arbolEspejo(arbol->der);
arbol->der = arbolEspejo(temp);
}
return arbol;
}
void menu2()
{
//system("cls"); // para limpiar pantalla
cout << endl;
cout << "\t [1] En Orden \n";
cout << "\t [2] Pre Orden \n";
cout << "\t [3] Post Orden \n";
cout << "\n\t Opcion : ";
}
int main()
{
ABB arbol = NULL;
int x;
int op, op2;
case 2:
verArbol(arbol, 0);
break;
case 3:
menu2(); cin>> op2;
if(arbol!=NULL)
{
switch(op2)
{
case 1:
enOrden(arbol); break;
case 2:
preOrden(arbol); break;
case 3:
postOrden(arbol); break;
}
}
else
cout << "\n\t Arbol vacio..!";
break;
case 4:
bool band;
band = busquedaRec(arbol,x);
if(band==1)
cout << "\n\tEncontrado...";
else
cout << "\n\tNo encontrado...";
break;
case 5:
cout<<" Valor a eliminar: "; cin>> x;
elimina(arbol, x);
cout << "\n\tEliminado..!";
break;
case 6:
cout<<"\n\n Mostrando recorrido por amplitud\n\n";
recorrerxNivel(arbol);
break;
case 7:
int h;
h = alturaAB(arbol);
cout << " La altura es : "<< h << endl;
break;
case 8:
ABB espejo;
espejo = NULL;
verArbol(arbol, 0);
espejo = arbolEspejo(arbol);
verArbol(espejo, 0);
break;
case 9:
verArbol(arbol, 0);
cout << "\n\n El numero de nodos es : ";
cout << numNodos;
break;
case 11:
cout << " Ingrese k: "; cin>> k;
nodosMenoresQueK(arbol, 0);
cout <<" Son "<< numK << " numeros";
break;
case 12:
exit(0);
}
cout<<"\n\n\n";
//system("pause"); // hacer pausa y presionar una tecla para continuar
}while(op!=11);
18)
#include <cstdlib>
#include <iostream>
/*
*
*
*
*
*
*/
struct misDatos{
string nombre;
int edad;
int num;
};
struct node{
misDatos datos;
node *left;
node *right;
};
node * newTree();
node * insert(int data, string name);
int main() {
//ARBOL 1
node *raiz = newTree();
node *current = raiz;
node *arbol=raiz;
/*
A
/ \
B C
/\ / \
D E F G
*/
//A=10
//B=14
//C=16
//D=18
//E=20
//F=24
//G=30
arbol->left = insert(14,"B");
arbol->right = insert(16,"C");
current=arbol->left;
current->left = insert(50,"D");
current->right = insert(60,"E");
current=arbol->right;
current->left = insert(24,"F");
current->right = insert(30,"G");
//cout<<endl<<endl;
// cout << "\tR E C O R R I D O P O S T - O R D E N\n";
//printPostOrden(raiz);
//cout<<endl<<endl;
//cout << "\tR E C O R R I D O I N - O R D E N\n";
// printInOrden(raiz);
cout<<endl<<endl<<endl<<endl;
cout<<"ALTURA : "<<maxALTURA(raiz)<<endl;
cout<<"NUMERO DE HOJAS: "<<contar_hojas(raiz)<<endl;
return 0;
}
//---------------> NEW TREE
node * newTree() {
node *nuevo;
nuevo = new node;
nuevo->datos.nombre= "A";
nuevo->datos.edad = 10; //
nuevo->right = nuevo;
nuevo->left = nuevo;
return nuevo;
}
// -------------> INSERTANDO
node* insert(int data, string name) {
node *nuevo;
nuevo = new node;
nuevo->datos.edad=data;
nuevo->datos.nombre=name;
nuevo->left=NULL;
nuevo->right=NULL;
return nuevo;
}
// -------------> O R D E N A M I E N T O S
// -----> PREORDEN
printRoot(current); //RAIZ
if(current->left!=NULL){
printPreOrden(current->left); //IZQUIERDA
}
if(current->right!=NULL){
printPreOrden(current->right); //DERECHA
}
return current->datos.edad;
}
if(current->left!=NULL){
printPostOrden(current->left); //IZQUIERDA
}
if(current->right!=NULL){
printPostOrden(current->right); //DERECHA
}
printRoot(current);
if(current->left!=NULL){
printInOrden(current->left); //IZQUIERDA
}
printRoot(current);
if(current->right!=NULL){
printInOrden(current->right); //DERECHA
}
//numero de nodos
int size(node *node){
if(node==NULL)
return 0;
else
return (size(node->left)+1+size(node->right));
}
//altura del arbol
int maxALTURA(node *node){
if(node==NULL)
return 0;
else{
int s=maxALTURA(node->left);
int m=maxALTURA(node->right);
if (s>m)
return (m+1);
else
return (s+1);
}return (size(node->left)+1+size(node->right));
}
//IMPRIMIENDO RAIZ
//NODOS INTERIORES
int nodosInteriores(node *raiz ){
return size(raiz)-contar_hojas(raiz);
}