-
Notifications
You must be signed in to change notification settings - Fork 6
/
skip-linked-list.ts
320 lines (278 loc) · 8.96 KB
/
skip-linked-list.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/**
* data-structure-typed
*
* @author Tyler Zeng
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
* @license MIT License
*/
import type { SkipLinkedListOptions } from '../../types';
export class SkipListNode<K, V> {
key: K;
value: V;
forward: SkipListNode<K, V>[];
constructor(key: K, value: V, level: number) {
this.key = key;
this.value = value;
this.forward = new Array(level);
}
}
export class SkipList<K, V> {
/**
* The constructor function initializes a SkipLinkedList object with optional options and elements.
* @param elements - The `elements` parameter is an iterable containing key-value pairs `[K, V]`. It
* is used to initialize the SkipLinkedList with the given key-value pairs. If no elements are
* provided, the SkipLinkedList will be empty.
* @param {SkipLinkedListOptions} [options] - The `options` parameter is an optional object that can
* contain two properties:
*/
constructor(elements: Iterable<[K, V]> = [], options?: SkipLinkedListOptions) {
if (options) {
const { maxLevel, probability } = options;
if (typeof maxLevel === 'number') this._maxLevel = maxLevel;
if (typeof probability === 'number') this._probability = probability;
}
if (elements) {
for (const [key, value] of elements) this.add(key, value);
}
}
protected _head: SkipListNode<K, V> = new SkipListNode<K, V>(undefined as any, undefined as any, this.maxLevel);
/**
* The function returns the head node of a SkipList.
* @returns The method is returning a SkipListNode object with generic key type K and value type V.
*/
get head(): SkipListNode<K, V> {
return this._head;
}
protected _level: number = 0;
/**
* The function returns the value of the protected variable _level.
* @returns The level property of the object.
*/
get level(): number {
return this._level;
}
protected _maxLevel: number = 16;
/**
* The function returns the maximum level.
* @returns The value of the variable `_maxLevel` is being returned.
*/
get maxLevel(): number {
return this._maxLevel;
}
protected _probability: number = 0.5;
/**
* The function returns the probability value.
* @returns The probability value stored in the protected variable `_probability` is being returned.
*/
get probability(): number {
return this._probability;
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(1)
* Space Complexity: O(1)
*
* Get the value of the first element (the smallest element) in the Skip List.
* @returns The value of the first element, or undefined if the Skip List is empty.
*/
get first(): V | undefined {
const firstNode = this.head.forward[0];
return firstNode ? firstNode.value : undefined;
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*
* Get the value of the last element (the largest element) in the Skip List.
* @returns The value of the last element, or undefined if the Skip List is empty.
*/
get last(): V | undefined {
let current = this.head;
for (let i = this.level - 1; i >= 0; i--) {
while (current.forward[i]) {
current = current.forward[i];
}
}
return current.value;
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*
* The add function adds a new node with a given key and value to a Skip List data structure.
* @param {K} key - The key parameter represents the key of the node that needs to be added to the skip list.
* @param {V} value - The "value" parameter represents the value associated with the key that is being added to the Skip
* List.
*/
add(key: K, value: V): void {
const newNode = new SkipListNode(key, value, this._randomLevel());
const update: SkipListNode<K, V>[] = new Array(this.maxLevel).fill(this.head);
let current = this.head;
for (let i = this.level - 1; i >= 0; i--) {
while (current.forward[i] && current.forward[i].key < key) {
current = current.forward[i];
}
update[i] = current;
}
for (let i = 0; i < newNode.forward.length; i++) {
newNode.forward[i] = update[i].forward[i];
update[i].forward[i] = newNode;
}
if (!newNode.forward[0]) {
this._level = Math.max(this.level, newNode.forward.length);
}
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*
* The function `get` retrieves the value associated with a given key from a skip list data structure.
* @param {K} key - The `key` parameter is the key of the element that we want to retrieve from the data structure.
* @returns The method `get(key: K)` returns the value associated with the given key if it exists in the data structure,
* otherwise it returns `undefined`.
*/
get(key: K): V | undefined {
let current = this.head;
for (let i = this.level - 1; i >= 0; i--) {
while (current.forward[i] && current.forward[i].key < key) {
current = current.forward[i];
}
}
current = current.forward[0];
if (current && current.key === key) {
return current.value;
}
return undefined;
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* The function checks if a key exists in a data structure.
* @param {K} key - The parameter "key" is of type K, which represents the type of the key being
* checked.
* @returns a boolean value.
*/
has(key: K): boolean {
return this.get(key) !== undefined;
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*
* The `delete` function removes a node with a specific key from a Skip List data structure.
* @param {K} key - The key parameter represents the key of the node that needs to be removed from the skip list.
* @returns The `delete` method returns a boolean value. It returns `true` if the key was successfully removed from the
* skip list, and `false` if the key was not found in the skip list.
*/
delete(key: K): boolean {
const update: SkipListNode<K, V>[] = new Array(this.maxLevel).fill(this.head);
let current = this.head;
for (let i = this.level - 1; i >= 0; i--) {
while (current.forward[i] && current.forward[i].key < key) {
current = current.forward[i];
}
update[i] = current;
}
current = current.forward[0];
if (current && current.key === key) {
for (let i = 0; i < this.level; i++) {
if (update[i].forward[i] !== current) {
break;
}
update[i].forward[i] = current.forward[i];
}
while (this.level > 0 && !this.head.forward[this.level - 1]) {
this._level--;
}
return true;
}
return false;
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*
* Get the value of the first element in the Skip List that is greater than the given key.
* @param key - the given key.
* @returns The value of the first element greater than the given key, or undefined if there is no such element.
*/
higher(key: K): V | undefined {
let current = this.head;
for (let i = this.level - 1; i >= 0; i--) {
while (current.forward[i] && current.forward[i].key <= key) {
current = current.forward[i];
}
}
const nextNode = current.forward[0];
return nextNode ? nextNode.value : undefined;
}
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*/
/**
* Time Complexity: O(log n)
* Space Complexity: O(1)
*
* Get the value of the last element in the Skip List that is less than the given key.
* @param key - the given key.
* @returns The value of the last element less than the given key, or undefined if there is no such element.
*/
lower(key: K): V | undefined {
let current = this.head;
let lastLess = undefined;
for (let i = this.level - 1; i >= 0; i--) {
while (current.forward[i] && current.forward[i].key < key) {
current = current.forward[i];
}
if (current.key < key) {
lastLess = current;
}
}
return lastLess ? lastLess.value : undefined;
}
/**
* Time Complexity: O(maxLevel)
* Space Complexity: O(1)
* where maxLevel is the maximum level of the SkipList, as it may iterate up to maxLevel times in the worst case.
*/
/**
* Time Complexity: O(maxLevel)
* Space Complexity: O(1)
*
* The function "_randomLevel" generates a random level based on a given probability and maximum level.
* @returns the level, which is a number.
*/
protected _randomLevel(): number {
let level = 1;
while (Math.random() < this.probability && level < this.maxLevel) {
level++;
}
return level;
}
}