java编写二叉树

用Java实现二叉树,实现数据的添加功能,并用先序打印输出。最开始的时候,知道怎么弄,但是一直没有成功,在网上找了很多资料,说的也是不全,最后经过努力,还是自己搞定了,拿出来和大家一起讨论一下,如果你们有简单的办法,可以补充哦!

首先定义一个结点类:

 1 package com.xiao.tree;
 2 /**
 3  * @author WJQ 树结点类
 4  */
 5 public class TreeNode {
 6 
 7   /*存数据的*/
 8   private Object value;
 9   /*左孩子结点*/
10   private TreeNode lChild;
11   /*右孩子结点*/
12   private TreeNode rChild;
13 
14  
15  /*以下是setter和getter方法*/
16   public Object getValue() {
17     return value;
18   }
19 
20   public void setValue(Object value) {
21     this.value = value;
22   }
23 
24  public TreeNode getlChild() {
25   return lChild;
26  }
27 
28  public void setlChild(TreeNode lChild) {
29   this.lChild = lChild;
30  }
31 
32  public TreeNode getrChild() {
33   return rChild;
34  }
35 
36  public void setrChild(TreeNode rChild) {
37   this.rChild = rChild;
38  }
39 }

定义一个树型类:

 1 package com.xiao.tree;
 2 /**
 3  *
 4  * @author WJQ
 5  * 树的结构,树中只有结点
 6  */
 7 
 8 public class Tree {
 9  /*结点属性*/
10  private TreeNode node;
11  
12  public TreeNode getNode() {
13   return node;
14  }
15  public void setNode(TreeNode node) {
16   this.node = node;
17  }
18 }

定义一个队列:

 1 package com.xiao.tree;
 2 /**
 3  * @author WJQ
 4  * 该类是在向树中加入结点时需要使用的
 5  */
 6 import java.util.LinkedList;
 7 
 8 public class Queue {
 9  
10  private LinkedList<TreeNode> list;
11 
12  /*一初始化就new一个list*/
13  public Queue(){
14   list = new LinkedList<TreeNode>();
15  }
16  
17  /*结点入队列*/
18  public void enQueue(TreeNode node){
19   this.list.add(node);
20  }
21  /*队首元素出队列*/
22  public TreeNode outQueue(){
23   return this.list.removeFirst();
24  }
25  /*队列是否为空*/
26  public boolean isEmpty(){
27   return this.list.isEmpty();
28  }
29  public LinkedList<TreeNode> getList() {
30   return list;
31  }
32  public void setList(LinkedList<TreeNode> list) {
33   this.list = list;
34  }
35 }
 定义一个二叉树类:
 1 package com.xiao.tree;
 2 
 3 /**
 4  * @author WJQ 二叉树,增加结点,前序遍历,中序遍历,后序遍历
 5  */
 6 public class BinaryTree {
 7 
 8  private Tree tree;
 9  private Queue queue;
10 
11  /* 构造函数,初始化的时候就生成一棵树 */
12  public BinaryTree() {
13   tree = new Tree();
14  }
15 
16  /* 向树中插入结点 */
17  public void insertNode(TreeNode node) {
18   /* 如果树是空树,则生成一颗树,之后把当前结点插入到树中,作为根节点 ,根节点处于第0层 */
19   if (tree.getNode() == null) {
20    tree.setNode(node);
21    return;
22   } else {
23    /* 根节点入队列 */
24    queue = new Queue();
25    queue.enQueue(tree.getNode());
26    /*
27     * 队列不空,取出队首结点,如果队首结点的左右孩子有一个为空的或者都为空,则将新结点插入到相应的左右位置,跳出循环,如果左右孩子都不为空
28     * ,则左右孩子入队列,继续while循环
29     */
30    while (!queue.isEmpty()) {
31     TreeNode temp = queue.outQueue();
32     if (temp.getlChild() == null) {
33      temp.setlChild(node);
34      return;
35     } else if (temp.getrChild() == null) {
36      temp.setrChild(node);
37      return;
38     } else {
39      /* 左右孩子不空,左右孩子入队列 */
40      queue.enQueue(temp.getlChild());
41      queue.enQueue(temp.getrChild());
42     }
43    }
44   }
45  }
46 
47  /* 中序遍历 */
48  public void midOrder(TreeNode node) {
49   if (node != null) {
50    this.midOrder(node.getlChild());
51    System.out.println(node.getValue());
52    this.midOrder(node.getrChild());
53   }
54 
55  }
56 
57  /* 先序遍历 */
58  public void frontOrder(TreeNode node) {
59   if (node != null) {
60    System.out.println(node.getValue());
61    this.frontOrder(node.getlChild());
62    this.frontOrder(node.getrChild());
63   }
64  }
65  /* 后序遍历 */
66  public void lastOrder(TreeNode node) {
67   if (node != null) {
68    this.lastOrder(node.getlChild());
69    this.lastOrder(node.getrChild());
70    System.out.println(node.getValue());
71   }
72  }
73 
74  public Tree getTree() {
75   return tree;
76  }
77 
78 }
 客户端:
 1 package com.xiao.tree;
 2 /**
 3  * @author WJQ
 4  * 验证二叉树的客户端
 5  */
 6 public class Client {
 7 
 8  public static void main(String[] args) {
 9   /*生成一棵二叉树*/
10   BinaryTree binaryTree = new BinaryTree();
11   /*模拟10个结点*/
12   TreeNode[] nodes = new TreeNode[10];
13   for (int i = 0; i < nodes.length; i++) {
14    nodes[i] = new TreeNode();
15    nodes[i].setValue(i);
16    /*向树中插入结点*/
17    binaryTree.insertNode(nodes[i]);
18   }
19   /*先序、中序、后序遍历二叉树*/
20   binaryTree.midOrder(binaryTree.getTree().getNode());
21   binaryTree.frontOrder(binaryTree.getTree().getNode());
22   binaryTree.lastOrder(binaryTree.getTree().getNode());
23  }
24 
25 }

转载请注明出处哦!!!(原来在百度空间里面,现在搬到这里)

posted @ 2013-06-16 13:59  小吴学长  阅读(1043)  评论(1编辑  收藏  举报