Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 525|回复: 0

[算法学习]composite模式写的二叉树的例子

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-28 23:57:41 | 显示全部楼层 |阅读模式
    1,Component 是抽象组件, Tree 和Leaf 继承Component  
       
         
         private String name; //树或叶子的名称

    addChild(Component leftChild,Component rightChild);
    //给一个树上加上一个左孩子,一个右孩子

    getName(){return name;}
    getTreeInfo(){} //得到树或叶子的详细信息
    getLength(); //得到树的高度  2,Tree 二叉树,一个左孩子,一个右孩子  3,Leaf 是叶子节点
    4,Test 是测试节点  /** Component.java **************/   
         
          
          
            
          

          
          
         
       
       
       
    1. /** Component.Java **************/

    2. package binarytree;
    3. public abstract class Component {
    4.   private String name;
    5.   public abstract Component addChild(Component leftChild,Component rightChild);
    6.   public String getName(){return name;}
    7.   public void getTreeInfo(){}
    8.   public abstract int getLength();
    9. }
    10. /** Leaf.java **************/
    11. package binarytree;
    12. public class Leaf extends Component{
    13.   private String name;
    14.   private Component leaf=null;
    15.   public Leaf(String name) {
    16.      this.name=name;
    17.   }
    复制代码

       

       
      
    1. public Component addChild(Component leftChild,Component rightChild){
    2.     return this;
    3.   }
    4.   public String getName(){
    5.    return name;
    6.   }
    7.    public int getLength() {
    8.      return 1;
    9.    }
    10.    public static void main(String[] args) {
    11.    }
    12. }
    13. /** Tree.java **************/
    14. package binarytree;
    15. public class Tree extends Component {
    16.    private String name;
    17.    private Component leftChild;
    18.    private Component rightChild;
    19.    public Tree(String name,Component leftChild,Component rightChild) {
    20.      this.name=name;
    21.      this.leftChild=leftChild;
    22.      this.rightChild=rightChild;
    23.     }
    24.     public Tree(String name) {
    25.        this.name=name;
    26.        this.leftChild=null;
    27.        this.rightChild=null;
    28.     }
    29.     public Component addChild(Component leftChild,Component rightChild){
    30.       this.leftChild=leftChild;
    31.       this.rightChild=rightChild;
    32.       return this;
    33.     }
    34.     public String getName(){
    35.       return name;
    36.     }
    37.     public void getTreeInfo()
    38.        //得到树或叶子的详细信息
    39.        //先打印自己的名字,再遍例左孩子,再遍例右孩子
    40.        //如果左孩子或右孩子是树,递归调用
    41.     {
    42.        System.out.println(" this trees name is "+getName());
    43.         if(this.leftChild instanceof Leaf){
    44.               System.out.println(getName()+"s left child is "+this.leftChild.getName()+",it is a Leaf");
    45.          }
    46.         if(this.leftChild instanceof Tree){
    47.             System.out.println(getName()+"s left child is "+this.leftChild.getName()+",it is a Tree");
    48.             this.leftChild.getTreeInfo();
    49.          }
    50.          if(this.leftChild==null){
    51.             System.out.println(getName()+"s left child is a null");
    52.           }
    53.           if(this.rightChild instanceof Leaf){
    54.             System.out.println(getName()+"s right child is "+this.rightChild.getName()+",it is a Leaf");
    55.           }
    56.          if(this.rightChild instanceof Tree) {
    57.              System.out.println(getName()+"s right child is "+this.rightChild.getName()+",it is a Tree");
    58.               this.rightChild.getTreeInfo();
    59.           }
    60.          if(this.rightChild==null){
    61.              System.out.println(getName()+"s right child is a null");
    62.           }
    63.           //System.out.println(getName()+"s 高度 是 "+getLength());
    64.          }
    65.        public int getLength() {
    66.               //比较左孩子或右孩子的高度,谁大,+1 返回
    67.               // 空孩子的处理
    68.          if(this.leftChild==null) {
    69.             if(this.rightChild==null)
    70.                return 1;
    71.             else
    72.                return this.rightChild.getLength()+1;
    73.          }else {
    74.             if(this.rightChild==null) {
    75.                return this.leftChild.getLength()+1;
    76.             }else {
    77.                 if((this.leftChild.getLength())>=(this.rightChild.getLength()))
    78.                     return this.leftChild.getLength()+1;
    79.                 else
    80.                   return this.rightChild.getLength()+1;
    81.             }
    82.          }
    83. }
    84.    public static void main(String[] args) {
    85.    }
    86. }
    87. /** Test.java 测试类 **************/
    88. package binarytree;
    89. public class Test {
    90. public Test() {}
    91. public static void main(String[] args) {
    92.    Component tree=new Tree("luopeng");
    93.    Component leaf_child=new Leaf("luopeng1");
    94.    Component right_child=new Leaf("luopeng2");
    95.    tree=tree.addChild(leaf_child,right_child);
    96.    //tree=tree.addRightChild(right_child);
    97.    tree.getTreeInfo();
    98.    Component tree1=new Tree("luopeng2");
    99.    tree1.addChild(tree,leaf_child);
    100.    tree1.getTreeInfo();
    101.    Component tree2=new Tree("luopeng3");
    102.    tree2.addChild(tree,null);
    103.    tree2.getTreeInfo();
    104.    Component tree4=new Tree("luopeng4");
    105.    tree4.addChild(null,tree);
    106.    tree4.getTreeInfo();
    107.    System.out.println(tree4.getName()+"的高度是 "+tree4.getLength());
    108. }
    109. }
    复制代码

    程序运行结果:   C:java>java Test
    this trees name is luopeng
    luopengs left child is luopeng1,it is a Leaf
    luopengs right child is luopeng2,it is a Leaf

    this trees name is luopeng2
    luopeng2s left child is luopeng,it is a Tree
    this trees name is luopeng
    luopengs left child is luopeng1,it is a Leaf
    luopengs right child is luopeng2,it is a Leaf
    luopeng2s right child is luopeng1,it is a Leaf

    this trees name is luopeng3
    luopeng3s left child is luopeng,it is a Tree
    this trees name is luopeng
    luopengs left child is luopeng1,it is a Leaf
    luopengs right child is luopeng2,it is a Leaf
    luopeng3s right child is a null

    this trees name is luopeng4
    luopeng4s left child is a null
    luopeng4s right child is luopeng,it is a Tree
    this trees name is luopeng
    luopengs left child is luopeng1,it is a Leaf
    luopengs right child is luopeng2,it is a Leaf
    luopeng4的高度是 3
      

      
      
       
       

         
       

         
       
      



    源码下载:http://file.javaxxz.com/2014/10/28/235740796.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-26 01:02 , Processed in 0.364043 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表