SkipList
代码:
//skiplist: A probabilistic alternative balanced tree
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <string.h>
#define MaxLevel 7
struct SkipNode
{
int value;
struct SkipNode** forward;
};
struct SkipList
{
int level;
int size;
struct SkipNode *head;
};
struct SkipList* init(struct SkipList* List)
{
struct SkipNode *Head = (struct SkipNode*)malloc(sizeof(struct SkipNode));
List->head = Head;
Head->value = INT_MAX;
Head->forward = (struct SkipNode**)malloc(
sizeof(struct SkipNode*) * (MaxLevel+1));
int i = 0;
for( i = 0; i<= MaxLevel; i++)
Head->forward[i] = List->head;
List->level = 1;
List->size = 0;
return List;
}
static int randLevel()
{
int level = 1;
while( (rand()%10) <5 && level < MaxLevel)
level++;
return level;
}
void sInsert(struct SkipList* List, int key)
{
struct SkipNode *update[MaxLevel+1];
struct SkipNode* p = List->head;
int i;
for( i = List->level; i>= 1 ; i--)
{
while( p->forward[i]->value < key)
p = p->forward[i];
update[i] = p;
} // 找到各层不大于 key 的指针
p = p->forward[1];
if( key == p->value)
return;
int level = randLevel();
// printf("%d ", level);
if( level > List->level)
{
for( i = List->level+1; i <= level; i++)
update[i] = List->head;
List->level = level;
}
++List->size;
struct SkipNode* x= (struct SkipNode* )malloc(sizeof(struct SkipNode));
x->value = key;
x->forward = (struct SkipNode**)malloc(sizeof(struct SkipNode*) * (level+1));
for(i = 1; i<= level; i++)
{
x->forward[i] = update[i]->forward[i];
update[i]->forward[i] = x;
}
}
struct SkipNode* sFind(struct SkipList* List, int key)
{
struct SkipNode* p = List->head;
int i;
for( i = List->level ; i>= 1; i--)
{
while(p->forward[i]->value < key)
p = p->forward[i];
}
if( p->forward[1]->value == key)
return p->forward[1];
else
return NULL;
}
void sDelete(struct SkipList* List, int key)
{
struct SkipNode* update[MaxLevel+1];
struct SkipNode* p = List->head;
int i;
for(i = List->level; i>=1; i--)
{
while( p->forward[i]->value < key)
p= p->forward[i];
update[i] = p;
}
p = p->forward[1];
if( p->value != key)
return ;
else
{
for(i = 1; i<= List->level; i++)
{
if(update[i]->forward[i] != p)
break;
update[i]->forward[i] = p->forward[i];
}
free(p->forward);
free(p);
--List->size;
while(List->level >1 && (List->head->forward[List->level] == List->head))
--List->level;
}
}
//for test
int main(int argc, char** argv)
{
struct SkipList List;
init(&List);
int array[] = {3,6,9,2,11,1,4};
int i;
for(i =0; i < 7; i++)
sInsert(&List, array[i]);
sDelete(&List, 4);
printf("%d\n", List.size);
struct SkipNode *q = sFind(&List, 11);
printf("%d\n", q->value);
printf("Hello World\n");
return 0;
}
//skiplist: A probabilistic alternative balanced tree
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <string.h>
#define MaxLevel 7
struct SkipNode
{
int value;
struct SkipNode** forward;
};
struct SkipList
{
int level;
int size;
struct SkipNode *head;
};
struct SkipList* init(struct SkipList* List)
{
struct SkipNode *Head = (struct SkipNode*)malloc(sizeof(struct SkipNode));
List->head = Head;
Head->value = INT_MAX;
Head->forward = (struct SkipNode**)malloc(
sizeof(struct SkipNode*) * (MaxLevel+1));
int i = 0;
for( i = 0; i<= MaxLevel; i++)
Head->forward[i] = List->head;
List->level = 1;
List->size = 0;
return List;
}
static int randLevel()
{
int level = 1;
while( (rand()%10) <5 && level < MaxLevel)
level++;
return level;
}
void sInsert(struct SkipList* List, int key)
{
struct SkipNode *update[MaxLevel+1];
struct SkipNode* p = List->head;
int i;
for( i = List->level; i>= 1 ; i--)
{
while( p->forward[i]->value < key)
p = p->forward[i];
update[i] = p;
} // 找到各层不大于 key 的指针
p = p->forward[1];
if( key == p->value)
return;
int level = randLevel();
// printf("%d ", level);
if( level > List->level)
{
for( i = List->level+1; i <= level; i++)
update[i] = List->head;
List->level = level;
}
++List->size;
struct SkipNode* x= (struct SkipNode* )malloc(sizeof(struct SkipNode));
x->value = key;
x->forward = (struct SkipNode**)malloc(sizeof(struct SkipNode*) * (level+1));
for(i = 1; i<= level; i++)
{
x->forward[i] = update[i]->forward[i];
update[i]->forward[i] = x;
}
}
struct SkipNode* sFind(struct SkipList* List, int key)
{
struct SkipNode* p = List->head;
int i;
for( i = List->level ; i>= 1; i--)
{
while(p->forward[i]->value < key)
p = p->forward[i];
}
if( p->forward[1]->value == key)
return p->forward[1];
else
return NULL;
}
void sDelete(struct SkipList* List, int key)
{
struct SkipNode* update[MaxLevel+1];
struct SkipNode* p = List->head;
int i;
for(i = List->level; i>=1; i--)
{
while( p->forward[i]->value < key)
p= p->forward[i];
update[i] = p;
}
p = p->forward[1];
if( p->value != key)
return ;
else
{
for(i = 1; i<= List->level; i++)
{
if(update[i]->forward[i] != p)
break;
update[i]->forward[i] = p->forward[i];
}
free(p->forward);
free(p);
--List->size;
while(List->level >1 && (List->head->forward[List->level] == List->head))
--List->level;
}
}
//for test
int main(int argc, char** argv)
{
struct SkipList List;
init(&List);
int array[] = {3,6,9,2,11,1,4};
int i;
for(i =0; i < 7; i++)
sInsert(&List, array[i]);
sDelete(&List, 4);
printf("%d\n", List.size);
struct SkipNode *q = sFind(&List, 11);
printf("%d\n", q->value);
printf("Hello World\n");
return 0;
}