<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 73,  comments - 55,  trackbacks - 0

    我用composite模式寫的一個二叉樹的例子
    1,Component 是抽象組件
    Tree 和Leaf 繼承Component

    private String name; //樹或葉子的名稱
    addChild(Component leftChild,Component rightChild);
    //給一個樹上加上一個左孩子,一個右孩子
    getName(){return name;}
    getTreeInfo(){} //得到樹或葉子的詳細信息
    getLength(); //得到樹的高度

    2,Tree 二叉樹,一個左孩子,一個右孩子

    3,Leaf 是葉子節(jié)點
    4,Test 是測試節(jié)點

    /** 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 left_child = new Leaf("luopeng1");
    ??Component right_child = new Leaf("luopeng2");
    ??tree = tree.addChild(left_child, right_child);
    ??// tree=tree.addRightChild(right_child);
    ??tree.getTreeInfo();
    ??Component tree1 = new Tree("luopeng2");
    ??tree1.addChild(tree, left_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());
    ?}
    }

    posted on 2006-07-31 17:33 保爾任 閱讀(562) 評論(0)  編輯  收藏 所屬分類: Design Patten

    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    常用鏈接

    留言簿(4)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲国产品综合人成综合网站| 国产成人麻豆亚洲综合无码精品 | 老司机亚洲精品影院无码| 国产精品无码永久免费888| 中文字幕亚洲专区| 好湿好大好紧好爽免费视频| 亚洲日韩中文在线精品第一 | 国产精品内射视频免费| 亚洲精品97久久中文字幕无码| 无人视频免费观看免费视频| 亚洲精品国产精品乱码不卡| 九九免费久久这里有精品23| 亚洲精品高清无码视频| 久久这里只精品热免费99| 亚洲成aⅴ人片在线影院八| 成人午夜视频免费| 国产亚洲精品仙踪林在线播放| 亚洲区小说区图片区| 在线免费观看伊人三级电影| 久久久无码精品亚洲日韩蜜桃 | 日韩一品在线播放视频一品免费| 亚洲精品第一国产综合亚AV| 亚洲成A人片在线观看无码3D| caoporn国产精品免费| 亚洲va久久久噜噜噜久久男同 | 免费无码H肉动漫在线观看麻豆| 亚洲AV乱码久久精品蜜桃| 亚洲美女视频免费| 极品色天使在线婷婷天堂亚洲| 亚洲午夜无码片在线观看影院猛| 七色永久性tv网站免费看| 亚洲AV综合色区无码二区偷拍| 四虎影视在线永久免费看黄| a级毛片毛片免费观看永久| 亚洲午夜电影在线观看| 国产自产拍精品视频免费看| 大地资源网高清在线观看免费| 亚洲AV综合色区无码二区爱AV| 国产偷窥女洗浴在线观看亚洲| 16女性下面无遮挡免费| 美女视频黄频a免费|