I am making a program in C language that aims to identify if two binary trees are mirror. In my program I have managed to create two trees with the structure that can be seen in the following image:
My problem is that I don't know how to create a recursive method to verify that the two binary trees are mirrored, one with respect to the other; I have tried to create the method, but I only manage to compare the root of the two binary trees, I can't get beyond the beginning.
Attached is the code I have so far.
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
//Define the structure for the two binary trees
typedef struct{
int dato;
struct nodo *izq;
struct nodo *der;
}nodo;
typedef struct{
int dato;
struct nodo *izq;
struct nodo *der;
}nodo1;
//Define auxiliary pointers
nodo *raiz = NULL;/
nodo1 *raiz1 = NULL;//
//Function to add nodes
void insertarNodo(int datoI){
nodo *nuevo = malloc(sizeof(nodo));
nuevo->dato = datoI;
nuevo->izq = NULL;
nuevo->der = NULL;
if(raiz == NULL)
raiz = nuevo;
else{
nodo *anterior, *recorrer;
anterior = NULL;
recorrer = raiz;
while(recorrer != NULL){
anterior = recorrer;
if(datoI < recorrer->dato)
recorrer = recorrer->izq;
else
recorrer = recorrer->der;
}
if(datoI < anterior->dato)
anterior->izq = nuevo;
else
anterior->der = nuevo;
}
}
//Function to add nodes
void insertarNodo1(datoI){
nodo1 *nuevo = malloc(sizeof(nodo1));
nuevo->dato = datoI;
nuevo->izq = NULL;
nuevo->der = NULL;
if(raiz1 == NULL)
raiz1 = nuevo;
else{
nodo1 *anterior, *recorrer;
anterior = NULL;
recorrer = raiz1;
while(recorrer != NULL){
anterior = recorrer;
if(datoI > recorrer->dato)
recorrer = recorrer->izq;
else
recorrer = recorrer->der;
}
if(datoI > anterior->dato)
anterior->izq = nuevo;
else
anterior->der = nuevo;
}
}
void mostrarArbol1(nodo *arbol, int contador){
int i = 0;
if(arbol == NULL){
return;
}else{
mostrarArbol1(arbol->der,contador+1);
for(i=0; i<contador;i++)
printf(" ");
printf("(%d)\n",arbol->dato);
mostrarArbol1(arbol->izq,contador+1);
}
}
void mostrarArbol2(nodo1 *arbol, int contador){
int i = 0;
if(arbol == NULL){
return;
}else{
mostrarArbol2(arbol->der,contador+1);
for(i=0; i<contador;i++)
printf(" ");
printf("(%d)\n",arbol->dato);
mostrarArbol2(arbol->izq,contador+1);
}
}
//Recursive method I need help with
void isMirror(nodo* p,nodo1* q) {
if(p->dato == q->dato){
printf("Mirrors");
}
else{
printf("Not mirrors");
}
isMirror(p->der, q->izq);
}
int main(){
insertarNodo(20);
insertarNodo(8);
insertarNodo(22);
insertarNodo(25);
insertarNodo(12);
insertarNodo(14);
insertarNodo(10);
insertarNodo(14);
insertarNodo1(20);
insertarNodo1(8);
insertarNodo1(22);
insertarNodo1(25);
insertarNodo1(12);
insertarNodo1(4);
insertarNodo1(10);
insertarNodo1(14);
mostrarArbol1(raiz, 0);
printf("\n\n--------------------------\n\n");
mostrarArbol2(raiz1, 0);
printf("\n\n--------------------------\n\n");
isMirror(raiz, raiz1);
return 0;
}
Could you please help me?
Thank you very much in advance.
Try the below fix for your isMirror
function. We just take left from first root node and right from second root node and compare their values.
void isMirror(nodo* p,nodo1* q) {
if(!p || !q) return;
if(p->dato == q->dato){
printf("%d Mirrors\n", p->dato);
} else {
printf("%d %d Not mirrors\n", p->dato,q->dato);
}
isMirror(p->der, q->izq);
isMirror(q->der, p->izq);
}
Update: added boolean result return
bool isMirror(nodo* p,nodo1* q) {
if(!p && !q) return true;
if(!p || !q) return false;
bool result = true;
if(p->dato == q->dato){
printf("%d Mirrors\n", p->dato);
} else {
printf("%d %d Not mirrors\n", p->dato,q->dato);
return false;
}
result = result && isMirror(p->der, q->izq);
result = result && isMirror(q->der, p->izq);
return result;
}