zoukankan      html  css  js  c++  java
  • 二叉树java遍历实现

    假设二叉树如图:

    import java.util.ArrayList;
    import java.util.List;
    
    import org.junit.Test;
    
    public class MyTree {
        private Node root = null;
    
        public MyTree() {
            init();
        }
    
        private List<Node> list = new ArrayList<Node>();
    
        private void init() {
            Node e = new Node("E", null, null);
            Node d = new Node("D", null, null);
            Node b = new Node("B", d, e);
            Node c = new Node("C", null, null);
            Node a = new Node("A", b, c);
            root = a;
        }
    
        private class Node {
            private String data;
    
            private Node lChild;
    
            private Node rChild;
    
            public Node(String data, Node lChild, Node rChild) {
                this.data = data;
                this.lChild = lChild;
                this.rChild = rChild;
            }
        }
    
        /**
         * 深度遍历
         * 
         * */
        public int getTreeDepth(Node node) {
    
            if (node.lChild == null && node.rChild == null) {
                return 1;
            }
            int left = 0, right = 0;
            if (node.lChild != null) {
                left = getTreeDepth(node.lChild);
            }
            if (node.rChild != null) {
                right = getTreeDepth(node.rChild);
            }
            return left > right ? left + 1 : right + 1;
        }
    
        /**
         * 前序遍历:ABDEC
         * 1.首先遍历左子树,遍历到叶子结点为止
         * @return 
         * */
        public boolean preorderTraverse(Node node) {
            //首先从根节点开始遍历
            list.add(node);
            if (node.lChild != null) {
                //
                preorderTraverse(node.lChild);
            }
            if (node.rChild != null) {
                preorderTraverse(node.rChild);
            }
            return true;
        }
    
        /**
         * 中序遍历:DBEAC
         * @return 
         * 
         * */
        public boolean inorderTarverse(Node node) {
            if (node.lChild != null) {
                inorderTarverse(node.lChild);
            }
            list.add(node);
            if (node.rChild != null) {
                inorderTarverse(node.rChild);
            }
            return true;
        }
    
        /**
         * 后序遍历:DEBCA
         * @return 
         * 
         * */
        public boolean postorderTarverse(Node node) {
            if (node.lChild != null) {
                postorderTarverse(node.lChild);
            }
            if (node.rChild != null) {
                postorderTarverse(node.rChild);
            }
            list.add(node);
            return true;
        }
        
        public List<Node> getList() {
            return list;
        }
    
        @Test
        public void testGetDeth() {
            MyTree mt = new MyTree();
            System.out.println("树的深度:" + getTreeDepth(mt.root));
            //if(mt.preorderTraverse(mt.root)) System.out.print("前序遍历结果:");
            //if(mt.inorderTarverse(mt.root)) System.out.print("中序遍历结果:");
            if(mt.postorderTarverse(mt.root)) System.out.print("后序遍历结果:");
            //if(mt.sequenceTraverse(mt.root)) System.out.print("层序遍历结果:");
            for (Node node : mt.getList()) {
                System.out.print(node.data);
            }
        }
    }
  • 相关阅读:
    【转载】我的七个建议Joel Spolsky
    C语言文件读写操作
    【转】RO段、RW段和ZI段 Image$$??$$Limit 含义(zz)
    给大家一个测试webservice的软件
    .net 实现深拷贝的方法
    第一次设计数据访问层,大家给你建议,谢谢
    重新写博客
    (转)理解 Thread.Sleep 函数
    slk解压缩
    SMTP Service设置
  • 原文地址:https://www.cnblogs.com/zyf-yxm/p/11385291.html
Copyright © 2011-2022 走看看