高级数据结构-SkipList

本文介绍了一种概率平衡树——跳表(Skip List)的数据结构实现及其基本操作,包括插入、查找和删除等功能,并通过C语言代码进行了详细的展示。

 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;
}




	
	
	


                                   


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值