先序、中序和后序数组两两结合重构二叉树

import java.util.HashMap;
/**
 * Created by lxw, liwei4939@126.com on 2017/11/7.
 * 先序、中序和后序数组两两结合重构二叉树
 * 给定二叉树的先序、中序和后序数组,从三个数组中任意选择两个
 * 重构原来的二叉树,并返回二叉树的头结点
 */
public class preIoPos {

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

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

    // 先序、中序结合重构二叉树
    public Node preInToTree(int[] pre, int[] in){
        if(pre == null || in == null){
            return null;
        }
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (int i=0; i< in.length; i++){
            map.put(in[i], i);
        }
        return preIn(pre, 0, pre.length-1, in, 0, in.length-1, map);
    }

    public Node preIn(int[] pre, int pi, int pj,int[] in, int ni, int nj,
                      HashMap<Integer,Integer> map){
        if(pi > pj){
            return null;
        }
        Node head = new Node(pre[pi]);
        int index = map.get(pre[pi]);
        head.left = preIn(pre, pi+1, pi+index-ni, in, ni, index-1, map);
        head.right = preIn(pre, pi+index-ni+1, pj, in, index+1, nj, map);
        return head;
    }

    //中序、后序数组重构二叉树
    public Node inPosToTree(int[] in, int[] pos){
        if(in == null || pos == null){
            return null;
        }
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (int i=0; i<in.length; i++){
            map.put(in[i], i);
        }
        return inPos(in, 0, in.length-1, pos, 0, pos.length-1, map);
    }

    public Node inPos(int[] in, int ni, int nj, int[] pos, int pi, int pj,
                      HashMap<Integer,Integer> map){
        if(pi > pj){
            return null;
        }
        Node head = new Node(pos[pj]);
        int index = map.get(pos[pj]);
        head.left = inPos(in, ni, index-1, pos, pi, pi+index-1-ni,map);
        head.right = inPos(in, index+1, nj, pos, pi+index-ni, pj-1, map);
        return head;
    }

    // 先序、后序数组重构二叉树
    // 每个节点的孩子数都为0或2的二叉树才能被先序和后序重构出来
    public Node prePosToTree(int[] pre, int[] pos){
        if(pre == null || pos == null){
            return null;
        }
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (int i=0; i<pos.length; i++){
            map.put(pos[i], i);
        }
        return prePos(pre, 0, pre.length-1, pos, 0, pos.length-1, map);
    }

    public Node prePos(int[] pre, int pi, int pj, int[] pos, int si, int sj,
                          HashMap<Integer,Integer> map){
        Node head = new Node(pos[sj--]);
        if(pi == pj){
            return head;
        }
        int index = map.get(pre[++pi]);
        head.left = prePos(pre, pi, pi+index-si, pos, si, index, map);
        head.right = prePos(pre, pi+index-si+1, pj, pos, index+1, sj, map);
        return head;
    }

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

    public 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 String getSpace(int num) {
        String space = " ";
        StringBuffer buf = new StringBuffer("");
        for (int i = 0; i < num; i++) {
            buf.append(space);
        }
        return buf.toString();
    }

    public static void main(String[] args) {
        preIoPos tmp = new preIoPos();
        int[] pre = {1, 2, 4, 5, 8, 9, 3, 6, 7};
        int[] in = {4, 2, 8, 5, 9, 1, 6, 3, 7};
        int[] pos = {4, 8, 9, 5, 2, 6, 7, 3, 1};
        tmp.printTree(tmp.preInToTree(pre, in));
        tmp.printTree(tmp.inPosToTree(in, pos));
        tmp.printTree(tmp.prePosToTree(pre, pos));
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值