1、由键盘输入的方式给出一组整数,把这些整数存到二叉树。 
2、实现对二叉树先序遍历,中序遍历,后序遍历,层次遍历,输出遍历结果。

解决方案 »

  1.   

    1. public class Tree {   
    2.   
    3.     private int data;// 数据节点   
    4.     private Tree left;// 左子树   
    5.     private Tree right;// 右子树   
    6.   
    7.     public Tree(int data) {   
    8.         this.data = data;   
    9.         this.left = null;   
    10.         this.right = null;   
    11.     }   
    12.   
    13.     /**  
    14.      * 创建二叉树,返回根结点  
    15.      *   
    16.      * @param input  
    17.      * @return  
    18.      */  
    19.     public static Tree createTree(int[] input) {   
    20.         Tree root = null;   
    21.         Tree temp = null;   
    22.         for (int i = 0; i < input.length; i++) {   
    23.             // 创建根节点   
    24.             if (root == null) {   
    25.                 root = temp = new Tree(input[i]);   
    26.             } else {   
    27.                 // 定位到根节点   
    28.                 temp = root;   
    29.                 // 添加子节点   
    30.                 while (input[i] != temp.data) {   
    31.                     if (input[i] <= temp.data) {   
    32.                         if (temp.left != null) {   
    33.                             temp = temp.left;   
    34.                         } else {   
    35.                             temp.left = new Tree(input[i]);   
    36.                         }   
    37.                     } else {   
    38.                         if (temp.right != null) {   
    39.                             temp = temp.right;   
    40.                         } else {   
    41.                             temp.right = new Tree(input[i]);   
    42.                         }   
    43.                     }   
    44.                 }   
    45.             }   
    46.         }   
    47.         return root;   
    48.     }   
    49.   
    50.     /**  
    51.      * 前序遍历  
    52.      *   
    53.      * @param tree  
    54.      */  
    55.     public static void preOrder(Tree tree) {   
    56.         if (tree != null) {   
    57.             System.out.print(tree.data + " ");   
    58.             preOrder(tree.left);   
    59.             preOrder(tree.right);   
    60.         }   
    61.     }   
    62.   
    63.     /**  
    64.      * 中序遍历  
    65.      *   
    66.      * @param tree  
    67.      */  
    68.     public static void midOrder(Tree tree) {   
    69.         if (tree != null) {   
    70.             midOrder(tree.left);   
    71.             System.out.print(tree.data + " ");   
    72.             midOrder(tree.right);   
    73.         }   
    74.     }   
    75.   
    76.     /**  
    77.      * 后序遍历  
    78.      *   
    79.      * @param tree  
    80.      */  
    81.     public static void posOrder(Tree tree) {   
    82.         if (tree != null) {   
    83.             posOrder(tree.left);   
    84.             posOrder(tree.right);   
    85.             System.out.print(tree.data + " ");   
    86.         }   
    87.     }   
    88.   
    89.     /**  
    90.      * @param args  
    91.      */  
    92.     public static void main(String[] args) {   
    93.         int[] input = { 4, 2, 6, 1, 3, 5, 7 };   
    94.         Tree tree = createTree(input);   
    95.         System.out.print("前序遍历:");   
    96.         preOrder(tree);   
    97.         System.out.print("\n中序遍历:");   
    98.         midOrder(tree);   
    99.         System.out.print("\n后序遍历:");   
    100.         posOrder(tree);   
    101.     }   
    102. }   
    103.   
    104. 前序遍历:4 2 1 3 6 5 7    
    105. 中序遍历:1 2 3 4 5 6 7    
    106. 后序遍历:1 3 2 5 7 6 4