SkipList jump table and the realization of the principle of

First, the concept

What is a jump table it?
Let's continue to analyze its definition from Wikipedia:
skip list is a data structure. It allows fast data query an ordered list of successive elements
and its fast query is by maintaining a multi-level list, and each layer is the list, in the list is a subset of the previous layer elements.
At the outset, the search algorithm in most sparse level, until the elements to look for in the middle layer of two adjacent elements. In this case, the algorithm will jump to the next level, just repeat the search until you find the required elements found. Element method can be skipped or deterministic random selection to choose from, the former is more common.
What does that mean?
We know that dichotomy query is dependent on an array of random access can only be applied to an array of structures, and the list based on the query `` dichotomy similar query will become what we call a jump table structure.
The principle is to increase the ordered list additional forward link, increase the randomization is done in the way, so the list can quickly skip find a partial list, hence the name. All operations were randomized to logarithmic time.

Here Insert Picture Description
The bottom is a jump table full amount ordered list, the top can be said to be "fast track" lower

Second, the nature

(1) a structure consisting of many layers;
(2) each layer is an ordered list;
(3) the lowest level (Level 1) that contains all the elements of the chain;
(4) If an element appears in the list of Level i in the list below it will also appear in the Level i;
(5) each node contains two pointers, a pointer to the next element in the same list, a pointer to one of the following elements.

Third, to achieve

(A) initialization

// 构建一个跳表节点属性
private static class SkipListNode<T>{
        T val;&emsp;
        SkipListNode<T> next,down;&emsp;
        double sorce;
 
        SkipListNode(){}
        SkipListNode(T val,double sorce){
            this.val = val;
            this.sorce =sorce;
        }
    }
    // 层数
    private int level = 0;
    // 顶层节点
    private SkipListNode<T> top; 
    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;
    }
复制代码

(B) Find

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;
    }
复制代码

(C) insert

 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;
            }
        }
    }
    private int getRandomLevel(){
        int lev = 1;
        while (random.nextInt() % 2 == 0)
            lev++;
        return  lev;
    }
复制代码

(D) Delete

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;
        }
    }
复制代码

Guess you like

Origin juejin.im/post/5d076eefe51d45773f2e8f52