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

26、跳表

在C++标准库中,std::mapstd::set 是使用红黑树作为底层数据结构的容器。
红黑树是一种自平衡二叉搜索树,能够保证插入、删除和查找操作的时间复杂度为O(log n)。

以下是一些使用红黑树的C++标准库容器:

  1. std::map:一种关联容器,存储键值对,并根据键进行排序。
  2. std::set:一种关联容器,存储唯一的键,并根据键进行排序。

示例代码:

#include <iostream>
#include <map>
#include <set>int main() {// 使用std::mapstd::map<int, std::string> myMap;myMap[1] = "one";myMap[2] = "two";myMap[3] = "three";for (const auto& pair : myMap) {std::cout << pair.first << ": " << pair.second << std::endl;}// 使用std::setstd::set<int> mySet;mySet.insert(1);mySet.insert(2);mySet.insert(3);for (const auto& element : mySet) {std::cout << element << std::endl;}return 0;
}

在这个示例中,std::mapstd::set 都使用红黑树作为底层数据结构来存储和管理元素。


以下是一个简单的跳表实现示例:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>class SkipListNode {
public:int value;std::vector<SkipListNode*> forward;SkipListNode(int val, int level) : value(val), forward(level, nullptr) {}
};class SkipList {
private:int maxLevel;float probability;SkipListNode* header;int randomLevel() {int level = 1;while (((float)std::rand() / RAND_MAX) < probability && level < maxLevel) {level++;}return level;}public:SkipList(int maxLevel, float probability) : maxLevel(maxLevel), probability(probability) {header = new SkipListNode(-1, maxLevel);}void insert(int value) {std::vector<SkipListNode*> update(maxLevel, nullptr);SkipListNode* current = header;for (int i = maxLevel - 1; i >= 0; i--) {while (current->forward[i] != nullptr && current->forward[i]->value < value) {current = current->forward[i];}update[i] = current;}int level = randomLevel();SkipListNode* newNode = new SkipListNode(value, level);for (int i = 0; i < level; i++) {newNode->forward[i] = update[i]->forward[i];update[i]->forward[i] = newNode;}}bool search(int value) {SkipListNode* current = header;for (int i = maxLevel - 1; i >= 0; i--) {while (current->forward[i] != nullptr && current->forward[i]->value < value) {current = current->forward[i];}}current = current->forward[0];return current != nullptr && current->value == value;}void display() {for (int i = 0; i < maxLevel; i++) {SkipListNode* node = header->forward[i];std::cout << "Level " << i + 1 << ": ";while (node != nullptr) {std::cout << node->value << " ";node = node->forward[i];}std::cout << std::endl;}}
};int main() {std::srand(std::time(0));SkipList list(4, 0.5);list.insert(3);list.insert(6);list.insert(7);list.insert(9);list.insert(12);list.insert(19);list.insert(17);list.insert(26);list.insert(21);list.insert(25);list.display();std::cout << "Search for 19: " << (list.search(19) ? "Found" : "Not Found") << std::endl;std::cout << "Search for 15: " << (list.search(15) ? "Found" : "Not Found") << std::endl;return 0;
}

这个示例实现了一个简单的跳表,支持插入和搜索操作。可以根据需要扩展这个实现。

http://www.xdnf.cn/news/12786.html

相关文章:

  • SEO长尾词优化实战策略
  • 【大模型原理与技术-毛玉仁】第五章 模型编辑
  • leetcode刷题日记——二叉搜索树中第 K 小的元素
  • MIT 6.S081 Lab 11 networking
  • RD-Agent-Quant:一个以数据为中心的因素与模型联合优化的多智能体框架
  • CANoe trace里面显示的Time 具体是什么意思
  • Python抽象基类实战:构建广告轮播框架ADAM的核心逻辑
  • Python绘制三十六计
  • OGG 23ai for DAA 部署与补丁升级
  • 雪花ID问题诊断与解决方案
  • C++调试(肆):WinDBG分析Dump文件汇总
  • stm32内存踩踏一例
  • 高斯消元法及其扩展
  • 【2025年软考中级】第二章2.3 编译程序基本原理
  • 当数据包从上层移动到下层时,OSI 模型中会发生什么?
  • Go爬虫开发学习记录
  • 从内存角度透视现代C++关键特性
  • freeRTOS 互斥量优先级继承机制函数实现xQueueGenericReceive()
  • C++ 信息学奥赛总复习题答案解析(第一章)
  • 电脑商城--用户注册登录
  • 步进电机调试记录(先让我的步进电机转起来)
  • 【Java学习笔记】String类(重点)
  • 沉金电路板的黑盘缺陷挑战与解决方案——高密度互连设计的关键考量
  • Jina AI 开源 node-DeepResearch
  • [面试精选] 0094. 二叉树的中序遍历
  • 【单源最短路经】Dijkstra 算法(朴素版和堆优化版)、Bellman-Ford 算法、spfa 算法 及 负环判断
  • win10环境配置-openpose pytorch版本
  • 网络协议通俗易懂详解指南
  • MyBatis 获取插入数据后的自增 ID 值
  • GoC指令测试卷 A