二叉树的先序遍历、后续遍历、层次遍历(非递归算法 java实现)

import java.util.Scanner;
import java.util.Stack;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;


/**
 *              1
 *
 *           2        3
 *
 *       4      5    6    7
 *
 *     先序遍历  1  2  4 5  3  6  7
 *
 *     输入  :
 *     7
 *     1 2 3 4 5 6 7
 *
 *
 */
public class Hello {

    public static void main(String[] args)
    {

        Scanner in = new Scanner(System.in);
        int count  = in.nextInt();
        int[] array = new int[count];
        for(int i = 0;i<count;i++){
            array[i] = in.nextInt();
        }
        Node[] nodeArray = new Node[count + 1];
        for(int i = 1;i<= count;i++)
        {
            nodeArray[i] = new Node();
            nodeArray[i].setData(array[i-1]) ;



        }
        for(int i = 1;i<= count;i++)
        {
            if(2*i <= count) {
                nodeArray[i].setLeftChild(nodeArray[2 * i]);
            }
            if(2*i + 1 <= count) {
                nodeArray[i].setRightChild(nodeArray[2 * i + 1]);
            }


        }
        Node root = nodeArray[1];

        firstVisitRecursion(root);

        firstVisit(root);

        System.out.println();
        endVisit(root);
        levelVisit(root);
    }

    /**
     *  先序遍历
     * @param root
     */
    private static void firstVisit(Node root)
    {
        Stack<Node>  stack = new Stack<>();
        Node p = root;
        int top = -1;
        while(p != null || top != -1)
        {
            while(p != null)
            {
                System.out.print(p.getData() + " ");
                stack.push(p);
                top ++;
                p = p.getLeftChild();
            }
            p = stack.pop();
            top --;
            p = p.getRightChild();
        }


    }

    /**
     *  先序遍历--递归
     * @param root
     */
    private static void firstVisitRecursion(Node root)
    {
        if(root != null) {
            System.out.println(root.getData());
            firstVisitRecursion(root.getLeftChild());
            firstVisitRecursion(root.getRightChild());
        }


    }

    /**
     * 后序遍历
     * @param root
     */
    private static void endVisit(Node root)
    {
        Stack<Node>  stack = new Stack<>();
        Stack<Integer>  stack2 = new Stack<>();
        Node p = root;
        int top = -1;
        while(p != null || top != -1)
        {
            while(p != null)
            {
                stack.push(p);
                stack2.push(0);
                top ++;
                p = p.getLeftChild();
            }
            p = stack.pop();
            top --;
            int result = stack2.pop();
            if(result == 0)
            {
                stack.push(p);
                stack2.push(1);
                top ++;
                p = p.getRightChild();
            }else
            {
                System.out.print(p.getData() + " ");
                p = null;
            }
        }


    }

    /**
     * 按照层次访问
     * @param root
     */
    private static void levelVisit(Node root)
    {
        System.out.println();
        BlockingQueue<Node> queue = new LinkedBlockingQueue<>();
        Node p = root;
        queue.offer(p);
        while(true)
        {
            Node temp = queue.poll();
            if(temp == null) {
                break;
            }
            System.out.print(temp.getData() + " ");
            if(temp.getLeftChild() != null) {
                queue.add(temp.getLeftChild());
            }
            if(temp.getRightChild() != null) {
                queue.add(temp.getRightChild());
            }
        }


    }





}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值