if two trees are identical

closed account (Nwb4iNh0)
Why we pass two parameters/pointers in bool function when we want to create function to identify if two trees are identical ?
and when we call this function in main body we should call it with

bool areTreesIdentical (root1, root2)???



 
  bool areTreesIdentical(Node *a, Node *b)
how do you compare 2 objects for equality if not supplying both objects to function?

if 2 objects are equal it doesn't matter which one you use afterwards.
your question is not clear, do you want to pass single parameter?
closed account (Nwb4iNh0)
the question is to create function to check and returns if the (sub) trees are completely identical.
but i did not know why we passed to pointers in the function as I mentioned above
Node *a, Node *b?
You don't have to pass pointers, 2 tree parameters could also be references, ex:

bool areTreesIdentical(const Node& a, const Node& b)

It's bad design to pass objects by value because comparison function must not modify objects:

bool areTreesIdentical(Node a, Node b) // bad

It's bad even if parameters are const because comparison function must not modify compared objects so there is no need to copy them:

bool areTreesIdentical(const Node a, const Node b) // again bad

If you want to return one of the 2 trees if identical you can use 3rd parameter:

1
2
3
4
5
6
bool areTreesIdentical(Node *a, Node *b, Node* result)
{
        if (*a == *b)
             result = a; //or b
        else result = nullptr;
}


return value of bool is there just to confirm equality, but in the case of result parameter it could be omited.
Last edited on
closed account (Nwb4iNh0)
Sorry I cant get you point here and I still dont understand why we use node * a and node *b in this function
I actullay should follow the teacher question with this form

 
 bool areTreesIdentical(Node *a, Node *b)


so why did we pass node a and node b here as a pointers??
so why did we pass node a and node b here as a pointers??

Ah that, I'm sorry for confusion!

Pointers unlike usual object variables point to memory location where the pointed objects lives.
When you pass a pointer you don't pass the real object but instead the memory address where those objects are, that is, a memory location.

By using an asterisk * as in *a or *b, that asterisk returns the real object at that memory (not the address).

just saying a or b is a memory address, while *a or *b is the actual object in memory.

Why is this important for your function bool areTreesIdentical(Node *a, Node *b)?
It's important because of performance reasons, that function takes just memory address instead of real objects.

A memory address is just an int, however the real object is usually much bigger, that's why specifying the address is faster because copying memory address as function parameter (an int) is much faster than copying the whole object.

Conclusion:
An address being of size int is 2 or 4 bytes, while the real object Node can be much more bytes, therefore working with pointers is faster.

To be more precise, the size of an adress (pointer) is either 32 bits on x86 CPU, or 64 bits on x64 CPU.

In your example a and b are memory addresses, while *a and *b are real object at those memory locations.
Last edited on
closed account (Nwb4iNh0)
Good
a or b is just a memory address but *a and *b is real object, then the function only takes the memory address so it needs to be

 
 bool areTreesIdentical(Node a, Node b)

?

I actually do have problem in pointers , and with your explanation I may catch the point so what the difference between

a , *a, &a ?
If you were to create a function that compares two trees, how would you do it?

How would you tell it what trees you wanted compared?
then the function only takes the memory address so it needs to be
bool areTreesIdentical(Node a, Node b)
?


No, bool areTreesIdentical(Node a, Node b), here in this function you specify real objects in memory, which is the same thing as *a or *b if using pointers.

The address of operator & takes the address of real objects, for example:

1
2
Node a; // a is real object
Node* ptr = &a; // "ptr" is pointer to memory location of "a"  


Keep in mind that address of operator & does not have the same meaning as reference, for example:

1
2
3
Node a; // a is real object, (not pointer and not reference)
Node& b = a; // b is reference to a, "&" operator declared a reference, (not pointer, not address).
Node* c = &a; // c is pointer to a, "&" operator takes the address of "a" 


References are one thing, pointer are not the same, so the & operator has different meaning in different contexts, simply memorize this.

EDIT:
Likewise the * operator (asterisk) has different meaning in different contexts:

1
2
3
Node a; // a is real object
Node* c = &a; // * (asterisk) declares a pointer named "c"
Node b = *c; // * (asterisk) dereferences the pointer "c", returns the real object at memory address 
Last edited on
closed account (Nwb4iNh0)
Thanks for you efforts :)
You're welcome friend ;)
Topic archived. No new replies allowed.