Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if two trees are equivalent

The following are three equivalent representations of the same tree (phylogenetic). I am trying to figure out an algorithm to check if two tree representations are equivalent. Trees are defined to be equivalent if the parent-child relationship between nodes are similar.

(Whale,(Seal,((Mouse,Rat),((((Carp,Loach),Frog),Chicken),Human))),Cow);
(Whale,(Seal,((Rat,Mouse),(Human,((Frog,(Loach,Carp)),Chicken)))),Cow);
((Seal,((Rat,Mouse),(Human,((Frog,(Loach,Carp)),Chicken)))), Cow, Whale);

Can anyone suggest a method?

like image 845
Sunder Avatar asked Feb 21 '23 18:02

Sunder


1 Answers

One way would be to walk the children in lexographical order (or any strict weak ordering) and compare.

like image 97
Andrew Tomazos Avatar answered Mar 11 '23 17:03

Andrew Tomazos