1.7 zset 結構
首先,介紹一下 skip list 的概念,然後再分析 zset 的實現.
1.7.1 Skip List 介紹1.7.1.1 有序鏈表
1) Searching a key in a Sorted linked list
650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131229/2101226458-0.jpg" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;text-align:center;background-color:#f3f3f3;padding:4px 5px 5px;margin:10px;" alt="Image011760.jpg" />
//Searching an element <em>xcell *p =head ;while (p->next->key < x ) p=p->next ;return p ;
Note: we return the element proceeding either the element containing x, or the smallest element with a key larger than x (if x does not exists)
2) inserting a key into a Sorted linked list
650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131229/210122M56-1.jpg" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;text-align:center;background-color:#f3f3f3;padding:4px 5px 5px;margin:10px;" alt="Image011770.jpg" />
1234567891011 |
//To insert 35 -p=find(35);CELL *p1 = (CELL *) malloc(sizeof(CELL));p1->key=35;p1->next = p->next ;p->next = p1 ; |
3) deleteing a key from a sorted list
650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131229/2101222Q8-2.jpg" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;text-align:center;background-color:#f3f3f3;padding:4px 5px 5px;margin:10px;" alt="Image011780.jpg" />
123456789 |
//To delete 37 -p=find(37);CELL *p1 =p->next;p->next = p1->next ;free(p1); |
1.7.1.2 SkipList(跳躍表)定義
SKIP LIST : A data structure for maintaing a set of keys in a sorted order.
Consists of several levels.
All keys appear in level 1
Each level is a sorted list.
If key x appears in level i, then it also appears in all levels below i
650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131229/21012231P-3.jpg" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;text-align:center;background-color:#f3f3f3;padding:4px 5px 5px;margin:10px;" alt="Image011790.jpg" />
An element in level i points (via down pointer) to the element with the same key in the level below.
In each level the keys and appear. (In our implementation, INT_MIN and INT_MAX
Top points to the smallest element in the highest level.
650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131229/210122A30-4.jpg" style="border-top-width:1px;border-right-width:1px;border-bottom-width:1px;border-left-width:1px;border-top-style:solid;border-right-style:solid;border-bottom-style:solid;border-left-style:solid;text-align:center;background-color:#f3f3f3;padding:4px 5px 5px;margin:10px;" alt="Image011800.jpg" />
本文出自 “螞蟻窩” 部落格,請務必保留此出處http://feihan21.blog.51cto.com/1364153/1300023