dedecms织梦内容管理系统    
首页 | java | C/C++ | PHP | 操作系统 | ajax | 脚本编程 | 安全技术 | 本站下载页 | 专题 | QQ群 | 测试中心 | 会员中心 | 积分规则
  当前位置:主页>java>文章内容
我用composite模式写的一个二叉树的例子
来源: 作者:
我用composite模式写的一个二叉树的例子 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    **************/ package binarytree; public abstract class   Component { private String name; public abstract Component addChild(Component leftChild,Component rightChild); public String getName(){return name;}     public void getTreeInfo(){}      public abstract int  getLength(); }   /**  Leaf.java    **************/ package binarytree; public class Leaf extends Component{  private String name;  private Component leaf=null;  public Leaf(String name) {  this.name=name;  }   public Component addChild(Component leftChild,Component rightChild){        return this;     }      public String getName(){      return name;     }       public int  getLength() {     return 1;   }  public static void main(String[] args) {  } }   /**  Tree.java    **************/ package binarytree; public class Tree extends Component {  private String name;  private Component leftChild;  private Component rightChild;  public Tree(String name,Component leftChild,Component rightChild) {  this.name=name;  this.leftChild=leftChild;  this.rightChild=rightChild;  }  public Tree(String name) {  this.name=name;  this.leftChild=null;  this.rightChild=null;  }    public Component addChild(Component leftChild,Component rightChild){    this.leftChild=leftChild;    this.rightChild=rightChild;    return this;    }    public String getName(){    return name;    }    public void getTreeInfo()         //得到树或叶子的详细信息   //先打印自己的名字,再遍例左孩子,再遍例右孩子   //如果左孩子或右孩子是树,递归调用   {    System.out.println(" this trees name is "+getName());    if(this.leftChild instanceof Leaf)    {    System.out.println(getName()+"s left child is "+this.leftChild.getName()+",it is a Leaf");    }    if(this.leftChild instanceof Tree){    System.out.println(getName()+"s left child is "+this.leftChild.getName()+",it is a Tree");    this.leftChild.getTreeInfo();    }    if(this.leftChild==null)    {    System.out.println(getName()+"s left child is a null");    }    if(this.rightChild instanceof Leaf)    {    System.out.println(getName()+"s right child is "+this.rightChild.getName()+",it is a Leaf");    }    if(this.rightChild instanceof Tree) {    System.out.println(getName()+"s right child is "+this.rightChild.getName()+",it is a Tree");    this.rightChild.getTreeInfo();    }    if(this.rightChild==null)    {    System.out.println(getName()+"s right child is a null");    }    //System.out.println(getName()+"s 高度 是 "+getLength());    }   public int  getLength() {  //比较左孩子或右孩子的高度,谁大,+1 返回  // 空孩子的处理   if(this.leftChild==null) {       if(this.rightChild==null)       return 1;       else            return this.rightChild.getLength()+1;      }   else {          if(this.rightChild==null) {           return this.leftChild.getLength()+1;                }          else {               if((this.leftChild.getLength())>=(this.rightChild.getLength()))               return this.leftChild.getLength()+1;               else               return this.rightChild.getLength()+1;               }        }   }  public static void main(String[] args) {  } }   /**  Test.java    测试类   **************/ package binarytree; public class Test {    public Test() {  }   public static void main(String[] args) {    Component tree=new Tree("luopeng");    Component leaf_child=new Leaf("luopeng1");    Component right_child=new Leaf("luopeng2");    tree=tree.addChild(leaf_child,right_child);    //tree=tree.addRightChild(right_child);    tree.getTreeInfo();    Component tree1=new Tree("luopeng2");    tree1.addChild(tree,leaf_child);    tree1.getTreeInfo();    Component tree2=new Tree("luopeng3");    tree2.addChild(tree,null);    tree2.getTreeInfo();    Component tree4=new Tree("luopeng4");    tree4.addChild(null,tree);    tree4.getTreeInfo();    System.out.println(tree4.getName()+"的高度是   "+tree4.getLength());  } 

上一篇:Ioc模式解析   下一篇:flex控制光标
[收藏] [推荐] [评论(0条)] [返回顶部] [打印本页] [关闭窗口]  
用户名: 新注册) 密码: 匿名评论
评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
 §最新评论
  热点文章
·java自学路线图
·JSP入门实例教程
·JUnit单元测试(2)
·JUnit单元测试(1)
·什么是Servlet
·用AJAX+J2EE实现网上会议室系统
·浅谈在Java语言中究竟是传值还是
·Java 5.0 多线程编程实践
·Java的文件 读和写
·构造器内部的多态方法的行为
·JSP常用内置对象使用说明
·对于JAVA基础测试中常见的异常问
  相关文章
·Ioc模式解析
·Flex + Java 中小型项目的代码结
·穿透Socks5 代理的UDP编程
·Java解析网络数据流的三种特殊方
·Java 在Client/Server 网络中的
·JSP和JSF合并 共同打造完美的Web
·JSP和JSF合并 共同打造完美的Web
·JSP和JSF合并 共同打造完美的Web
·JSP与JavaMail (一)
·JSP和JSF合并 共同打造完美的Web
·JSP与JavaMail (二)
·jsfl与Flash的完美结合
  相关信息
copy right @ 百家拳软件项目研究室 2007 辽ICP备07011763