二叉树的遍历

二叉树遍历

Node
public class Node {
    public int value;
    public Node left;
    public Node right;

    public Node(int data){
        this.value = data;
    }
}
recur
package com.wxl.DichotomizationTree.thinking;


import com.wxl.DichotomizationTree.Node.Node;

public class Recur {
    /**
     * 先序遍历
     * @param head 二叉树头节点
     */
    public void preOrderRecur(Node head){
        if(head == null ){
            return;
        }
        System.out.print(head.value + " ");
        preOrderRecur(head.left);
        preOrderRecur(head.right);
    }

    /**
     * 中序遍历
     * @param head 二叉树头节点
     */
    public void inOrderRecur(Node head){
        if(head == null ){
            return;
        }
        inOrderRecur(head.left);
        System.out.print(head.value + " ");
        inOrderRecur(head.right);
    }

    /**
     * 后序遍历
     * @param head 二叉树头节点
     */
    public void postOrderRecur(Node head){
        if(head == null ){
            return;
        }
        postOrderRecur(head.left);
        postOrderRecur(head.right);
        System.out.print(head.value + " ");
    }
    public static void main(String[] args){
        Recur recur = new Recur();
        //构造二叉树
        Node head = createTree();
        System.out.println("先序遍历");
        recur.preOrderRecur(head);
        System.out.println();
        System.out.println("中序遍历");
        recur.inOrderRecur(head);
        System.out.println();
        System.out.println("后序遍历");
        recur.postOrderRecur(head);
    }

    private static Node createTree() {
        Node head = new Node(0);
        Node leftHead = head;
        Node rightHead = head;
        head.left = new Node(1);
        head.right = new Node(2);
        leftHead = leftHead.left;
        leftHead.left = new Node(3);
        leftHead.right = new Node(4);
        rightHead = rightHead.right;
        rightHead.left = new Node(5);
        rightHead.right = new Node(6);
        return head;
    }
}
result
先序遍历
0 1 3 4 2 5 6 
中序遍历
3 1 4 0 5 2 6 
后序遍历
3 4 1 5 6 2 0 
unRecur
package com.wxl.DichotomizationTree.thinking;

import com.wxl.DichotomizationTree.Node.Node;
import org.omg.CORBA.NO_IMPLEMENT;

import java.nio.charset.StandardCharsets;
import java.util.Stack;

public class unRecur {
    /**
     * 先序遍历
     * @param head
     */
    public void preOrderUnRecur(Node head){
        System.out.println("pre-Order");
        if (head == null){
            return;
        }
        Stack<Node> stack = new Stack<Node>();
        stack.push(head);
        while (!stack.isEmpty()){
            head = stack.pop();
            System.out.print(head.value + " ");
            if (head.right != null){
                stack.push(head.right);
            }
            if (head.left != null){
                stack.push(head.left);
            }
        }
        System.out.println();
    }

    /**
     * 中序遍历
     * @param head
     */
    public void inOrderUnRecur(Node head){
        System.out.println("in-Order");
        Node cur = head;
        Stack<Node> stack = new Stack<Node>();
        while (!stack.isEmpty()||cur != null){//条件 ||
            if (cur!=null){
                stack.push(cur);
                cur = cur.left;
            }else {
                cur = stack.pop();
                System.out.print(cur.value + " ");
                cur = cur.right;
            }
        }
        System.out.println();
    }
    public void postOrderUnRecur(Node head){
        System.out.println("post-order:");
        if (head == null) return;
        Stack<Node> s1 = new Stack<Node>();
        Stack<Node> s2 = new Stack<Node>();
        s1.push(head);
        while (!s1.isEmpty()){
            head = s1.pop();
            s2.push(head);
            if (head.left != null){
                s1.push(head.left);
            }
            if (head.right != null){
                s1.push(head.right);
            }
        }
        while (!s2.isEmpty()){
            System.out.print(s2.pop().value + " ");
        }
        System.out.println();
    }
    public static void main(String[] args){
        unRecur unRecur = new unRecur();
        //构造二叉树
        Node head = createTree();
        System.out.println("先序遍历");
        unRecur.preOrderUnRecur(head);
        System.out.println("中序遍历");
        unRecur.inOrderUnRecur(head);
        System.out.println("后续序遍历");
        unRecur.postOrderUnRecur(head);
        System.out.println("后续序遍历==一个栈解决");
        unRecur.postOrderUnRecurN1(head);
    }

    private void postOrderUnRecurN1(Node head) {
        System.out.println("post-order:");
        if (head == null) return;
        Stack<Node> stack = new Stack<Node>();
        stack.push(head);
        Node c = null;
        while (!stack.isEmpty()){
            c = stack.peek();
            if (c.left != null && head != c.left && head != c.right){
                stack.push(c.left);
            }else if (c.right != null && head != c.right){
                stack.push(c.right);
            }else {
                System.out.print(stack.pop().value+" ");
                head = c;
            }
        }
        System.out.println();
    }

    private static Node createTree() {
        Node head = new Node(0);
        Node leftHead = head;
        Node rightHead = head;
        head.left = new Node(1);
        head.right = new Node(2);
        leftHead = leftHead.left;
        leftHead.left = new Node(3);
        leftHead.right = new Node(4);
        rightHead = rightHead.right;
        rightHead.left = new Node(5);
        rightHead.right = new Node(6);
        return head;
    }
}
result
先序遍历pre-Order
0 1 3 4 2 5 6 
中序遍历
in-Order
3 1 4 0 5 2 6 
后续序遍历
post-order:
3 4 1 5 6 2 0 
后续序遍历==一个栈解决
post-order:
3 4 1 5 6 2 0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值