用栈作为存储实现二叉树先序遍历 中序遍历 后续遍历

package com.example.ljia.Structure.tree;

import lombok.Data;

import java.util.Stack;

/**
 * @ Author     :SamLai
 * @ Description:用定义的栈 操作 二叉树 先序遍历 中序遍历  后续遍历
 * 先序遍历 :根 左 右
 * 中序遍历 :左 根  右
 * 后序遍历 :左 右  根
 * 发现规律:这里的顺序是根节点为核心来的
 * 以一棵树为例:
 *      1
 *    2  3
 *  4  5   6
 * 具体操作:[以一个栈作为存储对象]
 *     先序遍历:
 *     1 压入 打印 并 弹出
 *     3压入 2弹出  --> 2 弹出  压入 5 4
 *     4没有子节点则直接弹出  5同理弹出 --> 3弹出 再进行压入6再弹出
 *
 *
 *     中序遍历:
 *     压入根节点 再进行压入左子树  到达了叶子节点之后就要弹出数据并赋予左子树右节点的赋值 逐一弹出并到根节点的时候同样操作右子树操作
 *
 *
 *     后序遍历:
 *     用2个栈实现 压入A栈根节点 A弹出根 压入B栈  再进行A压入左 右节点  再进行弹出A栈顶右节点到B栈 压入原A栈顶的右节点的左右节点 重复此操作
 *     原理:
 *     A栈 先进去的是: 根 左 右  -->转换到B栈中则为:  右 左 根
 *

 *
   打印结果:
     前序遍历 :
     1 2 4 5 3 6
     中序遍历 :
     4 2 5 1 3 6
     后序遍历 :
     4 5 2 6 3 1
 *
 */
@Data
public class StackNode {
    private int value;
    private StackNode left;
    private StackNode right;

    public StackNode(int value) {
        this.value = value;
    }


    public static void Myin(StackNode root) {
        if (root == null) {
            return;
        }
        Stack<StackNode> stack = new Stack<StackNode>();
        while (!stack.isEmpty() || root != null) {
            if (root != null) {
                stack.push(root);
                root = root.getLeft();
            }else{
                root = stack.pop();
                System.out.print(root.getValue() + "  ");
                root = root.getRight();
            }
        }
        System.out.println();
    }

    /**
     * 前序
     * @param head
     */
    public static void preOrderRecur(StackNode head) {
        if (head == null) {
            return;
        }
        Stack<StackNode> stack = new Stack<StackNode>();
        stack.push(head);
        while (!stack.isEmpty()) {
            head = stack.pop();
            System.out.print(head.getValue() + "  ");
            if (head.getRight() != null) {
                stack.push(head.getRight());
            }
            if (head.getLeft() != null) {
                stack.push(head.getLeft());
            }
        }
        System.out.println();
    }

    /**
     * 中序
     * @param head
     */
    public static void inOrderRecur(StackNode head) {
        if (head == null) {
            return;
        }
        Stack<StackNode> stack = new Stack<StackNode>();
        while (!stack.isEmpty() || head != null) {
            if (head != null) {
                stack.push(head);
                head = head.getLeft();
            }else{
                head = stack.pop();
                System.out.print(head.getValue() + "  ");
                head = head.getRight();
            }
        }
        System.out.println();
    }

    /**
     * 后序
     * @param head
     */
    public static void posOrderRecur(StackNode head) {
        if (head == null) {
            return;
        }
        Stack<StackNode> oneStack = new Stack<StackNode>();
        Stack<StackNode> twoStack = new Stack<StackNode>();
        oneStack.push(head);
        while (!oneStack.isEmpty()) {
            head = oneStack.pop();
            twoStack.push(head);
            if (head.getLeft() != null) {
                oneStack.push(head.getLeft());
            }
            if (head.getRight() != null) {
                oneStack.push(head.getRight());
            }

        }
        while (!twoStack.isEmpty()) {
            System.out.print(twoStack.pop().getValue() + "  ");
        }
    }


    public static void main(String[] args) {

        /**
         * 以一棵树为例:
         *      1
         *    2  3
         *  4  5   6
         */
        StackNode head = new StackNode(1);
        StackNode left1 = new StackNode(2);
        StackNode right1 = new StackNode(3);
        head.setLeft(left1);
        head.setRight(right1);
        StackNode left2 = new StackNode(4);
        StackNode right2 = new StackNode(5);
        left1.setLeft(left2);
        left1.setRight(right2);
        StackNode right3 = new StackNode(6);
        right1.setRight(right3);

        System.out.println("前序遍历 : ");
        preOrderRecur(head);
        /**
         结果:
         前序遍历 :
         1  2  4  5  3  6
         */


        System.out.println("中序遍历 : ");
        inOrderRecur(head);

        System.out.println("我的中序遍历 : ");
        Myin(head);

        System.out.println("后序遍历 : ");
        posOrderRecur(head);

        /**
         *
             打印结果:
             前序遍历 :
             1 2 4 5 3 6
             中序遍历 :
             4 2 5 1 3 6
             后序遍历 :
             4 5 2 6 3 1
         *
         */
    }
}

 

点赞