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 fi +rst) 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: O +K # => HERE BOTH ARE PARENTS WITH SAME AMOUNT OF CHILDREN my $nchild = $t1->n_children($t1); my @permutes = 0 .. $nchild - 1; do { PERMUTATION: { for my $i (0 .. $nchild - 1) { if (!tstruct($t1->nth_child($t1, $i), $t2->nth_child($t2, $permutes[$i]))) { next PERMUTATION } } # if we make it here, all children compared okay, return TRUE; }} while (nextperm(\@permutes)); return FALSE; } # }}} # From Algorithm L in Knuth Vol. 4 Sec 7.2.1.2 (not yet published) sub nextperm { my ($arrayref) = @_; my $n = @{$arrayref}; my $j = $n - 2; while ($j >= 0 && ${$arrayref}[$j] >= ${$arrayref}[$j+1]) { $j--; } return 0 if $j < 0; my $q = $n - 1; while (${$arrayref}[$j] >= ${$arrayref}[$q]) { $q--; } # swap a[q], a[j] (${$arrayref}[$q], ${$arrayref}[$j]) = (${$arrayref}[$j], ${$arrayref}[$q]); @{$arrayref}[$j+1 .. $n-1] = reverse @{$arrayref}[$j+1 .. $n-1]; return 1; }
In reply to Re: Doing all combinations of comparisons between members of 2 lists
by Thelonius
in thread Doing all combinations of comparisons between members of 2 lists
by PetaMem
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |