二叉排序树

一、二叉排序树简介

二叉排序树,又叫二叉查找树,它或者是一棵空树;或者是具有以下性质的二叉树:

  1. 若它的左子树不空,则左子树上所有节点的值均小于它的根节点的值;
  2. 若它的右子树不空,则右子树上所有节点的值均大于它的根节点的值;
  3. 它的左右子树也分别为二叉排序树。

二、二叉排序树的创建

假设我们要为数组 a[] = {62, 88, 58, 47, 35, 73, 51, 99, 37, 93}构建一个二叉排序树,我们按顺序逐个插入元素。

插入过程是这样的:

在初始化状态下我们二叉排序树的根节点为空,我们依次将集合中的元素通过搜索插入到二叉排序树中合适的位置。
首先在二叉排序中进行搜索62的位置,树为空,所以将62存入到二叉排序树的根节点中,及root=(62)。
从集合中取出88,然后查找我们的二叉排序树,发现88大于我们的根节点62,所以将88插入到62节点的右子树中,即(62)->rchild=(88)。
从集合中取出58,然后从根节点开始查找我们现有的二叉排序树,发现55<62,将55作为62的左结点,即(62)->lchild=(55)。
从集合中取出47,然后对二叉排序树进行搜索,发现47<55, 所以(55)->leftChild=(47)。
从集合中取出35,再次对二叉排序树进行搜索,发现35又小于47,所以(47)->leftChild=(35)。
从集合中取出73,再次对二叉排序树进行搜索,发现62<73<88, 所以有(88)->leftChild=(73)。
以此类推,要做的事情就是不断从集合中取值,然后对二叉排序树进行查找,找到合适的插入点,然后将相应的节点进行插入,


二叉排序树构建代码:

 public static void main(String[] args) throws Exception{
    
      Node n = new Node();
      Integer[] l = new Integer[]{2,1,4,5,3,10,18,9,8,7,6,85,25};
      crateTree(n, Arrays.asList(l));

 }
 private static   void crateTree(Node n, List<Integer> list){
        if(list == null || list.size() == 0)
            return;
        n.setData(list.get(0));
        if(list.size() <= 1)
            return;
        for(int i = 1 ; i < list.size() ; i++){
            crateTree(n,list.get(i));
        }

    }
    private static   void crateTree(Node n, Integer d){
        if(n.getData() == null)
            return;
        if(n.getData() >= d && n.getLn() == null){
            Node ln = new Node();
            ln.setData(d);
            n.setLn(ln);
        }else if(n.getData() < d && n.getRn() == null){
            Node rn = new Node();
            rn.setData(d);
            n.setRn(rn);
        }else if(n.getData() >= d && n.getLn() != null){
            crateTree(n.getLn(),d);
        }else if(n.getData() < d && n.getRn() != null){
            crateTree(n.getRn(),d);
        }
        return;
    }


    static class Node{
        private Node ln;
        private Node rn;
        private Integer data;

        public Integer getData() {
            return data;
        }

        public void setData(Integer data) {
            this.data = data;
        }

        public Node getLn() {
            return ln;
        }

        public void setLn(Node ln) {
            this.ln = ln;
        }

        public Node getRn() {
            return rn;
        }

        public void setRn(Node rn) {
            this.rn = rn;
        }
    }

三、二叉排序树的删除

3.1 删除结点为叶子结点

  删除的结点没有左子树也没有右子树,也就是删除的结点为叶子结点。这种情况下我们有可以细分为两类:

  a、一种是该叶子结点就是二叉排序树的根节点,也就是二叉排序树中只有一个节点的情况。只需要将root指针置为空即可。
  b、第二种情况是有删除的叶子节点有父节点,直接将父节点连接该删除节点的指针置空即可。
 示意图如下所示:


 

3.2 删除的节点只有左子树的情况

  该情况也可以细分为两类:

 a、一种是该删除的结点没有父节点,也就删除的节点为根节点,我们需要将根节点的root指针指向即将删除结点的左孩子,然      后将删除结点的lchild置空即可。
b、如果该结点有父节点,那么将父节点相应的孩子指针指向删除节点的左孩子,然后将删除节点的lchild置空。
   示意图如下所示:

3.3 删除的节点只有右子树的情况

该情况也可以细分为两类:

a、一种是该删除的结点没有父节点,也就删除的节点为根节点,我们需要将根节点的root指针指向即将删除结点的右孩子,然后将删除结点的rightChild置空即可。
b、如果该结点有父节点,那么将父节点相应的孩子指针指向删除节点的右孩子,然后将删除节点的rightChild置空。
 示意图如下所示:


 

3.4 删除的节点既有左子树也有右子树的情况

这种情况会稍微复杂一些,我们采用覆盖,再删除的方式进行解决。也就是曲线解决。直接将有左子树也有右子树的结点干掉似乎不是很好实现,因为这样会破坏二叉排序树的结果。我们可以间接的去做。可以分为下方的两步。

     第一步:查找删除结点右子树中最小的那个值,也就是右子树中位于最左方的那个结点。然后将这个结点的值的父节点记录下来。并且将该节点的值赋给我们要删除的结点。也就是覆盖。
     第二步:然后将右子树中最小的那个结点(左子树中最大的那个节点也可以)进行删除,该节点肯定符合上述三种情况的某一种情况,所以可以使用上述的方法进行删除。
这样一来我们就间接的删除了既有左子树也有右子树的结点。

具体示意图如下所示:

删除代码如下:

public class TwoForkTreeSort {


    public static void main(String[] args) throws Exception{
        Node n = new Node();
        Integer[] l = new Integer[]{2,1,4,5,3,10,18,9,8,7,6,85,25};
        crateTree(n, Arrays.asList(l));
         delete(n,10);
   }
   private static void delete(Node n,Integer d){
        List<Node> node = findNode(null,n, d);
        System.out.println(node.size());
        if(node.size() == 1){
            Node now = node.get(0);
            if(now.getLn() == null || now.getRn() == null){
                n = now;
            }else{
                //查找右子树 最左边的节点
                Integer data = findL(now,now.getRn());
                //上面判断过了,l 不为空
                now.setData(data);
            }
        }else{
            Node pn = node.get(0);
            Node now = node.get(1);
            if(now.getLn() == null && now.getRn() == null ){
                if(pn.getLn() != null && pn.getLn().getData() == now.getData())
                    pn.setLn(null);
                else if(pn.getRn() != null && pn.getRn().getData() == now.getData())
                    pn.setRn(null);
            }else if(now.getLn() != null && now.getRn() == null){
                if(pn.getLn() != null && pn.getLn().getData() == now.getData()) {
                    pn.setLn(now.getLn());
                } else if(pn.getRn() != null && pn.getRn().getData() == now.getData()){
                    pn.setRn(now.getLn());
                }
            }else if(now.getLn() == null && now.getRn() != null){
                if(pn.getLn() != null && pn.getLn().getData() == now.getData()) {
                    pn.setLn(now.getRn());
                } else if(pn.getRn() != null && pn.getRn().getData() == now.getData()){
                    pn.setRn(now.getRn());
                }
            }else if(now.getLn() != null && now.getRn() != null){
                Integer data = findL(now,now.getRn());
                //上面判断过了,l 不为空
                now.setData(data);
            }
        }
    }
    //最左边的节点
    private static Integer findL(Node pn,Node n){
        if(n == null)
            return null;
        if(n.getLn() == null) {
            Integer data = n.getData();
            //找到删除节点右子树最小一个节点,代替删除的节点,并且将 代替的 节点的 的右子树(代替节点最小说明没有左子树) 变成 替代节点的父节点的孩子节点
            if(n.getRn() != null){
                if(pn.getLn() != null && pn.getLn().getData() == n.getData()){
                    pn.setLn(n.getRn());
                }
                if(pn.getRn() != null && pn.getRn().getData() == n.getData()){
                    pn.setRn(n.getRn());
                }
            }
            return data;
        }
        else
            return  findL(n,n.getLn());
    }

    /**
     * 查找节点,数组 0: 父节点,数组 1:当前节点
     * @return
     */
    private static List<Node> findNode(Node pn,Node n,Integer d){

        if(n == null)
            return new ArrayList<>();
        if(n.getData() == d){
            List<Node> l = new ArrayList<>();
            if(pn != null)
                l.add(pn);
            l.add(n);
            return l;
        }else if(n.getData() > d){
            return findNode(n, n.getLn(), d);
        }else{
            return findNode(n,n.getRn(),d);
        }

    }
}

四、二叉排序的插入

代码:

public class TwoForkTreeSort {


    public static void main(String[] args) throws Exception{
        Node n = new Node();
        Integer[] l = new Integer[]{2,1,4,5,3,10,18,9,8,7,6,85,25};
        crateTree(n, Arrays.asList(l));
        insert(n,24)
    }
    private static void insert(Node n,Integer d){
        crateTree(n, d);
    }
    private static   void crateTree(Node n, Integer d){
        if(n.getData() == null)
            return;
        if(n.getData() >= d && n.getLn() == null){
            Node ln = new Node();
            ln.setData(d);
            n.setLn(ln);
        }else if(n.getData() < d && n.getRn() == null){
            Node rn = new Node();
            rn.setData(d);
            n.setRn(rn);
        }else if(n.getData() >= d && n.getLn() != null){
            crateTree(n.getLn(),d);
        }else if(n.getData() < d && n.getRn() != null){
            crateTree(n.getRn(),d);
        }
        return;
    }

}

五、二叉排序树遍历输出(结果是有序的)

代码:

public class TwoForkTreeSort {


    public static void main(String[] args) throws Exception{
        Node n = new Node();
        Integer[] l = new Integer[]{2,1,4,5,3,10,18,9,8,7,6,85,25};
        crateTree(n, Arrays.asList(l));
        List<Integer> list = new ArrayList<>();
        pri(n,list);
    }
    /**
     * 树输出(有序)
     * @return
     */
    private static List<Integer> pri(Node n,List<Integer> l){
        if(l == null)
            l = new ArrayList<>();
        if(n == null)
            return l;
        if(n.getData() != null && n.getLn() == null && n.getRn() == null){
            l.add(n.getData());
            return l;
        }else if(n.getData() != null && n.getLn() != null && n.getRn() == null){
            pri(n.getLn(),l);
            l.add(n.getData());
            return l;
        }else if(n.getData() != null && n.getLn() == null && n.getRn() != null){
            l.add(n.getData());
            pri(n.getRn(),l);

            return l;
        }else if(n.getData() != null && n.getLn() != null && n.getRn() != null){
            pri(n.getLn(),l);
            l.add(n.getData());
            pri(n.getRn(),l);

            return l;
        }
        return l;

    }

}

六、二叉排序树查找

代码:

public class TwoForkTreeSort {


    public static void main(String[] args) throws Exception{
        Node n = new Node();
        Integer[] l = new Integer[]{2,1,4,5,3,10,18,9,8,7,6,85,25};
        crateTree(n, Arrays.asList(l));
        Integer r = search(n,18);
        System.out.println(r);
 
    }
    public static Integer search(Node n,Integer d){
        if(n == null)
            return null;
        if(n.getData() != null && n.getData().intValue() == d.intValue()){
            return n.getData();
        }else if(n.getData() != null && n.getData().intValue() > d.intValue() ){
            return  search(n.getLn(),d);
        }else if(n.getData() != null && n.getData().intValue() < d.intValue()){
            return  search(n.getRn(),d);
        }
        return null;

    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值