diff options
| author | drbaggy <js5@sanger.ac.uk> | 2021-08-10 22:26:12 +0100 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2021-08-10 22:26:12 +0100 |
| commit | 30cdb0fab6ac66313d925f55769ece128b549ad4 (patch) | |
| tree | b774ed34ff94232fd8885cba0eae98b30661832a | |
| parent | 6fddb13683f2f163aa9a83ea21230b1a07cc7fcf (diff) | |
| download | perlweeklychallenge-club-30cdb0fab6ac66313d925f55769ece128b549ad4.tar.gz perlweeklychallenge-club-30cdb0fab6ac66313d925f55769ece128b549ad4.tar.bz2 perlweeklychallenge-club-30cdb0fab6ac66313d925f55769ece128b549ad4.zip | |
fixed - using walk - missed out an obvious ommission
| -rw-r--r-- | challenge-125/james-smith/perl/BinaryTree.pm | 19 | ||||
| -rw-r--r-- | challenge-125/james-smith/perl/ch-2.pl | 27 |
2 files changed, 33 insertions, 13 deletions
diff --git a/challenge-125/james-smith/perl/BinaryTree.pm b/challenge-125/james-smith/perl/BinaryTree.pm index 1cbcbfc511..05703c9b26 100644 --- a/challenge-125/james-smith/perl/BinaryTree.pm +++ b/challenge-125/james-smith/perl/BinaryTree.pm @@ -32,18 +32,13 @@ sub depth { sub diameter { my $self = shift; - return 1 + $self->left->depth + $self->right->depth if $self->has_left && $self->has_right; - - my $diameter = $self->depth; ## Case 1 has a single depth.... - my $t = $self->has_left ? $self->left : $self->right; - while( $t->has_left || $t->has_right ) { - if( $t->has_left && $t->has_right ) { - my $d = 1 + $t->left->depth + $t->right->depth; - return $d > $diameter ? $d : $diameter; - } - $t = $t->has_left ? $t->left : $t->right; - } - return $diameter; + my $global = { 'diameter' => 0 }; + $self->walk( sub { + my $d = ($_[0]->has_left ? $_[0]->left->depth : 0 ) + + ($_[0]->has_right ? $_[0]->right->depth : 0 ); + $_[1]{'diameter'} = $d if $d > $_[1]->{'diameter'}; + }, $global ); + return $global->{'diameter'}; } sub value { diff --git a/challenge-125/james-smith/perl/ch-2.pl b/challenge-125/james-smith/perl/ch-2.pl index d4d2cb738d..71d510b253 100644 --- a/challenge-125/james-smith/perl/ch-2.pl +++ b/challenge-125/james-smith/perl/ch-2.pl @@ -72,6 +72,31 @@ $x->dump; say sprintf 'Max depth: %d, diameter %d', $x->depth, $x->diameter; say ''; +## 1 -> 2 -> 3 -> 4 -> 5 -> 6 +## `>10 `> 7 -> 8 -> 9 +## -------------------------- +## 9 -> 8 -> 7 -> 3 -> 4 -> 5 -> 6 + +$x = BinaryTree->new(1)->add_child_left( + BinaryTree->new(2)->add_child_left( + BinaryTree->new(3)->add_child_left( + BinaryTree->new(4)->add_child_left( + BinaryTree->new(5)->add_child_left( BinaryTree->new(6) ) + ) + )->add_child_right( + BinaryTree->new(7)->add_child_left( + BinaryTree->new(8)->add_child_left( BinaryTree->new(9) ) + ) + ) + )->add_child_right( BinaryTree->new(10)) + ); + +say '4) Tree with node further down having two children - but distance from'; +say ' root to node is greater than the depth of either child'; +$x->dump; +say sprintf 'Max depth: %d, diameter %d', $x->depth, $x->diameter; +say ''; + ## This time both child trees have depths longer than the number of ## ancestor nodes - so that is used to get the length... @@ -80,7 +105,7 @@ say ''; ## --------------------- ## 6 -> 5 -> 2 -> 3 -> 4 [ diameter 5 ] -say '4) Tree with node further down having two children - but distance from'; +say '5) Tree with node further down having two children - but distance from'; say ' root to node is less than the depth of both children'; $x = BinaryTree->new(1)->add_child_left( BinaryTree->new(2)->add_child_left( |
