前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >跳跃表原理

跳跃表原理

作者头像
王小明_HIT
发布2020-01-14 10:47:32
4960
发布2020-01-14 10:47:32
举报
文章被收录于专栏:程序员奇点程序员奇点

跳跃表

跳表是基于链表的,在链表的基础上加了多层索引结构。

跳表这种特殊的数据结果是有 Willam Pugh 发明的。最早出现在1990 年发表的论文《Skip Lists: A Probabilistic Alternative to Balanced Trees》

论文中有个描述:

代码语言:javascript
复制
Skip lists are a data structure that can be used in place of balanced trees.
Skip lists use probabilistic balancing rather than strictly enforced balancing and as a result the algorithms for insertion and deletion in skip lists are much simpler and significantly faster than equivalent algorithms for balanced trees.

简单的说,跳表是基于概率型的表。

先看个普通有序链表的结构:

如果要查找 23 那么起码需要比较 2次,查找 43 比较 4次,查找 59 比较 6次。有什么办法解决这个问题呢?容易想到二分搜索。

采用分层链表结构,以一些节点作为索引,

比如,提取了 14 34 50 72 作为一层链表,查找 59 的时候,就可以通过比较 14 24 50 59 共5次找到 59 来减少查找次数。

如果加一层,基本上就可以采用类似二分的方式进行查找了

现在看给完整的 快表插入一个新元素的过程:

参考代码:

代码语言:javascript
复制
public class SkipList {
    private static class SkipListNode {
        int data;

        SkipListNode[] next;

        SkipListNode(int d, int level) {
            data = d;
            next = new SkipListNode[level + ];
        }
    }

    private int maxLevel;

    SkipListNode header;

    private static final int INFINITY = Integer.MAX_VALUE;

    SkipList(int maxLevel) {
        this.maxLevel = maxLevel;
        header = new SkipListNode(, maxLevel);
        SkipListNode sentinel = new SkipListNode(INFINITY, maxLevel);
        for (int i = ; i <= maxLevel; i++)
            header.next[i] = sentinel;
    }

    public boolean find(int key) {
        SkipListNode current = header;

        for (int i = maxLevel; i >= ; i--) {
            SkipListNode next = current.next[i];
            while (next.data < key) {
                current = next;
                next = current.next[i];
            }
        }
        current = current.next[];

        if (current.data == key)
            return true;
        else
            return false;
    }

    public void insert(int searchKey, int newValue) {
        SkipListNode[] update = new SkipListNode[maxLevel + ];
        SkipListNode current = header;

        for (int i = maxLevel; i >= ; i--) {
            SkipListNode next = current.next[i];
            while (next.data < searchKey) {
                current = next;
                next = current.next[i];
            }
            update[i] = current;
        }
        current = current.next[];
        if (current.data == searchKey)
            current.data = newValue;
        else {
            int v = generateRandomLevel();
            SkipListNode node = new SkipListNode(newValue, maxLevel);
            for (int i = ; i <= v; i++) {
                node.next[i] = update[i].next[i];
                update[i].next[i] = node;
            }
            update = null;
        }
    }

    private int generateRandomLevel() {
        int newLevel = ;
        while (newLevel < maxLevel && Math.random() < 0.5)
            newLevel++;
        return newLevel;
    }

    public boolean delete(int searchKey) {
        SkipListNode[] update = new SkipListNode[maxLevel + ];
        SkipListNode current = header;
        for (int i = maxLevel; i >= ; i--) {
            SkipListNode next = current.next[i];
            while (next.data < searchKey) {
                current = next;
                next = current.next[i];
            }
            update[i] = current;
        }
        current = current.next[];
        if (current.data == searchKey) {
            for (int i = ; i <= maxLevel; i++) {
                if (update[i].next[i] == current) {
                    update[i].next[i] = current.next[i];
                    current.next[i] = null;
                } else
                    current.next[i] = null;
            }

            return true;
        }

        return false;
    }

    public static void main(String[] args) {

    }
}

关注公众号:【程序员开发社区】

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-01-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 程序员奇点 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档