Well, after your editing, where you introduced the requirement that the hash result should be different for different tree layouts, you just have to leave the option to traverse the whole tree and write its structure to one array.
This is done as follows: you cross the tree and perform the operations that you perform. For the source tree, which could be (for the structure on the left and right):
[1, child, 2, child, 3, sibling, 4, sibling, 5, parent, parent, //we're at root again sibling, 6, child, 7, child, 8, sibling, 9, parent, parent]
You can then assign the list (i.e., actually a string) as you like. As another option, you can even return this list as a result of a hash function, so it becomes a tree without collisions.
But adding accurate information about the entire structure does not mean that hash functions usually function. The proposed method is to calculate the hash function of each node, as well as traverse the entire tree. Therefore, you can consider other hashing methods described below.
If you do not want to move around the tree:
One of the algorithms that immediately crossed my mind is similar to this. Choose a large prime number H (greater than the maximum number of children). To have a hash tree, hash its root, select the child number H mod n , where n is the number of root children and recursively hash the subtree of this child.
This seems to be a bad option if the trees differ only deep in the leaves. But at least he should run fast after not very tall trees.
If you want a hash of fewer elements, but go through the whole tree :
Instead of hashing the subtree, you might want to use a hash layer. That is, the hash root, and not one of the nodes that are its children, then one of the children of the children, etc. This way you cover the whole tree instead of one of the specific paths. This makes the hash process slower, of course.
--- O ------- layer 0, n=1 / \ / \ --- O --- O ----- layer 1, n=2 /|\ | / | \ | / | \ | O - O - O O------ layer 2, n=4 / \ / \ ------ O --- O -- layer 3, n=2
A node from the layer is selected with the rule H mod n .
The difference between this version and the previous version is that the tree has to go through a rather illogical transformation in order to save the hash function.