data-structure-typed 1.39.4 → 1.39.6
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/CHANGELOG.md +3 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +6 -6
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +13 -13
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +7 -7
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +17 -17
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/cjs/data-structures/binary-tree/bst.js +13 -13
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +2 -2
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +4 -4
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/segment-tree.d.ts +7 -7
- package/dist/cjs/data-structures/binary-tree/segment-tree.js +16 -16
- package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multiset.d.ts +6 -6
- package/dist/cjs/data-structures/binary-tree/tree-multiset.js +18 -18
- package/dist/cjs/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +96 -96
- package/dist/cjs/data-structures/graph/abstract-graph.js +64 -64
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +68 -68
- package/dist/cjs/data-structures/graph/directed-graph.js +48 -48
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.d.ts +13 -13
- package/dist/cjs/data-structures/graph/map-graph.js +15 -15
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +42 -42
- package/dist/cjs/data-structures/graph/undirected-graph.js +32 -32
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-table.d.ts +4 -4
- package/dist/cjs/data-structures/hash/hash-table.js +8 -8
- package/dist/cjs/data-structures/hash/hash-table.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +31 -31
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +54 -54
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +24 -24
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +52 -52
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +1 -1
- package/dist/cjs/data-structures/queue/queue.js +4 -4
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +2 -2
- package/dist/cjs/interfaces/graph.d.ts +3 -3
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +6 -6
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +13 -13
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +7 -7
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +18 -18
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/mjs/data-structures/binary-tree/bst.js +13 -13
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +2 -2
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +4 -4
- package/dist/mjs/data-structures/binary-tree/segment-tree.d.ts +7 -7
- package/dist/mjs/data-structures/binary-tree/segment-tree.js +16 -16
- package/dist/mjs/data-structures/binary-tree/tree-multiset.d.ts +6 -6
- package/dist/mjs/data-structures/binary-tree/tree-multiset.js +18 -18
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +96 -96
- package/dist/mjs/data-structures/graph/abstract-graph.js +66 -66
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +68 -68
- package/dist/mjs/data-structures/graph/directed-graph.js +48 -48
- package/dist/mjs/data-structures/graph/map-graph.d.ts +13 -13
- package/dist/mjs/data-structures/graph/map-graph.js +15 -15
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +42 -42
- package/dist/mjs/data-structures/graph/undirected-graph.js +32 -32
- package/dist/mjs/data-structures/hash/hash-table.d.ts +4 -4
- package/dist/mjs/data-structures/hash/hash-table.js +9 -9
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +31 -31
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +55 -55
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +24 -24
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +53 -53
- package/dist/mjs/data-structures/queue/queue.d.ts +1 -1
- package/dist/mjs/data-structures/queue/queue.js +4 -4
- package/dist/mjs/interfaces/binary-tree.d.ts +2 -2
- package/dist/mjs/interfaces/graph.d.ts +3 -3
- package/dist/umd/data-structure-typed.min.js +1 -1
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +5 -5
- package/src/data-structures/binary-tree/avl-tree.ts +13 -13
- package/src/data-structures/binary-tree/binary-tree.ts +18 -18
- package/src/data-structures/binary-tree/bst.ts +16 -16
- package/src/data-structures/binary-tree/rb-tree.ts +6 -6
- package/src/data-structures/binary-tree/segment-tree.ts +15 -15
- package/src/data-structures/binary-tree/tree-multiset.ts +18 -18
- package/src/data-structures/graph/abstract-graph.ts +156 -154
- package/src/data-structures/graph/directed-graph.ts +99 -94
- package/src/data-structures/graph/map-graph.ts +22 -25
- package/src/data-structures/graph/undirected-graph.ts +62 -60
- package/src/data-structures/hash/hash-table.ts +9 -9
- package/src/data-structures/linked-list/doubly-linked-list.ts +61 -61
- package/src/data-structures/linked-list/singly-linked-list.ts +58 -58
- package/src/data-structures/queue/queue.ts +2 -2
- package/src/interfaces/binary-tree.ts +2 -2
- package/src/interfaces/graph.ts +3 -3
- package/test/integration/bst.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/binary-index-tree.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +8 -8
- package/test/unit/data-structures/binary-tree/bst.test.ts +4 -4
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +2 -2
- package/test/unit/data-structures/graph/abstract-graph.test.ts +99 -2
- package/test/unit/data-structures/graph/directed-graph.test.ts +38 -23
- package/test/unit/data-structures/graph/map-graph.test.ts +23 -23
- package/test/unit/data-structures/graph/undirected-graph.test.ts +10 -2
- package/test/unit/data-structures/hash/hash-table.test.ts +1 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +30 -30
- package/test/unit/data-structures/linked-list/linked-list.test.ts +1 -1
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +15 -15
- package/test/unit/data-structures/queue/queue.test.ts +8 -8
- package/test/utils/big-o.ts +7 -7
|
@@ -8,22 +8,22 @@
|
|
|
8
8
|
export class SinglyLinkedListNode<E = any> {
|
|
9
9
|
/**
|
|
10
10
|
* The constructor function initializes an instance of a class with a given value and sets the next property to null.
|
|
11
|
-
* @param {E}
|
|
11
|
+
* @param {E} value - The "value" parameter is of type E, which means it can be any data type. It represents the value that
|
|
12
12
|
* will be stored in the node of a linked list.
|
|
13
13
|
*/
|
|
14
|
-
constructor(
|
|
15
|
-
this.
|
|
14
|
+
constructor(value: E) {
|
|
15
|
+
this._value = value;
|
|
16
16
|
this._next = null;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
private
|
|
19
|
+
private _value: E;
|
|
20
20
|
|
|
21
|
-
get
|
|
22
|
-
return this.
|
|
21
|
+
get value(): E {
|
|
22
|
+
return this._value;
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
set
|
|
26
|
-
this.
|
|
25
|
+
set value(value: E) {
|
|
26
|
+
this._value = value;
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
private _next: SinglyLinkedListNode<E> | null;
|
|
@@ -88,12 +88,12 @@ export class SinglyLinkedList<E = any> {
|
|
|
88
88
|
}
|
|
89
89
|
|
|
90
90
|
/**
|
|
91
|
-
* The `push` function adds a new node with the given
|
|
92
|
-
* @param {E}
|
|
91
|
+
* The `push` function adds a new node with the given value to the end of a singly linked list.
|
|
92
|
+
* @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
|
|
93
93
|
* any type (E) as specified in the generic type declaration of the class or function.
|
|
94
94
|
*/
|
|
95
|
-
push(
|
|
96
|
-
const newNode = new SinglyLinkedListNode(
|
|
95
|
+
push(value: E): void {
|
|
96
|
+
const newNode = new SinglyLinkedListNode(value);
|
|
97
97
|
if (!this.head) {
|
|
98
98
|
this.head = newNode;
|
|
99
99
|
this.tail = newNode;
|
|
@@ -105,12 +105,12 @@ export class SinglyLinkedList<E = any> {
|
|
|
105
105
|
}
|
|
106
106
|
|
|
107
107
|
/**
|
|
108
|
-
* The `push` function adds a new node with the given
|
|
109
|
-
* @param {E}
|
|
108
|
+
* The `push` function adds a new node with the given value to the end of a singly linked list.
|
|
109
|
+
* @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
|
|
110
110
|
* any type (E) as specified in the generic type declaration of the class or function.
|
|
111
111
|
*/
|
|
112
|
-
addLast(
|
|
113
|
-
this.push(
|
|
112
|
+
addLast(value: E): void {
|
|
113
|
+
this.push(value);
|
|
114
114
|
}
|
|
115
115
|
|
|
116
116
|
/**
|
|
@@ -122,22 +122,22 @@ export class SinglyLinkedList<E = any> {
|
|
|
122
122
|
pop(): E | undefined {
|
|
123
123
|
if (!this.head) return undefined;
|
|
124
124
|
if (this.head === this.tail) {
|
|
125
|
-
const
|
|
125
|
+
const value = this.head.value;
|
|
126
126
|
this.head = null;
|
|
127
127
|
this.tail = null;
|
|
128
128
|
this._length--;
|
|
129
|
-
return
|
|
129
|
+
return value;
|
|
130
130
|
}
|
|
131
131
|
|
|
132
132
|
let current = this.head;
|
|
133
133
|
while (current.next !== this.tail) {
|
|
134
134
|
current = current.next!;
|
|
135
135
|
}
|
|
136
|
-
const
|
|
136
|
+
const value = this.tail!.value;
|
|
137
137
|
current.next = null;
|
|
138
138
|
this.tail = current;
|
|
139
139
|
this._length--;
|
|
140
|
-
return
|
|
140
|
+
return value;
|
|
141
141
|
}
|
|
142
142
|
|
|
143
143
|
/**
|
|
@@ -159,7 +159,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
159
159
|
const removedNode = this.head;
|
|
160
160
|
this.head = this.head.next;
|
|
161
161
|
this._length--;
|
|
162
|
-
return removedNode.
|
|
162
|
+
return removedNode.value;
|
|
163
163
|
}
|
|
164
164
|
|
|
165
165
|
/**
|
|
@@ -172,11 +172,11 @@ export class SinglyLinkedList<E = any> {
|
|
|
172
172
|
|
|
173
173
|
/**
|
|
174
174
|
* The unshift function adds a new node with the given value to the beginning of a singly linked list.
|
|
175
|
-
* @param {E}
|
|
175
|
+
* @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
|
|
176
176
|
* linked list.
|
|
177
177
|
*/
|
|
178
|
-
unshift(
|
|
179
|
-
const newNode = new SinglyLinkedListNode(
|
|
178
|
+
unshift(value: E): void {
|
|
179
|
+
const newNode = new SinglyLinkedListNode(value);
|
|
180
180
|
if (!this.head) {
|
|
181
181
|
this.head = newNode;
|
|
182
182
|
this.tail = newNode;
|
|
@@ -189,11 +189,11 @@ export class SinglyLinkedList<E = any> {
|
|
|
189
189
|
|
|
190
190
|
/**
|
|
191
191
|
* The addFirst function adds a new node with the given value to the beginning of a singly linked list.
|
|
192
|
-
* @param {E}
|
|
192
|
+
* @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
|
|
193
193
|
* linked list.
|
|
194
194
|
*/
|
|
195
|
-
addFirst(
|
|
196
|
-
this.unshift(
|
|
195
|
+
addFirst(value: E): void {
|
|
196
|
+
this.unshift(value);
|
|
197
197
|
}
|
|
198
198
|
|
|
199
199
|
/**
|
|
@@ -209,7 +209,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
209
209
|
for (let i = 0; i < index; i++) {
|
|
210
210
|
current = current!.next;
|
|
211
211
|
}
|
|
212
|
-
return current!.
|
|
212
|
+
return current!.value;
|
|
213
213
|
}
|
|
214
214
|
|
|
215
215
|
/**
|
|
@@ -243,7 +243,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
243
243
|
const removedNode = prevNode!.next;
|
|
244
244
|
prevNode!.next = removedNode!.next;
|
|
245
245
|
this._length--;
|
|
246
|
-
return removedNode!.
|
|
246
|
+
return removedNode!.value;
|
|
247
247
|
}
|
|
248
248
|
|
|
249
249
|
/**
|
|
@@ -257,7 +257,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
257
257
|
if (!valueOrNode) return false;
|
|
258
258
|
let value: E;
|
|
259
259
|
if (valueOrNode instanceof SinglyLinkedListNode) {
|
|
260
|
-
value = valueOrNode.
|
|
260
|
+
value = valueOrNode.value;
|
|
261
261
|
} else {
|
|
262
262
|
value = valueOrNode;
|
|
263
263
|
}
|
|
@@ -265,7 +265,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
265
265
|
prev = null;
|
|
266
266
|
|
|
267
267
|
while (current) {
|
|
268
|
-
if (current.
|
|
268
|
+
if (current.value === value) {
|
|
269
269
|
if (prev === null) {
|
|
270
270
|
this.head = current.next;
|
|
271
271
|
if (current === this.tail) {
|
|
@@ -291,23 +291,23 @@ export class SinglyLinkedList<E = any> {
|
|
|
291
291
|
* The `insertAt` function inserts a value at a specified index in a singly linked list.
|
|
292
292
|
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
|
|
293
293
|
* linked list. It is of type number.
|
|
294
|
-
* @param {E}
|
|
294
|
+
* @param {E} value - The `value` parameter represents the value that you want to insert into the linked list at the
|
|
295
295
|
* specified index.
|
|
296
296
|
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
|
|
297
297
|
* if the index is out of bounds.
|
|
298
298
|
*/
|
|
299
|
-
insertAt(index: number,
|
|
299
|
+
insertAt(index: number, value: E): boolean {
|
|
300
300
|
if (index < 0 || index > this.length) return false;
|
|
301
301
|
if (index === 0) {
|
|
302
|
-
this.unshift(
|
|
302
|
+
this.unshift(value);
|
|
303
303
|
return true;
|
|
304
304
|
}
|
|
305
305
|
if (index === this.length) {
|
|
306
|
-
this.push(
|
|
306
|
+
this.push(value);
|
|
307
307
|
return true;
|
|
308
308
|
}
|
|
309
309
|
|
|
310
|
-
const newNode = new SinglyLinkedListNode(
|
|
310
|
+
const newNode = new SinglyLinkedListNode(value);
|
|
311
311
|
const prevNode = this.getNodeAt(index - 1);
|
|
312
312
|
newNode.next = prevNode!.next;
|
|
313
313
|
prevNode!.next = newNode;
|
|
@@ -341,7 +341,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
341
341
|
const array: E[] = [];
|
|
342
342
|
let current = this.head;
|
|
343
343
|
while (current) {
|
|
344
|
-
array.push(current.
|
|
344
|
+
array.push(current.value);
|
|
345
345
|
current = current.next;
|
|
346
346
|
}
|
|
347
347
|
return array;
|
|
@@ -375,11 +375,11 @@ export class SinglyLinkedList<E = any> {
|
|
|
375
375
|
* @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
|
|
376
376
|
* the callback function. If no element satisfies the condition, it returns `null`.
|
|
377
377
|
*/
|
|
378
|
-
find(callback: (
|
|
378
|
+
find(callback: (value: E) => boolean): E | null {
|
|
379
379
|
let current = this.head;
|
|
380
380
|
while (current) {
|
|
381
|
-
if (callback(current.
|
|
382
|
-
return current.
|
|
381
|
+
if (callback(current.value)) {
|
|
382
|
+
return current.value;
|
|
383
383
|
}
|
|
384
384
|
current = current.next;
|
|
385
385
|
}
|
|
@@ -397,7 +397,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
397
397
|
let current = this.head;
|
|
398
398
|
|
|
399
399
|
while (current) {
|
|
400
|
-
if (current.
|
|
400
|
+
if (current.value === value) {
|
|
401
401
|
return index;
|
|
402
402
|
}
|
|
403
403
|
index++;
|
|
@@ -418,7 +418,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
418
418
|
let current = this.head;
|
|
419
419
|
|
|
420
420
|
while (current) {
|
|
421
|
-
if (current.
|
|
421
|
+
if (current.value === value) {
|
|
422
422
|
return current;
|
|
423
423
|
}
|
|
424
424
|
current = current.next;
|
|
@@ -440,18 +440,18 @@ export class SinglyLinkedList<E = any> {
|
|
|
440
440
|
|
|
441
441
|
let existingValue: E;
|
|
442
442
|
if (existingValueOrNode instanceof SinglyLinkedListNode) {
|
|
443
|
-
existingValue = existingValueOrNode.
|
|
443
|
+
existingValue = existingValueOrNode.value;
|
|
444
444
|
} else {
|
|
445
445
|
existingValue = existingValueOrNode;
|
|
446
446
|
}
|
|
447
|
-
if (this.head.
|
|
447
|
+
if (this.head.value === existingValue) {
|
|
448
448
|
this.unshift(newValue);
|
|
449
449
|
return true;
|
|
450
450
|
}
|
|
451
451
|
|
|
452
452
|
let current = this.head;
|
|
453
453
|
while (current.next) {
|
|
454
|
-
if (current.next.
|
|
454
|
+
if (current.next.value === existingValue) {
|
|
455
455
|
const newNode = new SinglyLinkedListNode(newValue);
|
|
456
456
|
newNode.next = current.next;
|
|
457
457
|
current.next = newNode;
|
|
@@ -505,7 +505,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
505
505
|
let current = this.head;
|
|
506
506
|
|
|
507
507
|
while (current) {
|
|
508
|
-
if (current.
|
|
508
|
+
if (current.value === value) {
|
|
509
509
|
count++;
|
|
510
510
|
}
|
|
511
511
|
current = current.next;
|
|
@@ -516,15 +516,15 @@ export class SinglyLinkedList<E = any> {
|
|
|
516
516
|
|
|
517
517
|
/**
|
|
518
518
|
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
519
|
-
* @param callback - The callback parameter is a function that takes two arguments:
|
|
519
|
+
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
|
|
520
520
|
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
521
521
|
* current node in the linked list.
|
|
522
522
|
*/
|
|
523
|
-
forEach(callback: (
|
|
523
|
+
forEach(callback: (value: E, index: number) => void): void {
|
|
524
524
|
let current = this.head;
|
|
525
525
|
let index = 0;
|
|
526
526
|
while (current) {
|
|
527
|
-
callback(current.
|
|
527
|
+
callback(current.value, index);
|
|
528
528
|
current = current.next;
|
|
529
529
|
index++;
|
|
530
530
|
}
|
|
@@ -538,11 +538,11 @@ export class SinglyLinkedList<E = any> {
|
|
|
538
538
|
* SinglyLinkedList).
|
|
539
539
|
* @returns The `map` function is returning a new instance of `SinglyLinkedList<U>` that contains the mapped values.
|
|
540
540
|
*/
|
|
541
|
-
map<U>(callback: (
|
|
541
|
+
map<U>(callback: (value: E) => U): SinglyLinkedList<U> {
|
|
542
542
|
const mappedList = new SinglyLinkedList<U>();
|
|
543
543
|
let current = this.head;
|
|
544
544
|
while (current) {
|
|
545
|
-
mappedList.push(callback(current.
|
|
545
|
+
mappedList.push(callback(current.value));
|
|
546
546
|
current = current.next;
|
|
547
547
|
}
|
|
548
548
|
return mappedList;
|
|
@@ -555,12 +555,12 @@ export class SinglyLinkedList<E = any> {
|
|
|
555
555
|
* It is used to determine whether a value should be included in the filtered list or not.
|
|
556
556
|
* @returns The filtered list, which is an instance of the SinglyLinkedList class.
|
|
557
557
|
*/
|
|
558
|
-
filter(callback: (
|
|
558
|
+
filter(callback: (value: E) => boolean): SinglyLinkedList<E> {
|
|
559
559
|
const filteredList = new SinglyLinkedList<E>();
|
|
560
560
|
let current = this.head;
|
|
561
561
|
while (current) {
|
|
562
|
-
if (callback(current.
|
|
563
|
-
filteredList.push(current.
|
|
562
|
+
if (callback(current.value)) {
|
|
563
|
+
filteredList.push(current.value);
|
|
564
564
|
}
|
|
565
565
|
current = current.next;
|
|
566
566
|
}
|
|
@@ -570,18 +570,18 @@ export class SinglyLinkedList<E = any> {
|
|
|
570
570
|
/**
|
|
571
571
|
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
572
572
|
* single value.
|
|
573
|
-
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `
|
|
573
|
+
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
|
|
574
574
|
* used to perform a specific operation on each element of the linked list.
|
|
575
575
|
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
576
576
|
* point for the reduction operation.
|
|
577
577
|
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
578
578
|
* elements in the linked list.
|
|
579
579
|
*/
|
|
580
|
-
reduce<U>(callback: (accumulator: U,
|
|
580
|
+
reduce<U>(callback: (accumulator: U, value: E) => U, initialValue: U): U {
|
|
581
581
|
let accumulator = initialValue;
|
|
582
582
|
let current = this.head;
|
|
583
583
|
while (current) {
|
|
584
|
-
accumulator = callback(accumulator, current.
|
|
584
|
+
accumulator = callback(accumulator, current.value);
|
|
585
585
|
current = current.next;
|
|
586
586
|
}
|
|
587
587
|
return accumulator;
|
|
@@ -594,7 +594,7 @@ export class SinglyLinkedList<E = any> {
|
|
|
594
594
|
let current = this.head;
|
|
595
595
|
|
|
596
596
|
while (current) {
|
|
597
|
-
yield current.
|
|
597
|
+
yield current.value;
|
|
598
598
|
current = current.next;
|
|
599
599
|
}
|
|
600
600
|
}
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import {SinglyLinkedList} from '../linked-list';
|
|
7
7
|
|
|
8
|
-
export class
|
|
8
|
+
export class SkipQueue<E = any> extends SinglyLinkedList<E> {
|
|
9
9
|
/**
|
|
10
10
|
* The enqueue function adds a value to the end of an array.
|
|
11
11
|
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
@@ -27,7 +27,7 @@ export class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
|
|
|
27
27
|
* @returns The `peek()` method is returning the value of the `head` node if it exists, otherwise it returns `undefined`.
|
|
28
28
|
*/
|
|
29
29
|
peek(): E | undefined {
|
|
30
|
-
return this.head?.
|
|
30
|
+
return this.head?.value;
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
33
|
|
|
@@ -2,9 +2,9 @@ import {BinaryTreeNode} from '../data-structures';
|
|
|
2
2
|
import {BinaryTreeDeletedResult, BTNKey, BinaryTreeNodeNested, BTNCallback} from '../types';
|
|
3
3
|
|
|
4
4
|
export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>> {
|
|
5
|
-
createNode(key: BTNKey,
|
|
5
|
+
createNode(key: BTNKey, value?: N['value']): N;
|
|
6
6
|
|
|
7
|
-
add(keyOrNode: BTNKey | N | null,
|
|
7
|
+
add(keyOrNode: BTNKey | N | null, value?: N['value']): N | null | undefined;
|
|
8
8
|
|
|
9
9
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeletedResult<N>[];
|
|
10
10
|
}
|
package/src/interfaces/graph.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {VertexKey} from '../types';
|
|
2
2
|
|
|
3
|
-
export interface IGraph<V, E> {
|
|
4
|
-
createVertex(key: VertexKey,
|
|
3
|
+
export interface IGraph<V, E, VO, EO> {
|
|
4
|
+
createVertex(key: VertexKey, value?: V): VO;
|
|
5
5
|
|
|
6
|
-
createEdge(srcOrV1: VertexKey
|
|
6
|
+
createEdge(srcOrV1: VertexKey, destOrV2: VertexKey, weight?: number, value?: E): EO;
|
|
7
7
|
}
|
|
@@ -6,8 +6,8 @@ describe('Individual package BST operations test', () => {
|
|
|
6
6
|
expect(bst).toBeInstanceOf(BST);
|
|
7
7
|
bst.add(11, 11);
|
|
8
8
|
bst.add(3, 3);
|
|
9
|
-
const
|
|
10
|
-
bst.addMany(
|
|
9
|
+
const idsOrValues = [15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
|
|
10
|
+
bst.addMany(idsOrValues, idsOrValues, false);
|
|
11
11
|
expect(bst.root).toBeInstanceOf(BSTNode);
|
|
12
12
|
|
|
13
13
|
if (bst.root) expect(bst.root.key).toBe(11);
|
|
@@ -31,7 +31,7 @@ describe('AVL Tree Test', () => {
|
|
|
31
31
|
expect(lesserSum).toBe(45);
|
|
32
32
|
|
|
33
33
|
// node15 has type problem. After the uniform design, the generics of containers (DirectedGraph, BST) are based on the type of value. However, this design has a drawback: when I attempt to inherit from the Vertex or BSTNode classes, the types of the results obtained by all methods are those of the parent class.
|
|
34
|
-
expect(node15?.
|
|
34
|
+
expect(node15?.value).toBe(15);
|
|
35
35
|
|
|
36
36
|
const dfs = tree.dfs(node => node, 'in');
|
|
37
37
|
expect(dfs[0].key).toBe(1);
|
|
@@ -140,7 +140,7 @@ describe('AVL Tree Test recursively', () => {
|
|
|
140
140
|
expect(lesserSum).toBe(45);
|
|
141
141
|
|
|
142
142
|
// node15 has type problem. After the uniform design, the generics of containers (DirectedGraph, BST) are based on the type of value. However, this design has a drawback: when I attempt to inherit from the Vertex or BSTNode classes, the types of the results obtained by all methods are those of the parent class.
|
|
143
|
-
expect(node15?.
|
|
143
|
+
expect(node15?.value).toBe(15);
|
|
144
144
|
|
|
145
145
|
const dfs = tree.dfs(node => node, 'in');
|
|
146
146
|
expect(dfs[0].key).toBe(1);
|
|
@@ -297,9 +297,9 @@ describe('', () => {
|
|
|
297
297
|
}
|
|
298
298
|
}
|
|
299
299
|
|
|
300
|
-
update(index: number,
|
|
301
|
-
this._tree.update(index + 1,
|
|
302
|
-
this._nums[index] =
|
|
300
|
+
update(index: number, value: number): void {
|
|
301
|
+
this._tree.update(index + 1, value - this._nums[index]);
|
|
302
|
+
this._nums[index] = value;
|
|
303
303
|
}
|
|
304
304
|
|
|
305
305
|
sumRange(left: number, right: number): number {
|
|
@@ -20,10 +20,10 @@ describe('BinaryTreeNode', () => {
|
|
|
20
20
|
|
|
21
21
|
it('should set and get the value correctly', () => {
|
|
22
22
|
const node: BinaryTreeNode<number> = new BinaryTreeNode<number>(1, 42);
|
|
23
|
-
expect(node.
|
|
23
|
+
expect(node.value).toBe(42);
|
|
24
24
|
|
|
25
|
-
node.
|
|
26
|
-
expect(node.
|
|
25
|
+
node.value = 55;
|
|
26
|
+
expect(node.value).toBe(55);
|
|
27
27
|
});
|
|
28
28
|
|
|
29
29
|
it('should set and get the left child correctly', () => {
|
|
@@ -110,7 +110,7 @@ describe('BinaryTree', () => {
|
|
|
110
110
|
const node4 = tree.get(4);
|
|
111
111
|
expect(tree.has(node4)).toBe(false);
|
|
112
112
|
expect(tree.has(node4, node => node)).toBe(false);
|
|
113
|
-
expect(tree.has('3', node => node.
|
|
113
|
+
expect(tree.has('3', node => node.value?.toString())).toBe(true);
|
|
114
114
|
});
|
|
115
115
|
|
|
116
116
|
it('should getDepth return correct depth', () => {
|
|
@@ -343,9 +343,9 @@ describe('BinaryTree', () => {
|
|
|
343
343
|
const nodeB = tree.get(3);
|
|
344
344
|
|
|
345
345
|
expect(nodeA?.key).toBe(5);
|
|
346
|
-
expect(nodeA?.
|
|
346
|
+
expect(nodeA?.value).toBe('A');
|
|
347
347
|
expect(nodeB?.key).toBe(3);
|
|
348
|
-
expect(nodeB?.
|
|
348
|
+
expect(nodeB?.value).toBe('B');
|
|
349
349
|
});
|
|
350
350
|
|
|
351
351
|
it('should return null when getting a non-existent node', () => {
|
|
@@ -437,14 +437,14 @@ describe('BinaryTree', () => {
|
|
|
437
437
|
tree.add(3, 'B');
|
|
438
438
|
tree.add(7, 'C');
|
|
439
439
|
|
|
440
|
-
const nodes = tree.getNodes('B', (node: BinaryTreeNode<string>) => node.
|
|
440
|
+
const nodes = tree.getNodes('B', (node: BinaryTreeNode<string>) => node.value);
|
|
441
441
|
|
|
442
442
|
expect(nodes.length).toBe(1);
|
|
443
443
|
expect(nodes[0].key).toBe(3);
|
|
444
444
|
|
|
445
445
|
const nodesRec = tree.getNodes(
|
|
446
446
|
'B',
|
|
447
|
-
(node: BinaryTreeNode<string>) => node.
|
|
447
|
+
(node: BinaryTreeNode<string>) => node.value,
|
|
448
448
|
false,
|
|
449
449
|
tree.root,
|
|
450
450
|
IterationType.RECURSIVE
|
|
@@ -26,7 +26,7 @@ describe('BST operations test', () => {
|
|
|
26
26
|
const nodeId10 = bst.get(10);
|
|
27
27
|
expect(nodeId10?.key).toBe(10);
|
|
28
28
|
|
|
29
|
-
const nodeVal9 = bst.get(9, node => node.
|
|
29
|
+
const nodeVal9 = bst.get(9, node => node.value);
|
|
30
30
|
expect(nodeVal9?.key).toBe(9);
|
|
31
31
|
|
|
32
32
|
const leftMost = bst.getLeftMost();
|
|
@@ -236,7 +236,7 @@ describe('BST operations test', () => {
|
|
|
236
236
|
expect(leftMost?.key).toBe(1);
|
|
237
237
|
|
|
238
238
|
const node15 = objBST.get(15);
|
|
239
|
-
expect(node15?.
|
|
239
|
+
expect(node15?.value).toEqual({key: 15, keyA: 15});
|
|
240
240
|
const minNodeBySpecificNode = node15 && objBST.getLeftMost(node15);
|
|
241
241
|
expect(minNodeBySpecificNode?.key).toBe(12);
|
|
242
242
|
|
|
@@ -416,7 +416,7 @@ describe('BST operations test recursively', () => {
|
|
|
416
416
|
const nodeId10 = bst.get(10);
|
|
417
417
|
expect(nodeId10?.key).toBe(10);
|
|
418
418
|
|
|
419
|
-
const nodeVal9 = bst.get(9, node => node.
|
|
419
|
+
const nodeVal9 = bst.get(9, node => node.value);
|
|
420
420
|
expect(nodeVal9?.key).toBe(9);
|
|
421
421
|
|
|
422
422
|
const leftMost = bst.getLeftMost();
|
|
@@ -626,7 +626,7 @@ describe('BST operations test recursively', () => {
|
|
|
626
626
|
expect(leftMost?.key).toBe(1);
|
|
627
627
|
|
|
628
628
|
const node15 = objBST.get(15);
|
|
629
|
-
expect(node15?.
|
|
629
|
+
expect(node15?.value).toEqual({key: 15, keyA: 15});
|
|
630
630
|
const minNodeBySpecificNode = node15 && objBST.getLeftMost(node15);
|
|
631
631
|
expect(minNodeBySpecificNode?.key).toBe(12);
|
|
632
632
|
|
|
@@ -16,10 +16,10 @@ describe('RBTreeNode', () => {
|
|
|
16
16
|
|
|
17
17
|
it('should set and get the value correctly', () => {
|
|
18
18
|
const node: RBTreeNode<number> = new RBTreeNode<number>(1, 42);
|
|
19
|
-
expect(node.
|
|
19
|
+
expect(node.value).toBe(42);
|
|
20
20
|
|
|
21
|
-
node.
|
|
22
|
-
expect(node.
|
|
21
|
+
node.value = 55;
|
|
22
|
+
expect(node.value).toBe(55);
|
|
23
23
|
});
|
|
24
24
|
|
|
25
25
|
it('should set and get the left child correctly', () => {
|
|
@@ -26,7 +26,7 @@ describe('TreeMultiset operations test', () => {
|
|
|
26
26
|
const nodeId10 = treeMultiset.get(10);
|
|
27
27
|
expect(nodeId10?.key).toBe(10);
|
|
28
28
|
|
|
29
|
-
const nodeVal9 = treeMultiset.get(9, node => node.
|
|
29
|
+
const nodeVal9 = treeMultiset.get(9, node => node.value);
|
|
30
30
|
expect(nodeVal9?.key).toBe(9);
|
|
31
31
|
|
|
32
32
|
const nodesByCount1 = treeMultiset.getNodes(1, node => node.count);
|
|
@@ -266,7 +266,7 @@ describe('TreeMultiset operations test recursively', () => {
|
|
|
266
266
|
const nodeId10 = treeMultiset.get(10);
|
|
267
267
|
expect(nodeId10?.key).toBe(10);
|
|
268
268
|
|
|
269
|
-
const nodeVal9 = treeMultiset.get(9, node => node.
|
|
269
|
+
const nodeVal9 = treeMultiset.get(9, node => node.value);
|
|
270
270
|
expect(nodeVal9?.key).toBe(9);
|
|
271
271
|
|
|
272
272
|
const nodesByCount1 = treeMultiset.getNodes(1, node => node.count);
|
|
@@ -1,5 +1,102 @@
|
|
|
1
|
+
import {AbstractEdge, AbstractGraph, AbstractVertex, VertexKey} from '../../../../src';
|
|
2
|
+
|
|
3
|
+
class MyVertex<V = any> extends AbstractVertex<V> {
|
|
4
|
+
data?: V;
|
|
5
|
+
|
|
6
|
+
constructor(key: VertexKey, value?: V) {
|
|
7
|
+
super(key, value);
|
|
8
|
+
this.data = value;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
class MyEdge<E = any> extends AbstractEdge<E> {
|
|
13
|
+
data?: E;
|
|
14
|
+
src: VertexKey;
|
|
15
|
+
dest: VertexKey;
|
|
16
|
+
|
|
17
|
+
constructor(srcOrV1: VertexKey, destOrV2: VertexKey, weight?: number, value?: E) {
|
|
18
|
+
super(weight, value);
|
|
19
|
+
this.src = srcOrV1;
|
|
20
|
+
this.dest = destOrV2;
|
|
21
|
+
this.data = value;
|
|
22
|
+
this._setHashCode('');
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
class MyGraph<
|
|
27
|
+
V = any,
|
|
28
|
+
E = any,
|
|
29
|
+
VO extends MyVertex<V> = MyVertex<V>,
|
|
30
|
+
EO extends MyEdge<E> = MyEdge<E>
|
|
31
|
+
> extends AbstractGraph<V, E, VO, EO> {
|
|
32
|
+
createVertex(key: VertexKey, value?: V): VO {
|
|
33
|
+
return new MyVertex(key, value) as VO;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
createEdge(srcOrV1: VertexKey, destOrV2: VertexKey, weight?: number, value?: E): EO {
|
|
37
|
+
return new MyEdge(srcOrV1, destOrV2, weight, value) as EO;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
deleteEdge(edge: EO): EO | null {
|
|
41
|
+
return edge;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
getEdge(srcOrKey: VertexKey, destOrKey: VertexKey): EO | null {
|
|
45
|
+
return new MyEdge(srcOrKey, destOrKey) as EO;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
degreeOf(vertexOrKey: VO | VertexKey): number {
|
|
49
|
+
return 1 ?? Number(vertexOrKey);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
edgeSet(): EO[] {
|
|
53
|
+
return [new MyEdge('a', 'b') as EO];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
edgesOf(vertexOrKey: VO | VertexKey): EO[] {
|
|
57
|
+
const a = typeof vertexOrKey === "string" ? vertexOrKey : "a";
|
|
58
|
+
return [new MyEdge(a, 'b') as EO];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
getNeighbors(vertexOrKey: VO | VertexKey): VO[] {
|
|
62
|
+
const a = typeof vertexOrKey === "string" ? vertexOrKey : "a";
|
|
63
|
+
return [new MyVertex(a, 'b') as VO];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
getEndsOfEdge(edge: EO): [VO, VO] | null {
|
|
67
|
+
return edge ? null : null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
protected _addEdgeOnly(edge: EO): boolean {
|
|
71
|
+
return edge ? true : true;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
1
75
|
describe('AbstractGraph Operation Test', () => {
|
|
2
|
-
|
|
3
|
-
|
|
76
|
+
const myGraph: MyGraph<number, string> = new MyGraph<number, string>();
|
|
77
|
+
|
|
78
|
+
beforeEach(() => {
|
|
79
|
+
});
|
|
80
|
+
it('should edge cases', function () {
|
|
81
|
+
myGraph.addVertex('A', 1);
|
|
82
|
+
myGraph.addVertex('B', 2);
|
|
83
|
+
myGraph.addVertex('C', 3);
|
|
84
|
+
myGraph.addVertex('D', 4);
|
|
85
|
+
|
|
86
|
+
const vA = myGraph.getVertex('A');
|
|
87
|
+
// const vB = myGraph.getVertex('B');
|
|
88
|
+
// const vC = myGraph.getVertex('C');
|
|
89
|
+
// const vD = myGraph.getVertex('D');
|
|
90
|
+
|
|
91
|
+
const eAB = new MyEdge('A', 'B');
|
|
92
|
+
// const eBC = new MyEdge('B', 'C');
|
|
93
|
+
// const eCD = new MyEdge('C', 'D');
|
|
94
|
+
vA!.key = vA?.key || 1;
|
|
95
|
+
vA!.value = vA?.value ?? 2;
|
|
96
|
+
|
|
97
|
+
eAB!.value = eAB.value;
|
|
98
|
+
const hs = eAB.hashCode;
|
|
99
|
+
|
|
100
|
+
expect(hs).toBe('');
|
|
4
101
|
});
|
|
5
102
|
});
|