leecode 1206|跳表的设计

跳表

跳表,一种链表数据结构,其增删改茶的效率能和平衡树相媲美

leecode1206

可以看上面的那个动画,动画效果很贴切。

我简单讲讲它的机制吧,每个节点不单单是一个,测试好几层,然后同一层的节点和统一节点的next 采用单链表产生联系

最核心的东西在于find

这也是为什么单链表的增删改查,花费开销最多的地方。

那它是怎么查的?

我们已经知道了跳表的结构了,最底层肯定是最全的,如果暴力最底层,那就和单链表没什么区别了。

它的这个查恰巧结合了跳表的结构,如果在上面某个层找到了节点x 的pre那么接下来的操作都好办了。

那它是怎么找到节点x 的pre呢?

从最上层,依次找该节点的next 知道找到pre 如果在这一层还没找到(因为最后一个节点的next 为nullptr),那么会在这个节点的下一层继续找同层找next。依此类推,最后肯定能找到的。

cpp 复制代码
class Skiplist {
private:
    static const int MAX_LEVEL = 10;
    struct Node{
        int val;
        std::vector<Node*> ne;
        Node(int _val, int level):val(_val), ne(level, nullptr){};
    };
    int level;
    Node* head;
    float probability;

    int randomLevel(){
        int lvl = 1;
        while((float) std::rand() / RAND_MAX < probability && lvl < MAX_LEVEL){
            lvl++;
        }
        return lvl;
    }

    void find(int t, std::vector<Node*>& ns){
        Node* cur = head;
        for(int i = level - 1; i >= 0; i--){
            while(cur->ne[i] != nullptr && cur->ne[i]->val < t){
                cur = cur->ne[i];
            }
            ns[i] = cur;
        }
    }
public:
    Skiplist() :level(MAX_LEVEL), head(new Node(-1, MAX_LEVEL)), probability(0.5){
        std::srand(std::time(0));

    }
    
    bool search(int t) {
        std::vector<Node*> ns(level, nullptr);
        find(t, ns);
        Node* target = ns[0]->ne[0];
        return target != nullptr && target->val == t;
    }
    
    void add(int t) {
        std::vector<Node*> ns(level, nullptr);
        find(t, ns);

        int nodeLevel = randomLevel();
        Node* newNode = new Node(t, nodeLevel);
        for(int i = 0; i < nodeLevel; i++){
            newNode->ne[i] = ns[i]->ne[i];
            ns[i]->ne[i] = newNode;
        }
    }
    
    bool erase(int t) {
        std::vector<Node*> ns(level, nullptr);
        find(t, ns);
        Node* target = ns[0]->ne[0];
        if(target == nullptr || target->val != t){
            return false;
        }

        for(int i = 0; i < level && ns[i]->ne[i] == target; i++){
            ns[i]->ne[i] = ns[i]->ne[i]->ne[i];
        }
        delete target;
        return true;

    }

    ~Skiplist(){
        Node* cur = head;
        while(cur){
            Node* next = cur->ne[0];
            delete cur;
            cur = next;
        }
    }
};

/**
 * Your Skiplist object will be instantiated and called as such:
 * Skiplist* obj = new Skiplist();
 * bool param_1 = obj->search(target);
 * obj->add(num);
 * bool param_3 = obj->erase(num);
 */

再贴一份,

cpp 复制代码
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>

class Skiplist {
private:
    static const int MAX_LEVEL = 10;
    struct Node {
        int val;
        std::vector<Node*> ne;
        Node(int _val, int level) : val(_val), ne(level, nullptr) {}
    };

    int level;
    Node* head;
    float probability;

    int randomLevel() {
        int lvl = 1;
        while ((float)std::rand() / RAND_MAX < probability && lvl < MAX_LEVEL) {
            lvl++;
        }
        return lvl;
    }

    void find(int t, std::vector<Node*>& ns) {
        Node* cur = head;
        for (int i = level - 1; i >= 0; i--) {
            while (cur->ne[i] != nullptr && cur->ne[i]->val < t) {
                cur = cur->ne[i];
            }
            ns[i] = cur;
        }
    }

public:
    Skiplist() : level(MAX_LEVEL), head(new Node(-1, MAX_LEVEL)), probability(0.5) {
        std::srand(std::time(0));
    }

    bool search(int t) {
        std::vector<Node*> ns(level, nullptr);
        find(t, ns);
        Node* target = ns[0]->ne[0];
        return target != nullptr && target->val == t;
    }

    void add(int t) {
        std::vector<Node*> ns(level, nullptr);
        find(t, ns);
        int nodeLevel = randomLevel();
        Node* newNode = new Node(t, nodeLevel);
        for (int i = 0; i < nodeLevel; i++) {
            newNode->ne[i] = ns[i]->ne[i];
            ns[i]->ne[i] = newNode;
        }
    }

    bool erase(int t) {
        std::vector<Node*> ns(level, nullptr);
        find(t, ns);
        Node* target = ns[0]->ne[0];
        if (target == nullptr || target->val != t) {
            return false;
        }
        for (int i = 0; i < level && ns[i]->ne[i] == target; i++) {
            ns[i]->ne[i] = ns[i]->ne[i]->ne[i];
        }
        delete target;
        return true;
    }

    ~Skiplist() {
        Node* cur = head;
        while (cur) {
            Node* next = cur->ne[0];
            delete cur;
            cur = next;
        }
    }
};

int main() {
    Skiplist skiplist;
    skiplist.add(1);
    skiplist.add(2);
    skiplist.add(3);
    std::cout << skiplist.search(1) << std::endl;  // returns true
    std::cout << skiplist.search(4) << std::endl;  // returns false
    skiplist.add(4);
    std::cout << skiplist.search(4) << std::endl;  // returns true
    std::cout << skiplist.erase(4) << std::endl;   // returns true
    std::cout << skiplist.search(4) << std::endl;  // returns false
    return 0;
}

redis

参考

相关推荐
2601_9491465331 分钟前
C语言语音通知接口接入教程:如何使用C语言直接调用语音预警API
c语言·开发语言
知南x3 小时前
【Ascend C系列课程(高级)】(1) 算子调试+调优
c语言·开发语言
2的n次方_4 小时前
Runtime 执行提交机制:NPU 硬件队列的管理与任务原子化下发
c语言·开发语言
凡人叶枫5 小时前
C++中智能指针详解(Linux实战版)| 彻底解决内存泄漏,新手也能吃透
java·linux·c语言·开发语言·c++·嵌入式开发
凡人叶枫7 小时前
C++中输入、输出和文件操作详解(Linux实战版)| 从基础到项目落地,避坑指南
linux·服务器·c语言·开发语言·c++
傻乐u兔8 小时前
C语言进阶————指针3
c语言·开发语言
CodeSheep程序羊9 小时前
拼多多春节加班工资曝光,没几个敢给这个数的。
java·c语言·开发语言·c++·python·程序人生·职场和发展
I'mChloe9 小时前
PTO-ISA 深度解析:PyPTO 范式生成的底层指令集与 NPU 算子执行的硬件映射
c语言·开发语言
2的n次方_9 小时前
Runtime 内存管理深化:推理批处理下的内存复用与生命周期精细控制
c语言·网络·架构
嵌入小生00710 小时前
标准IO---核心函数接口延续(嵌入式Linux)
c语言·vscode·vim·嵌入式·小白·标准io·函数接口