1 | # 跳跃表 SkipList\<T>
|
2 |
|
3 | 跳跃列表是按层建造的。底层是一个普通的有序链表。每个更高层都充当下面列表的“快速跑道”,这里在第i层中的元素按某个固定的概率 p(通常为 1/2 或 1/4)出现在第 i+1 层中。每个元素平均出现在 1/1-p 个列表中,而最高层的元素(通常是在跳跃列表前端的一个特殊的头元素)在 ![](https://wikimedia.org/api/rest_v1/media/math/render/svg/89a058995d1439fb242df41c0f04c365003c4798)(即基于 1/p的n的对数)个列表中出现。
|
4 | 跳跃列表不像某些传统平衡树数据结构那样提供绝对的最坏情况性能保证。由于用来建造跳跃列表采用随机选取元素进入更高层的方法,在小概率情况下会生成一个不平衡的跳跃列表(最坏情况是在最底层仅有一个元素进入了更高层,此时跳跃列表的查找与普通列表一致)。但是在实际中它工作的很好,随机化平衡方案比在平衡二叉查找树中用的确定性平衡方案容易实现。跳跃列表在并行计算中也很有用,这里的插入可以在跳跃列表不同的部分并行的进行,而不用全局的数据结构重新平衡。
|
5 |
|
6 | ![SkipList](https://upload.wikimedia.org/wikipedia/commons/thumb/8/86/Skip_list.svg/400px-Skip_list.svg.png)
|
7 |
|
8 | ## 基本操作的API及示例
|
9 |
|
10 | ###属性
|
11 |
|
12 | #### 层级 Level
|
13 | ##### Level:number
|
14 | ``` text
|
15 | 实例:
|
16 | const skiplist = new SkipList();
|
17 | const level = skiplist.Level;
|
18 | console.log(level);
|
19 | //0
|
20 |
|
21 | ```
|
22 |
|
23 | #### 节点个数 Count
|
24 | ##### Count:number
|
25 | ``` text
|
26 | 实例:
|
27 | const skiplist = new SkipList();
|
28 | const count = skiplist.Count;
|
29 | console.log(count);
|
30 | //0
|
31 | ```
|
32 |
|
33 |
|
34 | ###方法
|
35 | #### Constructor
|
36 | ##### new SkipList(compareKey?: keyof T);
|
37 | ``` text
|
38 | 实例:
|
39 | const skiplist = new SkipList();
|
40 |
|
41 | const skiplist2 = new SkipList<{key:number}>("key");
|
42 | ```
|
43 |
|
44 | #### 插入 insert
|
45 | ##### insert(item: T): SkipList\<T>;
|
46 | ``` text
|
47 | const skiplist = new SkipList();
|
48 | skiplist.insert(2);
|
49 | skiplist.insert(3);
|
50 | ```
|
51 |
|
52 | #### 删除 deleteNode
|
53 | ##### deleteNode(item: T): SkipList\<T>;
|
54 | ``` text
|
55 | const skiplist = new SkipList();
|
56 | skiplist.insert(2);
|
57 | skiplist.deleteNode(2);
|
58 | ```
|
59 |
|
60 | #### 删除 remove
|
61 | ##### remove(item: T): SkipList\<T>;
|
62 | ``` text
|
63 | const skiplist = new SkipList();
|
64 | skiplist.insert(2);
|
65 | skiplist.remove(2);
|
66 | ```
|
67 |
|
68 | #### 查找 findNode
|
69 | ##### findNode(item: T): SkipListNode\<T>;
|
70 | ``` text
|
71 | const skiplist = new SkipList();
|
72 | skiplist.insert(2);
|
73 | const node1 = skiplist.findNode(2);
|
74 | console.log(node1.getItem());
|
75 | //2
|
76 |
|
77 | const skiplist = new SkipList<{key:number,value?:string}>("key");
|
78 | skiplist.insert({key:3,value:"skiplist"});
|
79 | skiplist.insert({key:2,value:"hashtable"});
|
80 | skiplist.insert({key:4,value:"queue"});
|
81 | const node2 = skiplist.findNode({key:3});
|
82 | console.log(node2.getItem());
|
83 | //{key:3,value:"skiplist"}
|
84 |
|
85 | ```
|
86 |
|
87 | #### 是否为空 isEmpty
|
88 | ##### isEmpty(): boolean;
|
89 | ``` text
|
90 | const skiplist = new SkipList();
|
91 | const isEmpty = skiplist.isEmpty();
|
92 | console.log(isEmpty);
|
93 | //false
|
94 | ``` |
\ | No newline at end of file |