posts - 73,  comments - 55,  trackbacks - 0

          我用composite模式寫(xiě)的一個(gè)二叉樹(shù)的例子
          1,Component 是抽象組件
          Tree 和Leaf 繼承Component

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

          2,Tree 二叉樹(shù),一個(gè)左孩子,一個(gè)右孩子

          3,Leaf 是葉子節(jié)點(diǎn)
          4,Test 是測(cè)試節(jié)點(diǎn)

          /** 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()
          ?// 得到樹(shù)或葉子的詳細(xì)信息
          ?// 先打印自己的名字,再遍例左孩子,再遍例右孩子
          ?// 如果左孩子或右孩子是樹(shù),遞歸調(diào)用
          ?{
          ??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() {
          ??// 比較左孩子或右孩子的高度,誰(shuí)大,+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 測(cè)試類(lèi) **************/
          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 保爾任 閱讀(564) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): Design Patten

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

          常用鏈接

          留言簿(4)

          隨筆分類(lèi)

          隨筆檔案

          文章分類(lèi)

          文章檔案

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 黄山市| 临邑县| 河南省| 宁波市| 岐山县| 屏边| 长阳| 高台县| 三亚市| 垦利县| 缙云县| 聂荣县| 合肥市| 凤庆县| 成武县| 庄浪县| 临朐县| 孝昌县| 崇文区| 柳林县| 石首市| 色达县| 哈尔滨市| 西华县| 长宁县| 和田县| 香港| 长沙县| 璧山县| 台中市| 灵台县| 千阳县| 田东县| 罗山县| 泰来县| 辽宁省| 鹤庆县| 江津市| 白山市| 波密县| 横峰县|