Farid Hajji: Perl - Einführung, Anwendungen, Referenz
2., aktualisierte und erweiterte Auflage
Addison-Wesley Longman, ISBN 3-8273-1535-2
bintree-traverse.pl
#!/usr/local/bin/perl -w
# bintree-traverse.pl -- Traversiert einen binaeren Baum
use strict 'refs';
my $tree = {
VALUE => '1',
LEFT => { VALUE => '2',
LEFT => { VALUE => 3 },
RIGHT => { VALUE => 4 }
},
RIGHT => { VALUE => '5',
LEFT => { VALUE => 6 },
RIGHT => { VALUE => 7 }
}
};
print "Preorder : "; preorder($tree); print "\n";
print "Postorder: "; postorder($tree); print "\n";
print "Inorder : "; inorder($tree); print "\n";
sub proceed { print "(" . shift() . ")"; }
sub preorder {
my $root = shift;
proceed($root->{'VALUE'});
preorder($root->{'LEFT'}) if exists $root->{'LEFT'};
preorder($root->{'RIGHT'}) if exists $root->{'RIGHT'};
}
sub postorder {
my $root = shift;
postorder($root->{'LEFT'}) if exists $root->{'LEFT'};
postorder($root->{'RIGHT'}) if exists $root->{'RIGHT'};
proceed($root->{'VALUE'});
}
sub inorder {
my $root = shift;
inorder($root->{'LEFT'}) if exists $root->{'LEFT'};
proceed($root->{'VALUE'});
inorder($root->{'RIGHT'}) if exists $root->{'RIGHT'};
}
[Prev] [Up] [Relevant Chapter] [Next]
[Alte Quelle]
| Last modified: $Date: 2006/05/18 12:55:51 $ FH. Search :: Sitemap :: Disclaimer :: Copyright :: Privacy |
|