Binary tree for perl

Binary tree for perl,第1张

概述#!/usr/bin/perl -w# bintree - binary tree demo programuse strict;my ( $root, $n ); # first generate 20 random insertswhile ( $n++ < 20 ) { insert ( $root, int ( rand
#!/usr/bin/perl -w# bintree - binary tree demo programuse strict;my ( $root,$n );                       # first generate 20 random insertswhile ( $n++ < 20 ) { insert ( $root,int ( rand ( 1000 ) ) }                       # Now dump out the tree all three ways                               print "Pre order:  ";pre_order ( $root );print "\n";print "In order:   ";in_order ( $root );print "\n";print "Post order: ";post_order ( $root );print "\n";                       # prompt until EOFfor ( print "Search? "; <>; print "Search? " ){    chomp;    my $found = search ( $root,$_ );    if ( $found ) { print "Found $_ at $found,$found->{VALUE}\n" }    else        { print "No $_ in tree\n" }}                       exit;                       #########################################                       # insert given value into proper point of# provIDed tree.  If no tree provIDed,# use implicit pass by reference aspect of @_# to fill one in for our caller.sub insert{    my ( $tree,$value ) = @_;    unless ( $tree )    {        $tree = {};# allocate new node        $tree-> {VALUE}  = $value;        $tree-> {left}   = undef;        $tree-> {RIGHT}  = undef;        $_[0] = $tree;# $_[0] is reference param!        return;    }    if    ( $tree->{VALUE} > $value ) { insert ( $tree-> {left},$value ) }    elsif ( $tree->{VALUE} < $value ) { insert ( $tree-> {RIGHT},$value ) }    else                            { warn "dup insert of $value\n"  }# XXX: no dups}# recurse on left child,# then show current value,# then recurse on right child.sub in_order{    my ( $tree ) = @_;    return unless $tree;    in_order ( $tree->{left} );    print $tree->{VALUE}," ";    in_order ( $tree->{RIGHT} );}# show current value,# then recurse on left child,# then recurse on right child.sub pre_order{    my ( $tree ) = @_;    return unless $tree;    print $tree->{VALUE}," ";    pre_order ( $tree->{left} );    pre_order ( $tree->{RIGHT} );}                          # recurse on left child,# then recurse on right child,# then show current value.sub post_order{    my ( $tree ) = @_;    return unless $tree;    post_order ( $tree->{left} );    post_order ( $tree->{RIGHT} );    print $tree->{VALUE}," ";}                       # find out whether provIDed value is in the tree.# if so,return the node at which the value was found.# cut down search time by only looking in the correct# branch,based on current value.sub search{    my ( $tree,$value ) = @_;    return unless $tree;    if ( $tree->{VALUE} == $value )    {        return $tree;    }    search ( $tree->{ ( $value < $tree->{VALUE} ) ? "left" : "RIGHT"},$value )}__END__
总结

以上是内存溢出为你收集整理的Binary tree for perl全部内容,希望文章能够帮你解决Binary tree for perl所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1281130.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存