I want to compare two trees:
I will consider the trees equal if they are:
Isomeric-i.e have the same structure.
Nodes in both trees have the same values but the order of the children nodes are not necessarily the same.
A picture is worth a thousand words:
eg:
A) B) C) D)
(2) (2) (2) (2)
/ | \ / | \ / | \ / | \
(4) (5) (7) (5) (7) (4) (5) (4) (7) (5) (4) (7)
| | | | | | | |
(3) (7) (7) (3) (7) (3) (3) (7)
/ \ / \ / \ / \
(0) (1) (1) (0) (0) (1) (0) (1)
Trees A), B) and C) are equal.
Tree D) isnt equal to any of the trees due to subtrees:
(5) and (4)
| |
(3) (7)
/ \
(0) (1)
I have already looked at the classical linear time algorithm for rooted tree isomorphism due to Aho, Hopcroft and Ullman but his algo only checks for structure but doesnt account for contents/values in the respctive nodes.
link
I was wondering if its possible to incomporate the comparison of the actual node values into this algorithm?
A trivial algorithm will be to sort children nodes for all nodes seriallize the resulting tree for both trees then a simple comparison might suffice.
I was wondering if there exist a more eficient way of accomplishing this.
Update: NODE values are not distinct and could be repetive.