二叉树的算法

  3 import java.util.LinkedList;
  4 import java.util.List;
  5 import java.util.concurrent.BlockingDeque;
  7 import tree.BiTreeTraverse.Node;
  8 
  9 /**
 10  * 二叉树的三种遍历:
 11  * 先序遍历、中序遍历、后续遍历
 12  * @author Administrator
 13  *                    1
 14  *                  /   
 15  *                2      3
 16  *               /      / 
 17  *             4   5   6   7
 18  *            / 
 19  *          8    9 
 20  * 二叉树是每个节点最多只有两个子节点的有序树
 21  * 先序遍历:根->左子树->右子树        1,2,4,8,9,5,3,6,7    
 22  * 中序遍历:左子树->根->右子树        8,4,9,2,5,1,6,3,7
 23  * 后续遍历:左子树->右子树->根         8,9,4,5,2,6,7,3,1
 24  * 
 25  *               
 26  */
 27 public class BiTreeTraverse {
 28     private int[] array = {1,2,3,4,5,6,7,8,9};
 29     private static List<Node> nodeList = null;
 30     public static class Node{
 31         Node leftChild;
 32         Node rightChild;
 33         int data;
 34         public Node(int newData) {
 35             data = newData;
 36             leftChild = null;
 37             rightChild = null;
 38         }
 39     }
 40     public void createBitree() {
 41         nodeList = new LinkedList<Node>();
 42         for (int nodeIndex = 0; nodeIndex < array.length; nodeIndex++) {
 43             nodeList.add(new Node(array[nodeIndex]));
 44         }
 45         for (int parentIndex = 0; parentIndex < array.length/2-1; parentIndex++) {
 46             nodeList.get(parentIndex).leftChild = nodeList.get(parentIndex*2+1);
 47             nodeList.get(parentIndex).rightChild = nodeList.get(parentIndex*2+2);
 48         }
 49         int lastParentIndex = array.length/2-1;
 50         nodeList.get(lastParentIndex).leftChild = nodeList.get(lastParentIndex*2+1);
 51         if (array.length%2==1) {
 52             nodeList.get(lastParentIndex).rightChild = nodeList.get(lastParentIndex*2+2);
 53         }    
 54     }
 55     /**
 56      * 先序遍历
 57      * @param node
 58      */
 59     public static void preOrderTraverse(Node node) {
 60         if (node ==null) {
 61             return ;
 62         }
 63         System.out.print(node.data+",");
 64         preOrderTraverse(node.leftChild);
 65         preOrderTraverse(node.rightChild);
 66     }
 67     public static void inOrderTraverse(Node node) {
 68         if (node ==null) {
 69             return ;
 70         }
 71         
 72         inOrderTraverse(node.leftChild);
 73         System.out.print(node.data+",");
 74         inOrderTraverse(node.rightChild);
 75     }
 76     public static void postOrderTraverse(Node node) {
 77         if (node ==null) {
 78             return ;
 79         }
 80         
 81         postOrderTraverse(node.leftChild);
 82     
 83         postOrderTraverse(node.rightChild);
 84         System.out.print(node.data+",");
 85     }
 86     public static void main(String[] args) {
 87         BiTreeTraverse biTree = new BiTreeTraverse();
 88         biTree.createBitree();
 89         Node root = nodeList.get(0);
 90         System.out.println("先序遍历");
 91         preOrderTraverse(root);
 92         System.out.println();
 93         System.out.println("中序遍历");
 94         inOrderTraverse(root);
 95         System.out.println();
 96         System.out.println("后序遍历");
 97         postOrderTraverse(root);
 98     }
 99     
100  }
原文地址:https://www.cnblogs.com/springcloud/p/7783765.html