二叉树的四种遍历(java代码)

代码1:

四种遍历方式

package 第四章;
import java.util.LinkedList;
/**
 * @author 许湘扬
 * @email  547139255@qq.com
 * @detail 先序创建 各种遍历 二叉树  
 */
/*
 * 先序创建、输出 二叉树
 * 先序 二叉树
 * 
 * 创建参考自:http://www.cnblogs.com/llhthinker/p/4906631.html
 * 遍历参考自:http://blog.csdn.net/sjf0115/article/details/8645991
 */

public class BinaryTree<T>
{
    /*
     * 先序创建二叉树
     * 返回:根节点
     */
    public TreeNode<T>  creatBinaryPre(LinkedList<T> treeData)
    {
        TreeNode<T> root=null;
        T data=treeData.removeFirst();
        if (data!=null) 
        {
            root=new TreeNode<T>(data, null, null);
            root.left=creatBinaryPre(treeData);
            root.right=creatBinaryPre(treeData);
        }
        return root;
    }
    /*
     * 先序遍历二叉树(递归)
     */
    public void PrintBinaryTreePreRecur(TreeNode<T> root)
    {
        if (root!=null) 
        {
            System.out.print(root.data);
            PrintBinaryTreePreRecur(root.left);
            PrintBinaryTreePreRecur(root.right);
        }
    }
    /*
     * 中序遍历二叉树(递归)
     */
    public void PrintBinaryTreeMidRecur(TreeNode<T> root)
    {
        if (root!=null) 
        {
            PrintBinaryTreeMidRecur(root.left);
            System.out.print(root.data);
            PrintBinaryTreeMidRecur(root.right);
        }
    }
    /*
     * 后序遍历二叉树(递归)
     */
    public void PrintBinaryTreeBacRecur(TreeNode<T> root)
    {
        if (root!=null) 
        {
            PrintBinaryTreeBacRecur(root.left);
            PrintBinaryTreeBacRecur(root.right);
            System.out.print(root.data);
        }
    }
    /*
     * 先序遍历二叉树(非递归)
     * 思路:对于任意节点T,访问这个节点并压入栈中,然后访问节点的左子树,
     *      遍历完左子树后,取出栈顶的节点T,再先序遍历T的右子树
     */
    public void PrintBinaryTreePreUnrecur(TreeNode<T> root)
    {
        TreeNode<T> p=root;//p为当前节点
        LinkedList<TreeNode> stack=new LinkedList<>();
        //栈不为空时,或者p不为空时循环
        while(p!=null || !stack.isEmpty())
        {
            //当前节点不为空。访问并压入栈中。并将当前节点赋值为左儿子
            if (p!=null) 
            {
                stack.push(p);
                System.out.print(p.data);
                p=p.left;
            }
            //当前节点为空:
            //  1、当p指向的左儿子时,此时栈顶元素必然是它的父节点
            //  2、当p指向的右儿子时,此时栈顶元素必然是它的爷爷节点
            //取出栈顶元素,赋值为right
            else
            {
                p=stack.pop();
                p=p.right;
            }
        }
    }
    /*
     * 中序遍历二叉树(非递归)
     * 
     * 思路:先将T入栈,遍历左子树;遍历完左子树返回时,栈顶元素应为T,
     *       出栈,访问T->data,再中序遍历T的右子树。  
     */
    public void PrintBinaryTreeMidUnrecur(TreeNode<T> root)
    {
        TreeNode<T> p=root;//p为当前节点
        LinkedList<TreeNode> stack=new LinkedList<>();

        //栈不为空时,或者p不为空时循环
        while(p!=null || !stack.isEmpty())
        {
            //当前节点不为空。压入栈中。并将当前节点赋值为左儿子
            if (p!=null) 
            {
                stack.push(p);
                p=p.left;
            }
            //当前节点为空:
            //  1、当p指向的左儿子时,此时栈顶元素必然是它的父节点
            //  2、当p指向的右儿子时,此时栈顶元素必然是它的爷爷节点
            //取出并访问栈顶元素,赋值为right
            else
            {
                p=stack.pop();
                System.out.print(p.data);
                p=p.right;
            }
        }
    }
    /*
     * 后序遍历二叉树(非递归)
     *  
     */
    public void PrintBinaryTreeBacUnrecur(TreeNode<T> root)
    {
        class NodeFlag<T>
        {
            TreeNode<T> node;
            char tag;
            public NodeFlag(TreeNode<T> node, char tag) {
                super();
                this.node = node;
                this.tag = tag;
            }
        }
        LinkedList<NodeFlag<T>> stack=new LinkedList<>();
        TreeNode<T> p=root;
        NodeFlag<T> bt;
        //栈不空或者p不空时循环  
        while(p!=null || !stack.isEmpty())
        {
            //遍历左子树
            while(p!=null)
            {
                bt=new NodeFlag(p, 'L');
                stack.push(bt);
                p=p.left;
            }
            //左右子树访问完毕访问根节点 
            while(!stack.isEmpty() && stack.getFirst().tag=='R')
            {
                bt=stack.pop();
                System.out.print(bt.node.data);
            }
            //遍历右子树
            if (!stack.isEmpty()) 
            {
                bt=stack.peek();
                bt.tag='R';
                p=bt.node;
                p=p.right;
            }
        }
    }
    /*
     * 层次遍历二叉树(非递归)   
     */
    public void PrintBinaryTreeLayerUnrecur(TreeNode<T> root)
    {
        LinkedList<TreeNode> queue=new LinkedList<>();
        TreeNode<T> p;
        queue.push(root);
        while(!queue.isEmpty())
        {
            p=queue.removeFirst();
            System.out.print(p.data);
            if (p.left!=null)
                queue.addLast(p.left);
            if (p.right!=null)
                queue.addLast(p.right);
        }
    }
}
 class TreeNode<T>
{
    public T data;
    public TreeNode<T> left;
    public TreeNode<T> right;
    public TreeNode(T data, TreeNode<T> left, TreeNode<T> right)
    {
        this.data = data;
        this.left = left;
        this.right = right;
    }
}

代码2

测试上面的四种遍历+先序创建树

package 第四章;

import java.util.LinkedList;
/*
 * 测试二叉树的  创建、遍历
 * 二叉树结构如下
 *            A
 *       B         C
 *    D     E   F     G
 *  H   I        J 
 */
public class TestBinaryTree 
{
    public static void main(String[] args) 
    {
        BinaryTree<Character> binaryTree=new BinaryTree<>();

        //输入ABDH##I##E##CF#J##G##(#用null代替)
        LinkedList<Character> tree=new LinkedList<>();
        tree.add('A');tree.add('B');tree.add('D');
        tree.add('H');tree.add(null);tree.add(null);
        tree.add('I');tree.add(null);tree.add(null);
        tree.add('E');tree.add(null);tree.add(null);
        tree.add('C');tree.add('F');tree.add(null);
        tree.add('J');tree.add(null);tree.add(null);
        tree.add('G');tree.add(null);tree.add(null);

        TreeNode<Character> root=binaryTree.creatBinaryPre(tree);

        //先序遍历(递归)
        binaryTree.PrintBinaryTreePreRecur(root);System.out.println();
        //中序遍历(递归)
        binaryTree.PrintBinaryTreeMidRecur(root);System.out.println();
        //后序遍历(递归)
        binaryTree.PrintBinaryTreeBacRecur(root);System.out.println();


        //先序遍历(非递归)
        binaryTree.PrintBinaryTreePreUnrecur(root);System.out.println();
        //中序遍历(非递归)
        binaryTree.PrintBinaryTreeMidUnrecur(root);System.out.println();
        //后序遍历(非递归)
        binaryTree.PrintBinaryTreeBacUnrecur(root);System.out.println();
        //层次遍历(非递归)
        binaryTree.PrintBinaryTreeLayerUnrecur(root);System.out.println();


    }
}
  • 9
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值