二叉树的建立与遍历(递归、堆栈)

让我们先建立一棵树:

        A
     B       C
  D      E        F
package 树;

import javax.swing.tree.TreeNode;
import java.util.Stack;

/**
 * @author Dracular
 * @version $Rev$
 * @des ${TODO}
 * @date 2019/2/1 下午4:00
 * @updateAuthor $Author$
 * @updateDes ${TODO}
 */
public class BinaryTree {

    /**
     * 根结点
     */
    private TreeNode root = null;

    public BinaryTree() {
        root = new TreeNode(1, "A");
    }

    /**
     * 构造一颗二叉树
     *        A
     *    B       C
     * D      E        F
     */
    public void createBinaryTree() {
        TreeNode<String> nodeB = new TreeNode<>(2, "B");
        TreeNode<String> nodeC = new TreeNode<>(3, "C");
        TreeNode<String> nodeD = new TreeNode<>(4, "D");
        TreeNode<String> nodeE = new TreeNode<>(5, "E");
        TreeNode<String> nodeF = new TreeNode<>(6, "F");
        root.leftChild = nodeB;
        root.rightChild = nodeC;
        nodeB.leftChild = nodeD;
        nodeB.rightChild = nodeE;
        nodeC.rightChild = nodeF;
    }

    /**
     * 计算树的高度
     *
     * @return
     */
    public int getHeight() {
        return getHeight(root);
    }

    private int getHeight(TreeNode root) {
        if (root == null) {
            return 0;
        } else {
            int i = getHeight(root.leftChild);
            int j = getHeight(root.rightChild);
            return i > j ? i + 1 : j + 1;
        }
    }

    /**
     * 计算树的结点数
     *
     * @return
     */
    public int getSize() {
        return getSize(root);
    }

    private int getSize(TreeNode root) {
        if (root == null) {
            return 0;
        } else {
            return getSize(root.leftChild) + getSize(root.rightChild) + 1;
        }
    }

    /**
     * 先序遍历
     *
     * @param node
     */
    public void preOrder(TreeNode node) {
        if (node == null) {
            return;
        } else {
            System.out.println("preOrder:" + node.getData());
            preOrder(node.leftChild);
            preOrder(node.rightChild);
        }
    }

    /**
     * 先序遍历 - 非递归
     *
     * @param node
     */
    public void noonPreRecOrder(TreeNode node) {
        if (node == null) {
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        stack.push(node);
        while (!stack.isEmpty()) {
            TreeNode n = stack.pop();
            System.out.println("noonRecOrder:" + n.getData());
            if (n.rightChild != null) {
                stack.push(n.rightChild);
            }
            if (n.leftChild != null) {
                stack.push(n.leftChild);
            }
        }
    }

    /**
     * 中序遍历
     *
     * @param node
     */
    public void midOrder(TreeNode node) {
        if (node == null) {
            return;
        } else {
            midOrder(node.leftChild);
            System.out.println("midOrder:" + node.getData());
            midOrder(node.rightChild);
        }
    }

    /**
     * 后序遍历 - 非递归
     *
     * @param node
     */
    public void nonMidRecOrder(TreeNode node) {
        if (node == null) {
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        while (node != null || !stack.isEmpty()) {
            while (node != null) {
                stack.push(node);
                node = node.leftChild;
            }
            if (!stack.isEmpty()) {
                node = stack.pop();
                System.out.println("nonMidRecOrder:" + node.getData());
                node = node.rightChild;
            }
        }
    }

    /**
     * 后序遍历
     *
     * @param node
     */
    public void postOrder(TreeNode node) {
        if (node == null) {
            return;
        } else {
            postOrder(node.leftChild);
            postOrder(node.rightChild);
            System.out.println("postOrder" + node.getData());
        }
    }

    /**
     * 后序遍历 - 非递归
     *
     * @param node
     */
    public void noonPostRecOrder(TreeNode node) {
        if (node == null) {
            return;
        }
        Stack<TreeNode> stack1 = new Stack<>();
        Stack<Integer> stack2 = new Stack<>();
        while (node != null || !stack1.isEmpty()) {
            while (node != null) {
                stack1.push(node);
                stack2.push(0);
                node = node.leftChild;
            }
            while (!stack1.isEmpty() && stack2.peek() == 1) {
                stack2.pop();
                System.out.println("noonPostRecOrder:" + stack1.pop().getData());
            }
            if (!stack1.isEmpty()) {
                stack2.pop();
                stack2.push(1);
                node = stack1.peek();
                node = node.rightChild;
            }
        }
    }

    /**
     * 树的结点
     *
     * @param <T> 数据类型
     */
    public class TreeNode<T> {

        /**
         * 下标
         */
        private int index;

        private T data;

        /**
         * 左结点
         */
        private TreeNode leftChild;

        /**
         * 右结点
         */
        private TreeNode rightChild;

        public TreeNode(int index, T data) {
            this.index = index;
            this.data = data;
            this.leftChild = null;
            this.rightChild = null;
        }

        public int getIndex() {
            return index;
        }

        public void setIndex(int index) {
            this.index = index;
        }

        public T getData() {
            return data;
        }

        public void setData(T data) {
            this.data = data;
        }
    }

    public static void main(String[] args) {
        BinaryTree binaryTree = new BinaryTree();
        binaryTree.createBinaryTree();
        int height = binaryTree.getHeight();
        System.out.println("Height:" + height);
        int size = binaryTree.getSize();
        System.out.println("Size:" + size);
        System.out.println("----------------------------");
        binaryTree.preOrder(binaryTree.root);
        System.out.println("----------------------------");
        binaryTree.midOrder(binaryTree.root);
        System.out.println("----------------------------");
        binaryTree.postOrder(binaryTree.root);
        System.out.println("----------------------------");
        binaryTree.noonPreRecOrder(binaryTree.root);
        System.out.println("----------------------------");
        binaryTree.nonMidRecOrder(binaryTree.root);
        System.out.println("----------------------------");
        binaryTree.noonPostRecOrder(binaryTree.root);
    }
}

 

输出结果:

Height:3
Size:6
----------------------------
preOrder:A
preOrder:B
preOrder:D
preOrder:E
preOrder:C
preOrder:F
----------------------------
midOrder:D
midOrder:B
midOrder:E
midOrder:A
midOrder:C
midOrder:F
----------------------------
postOrderD
postOrderE
postOrderB
postOrderF
postOrderC
postOrderA
----------------------------
noonRecOrder:A
noonRecOrder:B
noonRecOrder:D
noonRecOrder:E
noonRecOrder:C
noonRecOrder:F
----------------------------
nonMidRecOrder:D
nonMidRecOrder:B
nonMidRecOrder:E
nonMidRecOrder:A
nonMidRecOrder:C
nonMidRecOrder:F
----------------------------
noonPostRecOrder:D
noonPostRecOrder:E
noonPostRecOrder:B
noonPostRecOrder:F
noonPostRecOrder:C
noonPostRecOrder:A

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值