skipList java 实现

本文详细介绍了跳表(Skip List)的数据结构实现及其核心操作,包括查找、插入和删除元素的过程。通过随机化水平来平衡跳表的性能,提供了完整的Java代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

class SkipList<T> {
    public static void main(String[] var0){

    }

    private static class SkipListNode<T>{
        T val;
        SkipListNode<T> next,down;
        double sorce;

        SkipListNode(){}
        SkipListNode(T val,double sorce){
            this.val = val;
            this.sorce =sorce;
        }
    }

    private static final int MAX_LEVEL = 1<<6;
    private int level = 0;
    private SkipListNode<T> top;
    private Random random = new Random();
    public SkipList(){
        this(4)
    }

    public SkipList(int level){
        this.level=level;
        int i = level;
        SkipListNode<T> temp = null;
        SkipListNode<T> pre = null;
        while (i--!==0){
            temp = new SkipListNode<T>(null,Double.MIN_VALUE);
            temp.down = pre;
            pre = temp;
        }
        top = temp;
    }
    private int getRandomLevel(){
        int lev = 1;
        while (random.nextInt() % 2 == 0)
            lev++;
        return lev > MAX_LEVEL ? MAX_LEVEL : lev;
    }

    public T get(double socre){
        SkipListNode<T> t = top;
        while (t!=null){
            if(t.sorce==socre){
                return t.val;
            }
            if(t.next==null){
                if(t.down!=null){
                    t = t.down;
                    continue;
                }else {
                    return null;
                }
            }
            if(t.next.sorce>socre){
                t = t.down;
            }else {
                t = t.next;
            }
        }
        return null;
    }

    public void put(double socre,T val){
        //1,找到需要插入的位置
        SkipListNode<T> t = top, cur = null;//若cur不为空,表示当前score值的节点存在
        List<SkipListNode<T>> path = new ArrayList<>();//记录每一层当前节点的前驱节点
        while (t != null) {
            if (t.score == score) {
                cur = t;
                break;//表示存在该值的点,表示需要更新该节点
            }
            if (t.next == null) {
                path.add(t);//需要向下查找,先记录该节点
                if (t.down != null) {
                    t = t.down;
                    continue;
                } else {
                    break;
                }
            }
            if (t.next.score > score) {
                path.add(t);//需要向下查找,先记录该节点
                if (t.down == null) {
                    break;
                }
                t = t.down;
            } else
                t = t.next;
        }
        if (cur != null) {
            while (cur != null) {
                cur.val = val;
                cur = cur.down;
            }
        } else {//当前表中不存在score值的节点,需要从下到上插入
            int lev = getRandomLevel();
            if (lev > level) {//需要更新top这一列的节点数量,同时需要在path中增加这些新的首节点
                SkipListNode<T> temp = null;
                SkipListNode<T> prev = top;//前驱节点现在是top了
                while (level++ != lev) {
                    temp = new SkipNode<T>(null, Double.MIN_VALUE);
                    path.add(0, temp);//加到path的首部
                    temp.down = prev;
                    prev = temp;
                }
                top = temp;//头节点
                level = lev;//level长度增加到新的长度
            }
            //从后向前遍历path中的每一个节点,在其后面增加一个新的节点
            SkipListNode<T> downTemp = null, temp = null, prev = null;
//            System.out.println("当前深度为"+level+",当前path长度为"+path.size());
            for (int i = level - 1; i >= level - lev; i--) {
                temp = new SkipNode<T>(val, score);
                prev = path.get(i);
                temp.next = prev.next;
                prev.next = temp;
                temp.down = downTemp;
                downTemp = temp;
            }
        }
    }

    public void delete(double sorce){
        SkipListNode<T> t = top;
        while (t != null) {
            if (t.next == null) {
                t = t.down;
                continue;
            }
            if (t.next.score == score) {
                // 在这里说明找到了该删除的节点
                t.next = t.next.next;
                t = t.down;
                //删除当前节点后,还需要继续查找之后需要删除的节点
                continue;
            }
            if (t.next.score > score)
                t = t.down;
            else
                t = t.next;
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值