I've the next problem and I can't solve it :/
There are two binary trees, arranged symmetrically One tree of the English course and another tree of the French course. Each tree has the following data: identification and student's name Each tree contains the students enrolled in those courses. A student can exist in the 2 trees enrolled. It is requested to make a function that returns the number of students who are enrolled in both courses (that is, they appear in the two trees)
How can I solve it?
I made the next code, using a search function with the two trees
typedef struct Alumno
{
int matricula;
char nombre[30];
}Alumno;
typedef struct NodoA* PuntA;
typedef struct NodoA
{
Alumno TAlumno;
PuntA izq;
PuntA der;
}NodoA;
int buscar(PuntA & aluI, PuntA & aluF)
{
Alumno datoI, datoF;
PuntA r=raizI;
int cont = 0;
while((r!=NULL) && (r->datoI.matricula != aluF->datoF.matricula))
{
if(r->datoI.matricula < aluF->datoF.matricula)
r=r->izq;
else
r=r->der;
}
if(r!=NULL)
cont++;
}
Sorry for the language of the code.
PS: I don't need to merge two binary trees, I need to count the equals nodes between the two trees.
Edit1: I did this code rethinking the excercise:
int buscar(PuntA raizFrances, int nro)
{
PuntA r=raizFrances;
while(r!=NULL && r->dato!=nro)
{
if(nro<r->dato)
r=r->izq;
else
r=r->der;
}
if(r==NULL)
return 0;
else
return 1;
}
void contarDobles(PuntA raizI) //MUESTRA PIMERO LA RAIZ Y DESPUES IZQUIERDA Y DESPUES DERECHA
{
int contador = 0;
int buscaEnF = 0;
if(raizI!=NULL)
{
listarPre(raizI->izq); //1
buscaEnF = buscar(raizI->dato, raizI->dato.matricula);
if(buscaEnF == 1)
contador++;
listarPre(raizI->der); //2
}
cout<<"El total de alumnos inscriptos en ambas materias es: "<<contador<<endl;
}