用java構建完全二叉樹

[java] view plaincopy
  1. //樹結點類  
  2. public class TreeNode {  
  3.   
  4.  private int value;  
  5.  private TreeNode leftchild;  
  6.  private TreeNode rightchild;  
  7.    
  8.  public int getValue() {  
  9.   return value;  
  10.  }  
  11.  public void setValue(int value) {  
  12.   this.value = value;  
  13.  }  
  14.  public TreeNode getLeftchild() {  
  15.   return leftchild;  
  16.  }  
  17.  public void setLeftchild(TreeNode leftchild) {  
  18.   this.leftchild = leftchild;  
  19.  }  
  20.  public TreeNode getRightchild() {  
  21.   return rightchild;  
  22.  }  
  23.  public void setRightchild(TreeNode rightchild) {  
  24.   this.rightchild = rightchild;  
  25.  }  
  26.  }  
  27.   
  28.    
  29.   
  30. //樹類  
  31. public class Tree {  
  32.  private TreeNode node;  
  33.   
  34.  public TreeNode getNode() {  
  35.   return node;  
  36.  }  
  37.   
  38.  public void setNode(TreeNode node) {  
  39.   this.node = node;  
  40.  }  
  41. }  
  42.   
  43. //隊列  
  44.   
  45. import java.util.LinkedList;  
  46.   
  47.   
  48. public class Queue {  
  49.   
  50.  private LinkedList<TreeNode> list;  
  51.  public LinkedList<TreeNode> getList() {  
  52.   return list;  
  53.  }  
  54.  public void setList(LinkedList<TreeNode> list) {  
  55.   this.list = list;  
  56.  }  
  57.  public Queue(){  
  58.   list = new LinkedList<TreeNode>();  
  59.  }  
  60.  public void enQueue(TreeNode node){  
  61.   list.add(node);  
  62.  }  
  63.  public TreeNode outQueue(){  
  64.   return list.removeFirst();  
  65.  }  
  66.  public boolean isEmpty(){  
  67.   return list.isEmpty();  
  68.  }  
  69.    
  70. }  
  71.   
  72. //二叉樹類  
  73. public class BinaryTree {  
  74.  private Tree tree;  
  75.  private Queue queue;  
  76.    
  77.  public BinaryTree(){  
  78.   tree = new Tree();  
  79.  }  
  80.   
  81. //插入結點  
  82.  public void insertNode(TreeNode node){  
  83.   if(tree.getNode()==null){  
  84.    tree.setNode(node);  
  85.    return;  
  86.   }  
  87.   else{  
  88.    queue = new Queue();  
  89.    queue.enQueue(tree.getNode());  
  90.    while(!queue.isEmpty()){  
  91.     TreeNode temp = queue.outQueue();  
  92.     if(temp.getLeftchild()==null){  
  93.      temp.setLeftchild(node);  
  94.      return;  
  95.     }  
  96.     else if(temp.getRightchild()==null){  
  97.      temp.setRightchild(node);  
  98.      return;  
  99.     }  
  100.     else {  
  101.      queue.enQueue(temp.getLeftchild());  
  102.      queue.enQueue(temp.getRightchild());  
  103.     }  
  104.    }  
  105.   }  
  106.  }  
  107.  //中序遍歷  
  108.  public void midOrder(TreeNode node){  
  109.   if(node!=null){  
  110.    this.midOrder(node.getLeftchild());  
  111.    System.out.println(node.getValue());  
  112.    this.midOrder(node.getRightchild());  
  113.   }  
  114.  }  
  115.   
  116.  //前序遍歷  
  117.   
  118.  public void frontOrder(TreeNode node){  
  119.   if(node !=null){  
  120.    System.out.println(node.getValue());  
  121.    frontOrder(node.getLeftchild());  
  122.    frontOrder(node.getRightchild());  
  123.   }  
  124.  }  
  125.   //後序遍歷  
  126.   
  127.  public void lastOrder(TreeNode node){  
  128.   if(node != null){  
  129.    this.lastOrder(node.getLeftchild());  
  130.    this.lastOrder(node.getRightchild());  
  131.    System.out.println(node.getValue());  
  132.   }  
  133.  }  
  134.    
  135.  public Tree getTree(){  
  136.   return tree;  
  137.  }  
  138.    
  139. }  
  140.   
  141.   
  142. //測試類  
  143. public class Client {   
  144.  public static void main(String[] args) {  
  145.   BinaryTree binaryTree = new BinaryTree();  
  146.   TreeNode[] nodes = new TreeNode[10];  
  147.   for(int i =0;i<nodes.length;i++){  
  148.    nodes[i] = new TreeNode();  
  149.    nodes[i].setValue(i);  
  150.    binaryTree.insertNode(nodes[i]);  
  151.   }  
  152.   System.out.println("先序遍歷");  
  153.   binaryTree.frontOrder(binaryTree.getTree().getNode());  
  154.   System.out.println("中序遍歷");  
  155.   binaryTree.midOrder(binaryTree.getTree().getNode());  
  156.   System.out.println("後序遍歷");  
  157.   binaryTree.lastOrder(binaryTree.getTree().getNode());  
  158.   
  159.  }  
  160.   
  161. }  
  162.   
  163.   
  164. 輸出結果:  
  165.   
  166. 先序遍歷  
  167. 0  
  168. 1  
  169. 3  
  170. 7  
  171. 8  
  172. 4  
  173. 9  
  174. 2  
  175. 5  
  176. 6  
  177. 中序遍歷  
  178. 7  
  179. 3  
  180. 8  
  181. 1  
  182. 9  
  183. 4  
  184. 0  
  185. 5  
  186. 2  
  187. 6  
  188. 後序遍歷  
  189. 7  
  190. 8  
  191. 3  
  192. 9  
  193. 4  
  194. 1  
  195. 5  
  196. 6  
  197. 2  
  198. 0  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章