二叉树遍历

前序遍历:A B D H P Q I E J Z Y K C F L M G N O    根左右

中序遍历:P H Q D I B Z J Y E K A L F M C N G O    左根有

后序遍历:P Q H I D Z Y J K E B L M F N O G C A    左右根

public class Tree {
    private String data;// 数据节点
    private Tree left;// 左子树
    private Tree right;// 右子树

    public Tree(String data) {
        this.data = data;
        this.left = null;
        this.right = null;
    }


    /**
     * 前序遍历
     * 根左右
     * @param tree
     */
    public static void preOrder(Tree tree) {
        if (tree != null) {
            System.out.print(tree.data + " ");
            preOrder(tree.left);
            preOrder(tree.right);
        }
    }

    /**
     * 中序遍历
     * 左根有
     * @param tree
     */
    public static void midOrder(Tree tree) {
        if (tree != null) {
            midOrder(tree.left);
            System.out.print(tree.data + " ");
            midOrder(tree.right);
        }
    }

    /**
     * 后序遍历
     * 左右根
     * @param tree
     */
    public static void posOrder(Tree tree) {
        if (tree != null) {
            posOrder(tree.left);
            posOrder(tree.right);
            System.out.print(tree.data + " ");
        }
    }

    /** 
      * @param args 
      */   
    public static void main(String[] args) {    
        Tree tree = new Tree("A");
        tree.left=new Tree("B"); 
        tree.right=new Tree("C");
        tree.left.left=new Tree("D");
        tree.left.right=new Tree("E");
        tree.right.left=new Tree("F");
        tree.right.right=new Tree("G");
        tree.left.left.left=new Tree("H");
        tree.left.left.right=new Tree("I");
        tree.left.right.left=new Tree("J");
        tree.left.right.left.left = new Tree("Z");
        tree.left.right.left.right = new Tree("Y");
        tree.left.right.right=new Tree("K");
        tree.right.left.left=new Tree("L");
        tree.right.left.right=new Tree("M");
        tree.right.right.left=new Tree("N");
        tree.right.right.right=new Tree("O");
        tree.left.left.left.left=new Tree("P");
        tree.left.left.left.right=new Tree("Q");
        System.out.println("前序遍历:");    
        preOrder(tree);    
        System.out.println("中序遍历:");    
        midOrder(tree);    
        System.out.println("后序遍历:");    
        posOrder(tree);    
     }
}

 另:

/**
 * 二叉树的建立和遍历
 * @author Administrator
 *
 */
public class TreeNode {

    
    
    
    public static void main(String[] args) {
        int[] arr = {3,1,2,5,0,7,9,8};
        //建立二叉树(无特殊要求)
        TreeNode tree = new TreeNode(arr[0]);
        for(int i = 1; i<arr.length;i++) {
            creatrTreeNode(tree ,arr[i]);
        }
        
        System.out.print("先序遍历:");
        preOrder(tree);
        System.out.print("中序遍历:");
        midOrder(tree);//中序遍历
        System.out.print("后序遍历:");
        posOrder(tree);//中序遍历
    }

    /**
     * 前序遍历二叉树(先序遍历)
     * 左根右
     */
    private static void preOrder(TreeNode tree) {
        if (tree != null) {
            System.out.print(tree.val + " ");
            preOrder(tree.getTreeLeft());
            preOrder(tree.getTreeRight());
        }
    }
    /**
     * 中序遍历
     * 左根右
     */
    private static void midOrder(TreeNode tree) {
        if (tree != null) {
            midOrder(tree.getTreeLeft());
            System.out.print(tree.val + " ");
            midOrder(tree.getTreeRight());
        }
    }
    /**
     * 后序遍历
     * 左右根
     */
    private static void posOrder(TreeNode tree) {
        if (tree != null) {
            posOrder(tree.getTreeLeft());
            posOrder(tree.getTreeRight());
            System.out.print(tree.val + " ");
        }
    }
    
    /**
     * 根据数组创建一个二叉树
     * @param tree
     * @param val
     * @return
     */
    private static TreeNode creatrTreeNode(TreeNode tree, int val) {
        if (tree == null) {
            tree = new TreeNode(val);
        }else {
            if (val > tree.val) {
                tree.treeRight = creatrTreeNode(tree.treeRight, val);
            }else if (val <= tree.val) {
                tree.treeLeft = creatrTreeNode(tree.treeLeft, val);
            }
        }
        return tree;
    }
    
    
    
    
    
    
    //基本属性和get、set
    private int val;
    private TreeNode treeLeft;
    private TreeNode treeRight;
    public TreeNode(int val) {
        this.val = val;
        this.treeLeft = null;
        this.treeRight = null;
    }
    public int getVal() {
        return val;
    }

    public void setVal(int val) {
        this.val = val;
    }

    public TreeNode getTreeLeft() {
        return treeLeft;
    }

    public void setTreeLeft(TreeNode treeLeft) {
        this.treeLeft = treeLeft;
    }

    public TreeNode getTreeRight() {
        return treeRight;
    }

    public void setTreeRight(TreeNode treeRight) {
        this.treeRight = treeRight;
    }
    
}
View Code

 

posted @ 2018-05-24 12:11  xj-record  阅读(116)  评论(0编辑  收藏  举报