我用composite模式写的一个二叉树的例子
我用composite模式写的一个二叉树的例子 N0*^6ag G
1,Component 是抽象组件 NwR)/y99
Tree 和Leaf 继承Component DK aGl^6G
V`ReB-?
private String name; //树或叶子的名称 JlG"P6nolP
addChild(Component leftChild,Component rightChild); [j1ODb{]
//给一个树上加上一个左孩子,一个右孩子 }E*eyym8
getName(){return name;} c1kcng
getTreeInfo(){} //得到树或叶子的详细信息 HbqP4ULn\G
getLength(); //得到树的高度 @)BHXhweD
BJj':$-
2,Tree 二叉树,一个左孩子,一个右孩子 =T!h}9jZc
-7b:t{}
3,Leaf 是叶子节点 Gclu= xE
4,Test 是测试节点 |wSH4e?
}gIHO$1
/** Component.java **************/ Xk"}^{+KP
package binarytree; Pmh;?
public abstract class Component { -D-z8'o
private String name; *<ya&S Al
public abstract Component addChild(Component leftChild,Component rightChild); .h$o(DW
public String getName(){return name;} -R8DN!)
public void getTreeInfo(){} ,Y]l qd
public abstract int getLength(); on1bW" _
} i?:7[h-J
biEq%1Ga=
/** Leaf.java **************/ )dg!e3wxr
package binarytree; >#Wy0?#tw
public class Leaf extends Component{ `M@hq
private String name; ZkS}5)
private Component leaf=null; 6IO,`vMC+
public Leaf(String name) { KM<EjQ
this.name=name; 9m"O"}
} VR a Nl=L
public Component addChild(Component leftChild,Component rightChild){ >=bo/b:e
return this; C[_q0R
} wqzKw1'
public String getName(){ ?CF77R?4%
return name; 0"J i"S
} ZE25nH F
public int getLength() { *Qt (}"i
return 1; W>shZ` j
} gz -\v?Ii
public static void main(String[] args) { Db`."lEE
} @@WDQ.4i_!
} CU}pXu'x
W5ar!|{Kn
/** Tree.java **************/ 8@"cvSt+
package binarytree; 6o|3nw3B
public class Tree extends Component { 2]pvd(M
private String name; Giv8 }
private Component leftChild; 0z,{?,W
private Component rightChild; Yt>vIL<
public Tree(String name,Component leftChild,Component rightChild) { wQi6V|
this.name=name; 5;0@O,g<
this.leftChild=leftChild; ]&? fCC
this.rightChild=rightChild; iAYvWA<
} Drg{g ,,P
public Tree(String name) { Zl0AI# :j
this.name=name; c{8Mfra
this.leftChild=null; UR<'@ |Y
this.rightChild=null; ):.8"EB.
} 6 -2*>g
public Component addChild(Component leftChild,Component rightChild){ }E,pQaL
this.leftChild=leftChild; w$Z(*fYjQn
this.rightChild=rightChild; b GyJ)I
return this; ttr Npr
} Rf20 |X
public String getName(){ GT>#"h.L
return name; $-+v(>,
} &(mRY
public void getTreeInfo() -G(IjjwKs
//得到树或叶子的详细信息 $e4hQ!!
//先打印自己的名字,再遍例左孩子,再遍例右孩子 2-fNm=qB
//如果左孩子或右孩子是树,递归调用 mg9x+n%
{ tGyae`=
System.out.println(" this trees name is "+getName()); :*2x9/
if(this.leftChild instanceof Leaf) ffSDSBY
{ n8c:Su|d
System.out.println(getName()+"s left child is "+this.leftChild.getName()+",it is a Leaf"); .h+PjR#
} *<4tsg%Nk
if(this.leftChild instanceof Tree){ {jK|?t('c
System.out.println(getName()+"s left child is "+this.leftChild.getName()+",it is a Tree"); .VO}PhEJ5
this.leftChild.getTreeInfo(); _T[ nD}
} 3)dg@g$X
if(this.leftChild==null) =>Bm tjH 1
{ $A d_9
System.out.println(getName()+"s left child is a null"); u0~`eEQ
} Fb1@4mn
if(this.rightChild instanceof Leaf) B*Yex;O(
{ \Wk/qRZ(=
System.out.println(getName()+"s right child is "+this.rightChild.getName()+",it is a Leaf"); ~=I-/h}@
} !Wj&H-!T
if(this.rightChild instanceof Tree) { Z?;=PMP>(~
System.out.println(getName()+"s right child is "+this.rightChild.getName()+",it is a Tree"); yqvDs
this.rightChild.getTreeInfo(); qs*D4hK6
} OII5$*5$.Y
if(this.rightChild==null) |^+x>"r x
{ ~6V<AG
System.out.println(getName()+"s right child is a null"); 4o~*i qtL
} sM]%34V0
//System.out.println(getName()+"s 高度 是 "+getLength()); ?6llpy"
} XrZ3\`X[Z
public int getLength() { EFU0#c+o:u
//比较左孩子或右孩子的高度,谁大,+1 返回 `Z,VW-nnQ>
// 空孩子的处理 (Y8igup"V
if(this.leftChild==null) { ><g>j,yG
if(this.rightChild==null) vQ*[ln<-P
return 1; "axf\bk'
else )*szsq)(
return this.rightChild.getLength()+1; @T0ksaxv\
} *d^'g<({B1
else { B u|Qy
if(this.rightChild==null) { z}h?1z0
return this.leftChild.getLength()+1; ?;Rm8 `h|p
} AZ}UmXaa w
else { =mftF )+v
if((this.leftChild.getLength())>=(this.rightChild.getLength())) 4{O{_?bR
return this.leftChild.getLength()+1; X5IK_)'n|
else t _ 2a%s~
return this.rightChild.getLength()+1; EEs4 ZrU
} joH(dx/3y
} g#>R 2{VR8
} @-_Mt-n`tl
public static void main(String[] args) { (u{Mrt@s
} W+Ta?C5\
} RY_mkS62
gW|O,.Gu
/** Test.java 测试类 **************/ d!lM3o a
package binarytree; 2;3xD_q
public class Test { byH ~ZMZ
6K aIoXH
public Test() { Vc^J ' [
} t?L} U|l
public static void main(String[] args) { .:]DMc3C
Component tree=new Tree("luopeng"); M al.dOX{
Component leaf_child=new Leaf("luopeng1"); "E wH2VKY
Component right_child=new Leaf("luopeng2"); W>MZ [w[UP
tree=tree.addChild(leaf_child,right_child); 7W+/~]
//tree=tree.addRightChild(right_child); V^Nw)o6x
tree.getTreeInfo(); gK(a
Component tree1=new Tree("luopeng2"); .^9U-!
tree1.addChild(tree,leaf_child); rHGnc!rE
tree1.getTreeInfo(); 5RZm+{o X)
Component tree2=new Tree("luopeng3"); e s6+^dh
tree2.addChild(tree,null); 9":'.I
tree2.getTreeInfo(); 8njs-p
Component tree4=new Tree("luopeng4"); ?HeIy"|22
tree4.addChild(null,tree); 4hiF)
tree4.getTreeInfo(); ~Q@F"h
System.out.println(tree4.getName()+"的高度是 "+tree4.getLength()); `d5gS&8A#
}
查看本文来源