二叉树专题(二叉树遍历与重构)

二叉树

二叉树的遍历


public class BinaryTree{
    //java二叉树的节点结构 重点!!!!
    public static class Node{
        int value;
        Node left;
        Node right;
        public Node(int data) {
            this.value=data;
        }
    }
    /*
    c/c++也是,java也是,每个教材都告诉你如何遍历一棵二叉树,
    却从来都没有人告诉你,那棵该死的树是啥样,是怎么被创建出来的。 
    没事,我告诉你,树就是这么被建立的,树的遍历不需要依赖整棵树, 
    得到头结点我们就可以完成这个过程 
    */
    public static void main(String[] args) {
        Node head = new Node(5);
        head.left = new Node(3);
        head.right = new Node(8);
        head.left.left = new Node(2);
        head.left.right = new Node(4);
        head.left.left.left = new Node(1);
        head.right.left = new Node(7);
        head.right.left.left = new Node(6);
        head.right.right = new Node(10);
        head.right.right.left = new Node(9);
        head.right.right.right = new Node(11);

        // recursive 递归
        System.out.println("==============recursive==============");
        System.out.print("pre-order: ");
        preOrderRecur(head);
        System.out.println();
        System.out.print("in-order: ");
        inOrderRecur(head);
        System.out.println();
        System.out.print("pos-order: ");
        posOrderRecur(head);
        System.out.println();

        // unrecursive 非递归
        System.out.println("============unrecursive=============");
        preOrderUnRecur(head);
        inOrderUnRecur(head);
        posOrderUnRecur1(head);
        posOrderUnRecur2(head);
    }
    public static void preOrderRecur(Node head) {
        //base case 为空就跳出
        if (head == null) {
            return;
        }
        //先序 根左右
        System.out.print(head.value + " ");
        preOrderRecur(head.left);
        preOrderRecur(head.right);
    }

    public static void inOrderRecur(Node head) {
        if (head == null) {
            return;
        }
        //中序 左根右
        inOrderRecur(head.left);
        System.out.print(head.value + " ");
        inOrderRecur(head.right);
    }

    public static void posOrderRecur(Node head) {
        if (head == null) {
            return;
        }
        //后序 左右根
        posOrderRecur(head.left);
        posOrderRecur(head.right);
        System.out.print(head.value + " ");
    }

    public static void preOrderUnRecur(Node head) {
        System.out.print("pre-order: ");
        if (head != null) {
            //栈和栈的使用 如果你会可以忽略这个链接 看代码就可以理解过程http://blog.csdn.net/wdays83892469/article/details/79312417
            Stack<Node> stack = new Stack<Node>();
            stack.add(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();
    }

    public static void inOrderUnRecur(Node head) {
        System.out.print("in-order: ");
        if (head != null) {
            Stack<Node> stack = new Stack<Node>();
            while (!stack.isEmpty() || head != null) {
                if (head != null) {
                    stack.push(head);
                    head = head.left;
                } else {
                    head = stack.pop();
                    System.out.print(head.value + " ");
                    head = head.right;
                }
            }
        }
        System.out.println();
    }

    public static void posOrderUnRecur1(Node head) {
        System.out.print("pos-order: ");
        if (head != null) {
            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 posOrderUnRecur2(Node h) {
        System.out.print("pos-order: ");
        if (h != null) {
            Stack<Node> stack = new Stack<Node>();
            stack.push(h);
            Node c = null;
            while (!stack.isEmpty()) {
                c = stack.peek();
                if (c.left != null && h != c.left && h != c.right) {
                    stack.push(c.left);
                } else if (c.right != null && h != c.right) {
                    stack.push(c.right);
                } else {
                    System.out.print(stack.pop().value + " ");
                    h = c;
                }
            }
        }
        System.out.println();
    }
}

这里写图片描述

二叉树的重构和创建

二叉树究竟是怎么被创建的呢,看到上面创建代码,一般大家不会这么建树,这是为了直观,

Node head = new Node(5);
head.left = new Node(3);
head.right = new Node(8);
head.left.left = new Node(2);
head.left.right = new Node(4);
head.left.left.left = new Node(1);
head.right.left = new Node(7);
head.right.left.left = new Node(6);
head.right.right = new Node(10);
head.right.right.left = new Node(9);
head.right.right.right = new Node(11);

而我们通常拿到的树是一个序列,比如给你先序中序求后续遍历。
我们就要根据先序中序两个数组来完成重构二叉树

《剑指offer》的第五题:

  • 题目描述:
    输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。

    时间限制:1秒 空间限制:32768K

    题目是重构二叉树并返回头结点,我们拿到后就可以用前面前中后序遍历来验证了,这里给一组数据
    先序 5 3 2 1 4 8 7 6 10 9 11
    中序 1 2 3 4 5 6 7 8 9 10 11
    后续 1 2 4 3 6 7 9 11 10 8 5

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

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

    public static void main(String[] args) {
        int pre[] = { 5, 3, 2, 1, 4, 8, 7, 6, 10, 9, 11 };
        int in[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
        Node head = reConstructBinaryTree(pre, in);
        //打印后序遍历结果,可以根据前面的先中后序遍历来验证结果是否正确
        pro(head);

    }

    public static Node reConstructBinaryTree(int pre[], int in[]) {
        if (pre.length == in.length && pre.length < 1 || pre == null
                || in == null) {
            return null;
        }
        return reConstructBinaryTreeCore(pre, in, 0, pre.length - 1, 0,
                in.length - 1);
    }

    public static Node reConstructBinaryTreeCore(int pre[], int in[],
            int preStart, int preEnd, int inStart, int inEnd) {
        Node tree = new Node(pre[preStart]);// 每次都是子树的根节点
        tree.left = null;// 给两个子节点开辟空间
        tree.right = null;
        // 最后一个节点 base case
        if (preStart == preEnd && inStart == inEnd) {
            return tree;
        }
        int root = 0;
        for (root = inStart; root < inEnd; root++) {
            // 找到中序遍历中和先序遍历的第一个节点相同的值
            // 中序遍历从这里把子树分为左右两部分
            if (pre[preStart] == in[root]) {
                break;
            }
        }
        int leftLength = root - inStart;
        int rightLength = inEnd - root;
        if (leftLength > 0) {
            //左子树的先序起始位置和结束位置 
            //中序起始位置和结束位置 是根据前面循环算出来的root位置 一分为二两部分 不清楚的看后面插图
            tree.left = reConstructBinaryTreeCore(pre, in, preStart + 1,
                    preStart + leftLength, inStart, root - 1);
        }
        if (rightLength > 0) {
            //
            tree.right = reConstructBinaryTreeCore(pre, in, preStart + 1
                    + leftLength, preEnd, root + 1, inEnd);
        }
        return tree;
    }

    public static void pro(Node head) {
        if (head == null) {
            return;
        }
        pro(head.left);
        pro(head.right);
        System.out.print(head.value + " ");
    }

}

core代码没看懂?不要紧,这里有幅图
这里写图片描述
然后是下一层递归(只画了左子树部分)
这里写图片描述
继续递归(只画了左子树部分)
这里写图片描述

自底而上,把子树拼成最后的二叉树
这里写图片描述
之后会更新二叉树序列化与反序列化。
判断,构建二叉搜索树…….等各种树
本文最后编辑时间 2018-2-14 23:19:26

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值