二叉树已知先序中序求后序,已知后序中序求先序

已知先序中序求后序

public class BinaryTree1 {
    private Node root;

    public void postOrder(){
        postOrder(root);
    }

    //二叉树构造完毕之后进行后续遍历
    private void postOrder(Node localroot) {
        if(localroot!=null){
            postOrder(localroot.left);
            postOrder(localroot.right);
            System.out.print(localroot.data+" ");
        }
    }

    public void InitTree(int[] preOrder,int[] inOrder){
        this.root=InitTree(preOrder,0,preOrder.length-1,inOrder,0,inOrder.length-1);
    }

    /**
     *
     * @param preOrder 先序遍历的数组
     * @param start1 递归每次遍历的数据段在先序遍历数组中的起始位置
     * @param end1 递归每次遍历的数据段在先序遍历数组中的终止位置
     * @param inOrder 中序遍历的数组
     * @param start2 递归每次遍历的数据段在中序遍历数组中的起始位置
     * @param end2 递归每次遍历的数据段在中序遍历数组中的终止位置
     * @return 返回本次递归所构造完毕的树的根节点(从子结点向上依次返回)
     */
    private Node InitTree(int[] preOrder, int start1, int end1, int[] inOrder, int start2, int end2) {
        if(start1>end1||start2>end2){
            return null;
        }
        int rootData=preOrder[start1];
        Node head=new Node(rootData);
        int rootindex=findIndexInArray(inOrder,rootData,start2,end2);
        /**
         * offset为以本次递归构造的节点为根节点的左子树的节点个数
         */
        int offset=rootindex-start2-1;
        Node left=InitTree(preOrder,start1+1,start1+offset+1,inOrder,start2,start2+offset);
        Node right=InitTree(preOrder,start1+offset+2,end1,inOrder,rootindex+1,end2);
        head.left=left;
        head.right=right;
        return head;
    }

    //检索出递归方法每次构造的节点在中序遍历数组中的索引
    public int  findIndexInArray(int[] a,int x,int begin,int end){
        for (int i = begin; i <=end; i++) {
            if(a[i]==x){
                return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        BinaryTree1 biTree=new BinaryTree1();
        int[] preOrder={2,1,8,7,4,3,6,5,7,9};
        int[] inorder={1,2,3,4,5,6,7,7,8,9};
        biTree.InitTree(preOrder,inorder);
        System.out.println("后序遍历");
        biTree.postOrder();
    }
}

解释:假设先序数组A B D E C F 中序数组D B E A F C

这里写图片描述

已知中序后序求先序

public class BinaryTree2 {
    private Node root;

    public void preOrder(){
        preOrder(root);
    }

    private void preOrder(Node localroot) {
        if(localroot!=null){
            System.out.print(localroot.data+" ");
            preOrder(localroot.left);
            preOrder(localroot.right);
        }
    }

    public void initTree(int[] inOrder,int[] postOrder){
        this.root=initTree(inOrder,0,inOrder.length-1,postOrder,0,postOrder.length-1);
    }

    private Node initTree(int[] inOrder, int start1, int end1, int[] postOrder, int start2, int end2) {
        if(start1>end1||start2>end2){
            return null;
        }
        int rootdata=postOrder[end2];
        Node head=new Node(rootdata);
        int rootindex=findIndexInArray(inOrder,rootdata,start1,end1);
        int offset=rootindex-1-start1;
        Node left=initTree(inOrder,start1,start1+offset,postOrder,start2,start2+offset);
        Node right=initTree(inOrder,rootindex+1,end1,postOrder,start2+offset+1,end2-1);
        head.left=left;
        head.right=right;
        return head;
    }

    public int  findIndexInArray(int[] a,int x,int begin,int end){
        for (int i = begin; i <=end; i++) {
            if(a[i]==x){
                return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        BinaryTree2 biTree=new BinaryTree2();
        int[] inorder={1,2,3,4,5,6,7,7,8,9};
        int[] postOrder={1,3,5,6,4,7,7,9,8,2 };
        biTree.initTree(inorder,postOrder);
        System.out.println("先序遍历");
        biTree.preOrder();
    }
}

解释同上。。。。。。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值