I am trying to check if these two nodes are the same, which means that they are the same number of children, and that these children lead to the same condition. Therefore, I am effectively trying to compare subtrees to two nodes. I am wondering if I can use hashing to perform this equality check.
No, hashing should not be used to verify equality. This is not his goal. Ultimately, this will help you find out if objects are objective, but he will not tell you if they are equal.
The same objects will generate the same hash value, but two different objects that are not equal can generate the same hash. In other words, if the hash values ββare different, you know for sure that the objects are different. What is it.
If you want to test equality, you need to implement equals. In your case, there is a danger that your method will be recursive and cause a stack overflow. What if your object contains a link to itself?
If you want to generate a hash, you can take the size of the array into account (and the fact that it is zero or not), but I would not use the hash value of the objects in the array, due to potential infinite loops. It is not perfect, but it is good enough.
There is another radical method that can also provide a good result. Instead of dynamically calculating the hash values, set a random int value for each instance of the Node object (I mean once to create at creation and always return that value). In your case, you do not risk endless loops by taking the hash value of the object instances in your array.
If the hashes are equal, then you will need to start comparing the instances of the array objects.
REM: If the nodes contain other attributes, then calculate the hash on these other attributes and forget about the array. Start exploring the contents or size of the array if and only if the hash is identical between two objects.
REM2: Comments mention the DAG graph, which means that we will not use recursion problems. However, this condition is not enough to guarantee that deepHashCode () will succeed. Moreover, this would also be redundant. There is a more efficient way to solve this problem.
If the hash method used by Node only uses an array to calculate the hash value, then deepHashCode () may work. But that would be ineffective. If the hash method uses other Node attributes, then these attributes must also be equal.
There is a faster way to compare nodes for equality. Label each instance of Node with a unique number. Then, to compare the two nodes, first compare their array size. If it is equal, compare the nodes from each array using their unique number. If one array does not have a "different" node, then we are not dealing with equal nodes. This solution is much faster than recursive.