1

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:

enter image description here

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Sasori1264
  • 113
  • 3
  • 1
    I'd start by writing a function to recursively walk a single tree, then extend that function to walk both trees at once. – 0x5453 Jun 07 '21 at 20:25
  • 1
    Hi Sasori, welcome to the site. You showed your effort in your question, that's really good! But just do you know, this is an English site. Your question/code would get more attention if the identifiers were in English. – Alexander Jun 07 '21 at 20:25
  • I mean, the English comments make it fairly understandable; thank you. – Neil Jun 08 '21 at 06:37

3 Answers3

3

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;
}
Beshambher Chaukhwan
  • 1,418
  • 2
  • 9
  • 13
3

Recursive way

int isMirror(nodo* p,nodo1* q) {
  if (p == NULL && q == NULL)) return 1; // Reached the end of the line
  if (p != NULL || q != NULL) return 0; // One branch is longer than the other

  if(p->dato != q->dato){
      return 0; // Not the same
  }

  return isMirror(p->der, q->izq) && isMirror(q->der, p->izq); // Both the same?
}
Ed Heal
  • 59,252
  • 17
  • 87
  • 127
1

For starters the function should be declared like

bool isMirror( const nodo *p, const nodo1 *q );

That is its parameters should have the qualifier const because passed nodes are not changed within the function. And the function should do one thing: to check whether one binary tree is a mirror of another binary tree, It is the caller of the function that decides whether to output a message.

The function can be defined the following way

bool isMirror( const nodo *p, const nodo1 *q )
{
    return ( p == NULL && q == NULL ) ||
           ( p != NULL && q != NULL && 
             p->dato == q->dato  && 
             isMirror( p->izq, q->der ) &&
             isMirror( p->der, q->izq ) ); 
}

Then in main you can write

printf( "raiz1 is a mirror of raiz is %s\n", 
        isMirror( raiz, raiz1 ) ? "true" : "false" );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335