Bottom: jump list skiplist

Bottom: jump list skiplist

Jump table

The structure of each node of: containing keys and values, there is a list composed of a plurality of node, this node is at a different level node pointer points.

Each time initialization initialization initial node, which is the left-most node, and its height is the maximum height of the jump table, each initial position pointer to null.

Insert element is first determined whether the element is present in the set, from the highest position of the first node to start looking forward pointer moves down key value larger than looking small moves forward. If there is inserted, with a first random function each time a random number of 0 or 1, 1 appears once stopped, the number 0 is the number of layers appear. Then starting from the node corresponding to the initial layer, a large value if the previous node, a Node is generated, so that the initial layer node pointer points to node, node would point to the value of the preceding forward, a small value if the previous node then continues to move downwardly Analyzing until the finish of the first layer, the corresponding node forwards all the pointers have been created.

Just need to find the node of the layer who directed it, the pointer to the previous node node when you can delete it.

java implementation jump table

Hop node table:

class SkipListNode{
        public Integer value;
        //指向下一个node,如果本node是5层,那么这个list大小为5,分别指向1-5层的下一个node
        public ArrayList<SkipListNode> nextNodes;
        public SkipListNode(Integer value) {
            this.value = value;
            nextNodes = new ArrayList<MySkipList.SkipListNode>();
        }
}

Jump table fields and construction method of the list:

class SkipList{
        //最小值,跳表的起始节点,它的高度和跳表中node的最大高度一致
        private SkipListNode head;
        
        //跳表达到的最大高度
        private int maxLevel;
        
        //跳表中元素的个数
        private int size;
        
        //跳表决定高度的概率,以0.5的概率产生0,如果是1就晋升一层
        private static final double PROBABILITY = 0.5;
        
        public SkipList() {
            size = 0;
            maxLevel = 0;
            head = new SkipListNode(null);
            head.nextNodes.add(null);
        }
}

Skip lists add method:

//添加元素
public void add(Integer newValue) {

    //如果不包含这个元素,就插入跳表
    if(!contains(newValue)) {
        //随机确定新的层高
        size++;
        int level = 0;
        while(Math.random() < PROBABILITY) {
            level++;
        }
        while(level > maxLevel) {
            head.nextNodes.add(null);
            maxLevel++;
        }
        //建立node,确定head为查找时的起始node
        SkipListNode newNode = new SkipListNode(newValue);
        SkipListNode current = head;
        //遍历的时候每次要么向前走,要么向下走,按照层数遍历
        //每一层都要建立新node,然后连接前后指针
        do {
            //获得这一层里最后一个大于newValue的node
            current = findNext(newValue, current, level);

            //生成newNode其中一个向前的指针(指向更大的node),每次都从0添加
            //这样最后如果一共有5层,相当于先添加第五层然后第四层。。
            //每个指针指向的就是current的前一个节点
            //连接从node到前
            newNode.nextNodes.add(0, current.nextNodes.get(level));

            //使newNode和它后面的节点产生联系,将后面节点的向前指针连接到newNode
            //连接从node到后(指向更小的node)
            current.nextNodes.set(level, newNode);
        }while(level-- > 0);
    }
}

Find method findNext:

//找到要找的目标值e在这一层level里刚刚大于e的node,current是寻找的起始位置
private SkipListNode findNext(Integer e, SkipListNode current, int level) {
    //找到current节点在当前层的前一个节点
    SkipListNode next = current.nextNodes.get(level);
    while(next != null) {
        Integer value = next.value;
        //如果本节点是8,前一个节点是10,要找的值是9就会出现这种情况
        //此时直接返回,进入下一层
        if(e < value) {
            break;
        }
        //如果本节点是8,前一个节点是10,要找的值是12就会继续向前找
        //更新current和next

        //每次如果前一个节点比要找的值小就前进,否则找下一层
        current = next;
        next = current.nextNodes.get(level);
    }
    return current;
}

contains methods:

public boolean contains(Integer value) {
    SkipListNode node = find(value);
    return node != null && node.value != null && node.value == value;
}

//找到e对应的节点
private SkipListNode find(Integer e) {
    return find(e, head, maxLevel);
}

delete method:

public void delete(Integer deleteValue) {
    if(contains(deleteValue)) {
        SkipListNode deleteNode = find(deleteValue);
        size--;
        int level = maxLevel;
        SkipListNode current = head;
        do {
            //找到刚刚比deleteNode稍大一点的node,然后将该node的各向前指针修正为
            //原来deleteNode向前指针的指向,相当于删除了deleteNode
            current = findNext(deleteNode.value, current, level);
            if(deleteNode.nextNodes.size() > level) {
                current.nextNodes.set(level, deleteNode.nextNodes.get(level));
            }
        }while(level-- > 0);
    }
}

Iterator jump table (through the results must be small to large):

class SkipListIterator implements Iterator<Integer>{

        SkipList list;
        SkipListNode current;
        
        public SkipListIterator(SkipList list) {
            this.list = list;
            this.current = list.getHead();
        }
        
        @Override
        public boolean hasNext() {
            // TODO Auto-generated method stub
            return current.nextNodes.get(0) != null;
        }

        @Override
        public Integer next() {
            // TODO Auto-generated method stub
            current = current.nextNodes.get(0);
            return current.value;
        }
        
}

redis in the jump table

In organizing score redis using jump table, and to store the mapping redis score from member to use a dictionary.

In the promotion redis probability of each layer was 25%, which is a more flattened jump table, the number of nodes on a single layer will be slightly more traversal.

When the weight adjustment element, redis node using the first cut after the manner of adding to.

If the weights are the same comparative value will redis value, the jump table in order redis.

When ranking rank redis computing elements, is a strengthening of the jump table function for each element has its value field rank, jump table when the change will update this value.

Guess you like

Origin www.cnblogs.com/shizhuoping/p/11521860.html
Recommended