Click here to Skip to main content
15,392,679 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C++
<pre>#include <iostream>            
sing namespace std;

struct Node {
    int ID;                 
    Node *left, *right;     
    Node(int id) { ID = id;  left = right = nullptr; }
};


Node* gRoot1 = nullptr, *gRoot2 = nullptr; 
Node* createTree();


void  setAllIDareEqual(Node* Z);
    
bool areTreesIdentical(Node *a, Node *b);
bool isTreeSymmetric(Node* a, Node* b)



 
 
int main() {
    gRoot1 = createTree();     gRoot2 = createTree();


    cout << "\n\nBoth are  "
       << (!areTreesIdentical(gRoot1, gRoot2) ? "NOT " : "") << "identical.";


    delete gRoot2->right->right->left;    
    gRoot2->right->right->left = nullptr;
    cout << "\n\nBoth are  "
       << (!areTreesIdentical(gRoot1, gRoot2) ? "NOT " : "") << "identical.";


    delete gRoot1->right->right->left;    
    gRoot1->right->right->left = nullptr;
    cout << "\n\nBoth are "
       << (!areTreesIdentical(gRoot1, gRoot2) ? "NOT " : "") << "identical.";


    cout << "\n\nTree is  "
       << (!isTreeSymmetric(gRoot1, gRoot1) ? "NOT " :"") << "symetric.";

:
    delete gRoot1->right->right->right;          gRoot1->right->right->right = nullptr;
                                   
    cout << "\n\nTree is  "        
       << (!isTreeSymmetric(gRoot1, gRoot1) ? "NOT " :"") << "Symetric.";


    setAllIDareEqual (gRoot1); 
    cout << "\n\nTree is  "
       << (!isTreeSymmetric(gRoot1, gRoot1) ? "NOT " :"") << "Symetric.";

    cout << "\n\n\n";
    return 0;
}



Node* createTree() {
    Node* p[12];
    for (int i = 1; i <= 11; i++)  p[i] = new Node(i); 
    p[1]->left = p[2];    p[1]->right = p[3];  
    p[2]->left = p[4];    p[2]->right = p[5];  
    p[3]->left = p[6];    p[3]->right = p[7];  
    p[5]->left = p[8];                         
                          p[6]->right = p[9];  
    p[7]->left = p[10];   p[7]->right = p[11]; 
    return p[1];                               
}



void setAllIDareEqual (Node* Z) {
  if (Z) {
      Z->ID = 99;
      setAllIDareEqual (Z->left);
      setAllIDareEqual (Z->right);
  }
}


bool areTreesIdentical(Node* a, Node* b) {

  

    if(a == NULL && b == NULL)  return 1;                          
    if(a != NULL && b != NULL)                                      
        return (a -> ID == b -> ID && areTreesIdentical (a -> left, b -> left) && areTreesIdentical(a -> right, b -> right));
    return 0;                                                         
}



bool isTreeSymmetric(Node* a, Node* b) {

 if (a == NULL && b== NULL) return true;
 if (a && b && a->ID == b-> ID)

     return isTreeSymmetric(a->left, b->right) && isTreeSymmetric(a->right, b->left);

 return false;

    
}




What I have tried:

I can not run this code due some errors , cant figure out any of the errors
Posted
Updated 25-Sep-21 0:31am
Comments
Richard Deeming 22-Sep-21 10:20am
   
If you want someone to help you fix errors in your code, you need to tell us what the errors are.

Click the green "Improve question" link and update your question to include the full details of the errors you're getting. Remember to indicate which line of code the errors relate to.

I see two problems with this. First, createTree is not going to work as it is. Mostly because it does a bunch of stuff with some local data on the stack and then it returns a pointer to one of the items. As soon as that function returns the stack and all of that data is gone so it is just not going to work. Plus, the logic in it is operating on data that is likely not valid so it is dereferencing null pointers. You might want to pass the array of pointers into it and make sure they are not null before you try to access their data.

The other thing likely isn't an error but I don't think it works the way you expect it to. That would be this line :
C++
if (a && b && a->ID == b-> ID)
I would guess it should have some parenthesis in there. That could be rewritten to look like this :
C++
bool isTreeSymmetric(Node* a, Node* b) {
   if( a == NULL ) return true;
   if( b == NULL ) return true;
   if( a->ID == b->ID )
       return isTreeSymmetric(a->left, b->right) && isTreeSymmetric(a->right, b->left);
   return false;
}
   
v2
Comments
TeleMark2002 22-Sep-21 11:46am
   
thanks for reply, but I got feedback from teacher that the creatTree is completly correct, but I got negative feedback on the last two functions ,which is the functions to check if the trees are identical trees or the tree is symetic so all I need is to re check the errors in the last two functions !
Rick York 22-Sep-21 13:13pm
   
My initial understanding of createTree was incorrect. I can see that it does what you want to but I would most definitely NOT call it "completely correct" and I find it difficult to believe that a teacher would. As KarstenK mentioned, you should return the first element of the array (item 0) and the loop should be for( i = 0; i < 11; ++i ) and that means the rest of that function should be revised accordingly. In C and C++ the first item of an array is [0]. You need to get in that habit now or it will cause you lots of problems as you progress.
The createTree function has the BUG that it doesnt return the first tree. You must return the first element which has the index 0. (That is C-standard). Even with that fix the code is messy.

Consider writing cleaner code by creating a class Node in extra files. Than you could use constructors like:
C++
Node(int id, Node *left, Node *right);
Make some output every time you are leaving a function esp when errors are occuring.

tip: write test code to verify your code. It is worth it.
   
Greetings Kind Regards There were two typing errors i.e. a missing terminating semi-colon in the declaration of isTreeSymmetric and a colon in the line above the third delete in main Why did your compiler not tell you of these as mine did Also a misspelling of "symmetric" Other than that the code below worked for me w/ the output below - Cheerio

#include <iostream>
using namespace std;

struct Node {
	int ID;
	Node* left, * right;
	Node(int id) { ID = id;  left = right = nullptr; }
};

Node* gRoot1 = nullptr, * gRoot2 = nullptr;
Node* createTree();
void  setAllIDareEqual(Node* Z);
bool areTreesIdentical(Node* a, Node* b);
bool isTreeSymmetric(Node* a, Node* b);

int main() {
	gRoot1 = createTree();     gRoot2 = createTree();
	cout << "\n\nBoth are  "
		<< (!areTreesIdentical(gRoot1, gRoot2) ? "NOT " : "") << "identical.";
	delete gRoot2->right->right->left;
	gRoot2->right->right->left = nullptr;
	cout << "\n\nBoth are  "
		<< (!areTreesIdentical(gRoot1, gRoot2) ? "NOT " : "") << "identical.";
	delete gRoot1->right->right->left;
	gRoot1->right->right->left = nullptr;
	cout << "\n\nBoth are "
		<< (!areTreesIdentical(gRoot1, gRoot2) ? "NOT " : "") << "identical.";
	cout << "\n\nTree is  "
		<< (!isTreeSymmetric(gRoot1, gRoot1) ? "NOT " : "") << "symmetric.";
	delete gRoot1->right->right->right;          gRoot1->right->right->right = nullptr;
	cout << "\n\nTree is  "
		<< (!isTreeSymmetric(gRoot1, gRoot1) ? "NOT " : "") << "Symmetric.";
	setAllIDareEqual(gRoot1);
	cout << "\n\nTree is  "
		<< (!isTreeSymmetric(gRoot1, gRoot1) ? "NOT " : "") << "Symmetric.";
	cout << "\n\n\n";
	return 0;
}

Node* createTree() {
	Node* p[12];
	for (int i = 1; i <= 11; i++)  p[i] = new Node(i);
	p[1]->left = p[2];    p[1]->right = p[3];
	p[2]->left = p[4];    p[2]->right = p[5];
	p[3]->left = p[6];    p[3]->right = p[7];
	p[5]->left = p[8];
	p[6]->right = p[9];
	p[7]->left = p[10];   p[7]->right = p[11];
	return p[1];
}

void setAllIDareEqual(Node* Z) {
	if (Z) {
		Z->ID = 99;
		setAllIDareEqual(Z->left);
		setAllIDareEqual(Z->right);
	}
}

bool areTreesIdentical(Node* a, Node* b) {
	if (a == NULL && b == NULL)  return 1;
	if (a != NULL && b != NULL)
		return (a->ID == b->ID && areTreesIdentical(a->left, b->left) && areTreesIdentical(a->right, b->right));
	return 0;
}

bool isTreeSymmetric(Node* a, Node* b) {
	if (a == NULL && b == NULL) return true;
	if (a && b && a->ID == b->ID)
		return isTreeSymmetric(a->left, b->right) && isTreeSymmetric(a->right, b->left);
	return false;
}

Both are identical.

Both are NOT identical.

Both are identical.

Tree is NOT symmetric.

Tree is NOT Symmetric.

Tree is Symmetric.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900