手机版
你好,游客 登录 注册
背景:
阅读新闻

Java实现二叉树的遍历

[日期:2017-10-12] 来源:Linux社区  作者:abelxu [字体:大 中 小]

Java实现二叉树的遍历

public class BinaryTreeNode {
    int value;
    BinaryTreeNode left;
    BinaryTreeNode right;
 
    // 前序遍历
    public static void preOrder(BinaryTreeNode tree) {
        if (tree == null)
            return;
        System.out.print(tree.value + " ");
        preOrder(tree.left);
        preOrder(tree.right);
    }
 
    /**
    * 二叉树前序遍历的循环实现方式
    * 
    * @param tree
    */
    public static void preOrderLoop(BinaryTreeNode tree) {
        if (tree == null)
            return;
        Stack<BinaryTreeNode> stack = new Stack<BinaryTreeNode>();
        BinaryTreeNode node = tree;
 
        while (node != null || !stack.isEmpty()) {
            if (node != null) {
                stack.push(node);
                System.out.print(node.value + " ");
                node = node.left;
            } else {
                BinaryTreeNode item = stack.pop();
                node = item.right;
            }
        }
    }
 
    // 中序遍历
    public static void inOrder(BinaryTreeNode tree) {
        if (tree == null)
            return;
        inOrder(tree.left);
        System.out.print(tree.value + " ");
        inOrder(tree.right);
    }
 
    /**
    * 二叉树中序遍历的循环实现
    * 
    * @param tree
    */
    public static void inOrderLoop(BinaryTreeNode tree) {
        if (tree == null)
            return;
        Stack<BinaryTreeNode> stack = new Stack<BinaryTreeNode>();
        BinaryTreeNode node = tree;
 
        while (node != null || !stack.empty()) {
            if (node != null) {
                stack.push(node);
                node = node.left;
            } else {
                BinaryTreeNode item = stack.pop();
                System.out.print(item.value + " ");
                node = item.right;
            }
        }
    }
 
    // 后序遍历
    public static void postOrder(BinaryTreeNode tree) {
        if (tree == null)
            return;
        postOrder(tree.left);
        postOrder(tree.right);
        System.out.print(tree.value + " ");
    }
 
    /**
    * 二叉树后序遍历的循环实现
    * 
    * @param tree
    */
    public static void postOrderLoop(BinaryTreeNode tree) {
        if (tree == null)
            return;
        HashSet<BinaryTreeNode> visited = new HashSet<BinaryTreeNode>();
        Stack<BinaryTreeNode> stack = new Stack<BinaryTreeNode>();
        BinaryTreeNode node = tree;
 
        while (node != null || !stack.isEmpty()) {
            if (node != null) {
                stack.push(node);
                node = node.left;
            } else {
                BinaryTreeNode item = stack.peek();
                if (item.right != null && !visited.contains(item.right))
                    node = item.right;
                else {
                    System.out.print(item.value + " ");
                    visited.add(item);
                    stack.pop();
                }
            }
        }
    }
 
    public static void main(String[] args) {
        List<Integer> list1 = new ArrayList<Integer>();
        List<Integer> list2 = new ArrayList<Integer>();
 
        list1.add(1);
        list1.add(2);
        list1.add(4);
        list1.add(7);
        list1.add(3);
        list1.add(5);
        list1.add(6);
        list1.add(8);
 
        list2.add(4);
        list2.add(7);
        list2.add(2);
        list2.add(1);
        list2.add(5);
        list2.add(3);
        list2.add(8);
        list2.add(6);
 
        BinaryTreeNode tree = BinaryTreeNode.Construct(list1, list2);
        BinaryTreeNode.preOrderLoop(tree);
        System.out.println();
        BinaryTreeNode.inOrderLoop(tree);
        System.out.println();
        BinaryTreeNode.postOrderLoop(tree);
    }
 
}

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-10/147480.htm

linux
相关资讯       二叉树遍历  Java二叉树遍历 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款