1.7 zset Structure
First, introduce the concept of skip list, and then analyze the implementation of zset.
1.7.1 introduction to Skip List 1.7.1.1 ordered linked List
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 containingX, Or the smallest element with a key largerX(IfXDoes 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 table) Definition
Skip list: A data structure for maintaing a set of keys in a sorted order.
Consists of severalLevels.
All keys appear in level 1
Each level is a sorted list.
If keyXAppears in levelI, Then it also appears in all levels belowI
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 levelIPoints (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 "/>
This article is from the "ant nest" blog, please be sure to keep this source http://feihan21.blog.51cto.com/1364153/1300023