java二叉树



叉树是一种非常重要的数据结构,很多其他数据机构都是基于二叉树的基础演变过来的。二叉树有前、中、后三种遍历方式,因为树的本身就是用递归定义的,因此采用递归的方法实现三种遍历,不仅代码简洁且容易理解,但其开销也比较大,而若采用非递归方法实现三种遍历,则要用栈来模拟实现(递归也是用栈实现的)。下面先简要介绍三种遍历方式的递归实现,再详细介绍三种遍历方式的非递归实现--基于Java实现的。

  1. package tree;  
  2.   
  3. import java.util.LinkedList;  
  4. import java.util.List;  
  5.   
  6. /** 
  7.  * 功能:把一个数组的值存入二叉树中,然后进行3种方式的遍历 
  8.  *  
  9.  * 参考资料0:数据结构(C语言版)严蔚敏 
  10.  *  
  11.  * 参考资料1:http://zhidao.baidu.com/question/81938912.html 
  12.  *  
  13.  * 参考资料2:http://cslibrary.stanford.edu/110/BinaryTrees.html#java 
  14.  *  
  15.  * @author ocaicai@yeah.net @date: 2011-5-17 
  16.  *  
  17.  */  
  18. public class BinTreeTraverse2 {  
  19.   
  20.     private int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };  
  21.     private static List<Node> nodeList = null;  
  22.   
  23.     /** 
  24.      * 内部类:节点 
  25.      *  
  26.      * @author ocaicai@yeah.net @date: 2011-5-17 
  27.      *  
  28.      */  
  29.     private static class Node {  
  30.         Node leftChild;  
  31.         Node rightChild;  
  32.         int data;  
  33.   
  34.         Node(int newData) {  
  35.             leftChild = null;  
  36.             rightChild = null;  
  37.             data = newData;  
  38.         }  
  39.     }  
  40.   
  41.     public void createBinTree() {  
  42.         nodeList = new LinkedList<Node>();  
  43.         // 将一个数组的值依次转换为Node节点  
  44.         for (int nodeIndex = 0; nodeIndex < array.length; nodeIndex++) {  
  45.             nodeList.add(new Node(array[nodeIndex]));  
  46.         }  
  47.         // 对前lastParentIndex-1个父节点按照父节点与孩子节点的数字关系建立二叉树  
  48.         for (int parentIndex = 0; parentIndex < array.length / 2 - 1; parentIndex++) {  
  49.             // 左孩子  
  50.             nodeList.get(parentIndex).leftChild = nodeList  
  51.                     .get(parentIndex * 2 + 1);  
  52.             // 右孩子  
  53.             nodeList.get(parentIndex).rightChild = nodeList  
  54.                     .get(parentIndex * 2 + 2);  
  55.         }  
  56.         // 最后一个父节点:因为最后一个父节点可能没有右孩子,所以单独拿出来处理  
  57.         int lastParentIndex = array.length / 2 - 1;  
  58.         // 左孩子  
  59.         nodeList.get(lastParentIndex).leftChild = nodeList  
  60.                 .get(lastParentIndex * 2 + 1);  
  61.         // 右孩子,如果数组的长度为奇数才建立右孩子  
  62.         if (array.length % 2 == 1) {  
  63.             nodeList.get(lastParentIndex).rightChild = nodeList  
  64.                     .get(lastParentIndex * 2 + 2);  
  65.         }  
  66.     }  
  67. /**
     * 二叉树的前序遍历的递归调用 --- 根节点 -- 左孩子--右孩子
     * 该方法很简单,根据需要遍历节点的顺序,递归的将遍历到的节点值放入list中。
     * 即:每次递归都是先放头结点,再遍历左子树,最后是右子树。
     * 递归调用简单,当是当节点过多时,其效率就会很差,而且比较耗费空间
     * Created by Administrator on 2016/9/5.
     */
    
    /*public class BinaryTree {
        public static void fun(TreeNode pHead){
            ArrayList<Integer> list = new ArrayList<>();
            if(pHead != null){
                list.add(pHead.value);
            }
            if(pHead.left != null)
                fun(pHead.left);
            if(pHead.right != null)
                fun(pHead.right);
        }
    }*/

  68.   
  69.     /** 
  70.      * 先序遍历 
  71.      *  
  72.      * 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已 
  73.      *  
  74.      * @param node 
  75.      *            遍历的节点 
  76.      */  
  77.     public static void preOrderTraverse(Node node) {  
  78.         if (node == null)  
  79.             return;  
  80.         System.out.print(node.data + " ");  
  81.         preOrderTraverse(node.leftChild);  
  82.         preOrderTraverse(node.rightChild);  
  83.     }  
  84.   
  85.     /** 
  86.      * 中序遍历 
  87.      *  
  88.      * 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已 
  89.      *  
  90.      * @param node 
  91.      *            遍历的节点 
  92.      */  
  93.     public static void inOrderTraverse(Node node) {  
  94.         if (node == null)  
  95.             return;  
  96.         inOrderTraverse(node.leftChild);  
  97.         System.out.print(node.data + " ");  
  98.         inOrderTraverse(node.rightChild);  
  99.     }  
  100.   
  101.     /** 
  102.      * 后序遍历 
  103.      *  
  104.      * 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已 
  105.      *  
  106.      * @param node 
  107.      *            遍历的节点 
  108.      */  
  109.     public static void postOrderTraverse(Node node) {  
  110.         if (node == null)  
  111.             return;  
  112.         postOrderTraverse(node.leftChild);  
  113.         postOrderTraverse(node.rightChild);  
  114.         System.out.print(node.data + " ");  
  115.     }  
  116.   
  117.     public static void main(String[] args) {  
  118.         BinTreeTraverse2 binTree = new BinTreeTraverse2();  
  119.         binTree.createBinTree();  
  120.         // nodeList中第0个索引处的值即为根节点  
  121.         Node root = nodeList.get(0);  
  122.   
  123.         System.out.println("先序遍历:");  
  124.         preOrderTraverse(root);  
  125.         System.out.println();  
  126.   
  127.         System.out.println("中序遍历:");  
  128.         inOrderTraverse(root);  
  129.         System.out.println();  
  130.   
  131.         System.out.println("后序遍历:");  
  132.         postOrderTraverse(root);  
  133.     }  
  134.   
  135. }  



输出结果: 

    1. 先序遍历:  
    2. 7   
    3. 中序遍历:  
    4. 7   
    5. 后序遍历:  
    6. 1   

关于二叉树的遍历还说一点:
前序序列获得的是根(第一个节点);
中序序列获得的是左右子树划分;
后序序列获得的是根(最后一个节点)。
前序序列和后序序列只能获得根,不能获得左右子树划分,所以不能唯一确定一颗二叉树。
(前序+中序)或者(后序+中序)都能唯一确定一颗二叉树。




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值