码迷,mamicode.com
首页 > 其他好文 > 详细

算法之二叉树遍历

时间:2014-05-06 22:06:28      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:二叉树   算法   

【代码示例】

package com.wcs.java;
import java.util.ArrayList;
import java.util.List;

public class BinaryTree { 
    
    
    class TreeNode {
        
        public String data; //数据
        
        public TreeNode leftNode; //左子树
        
        public TreeNode rightNode; //右子树
        
        public TreeNode(String data, TreeNode leftNode, TreeNode rightNode) {
            this.data = data;
            this.leftNode = leftNode;
            this.rightNode = rightNode;
        }

        /**
         * @return the data
         */
        public String getData() {
            return data;
        }

        /**
         * @param data the data to set
         */
        public void setData(String data) {
            this.data = data;
        }

        /**
         * @return the leftNode
         */
        public TreeNode getLeftNode() {
            return leftNode;
        }

        /**
         * @param leftNode the leftNode to set
         */
        public void setLeftNode(TreeNode leftNode) {
            this.leftNode = leftNode;
        }

        /**
         * @return the rightNode
         */
        public TreeNode getRightNode() {
            return rightNode;
        }

        /**
         * @param rightNode the rightNode to set
         */
        public void setRightNode(TreeNode rightNode) {
            this.rightNode = rightNode;
        }
    }

    
    public TreeNode init() {
        TreeNode B = new TreeNode("B", null, null);
        TreeNode D = new TreeNode("D", null, B);
        TreeNode C = new TreeNode("C", D, null);
        
        TreeNode J = new TreeNode("J", null, null);
        TreeNode I = new TreeNode("I", J, null);
        TreeNode G = new TreeNode("G", I, null);
        TreeNode F = new TreeNode("F", null, G);
        TreeNode H = new TreeNode("H", null, null);
        TreeNode E = new TreeNode("E", F, H);
        
        TreeNode A = new TreeNode("A", C, E);
        
        return A;
    }
    
    private static List<String> list;
    
    public String printOut() {
        StringBuffer stringBuffer = new StringBuffer();
        for(int i=0; i<list.size(); i++) {
            stringBuffer.append(list.get(i) + (i!=list.size()-1 ? "-" : ""));
        }
        return stringBuffer.toString();
    }
    
    public void  preorderTraversal(TreeNode node) { //递归先序遍历
        if(node != null && node.getData() != null) {
            list.add(node.getData()); 
            if(node.getLeftNode() != null && node.getLeftNode().getData() != null) { //左子树不为空
                preorderTraversal(node.getLeftNode());
            }
            if(node.getRightNode() != null && node.getRightNode().getData() != null) { //右子树不为空
                preorderTraversal(node.getRightNode());
            }
        }
    }
    
    public void inorderTraversal(TreeNode node) { //递归中序遍历
        if(node != null && node.getData() != null) {
            if(node.getLeftNode() != null && node.getLeftNode().getData() != null) {
                inorderTraversal(node.getLeftNode());
            }
            list.add(node.getData());
            if(node.getRightNode() != null && node.getRightNode().getData() != null) {
                inorderTraversal(node.getRightNode());
            }
        }
    }
    
    public void postorderTraversal(TreeNode node) { //递归后序遍历
        if(node != null && node.getData() != null) {
            if(node.getLeftNode() != null && node.getLeftNode().getData() != null) {
                postorderTraversal(node.getLeftNode());
            }
            if(node.getRightNode() != null && node.getRightNode().getData() != null) {
                postorderTraversal(node.getRightNode());
            }
            list.add(node.getData());
        }
    }
    
    public static void main(String[] args) {
        //二叉树初始化
        BinaryTree binaryTree = new BinaryTree();
        TreeNode treeNode = binaryTree.init();
        //先序遍历
        list = new ArrayList<String>();
        binaryTree.preorderTraversal(treeNode);
        System.out.println("先序遍历: " + binaryTree.printOut());
        //中序遍历
        list = new ArrayList<String>();
        binaryTree.inorderTraversal(treeNode);
        System.out.println("中序遍历: " + binaryTree.printOut());
        //后序遍历
        list = new ArrayList<String>();
        binaryTree.postorderTraversal(treeNode);
        System.out.println("后序遍历: " + binaryTree.printOut());
    }

}



【二叉树】


bubuko.com,布布扣


【运行结果】


先序遍历: A-C-D-B-E-F-G-I-J-H
中序遍历: D-B-C-A-F-J-I-G-E-H
后序遍历: B-D-C-J-I-G-F-H-E-A

算法之二叉树遍历,布布扣,bubuko.com

算法之二叉树遍历

标签:二叉树   算法   

原文地址:http://blog.csdn.net/lmtony/article/details/25100787

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!