欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

二叉树前序、中序、后序、层序遍历的递归与非递归java实现

程序员文章站 2022-05-16 14:37:03
...
import java.util.Queue;
import java.util.Stack;
import java.util.concurrent.LinkedBlockingQueue;

public class TreeTraverse {

    public static void main(String[] args){
        //测试如下二叉树
//                   1
//                /     \
//              2         3
//            /    \    /    \
//           4      5  6      7

        Node root = new Node(1);
        root.left = new Node(2);
        root.left.left = new Node(4);
        root.left.right = new Node(5);
        root.right = new Node(3);
        root.right.left = new Node(6);
        root.right.right = new Node(7);
        preOrderTraverse(root);
        System.out.println();
        preOrderTraverse2(root);
        System.out.println();
        inOrderTraverse(root);
        System.out.println();
        inOrderTraverse2(root);
        System.out.println();
        posOrderTraverse(root);
        System.out.println();
        posOrderTraverse2(root);
        System.out.println();
        floorTraverse(root);
    }

    //先序遍历的递归实现
    public static void preOrderTraverse(Node node){
        if(node == null)
            return;
        System.out.print(node.value + "  ");
        preOrderTraverse(node.left);
        preOrderTraverse(node.right);
    }

    //先序遍历的非递归实现
    public static void preOrderTraverse2(Node node){
        if(node == null)
            return;
        Stack<Node> stack = new Stack<>();
        stack.push(node);
        while(!stack.isEmpty()){
            node = stack.pop();
            System.out.print(node.value + "  ");
            if(node.right != null)
                stack.push(node.right);
            if(node.left != null)
                stack.push(node.left);
        }
    }

    //中序遍历的递归实现
    public static void inOrderTraverse(Node node){
        if(node == null)
            return;
        inOrderTraverse(node.left);
        System.out.print(node.value + "  ");
        inOrderTraverse(node.right);
    }

    //中序遍历的非递归实现
    public static void inOrderTraverse2(Node node){
        if(node == null)
            return;
       Stack<Node> stack = new Stack<>();
       while(!stack.isEmpty() || node != null){
           while(node != null){
               stack.push(node);
               node = node.left;
           }
           node = stack.pop();
           System.out.print(node.value + "  ");
           node = node.right;
       }
    }

    //后序遍历的递归实现
    public static void posOrderTraverse(Node node){
        if(node == null)
            return;
        posOrderTraverse(node.left);
        posOrderTraverse(node.right);
        System.out.print(node.value + "  ");
    }

    //后序遍历的非递归实现
    public static void posOrderTraverse2(Node node){
        if(node == null)
            return;
        Stack<Node> stack = new Stack<>();
        Stack<Node> help = new Stack<>();
        stack.push(node);
        while(!stack.isEmpty()){
            node = stack.pop();
            help.push(node);
            if(node.left != null)
                stack.push(node.left);
            if(node.right != null)
                stack.push(node.right);
        }
        while(!help.isEmpty())
            System.out.print(help.pop().value + "  ");
    }

    //层序遍历
    public static void floorTraverse(Node node){
        if(node == null)
            return;
        Queue<Node> queue = new LinkedBlockingQueue<>();
        queue.add(node);
        while(!queue.isEmpty()){
            node = queue.poll();
            System.out.print(node.value + "  ");
            if(node.left != null)
                queue.add(node.left);
            if(node.right != null)
                queue.add(node.right);
        }
    }
}

class Node{
    int value;
    Node left;
    Node right;
    public Node(int value){
        this.value = value;
        this.left = null;
        this.right = null;
    }
}


相关标签: 二叉树 遍历