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

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.lqws.cn/news/214021.html

相关文章:

  • Day15
  • Gartner《How to Create and Maintain a Knowledge Base forHumans and AI》学习报告
  • pycharm中提示C++ compiler not found -- please install a compiler
  • Gradle 7.0 及以上版本集中管理项目依赖项的版本号、插件版本和库坐标
  • 阿里巴巴ROLL:大规模强化学习优化的高效易用解决方案
  • Java-IO流之序列化与反序列化详解
  • 技巧小结:根据寄存器手册写常用外设的驱动程序
  • 室内电子地图制作核心技术解析:从三维建模到动态 POI 管理
  • C++常用的自动化测试库
  • HBuilderX安装(uni-app和小程序开发)
  • 1-2 Linux-虚拟机(2025.6.7学习篇- win版本)
  • QM系列闪测仪的强大功能解析
  • C++:用 libcurl 发送一封带有附件的邮件
  • LangChain4j 学习教程项目
  • 【C++进阶篇】C++11新特性(下篇)
  • 本地主机部署开源企业云盘Seafile并实现外部访问
  • 应用层协议:HTTPS
  • Linux进程控制
  • ZephyrOS 嵌入式开发Black Pill V1.2之Debug调试器
  • JAVA——反射
  • Windows 系统安装 Redis 详细教程
  • nginx日志的一点理解
  • Xxl-job——源码设计思考
  • Kerberos面试内容整理-未来发展趋势
  • 【大模型】大模型RAG(Retrieval-Augmented Generation)面试题合集
  • 解密LSTM(长短期记忆网络):让机器拥有记忆力的魔法网络
  • 【PhysUnits】15.17 比例因子模块 (ratio.rs)
  • 第二部分 方法,还是方法——“信管法则”的四大要点
  • 号外!PLC和安川伺服,通过Profinet转EtherCAT网关同步多个工作站的运动
  • SpiritTools:一款小而精的实用工具箱