数据结构---跳表改造

存储键值对id:value值

存储100万数据对

查询value区间,比如10到100;个数?

查询id区间,比如10到100;个数?

查询某id或某value,排名第几?

在跳表实体加个索引号;

 

SkipList skipList = new SkipList();
        skipList.put(5, 5);
        skipList.put(55, 55);
        skipList.put(45, 45);
        skipList.put(35, 35);
        skipList.put(25, 25);
        skipList.put(15, 15);
        skipList.put(53, 53);
        System.out.println(skipList.countByValueRange(15,53));
        skipList.doPrintln();

 

跳表实体:

public class SkipListEntry<v> {
    public Integer key;
    public v value;
    public SkipListEntry<v> down,up,left,right;
    public static int negative = Integer.MIN_VALUE;
    public static int positive = Integer.MAX_VALUE;
    public java.util.Random flag;
    public int indexCurrent;
    public SkipListEntry(int key,v value){
        this.key = key;
        this.value = value;
        this.down = this.up = this.left = this.right = null;
        this.flag = new java.util.Random();
    }
    public Integer getKey(){
        return key;
    }
    public v getValue(){
        return value;
    }
}

跳表列表:


public class SkipList<v> {
    private SkipListEntry<v> head;
    private SkipListEntry<v> tail;
    public int height;
    public int size;
    public SkipList(){
        this.head = new SkipListEntry(SkipListEntry.negative,SkipListEntry.negative);
        this.head.indexCurrent = 1;
        this.tail = new SkipListEntry(SkipListEntry.positive,SkipListEntry.positive);
        this.tail.indexCurrent = 2;
        head.right = tail;
        tail.left = head;
        size = 0;
        height = 0;
    }
    public SkipListEntry<v> getSkipListEntry(Integer key){
        SkipListEntry<v> p = head;
        while(true){
            while(!p.right.key.equals(this.tail.key)&&p.right.key.compareTo(key)<=0){
                p = p.right;
            }
            if(p.down != null){
                p = p.down;
            }else{
                break;
            }
        }
        return p;
    }
    public v get(Integer key){
        SkipListEntry<v> target = getSkipListEntry(key);
        if(target.getKey().equals(key)){
            return target.getValue();
        }else{
            return null;
        }        
    }
    public void put(Integer key,v value){
        SkipListEntry<v> target = getSkipListEntry(key);
        SkipListEntry<v> newTarget = new SkipListEntry(key,value);
        newTarget.left = target;
        newTarget.right = target.right;
        newTarget.indexCurrent = target.indexCurrent;
        doAddIndex(newTarget);
        target.right.left = newTarget;
        target.right = newTarget;
        int currentLevel = 0;
        while(newTarget.flag.nextDouble()<0.5){
            if(currentLevel>=height){
                SkipListEntry targetSkipListHead = new SkipListEntry(SkipListEntry.negative,SkipListEntry.negative);
                SkipListEntry targetSkipListTail = new SkipListEntry(SkipListEntry.positive,SkipListEntry.positive);
                targetSkipListHead.right = targetSkipListTail;
                targetSkipListHead.down = this.head;
                targetSkipListHead.indexCurrent = this.head.indexCurrent;
                this.head.up = targetSkipListHead;
                targetSkipListTail.left = targetSkipListHead;
                targetSkipListTail.down = this.tail;
                targetSkipListTail.indexCurrent = this.tail.indexCurrent;
                this.tail.up = targetSkipListTail;
                this.head = targetSkipListHead;
                this.tail = targetSkipListTail;
            }
            while(target!=this.head&&target.up==null){
                target = target.left;
            }
            target = target.up;
            SkipListEntry skipListEntry = new SkipListEntry(key,value);
            skipListEntry.left = target;
            skipListEntry.right = target.right;
            skipListEntry.down = newTarget;
            skipListEntry.indexCurrent = newTarget.indexCurrent;
            newTarget.up = skipListEntry;
            target.right.left = skipListEntry;
            target.right = skipListEntry;        
            currentLevel++;
        }
        size++;
    }
    public void doPrintln(){
        SkipListEntry skipListEntry = this.head;
        while(skipListEntry.down!=null){
            skipListEntry = skipListEntry.down;
        }
        while(skipListEntry.right!=null){
            System.out.println(skipListEntry.key+":"+skipListEntry.value + ":" + skipListEntry.indexCurrent);
            skipListEntry = skipListEntry.right;
        }
        System.out.println(skipListEntry.key+":"+skipListEntry.value + ":" + skipListEntry.indexCurrent);
    }
    public void doAddIndex(SkipListEntry<v> target){
        SkipListEntry<v> temp = target;
        while(temp!=null){
            SkipListEntry<v> tempUP = temp;
            ++temp.indexCurrent;
            while(tempUP!=null){
                tempUP.indexCurrent = temp.indexCurrent;
                tempUP=tempUP.up;
            }
            temp = temp.right;
        }
    }
    public SkipListEntry getSkipListEntryByValue(Integer value){
        SkipListEntry current = head;
        while(true){
            while(current.right.key!=this.tail.key && ((Integer) current.right.value).compareTo(value)<=0){
                current = current.right;
            }
            if(current.down!=null){
                current = current.down;
            }else{
                break;
            }            
        }
        return current;
    }
    public int countByValueRange(int startValue,int endValue){
        SkipListEntry skipListEntryStart = getSkipListEntry(startValue);
        SkipListEntry skipListEntryEnd = getSkipListEntry(endValue);
        return skipListEntryEnd.indexCurrent - skipListEntryStart.indexCurrent;
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值