int GetHeight(int max_height, int prob) {
int level = 1;
while (random()< prob && level < max_hight) {
level++;
}
return level;
}
struct SkipNode {
int key;
std::string value;
std::vector<SkipNode*> forward;
SkipList(int k, std::string& v, int level): key(), value() {
for (int i = 0; i < level, i ++) {
forward.push_back(nullptr);
}
}
};
SkipList::find(int key) {
SkipNode* cur = head;
for (int i = max_level - 1; i >= 0; i--) {
while (cur->forward[i] != nil && cur->forward[i]->key < key) {
cur = cur->forward[i];
}
if (cur->forward[i] != nil && cur->forward[i]->key == key) {
return cur->forward[i];
}
}
return nullptr;
}
Skip List 实现思路
最新推荐文章于 2021-11-04 17:58:11 发布