I am frequently searching a large forest for many different tree fragments , and would like to have the "equality check" part of the search occur faster than O(N) time. The idea is to "hash" tree fragments and treat collisions as equality.
My tree structure is a standard Node object with an int value field and a children field. I would like the hash value of a tree rooted at a certain node to depend only on it's children, and that given a child's hashcode has changed, a relatively cheap update can be applied to the hashcode of the child's parent. If we can achieve an O(1) update operation, then a value change at a leaf node will result in a O(log n) chain reaction up the tree.
I have read the following question: Hashing a Tree Structure
which gives a great answer. However, I am having implementation troubles with the algorithm. As strings are immutable in Java, an update in the child-node would translate to an O(N) hashcode update operation in the parent. Please correct me here if it seems I've misunderstood the answer above.
What is a possible hash function for what I'm looking for?