当前位置: 首页 > news >正文

SkipList跳表

参考链接:
Redis内部数据结构详解(6)——skiplist - 铁蕾的个人博客

题目:
1206. 设计跳表 - 力扣(LeetCode)

时间复杂度:
增删查均为O(logN)

模板:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;template<class V>
struct skipListNode
{V _val;vector<skipListNode<V>*> _nextV;skipListNode(const V&v, int level):_val(v), _nextV(level,nullptr){}
};template<class V>
class skipList
{
private:typedef skipListNode<V> Node;
public:skipList():_head(new Node(V(), 1)){srand(time(0));}pair<bool, Node*> search(const V& v){Node* cur = _head;int level = _head->_nextV.size() - 1;while (level >= 0){//1、下一个节点存在且小于目标值if (cur->_nextV[level] && cur->_nextV[level]->_val < v)cur = cur->_nextV[level];//2、下一个节点不存在或者存在且大于目标值else if (cur->_nextV[level] == nullptr || cur->_nextV[level]->_val > v)--level;//3、下一个节点目标节点elsereturn make_pair<bool, Node*>(true, cur->_nextV[level]);}return make_pair<bool, Node*>(false, nullptr);}//查找位置每一层前一个节点指针vector<Node*> FindPrevNode(const V& v){Node* cur = _head;int level = _head->_nextV.size() - 1;vector<Node*> prevV(level + 1, _head);while (level >= 0){if (cur->_nextV[level] && cur->_nextV[level]->_val < v)cur = cur->_nextV[level];else if (cur->_nextV[level] == nullptr || cur->_nextV[level]->_val >= v){prevV[level] = cur;--level;}}return prevV;}void add(const V& v){vector<Node*> prevV = FindPrevNode(v);int n = RandomLevel();Node* newNode = new Node(v, n);if (n > _head->_nextV.size()) {_head->_nextV.resize(n, nullptr);prevV.resize(n, _head);}for (size_t i = 0; i < n; ++i){newNode->_nextV[i] = prevV[i]->_nextV[i];prevV[i]->_nextV[i] = newNode;}}bool erase(const V& v){vector<Node*> prevV = FindPrevNode(v);//没有就删除失败if (prevV[0]->_nextV[0] == nullptr || prevV[0]->_nextV[0]->_val != v)return false;else{Node* del = prevV[0]->_nextV[0];for (int i = 0; i < del->_nextV.size(); i++){prevV[i]->_nextV[i] = del->_nextV[i];}delete del;//如果删除节点是层数最高的,把头节点降下一层int i = _head->_nextV.size() - 1;while (i >= 0){if (_head->_nextV[i] == nullptr)--i;elsebreak;}_head->_nextV.resize(i + 1);return true;}}int RandomLevel(){size_t level = 1;// rand() ->[0, RAND_MAX]之间while (rand() <= RAND_MAX * _p && level < _maxLevel){++level;}return level;}void Print(){Node* cur = _head;while (cur){printf("%d\n", cur->_val);// 打印每个每个cur节点for (auto e : cur->_nextV){printf("%2s", "↓");}printf("\n");cur = cur->_nextV[0];}}private:Node* _head; //头节点double _p = 0.25; //当前层数新增一层的概率因子size_t  _maxLevel = 32; //最大层数
};int main()
{skipList<int> skiplist;skiplist.add(2);skiplist.add(1);skiplist.add(3);skiplist.add(7);skiplist.add(4);skiplist.add(8);skiplist.add(2);skiplist.Print();return 0;
}

http://www.dtcms.com/a/487586.html

相关文章:

  • BEVFUSION解读(八)
  • 网站的icp备案怎么在百度做网站
  • SAP MM 自制=>外协加工
  • Spring Boot临时解决循环依赖注入问题
  • 数字身份安全在重点行业的应用与机遇:现状复盘、需求洞察与趋势展望
  • 网站三元素怎么做网站流量建设
  • wordpress单本小说站wordpress如何做页面模板下载
  • seo在网站建设中的作用网站开发怎么使用维语
  • 个人创业:如何找到适合自己且值得投入的小项目?
  • Go 语言开发工具
  • Golang并发编程篇001_并发编程相关概念解释
  • 营销型网站建设易网拓网站要求wordpress
  • QIIME 2 2025.7:微生物数据分析再升级!
  • 高斯分布的贝叶斯参数估计(Bayesian estimation)
  • 招聘网站建设销售wordpress 注册链接
  • 公众号兼职网站开发设计素材网站花瓣
  • 操作系统3.2 虚拟内存管理
  • 惠州市做网站的公司博客类网站怎么做
  • SpringBoot民宿管理系统l2548(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
  • 手机网站建设哪里好wordpress 访问地址修改密码
  • 昆明汽车建站网站模板视频剪辑培训比较有名的学校
  • ollama记录
  • 【数据结构01课_绪论】
  • chage -d 0 强制密码修改的完整流程
  • 大学网站建设宣传方案聊城 网站建设
  • 网站正在建设中 html 模板遵义建站
  • 网站设计的基本方法有网站源码怎么做网站
  • iOS混淆与IPA加密实战指南,苹果软件加固、防反编译与无源码混淆的工程实践
  • SpringCloud 学习之环境工程搭建 - 1
  • 大模型-AIGC应用:基于RAG构建个人知识库