# {{{ tstruct have 2 trees same structure? (must be normalized) # sub tstruct { my $t1 = shift; # get reference to "root" of tree1 my $t2 = shift; # get reference to "root" of tree2 my $safe = shift || FALSE; # Flag for safe comparison (normalize first) if($safe == TRUE) { # If safe comparison should happen &tnormalize($t1); # Normalize Tree1 &tnormalize($t2); # Normalize Tree2 } # Exit if one of them is leaf and the other isn't return FALSE if(( $t1->is_leaf($t1) && !$t2->is_leaf($t2)) or (!$t1->is_leaf($t1) && $t2->is_leaf($t2))); # exit if they have different amount of children return FALSE if($t1->n_children($t1) != $t2->n_children($t2)); return TRUE if($t1->is_leaf($t1)); # if t1 is leaf -> both are: OK # => HERE BOTH ARE PARENTS WITH SAME AMOUNT OF CHILDREN # get the children references for $t1 and $t2 my @t1childs; my @t2childs; $t1->children_foreach($t1,$Tree::Nary::TRAVERSE_ALL,\&_pchild_ref,\@t1childs); $t2->children_foreach($t2,$Tree::Nary::TRAVERSE_ALL,\&_pchild_ref,\@t2childs); # get the amount of grandchildren ordered my %grand1; my %grand2; for my $i (0 ... scalar(@t1childs)-1) { # iterate all children by index my $ngk1 = $t1childs[$i]->n_children($t1childs[$i]); # amount of grandkids1 my $ngk2 = $t2childs[$i]->n_children($t2childs[$i]); # amount of grandkids2 $grand1{$ngk1} = () if(!exist($grand1{$ngk1})); push $grand1{$ngk1}, $t1childs[$i]; $grand2{$ngk2} = () if(!exist($grand2{$ngk2})); push $grand2{$ngk2}, $t2childs[$i++]; } # NOW WE HAVE IN THE grand1 & grand2 HASHES THE DISTRIBUTION OF THE GRANDCHILDREN # OF THE CHILDREN PLUS THE CORRESPONDENDING CHILDREN REFERENCES # ensure, that the distributions have same amount of entries return FALSE if(scalar(keys %grand1) != scalar(keys %grand2)); while (my ($key, $value) = each %grand1) { # iterate all T1 grandchild distributions # exit if distribution varies, because they must have same length also, # the following condition ensures we have same distribution in grand1 and grand2 return FALSE if(scalar($grand1{$key}) != scalar($grand2{$key})); } # NO WE HAVE TWO IDENTICAL DISTRIBUTIONS AT CHILD/GRANDCHILD LEVEL while (my ($key, $value) = each %grand1) { # iterate all T1 grandchild distributions if($key > 0) { # we don't need to consider children that are leafs # ULTRABRUTAL CORE RECURSIVE DESCENT COMPARISON ROUTINE HERE # VODOO, BLACK MAGIC, SILVER BULLETS, DIVINE INTERVENTION } } return TRUE; } # }}}