二叉树的遍历:前中后序(递归、非递归、不借助辅助空间)、按层遍历,可直接运行!

二叉树的遍历:前中后序(递归、非递归、不借助辅助空间)、按层遍历

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

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

        public Node(int data) {
            this.value = data;
        }
    }

    public static void main(String[] args) {
        Node head = new Node(4);
        head.left = new Node(2);
        head.right = new Node(6);
        head.left.left = new Node(1);
        head.left.right = new Node(3);
        head.right.left = new Node(5);
        head.right.right = new Node(7);
        printTree(head); //输出原始树
        System.out.println("中序遍历(morris遍历):");
        morrisIn(head); //不借助辅助空间的中序遍历
        System.out.println("前序遍历(morris遍历):");
        morrisPre(head); //不借助辅助空间的先序遍历
        //下面是递归遍历
        System.out.println("前序遍历(递归):");
        printTreePreOrderRecursively(head); //递归前序遍历
        System.out.println("\n中序遍历(递归):");
        printTreeInOrderRecursively(head);//递归中序遍历
        System.out.println("\n后序遍历(递归):");
        printTreePostOrderRecursively(head);//递归后序遍历
        //下面是非递归遍历但借助栈【前中后序的循环条件都是一样的!】
        System.out.println("\n前序遍历(非递归):");
        printTreePreOrderNotRecur(head);//非递归前序遍历
        System.out.println("\n中序遍历(非递归):");
        printTreeInOrderNotRecur(head);//非递归中序遍历
        System.out.println("\n后序遍历(非递归):");
        printTreePostOrderNotRecur(head);//非递归后序遍历
        //按层遍历,借助队列
        System.out.println("\n按层遍历:");
        printTreeQueue(head);
        //输出原始树,检查原始树是否被改变,上面的遍历正确的话原始树就没有被改变
        printTree(head); 

    }



    // for test -- print tree
    public static void printTree(Node head) {
        System.out.println("Binary Tree:");
        printInOrder(head, 0, "H", 17);
        System.out.println();
    }

    public static void printInOrder(Node head, int height, String to, int len) {
        if (head == null) {
            return;
        }
        printInOrder(head.right, height + 1, "v", len);
        String val = to + head.value + to;
        int lenM = val.length();
        int lenL = (len - lenM) / 2;
        int lenR = len - lenM - lenL;
        val = getSpace(lenL) + val + getSpace(lenR);
        System.out.println(getSpace(height * len) + val);
        printInOrder(head.left, height + 1, "^", len);
    }

    public static String getSpace(int num) {
        String space = " ";
        StringBuffer buf = new StringBuffer("");
        for (int i = 0; i < num; i++) {
            buf.append(space);
        }
        return buf.toString();
    }



    private static void morrisPre(Node head) {
        if(head == null) {
            return;
        }
        while(head != null) {
            if(head.left != null) {
                Node morisRight = getMorrisRight(head);
                if(morisRight.right != null) {
                     morisRight.right = null;
                     head = head.right;
                } else {
                    morisRight.right = head;
                    System.out.print(head.value  + ",");
                    head = head.left;
                }
            } else {
                System.out.print(head.value + ",");
                head = head.right;
            }
        }
        System.out.println();
    }

    public static void morrisIn(Node head) {

        if(head == null) {
            return;
        }
        while(head != null) {
            if(head.left != null) {
                Node morisRight = getMorrisRight(head);
                if(morisRight.right != null) {
                     System.out.print(head.value + ",");
                     morisRight.right = null;
                     head = head.right;
                } else {
                    morisRight.right = head;
                    head = head.left;
                }
            } else {
                System.out.print(head.value + ",");
                head = head.right;
            }
        }
        System.out.println();
    }

    public static Node getMorrisRight(Node head) {
        Node left = head.left;
        while(left.right != null && left.right != head) {
            left = left.right;
        }
        return left;
    }

    private static void printTreeQueue(Node head) {
        if(head == null) {
            return;
        }
        Queue<Node> queue = new LinkedList<>();
        queue.add(head);
        while(!queue.isEmpty()) {
            head = queue.poll();
            System.out.print(head.value + ",");
            if(head.left != null) {
                queue.add(head.left);
            }
            if(head.right != null) {
                queue.add(head.right);
            }
        }
        System.out.println();
    }

    private static void printTreePostOrderNotRecur(Node head) {
        if(head == null) {
            return;
        }
        Stack<Node> stack = new Stack<>();
        Stack<Node> help = new Stack<>();
        while(head != null || !stack.isEmpty()) {
            if(head != null) {
                stack.push(head);
                help.push(head);
                head = head.right;
            } else {
                head = stack.pop();
                head = head.left;
            }
        }
        while(!help.isEmpty()) {
            System.out.print(help.pop().value + ",");
        }
    }

    private static void printTreeInOrderNotRecur(Node head) {
        if(head == null) {
            return;
        }
        Stack<Node> stack = new Stack<>();
        while(head != null || !stack.isEmpty()) {
            if(head != null) {
                stack.push(head);
                head = head.left;
            } else {
                head = stack.pop();
                System.out.print(head.value + ",");
                head = head.right;
            }
        }
    }

    private static void printTreePreOrderNotRecur(Node head) {
        if(head == null) {
            return;
        }
        Stack<Node> stack = new Stack<>();
        while(head != null || !stack.isEmpty()) {
            if(head != null) {
                System.out.print(head.value + ",");
                stack.push(head);
                head = head.left;
            } else {
                head = stack.pop();
                head = head.right;
            }
        }
    }

    private static void printTreePostOrderRecursively(Node head) {
        if(head == null) {
            return;
        } else {
            printTreePostOrderRecursively(head.left);
            printTreePostOrderRecursively(head.right);
            System.out.print(head.value + ",");
        }

    }

    private static void printTreeInOrderRecursively(Node head) {
        if(head == null) {
            return;
        } else {
            printTreeInOrderRecursively(head.left);
            System.out.print(head.value + ",");
            printTreeInOrderRecursively(head.right);
        }
    }

    private static void printTreePreOrderRecursively(Node head) {
        if(head == null) {
            return;
        } else {
            System.out.print(head.value + ",");
            printTreePreOrderRecursively(head.left);
            printTreePreOrderRecursively(head.right);
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值