Write composite model example of the binary tree

  Abstract: composite model examples of writing binary tree 

  </ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width = "100%" border = "0" cellspacing = "0" cellpadding = " 0 "> <tr> <td width="198" height="86" align="center" valign="top"> </ td> <td width="486" valign="top"> 

  1, Component abstract components 
  Tree and Leaf succession Component 

  Private String name; / / tree leaves or the name of 

  AddChild (Component leftChild, Component rightChild); 
  / / Add a tree to the left of a child, a child right 
  GetName () (return name;) 
  GetTreeInfo () () / / or tree leaves have detailed information 
  GetLength (); / / be the height of the tree 

  2, Tree binary tree, a left child, a child right 

  3, Leaf is the leaf nodes 
  4, Test test node 

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

  / ** 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;) 

  </ Td> </ tr> </ table> 

  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) = (leftChild this.leftChild; this.rightChild = rightChild; return this;) public String getName () (return name;) public void getTreeInfo () / / or tree leaves have detailed information / / first print their names, left with the children all over again, all over again right of the child / / If children left or right child is the tree, recursive call (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 height "+ getLength ());) public int getLength () (/ / child left or right compare the height of the child, who, + 1 / / the air if the child's treatment (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 test-class *********** *** / 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 () + "is the height of the" + tree4.getLength ());)) 

  Running Results: 

  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 height is 3 

  </ Td> </ tr> <tr> 

  ↑ Back 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags:

Releated Java Articles

Comments

Leave a Reply