二叉树遍历(递归、迭代)

import java.util.LinkedList;
import java.util.Stack;

public class Traversal {
    public static void main(String[] args) {
        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 treeNode = creatBinaryPre(tree);

        PrintBinaryTreePreRecur(treeNode);
        System.out.print("\n");
        PrintPre(treeNode);

        System.out.println("\n");

        PrintBinaryTreeMidRecur(treeNode);
        System.out.print("\n");
        PrintMid(treeNode);

        System.out.println("\n");

        PrintBinaryTreeBacRecur(treeNode);
        System.out.print("\n");
        PrintBack(treeNode);

        System.out.println("\n");

        PrintFloor(treeNode);
    }

    public static TreeNode<Character>  creatBinaryPre(LinkedList<Character> treeData)
    {
        TreeNode<Character> root=null;
        Character data=treeData.removeFirst();
        if (data!=null)
        {
            root=new TreeNode<Character>(data, null, null);
            root.left=creatBinaryPre(treeData);
            root.right=creatBinaryPre(treeData);
        }
        return root;
    }

    /*
     * 先序遍历二叉树(递归)
     */
    public static void PrintBinaryTreePreRecur(TreeNode<Character> root)
    {
        if (root!=null)
        {
            System.out.print(root.data);
            PrintBinaryTreePreRecur(root.left);
            PrintBinaryTreePreRecur(root.right);
        }
    }

    /*
     * 中序遍历二叉树(递归)
     */
    public static void PrintBinaryTreeMidRecur(TreeNode<Character> root)
    {
        if (root!=null)
        {
            PrintBinaryTreeMidRecur(root.left);
            System.out.print(root.data);
            PrintBinaryTreeMidRecur(root.right);
        }
    }

    /*
     * 后序遍历二叉树(递归)
     */
    public static void PrintBinaryTreeBacRecur(TreeNode<Character> root)
    {
        if (root!=null)
        {
            PrintBinaryTreeBacRecur(root.left);
            PrintBinaryTreeBacRecur(root.right);
            System.out.print(root.data);
        }
    }

    public static void PrintFloor(TreeNode<Character> root){
        LinkedList<TreeNode> linkedList = new LinkedList<>();
        linkedList.add(root);
        while (!linkedList.isEmpty()){
            TreeNode cur = linkedList.removeFirst();
            System.out.print(cur.data);
            if(cur.left!=null){
                linkedList.add(cur.left);
            }
            if(cur.right!=null){
                linkedList.add(cur.right);
            }
        }
    }

    public static void PrintPre(TreeNode<Character> root){
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()){
            TreeNode cur = stack.pop();
            System.out.print(cur.data);
            if(cur.right!=null){
                stack.push(cur.right);
            }
            if(cur.left!=null){
                stack.push(cur.left);
            }
        }
    }

    public static void PrintMid(TreeNode<Character> root){
        TreeNode cur = root;
        Stack<TreeNode> stack = new Stack<>();
        while (cur!=null || !stack.isEmpty()){
            if(cur!=null){
                stack.push(cur);
                cur = cur.left;
            }else {
                cur = stack.pop();
                System.out.print(cur.data);
                cur = cur.right;
            }
        }
    }

    public static void PrintBack(TreeNode<Character> root){
        Stack<TreeNode> stack = new Stack<>();
        Stack<TreeNode> output = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()){
            TreeNode cur = stack.pop();
            output.push(cur);
            if(cur.left!=null){
                stack.push(cur.left);
            }
            if(cur.right!=null){
                stack.push(cur.right);
            }
        }

        while (!output.isEmpty()){
            System.out.print(output.pop().data);
        }
    }
}

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;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值