-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSkiplist.js
76 lines (71 loc) · 1.68 KB
/
Skiplist.js
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
class Node {
constructor(val, next, down) {
this.val = val;
this.next = next;
this.down = down;
}
}
class Skiplist {
constructor() {
this.head = new Node(-Infinity, null, null);
}
search = function (target) {
let head = this.head;
while (head) {
// 找到此层小于target最近的值
while (head.next && head.next.val < target) {
head = head.next;
}
// 如果找到了目标值,直接返回
if (head.next && head.next.val == target) {
return true;
}
// 将当前指针下移,降层
head = head.down;
}
return false;
};
add = function (num) {
let list = [], head = this.head;
while (head) {
while (head.next && head.next.val < num) {
head = head.next;
}
list.push(head);
head = head.down;
}
let insertUp = true, downNode = null;
while (insertUp && list.length) {
head = list.pop()
head.next = new Node(num, head.next, downNode)
downNode = head.next;
// 随机加入层级
insertUp = (Math.random() > 0.5)
}
if (insertUp) {
this.head = new Node(-1, null, this.head)
}
};
erase = function (num) {
let head = this.head;
let found = false;
while (head) {
while (head.next && head.next.val < num) {
head = head.next;
}
if (head.next && head.next.val == num) {
head.next = head.next.next;
found = true;
}
head = head.down;
}
return found;
};
}
/**
* Your Skiplist object will be instantiated and called as such:
* var obj = new Skiplist()
* var param_1 = obj.search(target)
* obj.add(num)
* var param_3 = obj.erase(num)
*/