graph-typed 1.39.6 → 1.41.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/avl-tree.js +0 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -3
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +2 -11
- package/dist/data-structures/binary-tree/binary-tree.d.ts +13 -20
- package/dist/data-structures/binary-tree/binary-tree.js +30 -31
- package/dist/data-structures/binary-tree/bst.d.ts +2 -2
- package/dist/data-structures/binary-tree/bst.js +4 -4
- package/dist/data-structures/binary-tree/rb-tree.d.ts +95 -11
- package/dist/data-structures/binary-tree/rb-tree.js +379 -18
- package/dist/data-structures/binary-tree/segment-tree.d.ts +10 -26
- package/dist/data-structures/binary-tree/segment-tree.js +10 -58
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.js +6 -6
- package/dist/data-structures/graph/abstract-graph.d.ts +5 -24
- package/dist/data-structures/graph/abstract-graph.js +4 -43
- package/dist/data-structures/graph/directed-graph.d.ts +4 -10
- package/dist/data-structures/graph/directed-graph.js +2 -20
- package/dist/data-structures/graph/map-graph.d.ts +4 -10
- package/dist/data-structures/graph/map-graph.js +2 -20
- package/dist/data-structures/graph/undirected-graph.d.ts +1 -8
- package/dist/data-structures/graph/undirected-graph.js +1 -14
- package/dist/data-structures/hash/coordinate-map.d.ts +0 -1
- package/dist/data-structures/hash/coordinate-map.js +0 -3
- package/dist/data-structures/hash/coordinate-set.d.ts +0 -1
- package/dist/data-structures/hash/coordinate-set.js +0 -3
- package/dist/data-structures/hash/hash-map.d.ts +8 -14
- package/dist/data-structures/hash/hash-map.js +4 -22
- package/dist/data-structures/hash/hash-table.d.ts +6 -9
- package/dist/data-structures/hash/hash-table.js +0 -9
- package/dist/data-structures/heap/heap.d.ts +12 -6
- package/dist/data-structures/heap/heap.js +40 -22
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +6 -14
- package/dist/data-structures/linked-list/doubly-linked-list.js +18 -42
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +5 -11
- package/dist/data-structures/linked-list/singly-linked-list.js +17 -35
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +29 -10
- package/dist/data-structures/linked-list/skip-linked-list.js +62 -17
- package/dist/data-structures/matrix/matrix.d.ts +1 -1
- package/dist/data-structures/matrix/matrix2d.d.ts +1 -1
- package/dist/data-structures/matrix/navigator.d.ts +4 -4
- package/dist/data-structures/queue/deque.d.ts +8 -12
- package/dist/data-structures/queue/deque.js +31 -43
- package/dist/data-structures/queue/queue.d.ts +20 -5
- package/dist/data-structures/queue/queue.js +35 -18
- package/dist/data-structures/stack/stack.d.ts +2 -1
- package/dist/data-structures/stack/stack.js +10 -7
- package/dist/data-structures/tree/tree.d.ts +3 -9
- package/dist/data-structures/tree/tree.js +3 -21
- package/dist/data-structures/trie/trie.d.ts +6 -12
- package/dist/data-structures/trie/trie.js +6 -24
- package/dist/interfaces/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -7
- package/dist/types/data-structures/binary-tree/rb-tree.js +11 -6
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +2 -4
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -15
- package/src/data-structures/binary-tree/binary-tree.ts +40 -43
- package/src/data-structures/binary-tree/bst.ts +9 -10
- package/src/data-structures/binary-tree/rb-tree.ts +415 -355
- package/src/data-structures/binary-tree/segment-tree.ts +16 -83
- package/src/data-structures/binary-tree/tree-multiset.ts +8 -9
- package/src/data-structures/graph/abstract-graph.ts +21 -67
- package/src/data-structures/graph/directed-graph.ts +13 -39
- package/src/data-structures/graph/map-graph.ts +7 -32
- package/src/data-structures/graph/undirected-graph.ts +9 -26
- package/src/data-structures/hash/coordinate-map.ts +0 -4
- package/src/data-structures/hash/coordinate-set.ts +0 -4
- package/src/data-structures/hash/hash-map.ts +13 -37
- package/src/data-structures/hash/hash-table.ts +6 -18
- package/src/data-structures/hash/tree-map.ts +2 -1
- package/src/data-structures/hash/tree-set.ts +2 -1
- package/src/data-structures/heap/heap.ts +58 -30
- package/src/data-structures/heap/max-heap.ts +1 -1
- package/src/data-structures/heap/min-heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +26 -60
- package/src/data-structures/linked-list/singly-linked-list.ts +24 -49
- package/src/data-structures/linked-list/skip-linked-list.ts +73 -25
- package/src/data-structures/matrix/matrix.ts +2 -2
- package/src/data-structures/matrix/matrix2d.ts +1 -1
- package/src/data-structures/matrix/navigator.ts +4 -4
- package/src/data-structures/matrix/vector2d.ts +2 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/data-structures/queue/deque.ts +38 -53
- package/src/data-structures/queue/queue.ts +38 -20
- package/src/data-structures/stack/stack.ts +13 -9
- package/src/data-structures/tree/tree.ts +7 -33
- package/src/data-structures/trie/trie.ts +14 -40
- package/src/interfaces/binary-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/bst.ts +1 -1
- package/src/types/data-structures/binary-tree/rb-tree.ts +6 -6
- package/src/types/data-structures/matrix/navigator.ts +1 -1
- package/src/types/utils/utils.ts +1 -1
- package/src/types/utils/validate-type.ts +2 -2
|
@@ -6,34 +6,17 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
export class SinglyLinkedListNode<E = any> {
|
|
9
|
+
value: E;
|
|
10
|
+
next: SinglyLinkedListNode<E> | null;
|
|
11
|
+
|
|
9
12
|
/**
|
|
10
13
|
* The constructor function initializes an instance of a class with a given value and sets the next property to null.
|
|
11
14
|
* @param {E} value - The "value" parameter is of type E, which means it can be any data type. It represents the value that
|
|
12
15
|
* will be stored in the node of a linked list.
|
|
13
16
|
*/
|
|
14
17
|
constructor(value: E) {
|
|
15
|
-
this.
|
|
16
|
-
this.
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
private _value: E;
|
|
20
|
-
|
|
21
|
-
get value(): E {
|
|
22
|
-
return this._value;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
set value(value: E) {
|
|
26
|
-
this._value = value;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
private _next: SinglyLinkedListNode<E> | null;
|
|
30
|
-
|
|
31
|
-
get next(): SinglyLinkedListNode<E> | null {
|
|
32
|
-
return this._next;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
set next(value: SinglyLinkedListNode<E> | null) {
|
|
36
|
-
this._next = value;
|
|
18
|
+
this.value = value;
|
|
19
|
+
this.next = null;
|
|
37
20
|
}
|
|
38
21
|
}
|
|
39
22
|
|
|
@@ -47,27 +30,19 @@ export class SinglyLinkedList<E = any> {
|
|
|
47
30
|
this._length = 0;
|
|
48
31
|
}
|
|
49
32
|
|
|
50
|
-
|
|
33
|
+
protected _head: SinglyLinkedListNode<E> | null;
|
|
51
34
|
|
|
52
35
|
get head(): SinglyLinkedListNode<E> | null {
|
|
53
36
|
return this._head;
|
|
54
37
|
}
|
|
55
38
|
|
|
56
|
-
|
|
57
|
-
this._head = value;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
private _tail: SinglyLinkedListNode<E> | null;
|
|
39
|
+
protected _tail: SinglyLinkedListNode<E> | null;
|
|
61
40
|
|
|
62
41
|
get tail(): SinglyLinkedListNode<E> | null {
|
|
63
42
|
return this._tail;
|
|
64
43
|
}
|
|
65
44
|
|
|
66
|
-
|
|
67
|
-
this._tail = value;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
private _length: number;
|
|
45
|
+
protected _length: number;
|
|
71
46
|
|
|
72
47
|
get length(): number {
|
|
73
48
|
return this._length;
|
|
@@ -95,11 +70,11 @@ export class SinglyLinkedList<E = any> {
|
|
|
95
70
|
push(value: E): void {
|
|
96
71
|
const newNode = new SinglyLinkedListNode(value);
|
|
97
72
|
if (!this.head) {
|
|
98
|
-
this.
|
|
99
|
-
this.
|
|
73
|
+
this._head = newNode;
|
|
74
|
+
this._tail = newNode;
|
|
100
75
|
} else {
|
|
101
76
|
this.tail!.next = newNode;
|
|
102
|
-
this.
|
|
77
|
+
this._tail = newNode;
|
|
103
78
|
}
|
|
104
79
|
this._length++;
|
|
105
80
|
}
|
|
@@ -123,8 +98,8 @@ export class SinglyLinkedList<E = any> {
|
|
|
123
98
|
if (!this.head) return undefined;
|
|
124
99
|
if (this.head === this.tail) {
|
|
125
100
|
const value = this.head.value;
|
|
126
|
-
this.
|
|
127
|
-
this.
|
|
101
|
+
this._head = null;
|
|
102
|
+
this._tail = null;
|
|
128
103
|
this._length--;
|
|
129
104
|
return value;
|
|
130
105
|
}
|
|
@@ -135,7 +110,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
135
110
|
}
|
|
136
111
|
const value = this.tail!.value;
|
|
137
112
|
current.next = null;
|
|
138
|
-
this.
|
|
113
|
+
this._tail = current;
|
|
139
114
|
this._length--;
|
|
140
115
|
return value;
|
|
141
116
|
}
|
|
@@ -157,7 +132,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
157
132
|
shift(): E | undefined {
|
|
158
133
|
if (!this.head) return undefined;
|
|
159
134
|
const removedNode = this.head;
|
|
160
|
-
this.
|
|
135
|
+
this._head = this.head.next;
|
|
161
136
|
this._length--;
|
|
162
137
|
return removedNode.value;
|
|
163
138
|
}
|
|
@@ -178,11 +153,11 @@ export class SinglyLinkedList<E = any> {
|
|
|
178
153
|
unshift(value: E): void {
|
|
179
154
|
const newNode = new SinglyLinkedListNode(value);
|
|
180
155
|
if (!this.head) {
|
|
181
|
-
this.
|
|
182
|
-
this.
|
|
156
|
+
this._head = newNode;
|
|
157
|
+
this._tail = newNode;
|
|
183
158
|
} else {
|
|
184
159
|
newNode.next = this.head;
|
|
185
|
-
this.
|
|
160
|
+
this._head = newNode;
|
|
186
161
|
}
|
|
187
162
|
this._length++;
|
|
188
163
|
}
|
|
@@ -267,14 +242,14 @@ export class SinglyLinkedList<E = any> {
|
|
|
267
242
|
while (current) {
|
|
268
243
|
if (current.value === value) {
|
|
269
244
|
if (prev === null) {
|
|
270
|
-
this.
|
|
245
|
+
this._head = current.next;
|
|
271
246
|
if (current === this.tail) {
|
|
272
|
-
this.
|
|
247
|
+
this._tail = null;
|
|
273
248
|
}
|
|
274
249
|
} else {
|
|
275
250
|
prev.next = current.next;
|
|
276
251
|
if (current === this.tail) {
|
|
277
|
-
this.
|
|
252
|
+
this._tail = prev;
|
|
278
253
|
}
|
|
279
254
|
}
|
|
280
255
|
this._length--;
|
|
@@ -365,7 +340,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
365
340
|
current = next;
|
|
366
341
|
}
|
|
367
342
|
|
|
368
|
-
[this.
|
|
343
|
+
[this._head, this._tail] = [this.tail!, this.head!];
|
|
369
344
|
}
|
|
370
345
|
|
|
371
346
|
/**
|
|
@@ -486,7 +461,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
486
461
|
newNode.next = existingNode.next;
|
|
487
462
|
existingNode.next = newNode;
|
|
488
463
|
if (existingNode === this.tail) {
|
|
489
|
-
this.
|
|
464
|
+
this._tail = newNode;
|
|
490
465
|
}
|
|
491
466
|
this._length++;
|
|
492
467
|
return true;
|
|
@@ -590,7 +565,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
590
565
|
/**
|
|
591
566
|
* The function returns an iterator that iterates over the values of a linked list.
|
|
592
567
|
*/
|
|
593
|
-
*[Symbol.iterator]() {
|
|
568
|
+
* [Symbol.iterator]() {
|
|
594
569
|
let current = this.head;
|
|
595
570
|
|
|
596
571
|
while (current) {
|
|
@@ -33,46 +33,30 @@ export class SkipList<K, V> {
|
|
|
33
33
|
this._probability = probability;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
|
|
36
|
+
protected _head: SkipListNode<K, V>;
|
|
37
37
|
|
|
38
38
|
get head(): SkipListNode<K, V> {
|
|
39
39
|
return this._head;
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
|
|
43
|
-
this._head = value;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
private _level: number;
|
|
42
|
+
protected _level: number;
|
|
47
43
|
|
|
48
44
|
get level(): number {
|
|
49
45
|
return this._level;
|
|
50
46
|
}
|
|
51
47
|
|
|
52
|
-
|
|
53
|
-
this._level = value;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
private _maxLevel: number;
|
|
48
|
+
protected _maxLevel: number;
|
|
57
49
|
|
|
58
50
|
get maxLevel(): number {
|
|
59
51
|
return this._maxLevel;
|
|
60
52
|
}
|
|
61
53
|
|
|
62
|
-
|
|
63
|
-
this._maxLevel = value;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
private _probability: number;
|
|
54
|
+
protected _probability: number;
|
|
67
55
|
|
|
68
56
|
get probability(): number {
|
|
69
57
|
return this._probability;
|
|
70
58
|
}
|
|
71
59
|
|
|
72
|
-
set probability(value: number) {
|
|
73
|
-
this._probability = value;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
60
|
/**
|
|
77
61
|
* The add function adds a new node with a given key and value to a Skip List data structure.
|
|
78
62
|
* @param {K} key - The key parameter represents the key of the node that needs to be added to the skip list.
|
|
@@ -80,7 +64,7 @@ export class SkipList<K, V> {
|
|
|
80
64
|
* List.
|
|
81
65
|
*/
|
|
82
66
|
add(key: K, value: V): void {
|
|
83
|
-
const newNode = new SkipListNode(key, value, this.
|
|
67
|
+
const newNode = new SkipListNode(key, value, this._randomLevel());
|
|
84
68
|
const update: SkipListNode<K, V>[] = new Array(this.maxLevel).fill(this.head);
|
|
85
69
|
let current = this.head;
|
|
86
70
|
|
|
@@ -97,7 +81,7 @@ export class SkipList<K, V> {
|
|
|
97
81
|
}
|
|
98
82
|
|
|
99
83
|
if (newNode.forward[0] !== null) {
|
|
100
|
-
this.
|
|
84
|
+
this._level = Math.max(this.level, newNode.forward.length);
|
|
101
85
|
}
|
|
102
86
|
}
|
|
103
87
|
|
|
@@ -124,6 +108,10 @@ export class SkipList<K, V> {
|
|
|
124
108
|
return undefined;
|
|
125
109
|
}
|
|
126
110
|
|
|
111
|
+
has(key: K): boolean {
|
|
112
|
+
return this.get(key) !== undefined;
|
|
113
|
+
}
|
|
114
|
+
|
|
127
115
|
/**
|
|
128
116
|
* The `delete` function removes a node with a specific key from a Skip List data structure.
|
|
129
117
|
* @param {K} key - The key parameter represents the key of the node that needs to be removed from the skip list.
|
|
@@ -151,7 +139,7 @@ export class SkipList<K, V> {
|
|
|
151
139
|
update[i].forward[i] = current.forward[i];
|
|
152
140
|
}
|
|
153
141
|
while (this.level > 0 && this.head.forward[this.level - 1] === null) {
|
|
154
|
-
this.
|
|
142
|
+
this._level--;
|
|
155
143
|
}
|
|
156
144
|
return true;
|
|
157
145
|
}
|
|
@@ -160,10 +148,70 @@ export class SkipList<K, V> {
|
|
|
160
148
|
}
|
|
161
149
|
|
|
162
150
|
/**
|
|
163
|
-
*
|
|
151
|
+
* Get the value of the first element (the smallest element) in the Skip List.
|
|
152
|
+
* @returns The value of the first element, or undefined if the Skip List is empty.
|
|
153
|
+
*/
|
|
154
|
+
getFirst(): V | undefined {
|
|
155
|
+
const firstNode = this.head.forward[0];
|
|
156
|
+
return firstNode ? firstNode.value : undefined;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Get the value of the last element (the largest element) in the Skip List.
|
|
161
|
+
* @returns The value of the last element, or undefined if the Skip List is empty.
|
|
162
|
+
*/
|
|
163
|
+
getLast(): V | undefined {
|
|
164
|
+
let current = this.head;
|
|
165
|
+
for (let i = this.level - 1; i >= 0; i--) {
|
|
166
|
+
while (current.forward[i]) {
|
|
167
|
+
current = current.forward[i];
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return current.value;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Get the value of the first element in the Skip List that is greater than the given key.
|
|
175
|
+
* @param key - the given key.
|
|
176
|
+
* @returns The value of the first element greater than the given key, or undefined if there is no such element.
|
|
177
|
+
*/
|
|
178
|
+
higher(key: K): V | undefined {
|
|
179
|
+
let current = this.head;
|
|
180
|
+
for (let i = this.level - 1; i >= 0; i--) {
|
|
181
|
+
while (current.forward[i] && current.forward[i].key <= key) {
|
|
182
|
+
current = current.forward[i];
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
const nextNode = current.forward[0];
|
|
186
|
+
return nextNode ? nextNode.value : undefined;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Get the value of the last element in the Skip List that is less than the given key.
|
|
191
|
+
* @param key - the given key.
|
|
192
|
+
* @returns The value of the last element less than the given key, or undefined if there is no such element.
|
|
193
|
+
*/
|
|
194
|
+
lower(key: K): V | undefined {
|
|
195
|
+
let current = this.head;
|
|
196
|
+
let lastLess = null;
|
|
197
|
+
|
|
198
|
+
for (let i = this.level - 1; i >= 0; i--) {
|
|
199
|
+
while (current.forward[i] && current.forward[i].key < key) {
|
|
200
|
+
current = current.forward[i];
|
|
201
|
+
}
|
|
202
|
+
if (current.key < key) {
|
|
203
|
+
lastLess = current;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return lastLess ? lastLess.value : undefined;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* The function "_randomLevel" generates a random level based on a given probability and maximum level.
|
|
164
212
|
* @returns the level, which is a number.
|
|
165
213
|
*/
|
|
166
|
-
|
|
214
|
+
protected _randomLevel(): number {
|
|
167
215
|
let level = 1;
|
|
168
216
|
while (Math.random() < this.probability && level < this.maxLevel) {
|
|
169
217
|
level++;
|
|
@@ -7,14 +7,14 @@
|
|
|
7
7
|
*/
|
|
8
8
|
// todo need to be improved
|
|
9
9
|
export class MatrixNTI2D<V = any> {
|
|
10
|
-
|
|
10
|
+
protected readonly _matrix: Array<Array<V>>;
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* The constructor creates a matrix with the specified number of rows and columns, and initializes all elements to a
|
|
14
14
|
* given initial value or 0 if not provided.
|
|
15
15
|
* @param options - An object containing the following properties:
|
|
16
16
|
*/
|
|
17
|
-
constructor(options: {row: number; col: number; initialVal?: V}) {
|
|
17
|
+
constructor(options: { row: number; col: number; initialVal?: V }) {
|
|
18
18
|
const {row, col, initialVal} = options;
|
|
19
19
|
this._matrix = new Array(row).fill(undefined).map(() => new Array(col).fill(initialVal || 0));
|
|
20
20
|
}
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
import {Vector2D} from './vector2d';
|
|
9
9
|
|
|
10
10
|
export class Matrix2D {
|
|
11
|
-
|
|
11
|
+
protected readonly _matrix: number[][];
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* The constructor function initializes a Matrix2D object with either a default identity matrix, or a provided matrix
|
|
@@ -27,10 +27,10 @@ export class Character {
|
|
|
27
27
|
|
|
28
28
|
export class Navigator<T = number> {
|
|
29
29
|
onMove: (cur: [number, number]) => void;
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
30
|
+
protected readonly _matrix: T[][];
|
|
31
|
+
protected readonly _cur: [number, number];
|
|
32
|
+
protected _character: Character;
|
|
33
|
+
protected readonly _VISITED: T;
|
|
34
34
|
|
|
35
35
|
/**
|
|
36
36
|
* The constructor initializes the Navigator object with the given parameters and sets the current position as visited
|
|
@@ -10,7 +10,7 @@ import type {Comparator} from '../../types';
|
|
|
10
10
|
|
|
11
11
|
export class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
12
12
|
constructor(
|
|
13
|
-
options: {comparator: Comparator<E>; nodes?: E[]} = {
|
|
13
|
+
options: { comparator: Comparator<E>; nodes?: E[] } = {
|
|
14
14
|
comparator: (a: E, b: E) => {
|
|
15
15
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
16
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -10,7 +10,7 @@ import type {Comparator} from '../../types';
|
|
|
10
10
|
|
|
11
11
|
export class MinPriorityQueue<E = any> extends PriorityQueue<E> {
|
|
12
12
|
constructor(
|
|
13
|
-
options: {comparator: Comparator<E>; nodes?: E[]} = {
|
|
13
|
+
options: { comparator: Comparator<E>; nodes?: E[] } = {
|
|
14
14
|
comparator: (a: E, b: E) => {
|
|
15
15
|
if (!(typeof a === 'number' && typeof b === 'number')) {
|
|
16
16
|
throw new Error('The a, b params of compare function must be number');
|
|
@@ -10,7 +10,7 @@ import {Heap} from '../heap';
|
|
|
10
10
|
import {Comparator} from '../../types';
|
|
11
11
|
|
|
12
12
|
export class PriorityQueue<E = any> extends Heap<E> {
|
|
13
|
-
constructor(options: {comparator: Comparator<E>; nodes?: E[]}) {
|
|
13
|
+
constructor(options: { comparator: Comparator<E>; nodes?: E[] }) {
|
|
14
14
|
super(options);
|
|
15
15
|
}
|
|
16
16
|
}
|
|
@@ -9,7 +9,8 @@ import {DoublyLinkedList} from '../linked-list';
|
|
|
9
9
|
|
|
10
10
|
// O(n) time complexity of obtaining the value
|
|
11
11
|
// O(1) time complexity of adding at the beginning and the end
|
|
12
|
-
export class Deque<E = any> extends DoublyLinkedList<E> {
|
|
12
|
+
export class Deque<E = any> extends DoublyLinkedList<E> {
|
|
13
|
+
}
|
|
13
14
|
|
|
14
15
|
// O(1) time complexity of obtaining the value
|
|
15
16
|
// O(n) time complexity of adding at the beginning and the end
|
|
@@ -19,43 +20,31 @@ export class ObjectDeque<E = number> {
|
|
|
19
20
|
if (capacity !== undefined) this._capacity = capacity;
|
|
20
21
|
}
|
|
21
22
|
|
|
22
|
-
|
|
23
|
+
protected _nodes: { [key: number]: E } = {};
|
|
23
24
|
|
|
24
|
-
get nodes(): {[p: number]: E} {
|
|
25
|
+
get nodes(): { [p: number]: E } {
|
|
25
26
|
return this._nodes;
|
|
26
27
|
}
|
|
27
28
|
|
|
28
|
-
|
|
29
|
+
protected _capacity = Number.MAX_SAFE_INTEGER;
|
|
29
30
|
|
|
30
31
|
get capacity(): number {
|
|
31
32
|
return this._capacity;
|
|
32
33
|
}
|
|
33
34
|
|
|
34
|
-
|
|
35
|
-
this._capacity = value;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
private _first = -1;
|
|
35
|
+
protected _first = -1;
|
|
39
36
|
|
|
40
37
|
get first(): number {
|
|
41
38
|
return this._first;
|
|
42
39
|
}
|
|
43
40
|
|
|
44
|
-
|
|
45
|
-
this._first = value;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
private _last = -1;
|
|
41
|
+
protected _last = -1;
|
|
49
42
|
|
|
50
43
|
get last(): number {
|
|
51
44
|
return this._last;
|
|
52
45
|
}
|
|
53
46
|
|
|
54
|
-
|
|
55
|
-
this._last = value;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
private _size = 0;
|
|
47
|
+
protected _size = 0;
|
|
59
48
|
|
|
60
49
|
get size(): number {
|
|
61
50
|
return this._size;
|
|
@@ -67,14 +56,14 @@ export class ObjectDeque<E = number> {
|
|
|
67
56
|
* structure.
|
|
68
57
|
*/
|
|
69
58
|
addFirst(value: E) {
|
|
70
|
-
if (this.
|
|
71
|
-
const mid = Math.floor(this.
|
|
59
|
+
if (this.size === 0) {
|
|
60
|
+
const mid = Math.floor(this.capacity / 2);
|
|
72
61
|
this._first = mid;
|
|
73
62
|
this._last = mid;
|
|
74
63
|
} else {
|
|
75
64
|
this._first--;
|
|
76
65
|
}
|
|
77
|
-
this.
|
|
66
|
+
this.nodes[this.first] = value;
|
|
78
67
|
this._size++;
|
|
79
68
|
}
|
|
80
69
|
|
|
@@ -83,14 +72,14 @@ export class ObjectDeque<E = number> {
|
|
|
83
72
|
* @param {E} value - The `value` parameter represents the value that you want to add to the end of the data structure.
|
|
84
73
|
*/
|
|
85
74
|
addLast(value: E) {
|
|
86
|
-
if (this.
|
|
87
|
-
const mid = Math.floor(this.
|
|
75
|
+
if (this.size === 0) {
|
|
76
|
+
const mid = Math.floor(this.capacity / 2);
|
|
88
77
|
this._first = mid;
|
|
89
78
|
this._last = mid;
|
|
90
79
|
} else {
|
|
91
80
|
this._last++;
|
|
92
81
|
}
|
|
93
|
-
this.
|
|
82
|
+
this.nodes[this.last] = value;
|
|
94
83
|
this._size++;
|
|
95
84
|
}
|
|
96
85
|
|
|
@@ -99,9 +88,9 @@ export class ObjectDeque<E = number> {
|
|
|
99
88
|
* @returns The value of the first element in the data structure.
|
|
100
89
|
*/
|
|
101
90
|
popFirst() {
|
|
102
|
-
if (!this.
|
|
91
|
+
if (!this.size) return;
|
|
103
92
|
const value = this.getFirst();
|
|
104
|
-
delete this.
|
|
93
|
+
delete this.nodes[this.first];
|
|
105
94
|
this._first++;
|
|
106
95
|
this._size--;
|
|
107
96
|
return value;
|
|
@@ -112,7 +101,7 @@ export class ObjectDeque<E = number> {
|
|
|
112
101
|
* @returns The element at the first position of the `_nodes` array.
|
|
113
102
|
*/
|
|
114
103
|
getFirst() {
|
|
115
|
-
if (this.
|
|
104
|
+
if (this.size) return this.nodes[this.first];
|
|
116
105
|
}
|
|
117
106
|
|
|
118
107
|
/**
|
|
@@ -120,9 +109,9 @@ export class ObjectDeque<E = number> {
|
|
|
120
109
|
* @returns The value that was removed from the data structure.
|
|
121
110
|
*/
|
|
122
111
|
popLast() {
|
|
123
|
-
if (!this.
|
|
112
|
+
if (!this.size) return;
|
|
124
113
|
const value = this.getLast();
|
|
125
|
-
delete this.
|
|
114
|
+
delete this.nodes[this.last];
|
|
126
115
|
this._last--;
|
|
127
116
|
this._size--;
|
|
128
117
|
|
|
@@ -134,7 +123,7 @@ export class ObjectDeque<E = number> {
|
|
|
134
123
|
* @returns The last element in the array "_nodes" is being returned.
|
|
135
124
|
*/
|
|
136
125
|
getLast() {
|
|
137
|
-
if (this.
|
|
126
|
+
if (this.size) return this.nodes[this.last];
|
|
138
127
|
}
|
|
139
128
|
|
|
140
129
|
/**
|
|
@@ -145,7 +134,7 @@ export class ObjectDeque<E = number> {
|
|
|
145
134
|
* index, `null` is returned.
|
|
146
135
|
*/
|
|
147
136
|
get(index: number) {
|
|
148
|
-
return this.
|
|
137
|
+
return this.nodes[this.first + index] || null;
|
|
149
138
|
}
|
|
150
139
|
|
|
151
140
|
/**
|
|
@@ -153,15 +142,7 @@ export class ObjectDeque<E = number> {
|
|
|
153
142
|
* @returns The method is returning a boolean value indicating whether the size of the object is less than or equal to 0.
|
|
154
143
|
*/
|
|
155
144
|
isEmpty() {
|
|
156
|
-
return this.
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
protected _seNodes(value: {[p: number]: E}) {
|
|
160
|
-
this._nodes = value;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
protected _setSize(value: number) {
|
|
164
|
-
this._size = value;
|
|
145
|
+
return this.size <= 0;
|
|
165
146
|
}
|
|
166
147
|
}
|
|
167
148
|
|
|
@@ -170,8 +151,12 @@ export class ObjectDeque<E = number> {
|
|
|
170
151
|
export class ArrayDeque<E> {
|
|
171
152
|
protected _nodes: E[] = [];
|
|
172
153
|
|
|
154
|
+
get nodes(): E[] {
|
|
155
|
+
return this._nodes;
|
|
156
|
+
}
|
|
157
|
+
|
|
173
158
|
get size() {
|
|
174
|
-
return this.
|
|
159
|
+
return this.nodes.length;
|
|
175
160
|
}
|
|
176
161
|
|
|
177
162
|
/**
|
|
@@ -184,7 +169,7 @@ export class ArrayDeque<E> {
|
|
|
184
169
|
* @returns The return value is the new length of the array after the value has been added.
|
|
185
170
|
*/
|
|
186
171
|
addLast(value: E) {
|
|
187
|
-
return this.
|
|
172
|
+
return this.nodes.push(value);
|
|
188
173
|
}
|
|
189
174
|
|
|
190
175
|
/**
|
|
@@ -192,7 +177,7 @@ export class ArrayDeque<E> {
|
|
|
192
177
|
* @returns The method `popLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
193
178
|
*/
|
|
194
179
|
popLast(): E | null {
|
|
195
|
-
return this.
|
|
180
|
+
return this.nodes.pop() ?? null;
|
|
196
181
|
}
|
|
197
182
|
|
|
198
183
|
/**
|
|
@@ -201,7 +186,7 @@ export class ArrayDeque<E> {
|
|
|
201
186
|
* empty.
|
|
202
187
|
*/
|
|
203
188
|
popFirst(): E | null {
|
|
204
|
-
return this.
|
|
189
|
+
return this.nodes.shift() ?? null;
|
|
205
190
|
}
|
|
206
191
|
|
|
207
192
|
/**
|
|
@@ -215,7 +200,7 @@ export class ArrayDeque<E> {
|
|
|
215
200
|
* `value` at the beginning.
|
|
216
201
|
*/
|
|
217
202
|
addFirst(value: E) {
|
|
218
|
-
return this.
|
|
203
|
+
return this.nodes.unshift(value);
|
|
219
204
|
}
|
|
220
205
|
|
|
221
206
|
/**
|
|
@@ -224,7 +209,7 @@ export class ArrayDeque<E> {
|
|
|
224
209
|
* empty, it will return `null`.
|
|
225
210
|
*/
|
|
226
211
|
getFirst(): E | null {
|
|
227
|
-
return this.
|
|
212
|
+
return this.nodes[0] ?? null;
|
|
228
213
|
}
|
|
229
214
|
|
|
230
215
|
/**
|
|
@@ -232,7 +217,7 @@ export class ArrayDeque<E> {
|
|
|
232
217
|
* @returns The method `getLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
233
218
|
*/
|
|
234
219
|
getLast(): E | null {
|
|
235
|
-
return this.
|
|
220
|
+
return this.nodes[this.nodes.length - 1] ?? null;
|
|
236
221
|
}
|
|
237
222
|
|
|
238
223
|
/**
|
|
@@ -247,7 +232,7 @@ export class ArrayDeque<E> {
|
|
|
247
232
|
* will be returned. If the element does not exist (i.e., the index is out of bounds), `null` will be returned.
|
|
248
233
|
*/
|
|
249
234
|
get(index: number): E | null {
|
|
250
|
-
return this.
|
|
235
|
+
return this.nodes[index] ?? null;
|
|
251
236
|
}
|
|
252
237
|
|
|
253
238
|
/**
|
|
@@ -259,7 +244,7 @@ export class ArrayDeque<E> {
|
|
|
259
244
|
* @returns The value that is being set at the specified index in the `_nodes` array.
|
|
260
245
|
*/
|
|
261
246
|
set(index: number, value: E) {
|
|
262
|
-
return (this.
|
|
247
|
+
return (this.nodes[index] = value);
|
|
263
248
|
}
|
|
264
249
|
|
|
265
250
|
/**
|
|
@@ -273,7 +258,7 @@ export class ArrayDeque<E> {
|
|
|
273
258
|
* are being removed, an empty array will be returned.
|
|
274
259
|
*/
|
|
275
260
|
insert(index: number, value: E) {
|
|
276
|
-
return this.
|
|
261
|
+
return this.nodes.splice(index, 0, value);
|
|
277
262
|
}
|
|
278
263
|
|
|
279
264
|
/**
|
|
@@ -283,7 +268,7 @@ export class ArrayDeque<E> {
|
|
|
283
268
|
* @returns The method is returning an array containing the removed element.
|
|
284
269
|
*/
|
|
285
270
|
delete(index: number) {
|
|
286
|
-
return this.
|
|
271
|
+
return this.nodes.splice(index, 1);
|
|
287
272
|
}
|
|
288
273
|
|
|
289
274
|
/**
|
|
@@ -292,6 +277,6 @@ export class ArrayDeque<E> {
|
|
|
292
277
|
* is 0, indicating that the array is empty. Otherwise, it returns `false`.
|
|
293
278
|
*/
|
|
294
279
|
isEmpty() {
|
|
295
|
-
return this.
|
|
280
|
+
return this.nodes.length === 0;
|
|
296
281
|
}
|
|
297
282
|
}
|