基于二叉查找树的符号表

基于二叉查找树的符号表

public class BST <Key extends Comparable<Key>,Value>{
    private Node root;  //根节点
    private class Node{
        private Key key;
        private Value value;
        private Node left;  //左子节点,比自己小的
        private Node right;  //右子节点,比自己大的
        private int  n;  //以自己作为根节点,其下所有的子节点数目

        public Node(Key key, Value value, int n) {
            this.key = key;
            this.value = value;
            this.n = n;
        }
    }

    public Value get(Key key){
        return get(root, key);
    }

    private Value get(Node x,Key key){
        if (x == null)
            return null;
        int res = key.compareTo(x.key);
        if(res==0){
            return x.value;
        }
        else if(res<0)
            return get(x.left,key);     //比自己小,往左边遍历
        else
            return get(x.right,key);   //比自己大,往右边遍历
    }

    public void put(Key key,Value value){
            root=put(root,key,value);
    }

    private Node put(Node x,Key key,Value value){
        if(x==null)
            return new Node(key,value,1);
        int res = key.compareTo(x.key);
        if(res==0){
            x.value=value;
        }
        else if(res<0)
             x.left=put(x.left,key,value);
        else
            x.right=put(x.right,key,value);

        x.n=size(x.left)+size(x.right)+1;
        return x;
    }


    public void delete(Key key){   //删除指定键的节点
         root = delete(this.root, key);
    }

    private Node delete(Node x,Key key){
            if(x==null)
                return null;
            int t = key.compareTo(x.key);
            if (t<0)
                x.left=delete(x.left,key);

            else if(t>0)
                x.right=delete(x.right,key);
            else {
                if(x.left==null)
                    return x.right;
                if(x.right==null)
                    return x.left;
                Node tt= x;
                x=min(tt.right);
                x.right=deleteMin(tt.right);
                x.left=tt.left;
            }
            x.n=size(x.left)+size(x.right)+1;
            return x;
    }

    public void deleteMin(){   //删除最小的节点
        root=deleteMin(root);
    }

    private Node deleteMin(Node x){
        if(x.left==null){
            return x.right;
        }
        x.left=deleteMin(x.left);
        x.n=size(x.left)+size(x.right)+1;
        return x;
    }



    public int size(){
        return size(root);
    }

    private int size(Node x){
        if(x==null)
            return 0;
        else
            return x.n;
    }


    public Key min(){   //获取最小的节点
        return min(root).key;
    }
    private Node min(Node x){   //获取以当前节点为根节点的二叉树的最小节点
        if(x.left==null)
            return x;
        return min(x.left);
    }

    public Key select(int k){
        return select(root,k).key;
    }

    private Node select(Node x,int k){
        //返回排名为k的节点
        if(x==null){
            return null;
        }
        int t =size(x.left);
        if(t>k)
            return select(x.left,k);
        else if(t<k)
            return select(x.right,k-t-1);
        else
            return x;
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值