#include <iostream.h>
#include <windows.h>
#include <conio.h>
struct Nodo
{
Nodo *izq;
int dato;
Nodo *der;
};
class arbolbinario
{
private:
Nodo *raiz;
public:
arbolbinario()
{
raiz=NULL;
}
void insertar(int d)
{
Nodo *nuevo;
nuevo = new Nodo();
nuevo->izq= NULL;
nuevo->dato=d;
nuevo->der=NULL;
if (raiz==NULL)
{
raiz=nuevo;
}
else
{
Nodo *temp, *a;
int b=0;
temp = raiz;
a = NULL;
while(temp!=NULL)
{
a = temp;
if(nuevo->dato<temp->dato)
{
temp = temp->izq;
b=1;
}
else if (nuevo->dato>temp->dato)
{
temp = temp->der;
b=2;
}
}
if (b==1)
{
a->izq=nuevo;
}
else if (b==2)
{
a->der=nuevo;
}
}
}
******************************************AQUI
int cpositivos()
{
int z=0;
Nodo *recorrer;
recorrer = raiz;
if (recorrer!=NULL)
{
do
{
if(recorrer->dato>0)
{
recorrer=recorrer->izq;
z=z+1;
}
}while(recorrer->dato!=NULL);
if(recorrer!=NULL)
{
do
{
if(recorrer->dato>0)
{
recorrer=recorrer->der;
z=z+1;
}
}while(recorrer->dato!=NULL);
cout<<z;
}
else
{
cout << "arbol vacia" << endl;
}
return z;
}
}
***************************************************
void MostrarIn()
{
Nodo *temp;
temp = raiz;
MostrarInorden (temp);
}
void MostrarInorden(Nodo *T)
{
if (T!=NULL)
{
MostrarInorden(T->izq);
cout << T->dato << endl;
MostrarInorden(T->der);
}
}
void MostrarPr()
{
Nodo *temp;
temp = raiz;
MostrarPreorden (temp);
}
void MostrarPreorden(Nodo *T)
{
if (T!=NULL)
{
cout << T->dato << endl;
MostrarPreorden(T->izq);
MostrarPreorden(T->der);
}
}
void mostrarpost()
{
Nodo *temp;
temp=raiz;
mostrarpostorden(temp);
}
void mostrarpostorden(Nodo *T)
{
if (T!=NULL)
{
cout<<T->dato<<endl;
mostrarpostorden(T->der);
mostrarpostorden(T->izq);
}
}
};
void main (void)
{
arbolbinario numeros;
int op, n;
do
{
cout << "A R B O L B I N A R I O" << endl<<endl;
cout << "1.- Insertar nodo"<< endl;
cout << "2.- Mostrar inorden"<< endl;
cout << "3.- Mostrar preorden"<< endl;
cout << "4.- Mostrar postorden"<< endl;
cout << "5.- contar numeros negativos"<< endl;
cout << "6.- Salir"<< endl << endl;
cout << "Elige una opcion-> ";
cin >> op;
switch(op)
{
case 1:
cout << "Introduce el numero: ";
cin >> n;
numeros.insertar(n);
break;
case 2:
cout << "Inorden"<<endl;
numeros.MostrarIn();
break;
case 3:
cout << "Preorden"<<endl;
numeros.MostrarPr();
break;
case 4:
cout << "Postorden"<<endl;
numeros.mostrarpost();
break;
case 5:
cout<<"el total de positivos es: "<<endl;
numeros.cpositivos();
default:
cout << "Bye!!!"<<endl;
}
}while(1);
}