Chinaunix首页 | 论坛 | 博客
  • 博客访问: 18785
  • 博文数量: 3
  • 博客积分: 130
  • 博客等级: 入伍新兵
  • 技术积分: 40
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-25 16:27
文章分类

全部博文(3)

文章存档

2008年(3)

我的朋友
最近访客

分类:

2008-11-24 10:05:41

打印格式时费了点时间。
 

#!/usr/bin/perl


$rootname = "parent";
%tree = ("parentleft","child1",
                 "parentright","child2",
                 "child1left","grandchild1",
                 "child1right","grandchild2",
                 "child2left","grandchild3",
                 "child2right","grandchild4",
                 "grandchild3left","gc3child1",
                 "grandchild3right","gc3child2"
                );
$i=@lentree=values(%tree);
 
print ("\n节点数:".($i+1)."\n------------\n");                

&print_tree($rootname);

sub print_tree{
    local ($nodename)=@_;
    local ($leftchildname,$rightchildname);
  
    $leftchildname =$nodename."left";
    $rightchildname=$nodename."right";
    if ($tree{$leftchildname} ne ""){
     $j+=1;     
        &print_tree($tree{$leftchildname});
        $j-=1;
        }    

# print ("j=$j\n");

#    print ("k=$k\n");

print ((" |")x ($j+$k) );
print ( "$nodename\n");
    if ($tree{$rightchildname} ne ""){
        $k+=1;        
        &print_tree($tree{$rightchildname});
        $k-=1;
    }    

}

阅读(1410) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~