跳转表(skip lists)
来源:互联网 发布:mac win10截屏快捷键 编辑:程序博客网 时间:2024/04/30 05:57
跳转表(skip lists)
跳转表是动态平衡数据结构( balanced dynamic search structure )的一种, 动态平衡数据结构还包括AVL树,2-3-4数,B数,红黑树,Treaps等.
跳转表由William Pugh在1989年创造,相对其他动态平衡数据结构,它具有容易编码实现的优点.
跳转表的一次操作(包括某个元素的插入,删除和查找)绝大多数情况下能够在O(lgn)的期望时间内执行完毕,而且这个概率是相当高的.
跳转表是在有序链表的基础上发展起来的(在本人实现中采用单链表实现,使用双链表也可以),它的想法来源于高速地铁与慢速地铁,高速地铁所到达的站与站之间的距离较远,而慢速地铁的距离较近,我们可以乘坐高速地铁到达离目的地最近的车站再转乘慢速地铁达到目的地.如下图所示,我们要到达目的地59,先乘坐高速地铁达到42,再转乘慢速地铁达到59.
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <malloc.h>
- typedef struct SkipNode
- {
- int key;
- struct SkipNode* next[1];
- }SkipNode;
- #define MaxNumOfLevels 16
- #define MaxLevel (MaxNumOfLevels-1)
- #define newSkipNode(n) (SkipNode*)malloc(sizeof(SkipNode)+n*sizeof(SkipNode*))
- typedef struct SkipList
- {
- int level;
- SkipNode* header;
- }SkipList;
- #define true 1
- #define false 0
- SkipNode *NILnode;
- void init()
- {
- NILnode = newSkipNode(0);
- NILnode->key = 0x7fffffff;
- }
- SkipList* newSkipList()
- {
- SkipList* list;
- list = (SkipList*)malloc(sizeof(SkipList));
- list->level=0;
- // it's convenient to add a head node in the Skip List
- list->header = newSkipNode(MaxLevel);
- int i;
- for(i=0; i<MaxNumOfLevels; i++)
- list->header->next[i] = NILnode;
- return list;
- }
- void freeSkipList(SkipList** plist)
- {
- SkipNode* nd = (*plist)->header;
- SkipNode* p;
- // free each node in the bottom List including head node
- while (nd!=NILnode)
- {
- p = nd;
- nd = nd->next[0];
- free(p);
- }
- free(*plist);
- plist = NULL;
- }
- int randLevel()
- {
- int randnum = rand(); // 32bit data
- int i;
- for (i=0; i<MaxLevel; i++)
- {
- // chech whether bit 2*i and 2*i+1 is both zero
- if (randnum==0 || randnum&3!=0)
- break;
- randnum >>= 2;
- }
- return i;
- }
- int insertSkipList(SkipList* list, int key)
- {
- SkipNode* nd = list->header;
- SkipNode* update[MaxNumOfLevels];
- int level = list->level;
- while (level >= 0)
- {
- // found each pre node in each level of
- while (nd->next[level]->key < key)
- nd = nd->next[level];
- update[level] = nd;
- level--;
- }
- if (nd->next[0]->key == key)
- return false;
- int nl = randLevel();
- level = list->level;
- if (level < nl)
- {
- while (++level <= nl)
- update[level] = list->header;
- list->level = nl;
- }
- nd = newSkipNode(nl);
- nd->key = key;
- int i;
- for(i = nl; i>=0; i--)
- {
- SkipNode* tmp = update[i]->next[i];
- update[i]->next[i] = nd;
- nd->next[i] = tmp;
- }
- return true;
- }
- int deleteSkipList(SkipList* list, int key)
- {
- SkipNode* nd = list->header;
- SkipNode* update[MaxNumOfLevels];
- int level = list->level;
- int ndlevel = -1;
- printf(">>Delete %d: ",key);
- while (level >= 0)
- {
- while (nd->next[level]->key < key)
- nd = nd->next[level];
- printf("%d ",nd->key);
- // get the level of the node to be deleted
- if (ndlevel == -1 && nd->next[level]->key == key)
- ndlevel = level;
- update[level] = nd;
- level--;
- }
- nd = nd->next[0];
- // if the node to be deleted is not in the Skip List
- if (nd->key != key)
- return false;
- int i;
- // modify all nodes in update list from the level
- // of the node to be deleted
- for (i=ndlevel; i>=0; i--)
- {
- update[i]->next[i] = nd->next[i];
- if (list->header->next[i] == NILnode)
- list->level--;
- }
- free(nd);
- return true;
- }
- int searchSkipList(SkipList* list, int key)
- {
- printf(">>Search %d: ",key);
- SkipNode* nd = list->header;
- int level = list->level;
- while (level >= 0)
- {
- while (nd->next[level]->key <= key)
- nd = nd->next[level];
- printf("%d ",nd->key);
- // have found the node of correct key
- if (nd->key == key)
- return true;
- level--;
- }
- return false;
- }
- void outputSkipList(SkipList* list)
- {
- int level = list->level;
- printf(">>%d\n", level);
- while (level >= 0)
- {
- SkipNode* nd = list->header->next[level];
- while (nd != NILnode)
- {
- printf("%d ",nd->key);
- nd = nd->next[level];
- }
- printf("\n");
- level--;
- }
- }
- int main()
- {
- const int n = 20;
- int a[] = {53, 13, 4, 9, 31, 26, 16, 19, 62, 64, 41, 43, 47, 58, 52, 83, 89, 73, 79, 59};
- init();
- SkipList *list = newSkipList();
- //srand(time(0));
- int i;
- for(i=0; i<n; i++)
- {
- int ok=insertSkipList(list,a[i]);
- if (!ok)
- printf("Duplicated Data: %d\n", a[i]);
- }
- outputSkipList(list);
- // int ok = searchSkipList(list, 43);
- // if (ok)
- // printf("found\n");
- int ok = deleteSkipList(list, 41);
- if (ok)
- printf("deleted\n");
- outputSkipList(list);
- freeSkipList(&list);
- }
参考来源:http://blog.csdn.net/linyunzju/article/details/7619987
- 跳转表 skip lists
- 跳转表(skip lists)
- Skip Lists
- Data Structures (Weiss) Chapter 12: Deterministic Skip Lists (跳跃链表)
- Data Structures (Weiss) Chapter 12: Deterministic Skip Lists (跳跃链表,Comparable)
- Lecture 12 Skip Lists [Pugh 1989]
- 基于跳表指针(skip points)的倒排记录表(postings lists)合并算法
- Skip Lists: A Probabilistic Alternative to Balanced Trees
- Skip List(跳跃表)
- 跳跃表Skip List
- 跳跃表 Skip List
- 跳跃表(Skip List)
- 跳跃表(Skip List)
- Skip List跳跃链表
- skip list跳跃表实现
- LevelDB中的Skip List(跳跃表)
- Skip List跳跃表代码实现
- 跳跃表(skip list) 的实现
- android面试题:通过Intent传递一些二进制数据的方法有哪些?
- 小心除数不能为0奥
- Iphone开发(一)基础知识
- 一些链接
- java中I/O中的File类
- 跳转表(skip lists)
- 信息编码:基本整型
- Datasets.IsAvailableDatasetName
- Android学习笔记之Bitmap位图触摸点移动
- 基础学习笔记之opencv(5):实现鼠标选定矩形框
- C++ redis client安装全过程
- Android AIDL使用详解
- iBatis开发环境搭建和第一个程序
- Zigbee 之SerialApp HalDriverInit()