Java 二叉树的四种递归与非递归遍历

来源:互联网 发布:全球数据化时代txt 编辑:程序博客网 时间:2024/06/07 02:47

二叉树基础知识,四种遍历分别为前序、中序、后序,层次遍历。

前序的递归/非递归遍历代码为:

    public static void main(String[] arg) {        Node1 a = new Node1('A');        a.left=new Node1('B');        a.rigth=new Node1('C');        a.left.left=new Node1('D');        a.left.rigth=new Node1('E');        a.rigth.left=new Node1('F');        a.rigth.rigth=new Node1('G');        System.out.println("前序递归输出:");        perOrderRecur(a);//递归输出        System.out.println("\n"+"前序非递归输出:");        perOrderRecurs(a);//递归输出    }    private static void perOrderRecurs(Node1 a) {        if(a==null) {            return ;        }        ArrayList<Node1> list=new ArrayList<>();        Stack<Node1> stack=new Stack<Node1>();        stack.push(a);        while(!stack.isEmpty()) {            Node1 nd=stack.pop();            list.add(nd);            if(nd.rigth!=null) {                stack.push(nd.rigth);            }            if(nd.left!=null) {                stack.push(nd.left);            }        }        for(int i=0;i<list.size();i++) {            System.out.print(list.get(i).val+" ");        }    }    private static void perOrderRecur(Node1 a) {        if(a==null) {            return ;        }        System.out.print(a.val+" ");        perOrderRecur(a.left);        perOrderRecur(a.rigth);    }

这里写图片描述

中序的递归/非递归遍历代码为:

    public static void main(String[] arg) {        Node1 a = new Node1('A');        a.left=new Node1('B');        a.rigth=new Node1('C');        a.left.left=new Node1('D');        a.left.rigth=new Node1('E');        a.rigth.left=new Node1('F');        a.rigth.rigth=new Node1('G');        System.out.println("中序递归输出:");        inOrderRecur(a);//递归输出        System.out.println("\n"+"中序非递归输出:");        inOrderRecurs(a);//递归输出    }
private static void inOrderRecurs(Node1 a) {    if(a==null) {        return ;    }    ArrayList<Node1> list=new ArrayList<Node1>();    Stack<Node1> stack=new Stack<Node1>();    while(!stack.isEmpty()||a!=null) {        if(a!=null) {            stack.push(a);            a=a.left;        }else {            Node1 nd=stack.pop();            list.add(nd);            a=nd.rigth;        }    }    for(int i=0;i<list.size();i++) {        System.out.print(list.get(i).val+" ");    }}private static void inOrderRecur(Node1 a) {    if(a==null) {        return ;    }    inOrderRecur(a.left);    System.out.print(a.val+" ");    inOrderRecur(a.rigth);}

这里写图片描述

后序的递归/非递归遍历代码为:

public static void main(String[] arg) {    Node1 a = new Node1('A');    a.left=new Node1('B');    a.rigth=new Node1('C');    a.left.left=new Node1('D');    a.left.rigth=new Node1('E');    a.rigth.left=new Node1('F');    a.rigth.rigth=new Node1('G');    System.out.println("后序递归输出:");    posOrderRecur(a);//递归输出    System.out.println("\n"+"后序非递归输出:");    posOrderRecurs(a);//递归输出}private static void posOrderRecurs(Node1 a) {    if(a==null) {        return ;    }    Stack<Node1> stack1=new Stack<Node1>();    stack1.push(a);    Stack<Node1> stack2=new Stack<Node1>();    while(!stack1.isEmpty()) {        Node1 nd=stack1.pop();        stack2.push(nd);        if(nd.left!=null) {            stack1.push(nd.left);        }        if(nd.rigth!=null) {            stack1.push(nd.rigth);        }    }    while(!stack2.isEmpty()) {        System.out.print(stack2.pop().val+" ");    }}private static void posOrderRecur(Node1 a) {    if(a==null) {        return ;    }    posOrderRecur(a.left);    posOrderRecur(a.rigth);    System.out.print(a.val+" ");}

这里写图片描述

层次的非递归遍历代码为:

public static void main(String[] arg) {    Node a = new Node('A');    a.left=new Node('B');    a.rigth=new Node('C');    a.left.left=new Node('D');    a.left.rigth=new Node('E');    a.rigth.left=new Node('F');    a.rigth.rigth=new Node('G');    System.out.println("层次遍历非递归为:");    levelRecur(a);}private static void levelRecur(Node a) {    if(a==null) {        return;    }    Queue<Node> queue=new LinkedList<Node>();    queue.offer(a);    while(!queue.isEmpty()) {        Node nd=queue.poll();        System.out.print(nd.val+" ");        if(nd.left!=null) {            queue.offer(nd.left);        }        if(nd.rigth!=null) {            queue.offer(nd.rigth);        }    }}

这里写图片描述

阅读全文
0 0