二叉树详解

  • 构建二叉树节点
public class TNode<T> {
    public T nodeValue;
    public TNode<T> left,right;
    public TNode(T item){
        nodeValue=item;
        left=right=null;
    }
    public TNode(T item,TNode<T> left,TNode<T> right){
        nodeValue=item;
        this.left=left;
        this.right=right;
    }
}


  • 构建二叉树
    TNode<Integer> p,q;
        p=new TNode<Integer>(8);
        q=new TNode<Integer>(4,null,p);

  • 递规构建二叉树(前序加中序)
    public TNode BT(T [] pre,T[] mid){
        if(pre==null||pre.length==0)
            return null;

        TNode root=new TNode(pre[0]);
        int Indexmid=0,i;
        for( i=0;i<mid.length;i++){
            if(mid[i]==root.nodeValue)
                break;
        }
        Indexmid=i;
        T[] preLeft= Arrays.copyOfRange(pre,1,1+Indexmid);
        T[] prerigh=Arrays.copyOfRange(pre,1+Indexmid,pre.length);
        T[] midLeft= Arrays.copyOfRange(mid,0,Indexmid);
        T[] midrigh= Arrays.copyOfRange(mid,Indexmid+1,mid.length);
        TNode leftChild=BT(preLeft,midLeft);
        TNode rightChild=BT(prerigh,midrigh);

        root.left=leftChild;
        root.right=rightChild;
        return root;
    }

  • 递规构建二叉树(后序加中序)
  public TNode BT2(T[] succ,T[] mid){
        if(succ==null||succ.length==0)
            return null;

        TNode root=new TNode(succ[succ.length-1]);
        int Indexmid=0,i;
        for( i=0;i<mid.length;i++){
            if(mid[i]==root.nodeValue)
                break;
        }
        Indexmid=i;
        T[] succLeft= Arrays.copyOfRange(succ,0,Indexmid);
        T[] succrigh=Arrays.copyOfRange(succ,Indexmid,succ.length-1);
        T[] midLeft= Arrays.copyOfRange(mid,0,Indexmid);
        T[] midrigh= Arrays.copyOfRange(mid,Indexmid+1,mid.length);
        TNode leftChild=BT2(succLeft,midLeft);
        TNode rightChild=BT2(succrigh,midrigh);

        root.left=leftChild;
        root.right=rightChild;
        return root;
    }
  • 树的广度优先遍历

    public void  breadthFirst(TNode<T>  root){
        if(root==null)
            return ;
        TNode<T> p=root;
        Queue<TNode<T>> que=new LinkedList<TNode<T>>();
        que.add(p);
        while(!que.isEmpty()){
            p=que.poll();
            p.visit();// System.out.println(nodeValue+"");
            if(p.left!=null)
                que.add(p.left);
            if(p.right!=null)
                que.add(p.right);
        }

    }
  • 完整 代码:
package learn.java.com.java.datastru;


import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

public class TNode<T> {
    public T nodeValue;
    public TNode<T> left,right;
    public TNode(T item){
        nodeValue=item;
        left=right=null;
    }
    public TNode(T item,TNode<T> left,TNode<T> right){
        nodeValue=item;
        this.left=left;
        this.right=right;
    }
    public void visit(){
        System.out.print(nodeValue+"");
    }

    public void  breadthFirst(TNode<T>  root){
        if(root==null)
            return ;
        TNode<T> p=root;
        Queue<TNode<T>> que=new LinkedList<TNode<T>>();
        que.add(p);
        while(!que.isEmpty()){
            p=que.poll();
            p.visit();// System.out.println(nodeValue+"");
            if(p.left!=null)
                que.add(p.left);
            if(p.right!=null)
                que.add(p.right);
        }

    }

    public TNode BT(T [] pre,T[] mid){
        if(pre==null||pre.length==0)
            return null;

        TNode root=new TNode(pre[0]);
        int Indexmid=0,i;
        for( i=0;i<mid.length;i++){
            if(mid[i]==root.nodeValue)
                break;
        }
        Indexmid=i;
        T[] preLeft= Arrays.copyOfRange(pre,1,1+Indexmid);
        T[] prerigh=Arrays.copyOfRange(pre,1+Indexmid,pre.length);
        T[] midLeft= Arrays.copyOfRange(mid,0,Indexmid);
        T[] midrigh= Arrays.copyOfRange(mid,Indexmid+1,mid.length);
        TNode leftChild=BT(preLeft,midLeft);
        TNode rightChild=BT(prerigh,midrigh);

        root.left=leftChild;
        root.right=rightChild;
        return root;
    }


    public TNode BT2(T[] succ,T[] mid){
        if(succ==null||succ.length==0)
            return null;

        TNode root=new TNode(succ[succ.length-1]);
        int Indexmid=0,i;
        for( i=0;i<mid.length;i++){
            if(mid[i]==root.nodeValue)
                break;
        }
        Indexmid=i;
        T[] succLeft= Arrays.copyOfRange(succ,0,Indexmid);
        T[] succrigh=Arrays.copyOfRange(succ,Indexmid,succ.length-1);
        T[] midLeft= Arrays.copyOfRange(mid,0,Indexmid);
        T[] midrigh= Arrays.copyOfRange(mid,Indexmid+1,mid.length);
        TNode leftChild=BT2(succLeft,midLeft);
        TNode rightChild=BT2(succrigh,midrigh);

        root.left=leftChild;
        root.right=rightChild;
        return root;
    }



    public static void main(String[] args){
//        TNode<Integer> p,q;
//        p=new TNode<Integer>(8);
//        q=new TNode<Integer>(4,null,p);
//        q.breadthFirst(q);
   TNode<Integer>p=new TNode<Integer>(0);
        Integer []pre ={1,2,4,8,9,5,3,6,7};
        Integer [] mid={8,4,9,2,5,1,6,3,7};
        p=p.BT(pre,mid);
        p.breadthFirst(p);
        System.out.println("----------");
        TNode<String>q=new TNode<String >("0");
        String  [] pre2 ={"a","b","f","g","c","d","e"};
        String [] mid2={"f","b","g","a","d","c","e"};

        q=q.BT(pre2,mid2);
        q.breadthFirst(q);
        System.out.println("-------");
        String [] succ={"f","g","b","d","e","c","a"};
        q=q.BT2(succ,mid2);
        q.breadthFirst(q);


    }
}



测试结果:

123456789----------
abcfgde-------
abcfgde

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值