graph-typed 1.39.5 → 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/dist/data-structures/binary-tree/avl-tree.d.ts +6 -6
- package/dist/data-structures/binary-tree/avl-tree.js +13 -13
- package/dist/data-structures/binary-tree/binary-tree.d.ts +7 -7
- package/dist/data-structures/binary-tree/binary-tree.js +17 -17
- package/dist/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/data-structures/binary-tree/bst.js +13 -13
- package/dist/data-structures/binary-tree/rb-tree.d.ts +2 -2
- package/dist/data-structures/binary-tree/rb-tree.js +4 -4
- package/dist/data-structures/binary-tree/segment-tree.d.ts +7 -7
- package/dist/data-structures/binary-tree/segment-tree.js +16 -16
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +6 -6
- package/dist/data-structures/binary-tree/tree-multiset.js +18 -18
- package/dist/data-structures/graph/abstract-graph.d.ts +17 -17
- package/dist/data-structures/graph/abstract-graph.js +24 -24
- package/dist/data-structures/graph/directed-graph.d.ts +12 -12
- package/dist/data-structures/graph/directed-graph.js +15 -15
- package/dist/data-structures/graph/map-graph.d.ts +9 -9
- package/dist/data-structures/graph/map-graph.js +13 -13
- package/dist/data-structures/graph/undirected-graph.d.ts +11 -11
- package/dist/data-structures/graph/undirected-graph.js +14 -14
- package/dist/data-structures/hash/hash-table.d.ts +4 -4
- package/dist/data-structures/hash/hash-table.js +8 -8
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +31 -31
- package/dist/data-structures/linked-list/doubly-linked-list.js +54 -54
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +24 -24
- package/dist/data-structures/linked-list/singly-linked-list.js +52 -52
- package/dist/data-structures/queue/queue.js +1 -1
- package/dist/interfaces/binary-tree.d.ts +2 -2
- package/dist/interfaces/graph.d.ts +2 -2
- package/package.json +2 -2
- 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 +34 -34
- package/src/data-structures/graph/directed-graph.ts +16 -16
- package/src/data-structures/graph/map-graph.ts +13 -13
- package/src/data-structures/graph/undirected-graph.ts +15 -15
- 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 +1 -1
- package/src/interfaces/binary-tree.ts +2 -2
- package/src/interfaces/graph.ts +2 -2
|
@@ -11,18 +11,18 @@ exports.SinglyLinkedList = exports.SinglyLinkedListNode = void 0;
|
|
|
11
11
|
class SinglyLinkedListNode {
|
|
12
12
|
/**
|
|
13
13
|
* The constructor function initializes an instance of a class with a given value and sets the next property to null.
|
|
14
|
-
* @param {E}
|
|
14
|
+
* @param {E} value - The "value" parameter is of type E, which means it can be any data type. It represents the value that
|
|
15
15
|
* will be stored in the node of a linked list.
|
|
16
16
|
*/
|
|
17
|
-
constructor(
|
|
18
|
-
this.
|
|
17
|
+
constructor(value) {
|
|
18
|
+
this._value = value;
|
|
19
19
|
this._next = null;
|
|
20
20
|
}
|
|
21
|
-
get
|
|
22
|
-
return this.
|
|
21
|
+
get value() {
|
|
22
|
+
return this._value;
|
|
23
23
|
}
|
|
24
|
-
set
|
|
25
|
-
this.
|
|
24
|
+
set value(value) {
|
|
25
|
+
this._value = value;
|
|
26
26
|
}
|
|
27
27
|
get next() {
|
|
28
28
|
return this._next;
|
|
@@ -70,12 +70,12 @@ class SinglyLinkedList {
|
|
|
70
70
|
return singlyLinkedList;
|
|
71
71
|
}
|
|
72
72
|
/**
|
|
73
|
-
* The `push` function adds a new node with the given
|
|
74
|
-
* @param {E}
|
|
73
|
+
* The `push` function adds a new node with the given value to the end of a singly linked list.
|
|
74
|
+
* @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
|
|
75
75
|
* any type (E) as specified in the generic type declaration of the class or function.
|
|
76
76
|
*/
|
|
77
|
-
push(
|
|
78
|
-
const newNode = new SinglyLinkedListNode(
|
|
77
|
+
push(value) {
|
|
78
|
+
const newNode = new SinglyLinkedListNode(value);
|
|
79
79
|
if (!this.head) {
|
|
80
80
|
this.head = newNode;
|
|
81
81
|
this.tail = newNode;
|
|
@@ -87,12 +87,12 @@ class SinglyLinkedList {
|
|
|
87
87
|
this._length++;
|
|
88
88
|
}
|
|
89
89
|
/**
|
|
90
|
-
* The `push` function adds a new node with the given
|
|
91
|
-
* @param {E}
|
|
90
|
+
* The `push` function adds a new node with the given value to the end of a singly linked list.
|
|
91
|
+
* @param {E} value - The "value" parameter represents the value that you want to add to the linked list. It can be of
|
|
92
92
|
* any type (E) as specified in the generic type declaration of the class or function.
|
|
93
93
|
*/
|
|
94
|
-
addLast(
|
|
95
|
-
this.push(
|
|
94
|
+
addLast(value) {
|
|
95
|
+
this.push(value);
|
|
96
96
|
}
|
|
97
97
|
/**
|
|
98
98
|
* The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
@@ -104,21 +104,21 @@ class SinglyLinkedList {
|
|
|
104
104
|
if (!this.head)
|
|
105
105
|
return undefined;
|
|
106
106
|
if (this.head === this.tail) {
|
|
107
|
-
const
|
|
107
|
+
const value = this.head.value;
|
|
108
108
|
this.head = null;
|
|
109
109
|
this.tail = null;
|
|
110
110
|
this._length--;
|
|
111
|
-
return
|
|
111
|
+
return value;
|
|
112
112
|
}
|
|
113
113
|
let current = this.head;
|
|
114
114
|
while (current.next !== this.tail) {
|
|
115
115
|
current = current.next;
|
|
116
116
|
}
|
|
117
|
-
const
|
|
117
|
+
const value = this.tail.value;
|
|
118
118
|
current.next = null;
|
|
119
119
|
this.tail = current;
|
|
120
120
|
this._length--;
|
|
121
|
-
return
|
|
121
|
+
return value;
|
|
122
122
|
}
|
|
123
123
|
/**
|
|
124
124
|
* The `popLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
@@ -139,7 +139,7 @@ class SinglyLinkedList {
|
|
|
139
139
|
const removedNode = this.head;
|
|
140
140
|
this.head = this.head.next;
|
|
141
141
|
this._length--;
|
|
142
|
-
return removedNode.
|
|
142
|
+
return removedNode.value;
|
|
143
143
|
}
|
|
144
144
|
/**
|
|
145
145
|
* The `popFirst()` function removes and returns the value of the first node in a linked list.
|
|
@@ -150,11 +150,11 @@ class SinglyLinkedList {
|
|
|
150
150
|
}
|
|
151
151
|
/**
|
|
152
152
|
* The unshift function adds a new node with the given value to the beginning of a singly linked list.
|
|
153
|
-
* @param {E}
|
|
153
|
+
* @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
|
|
154
154
|
* linked list.
|
|
155
155
|
*/
|
|
156
|
-
unshift(
|
|
157
|
-
const newNode = new SinglyLinkedListNode(
|
|
156
|
+
unshift(value) {
|
|
157
|
+
const newNode = new SinglyLinkedListNode(value);
|
|
158
158
|
if (!this.head) {
|
|
159
159
|
this.head = newNode;
|
|
160
160
|
this.tail = newNode;
|
|
@@ -167,11 +167,11 @@ class SinglyLinkedList {
|
|
|
167
167
|
}
|
|
168
168
|
/**
|
|
169
169
|
* The addFirst function adds a new node with the given value to the beginning of a singly linked list.
|
|
170
|
-
* @param {E}
|
|
170
|
+
* @param {E} value - The parameter "value" represents the value of the new node that will be added to the beginning of the
|
|
171
171
|
* linked list.
|
|
172
172
|
*/
|
|
173
|
-
addFirst(
|
|
174
|
-
this.unshift(
|
|
173
|
+
addFirst(value) {
|
|
174
|
+
this.unshift(value);
|
|
175
175
|
}
|
|
176
176
|
/**
|
|
177
177
|
* The function `getAt` returns the value at a specified index in a linked list, or null if the index is out of range.
|
|
@@ -187,7 +187,7 @@ class SinglyLinkedList {
|
|
|
187
187
|
for (let i = 0; i < index; i++) {
|
|
188
188
|
current = current.next;
|
|
189
189
|
}
|
|
190
|
-
return current.
|
|
190
|
+
return current.value;
|
|
191
191
|
}
|
|
192
192
|
/**
|
|
193
193
|
* The function `getNodeAt` returns the node at a given index in a singly linked list.
|
|
@@ -221,7 +221,7 @@ class SinglyLinkedList {
|
|
|
221
221
|
const removedNode = prevNode.next;
|
|
222
222
|
prevNode.next = removedNode.next;
|
|
223
223
|
this._length--;
|
|
224
|
-
return removedNode.
|
|
224
|
+
return removedNode.value;
|
|
225
225
|
}
|
|
226
226
|
/**
|
|
227
227
|
* The delete function removes a node with a specific value from a singly linked list.
|
|
@@ -235,14 +235,14 @@ class SinglyLinkedList {
|
|
|
235
235
|
return false;
|
|
236
236
|
let value;
|
|
237
237
|
if (valueOrNode instanceof SinglyLinkedListNode) {
|
|
238
|
-
value = valueOrNode.
|
|
238
|
+
value = valueOrNode.value;
|
|
239
239
|
}
|
|
240
240
|
else {
|
|
241
241
|
value = valueOrNode;
|
|
242
242
|
}
|
|
243
243
|
let current = this.head, prev = null;
|
|
244
244
|
while (current) {
|
|
245
|
-
if (current.
|
|
245
|
+
if (current.value === value) {
|
|
246
246
|
if (prev === null) {
|
|
247
247
|
this.head = current.next;
|
|
248
248
|
if (current === this.tail) {
|
|
@@ -267,23 +267,23 @@ class SinglyLinkedList {
|
|
|
267
267
|
* The `insertAt` function inserts a value at a specified index in a singly linked list.
|
|
268
268
|
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
|
|
269
269
|
* linked list. It is of type number.
|
|
270
|
-
* @param {E}
|
|
270
|
+
* @param {E} value - The `value` parameter represents the value that you want to insert into the linked list at the
|
|
271
271
|
* specified index.
|
|
272
272
|
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
|
|
273
273
|
* if the index is out of bounds.
|
|
274
274
|
*/
|
|
275
|
-
insertAt(index,
|
|
275
|
+
insertAt(index, value) {
|
|
276
276
|
if (index < 0 || index > this.length)
|
|
277
277
|
return false;
|
|
278
278
|
if (index === 0) {
|
|
279
|
-
this.unshift(
|
|
279
|
+
this.unshift(value);
|
|
280
280
|
return true;
|
|
281
281
|
}
|
|
282
282
|
if (index === this.length) {
|
|
283
|
-
this.push(
|
|
283
|
+
this.push(value);
|
|
284
284
|
return true;
|
|
285
285
|
}
|
|
286
|
-
const newNode = new SinglyLinkedListNode(
|
|
286
|
+
const newNode = new SinglyLinkedListNode(value);
|
|
287
287
|
const prevNode = this.getNodeAt(index - 1);
|
|
288
288
|
newNode.next = prevNode.next;
|
|
289
289
|
prevNode.next = newNode;
|
|
@@ -314,7 +314,7 @@ class SinglyLinkedList {
|
|
|
314
314
|
const array = [];
|
|
315
315
|
let current = this.head;
|
|
316
316
|
while (current) {
|
|
317
|
-
array.push(current.
|
|
317
|
+
array.push(current.value);
|
|
318
318
|
current = current.next;
|
|
319
319
|
}
|
|
320
320
|
return array;
|
|
@@ -347,8 +347,8 @@ class SinglyLinkedList {
|
|
|
347
347
|
find(callback) {
|
|
348
348
|
let current = this.head;
|
|
349
349
|
while (current) {
|
|
350
|
-
if (callback(current.
|
|
351
|
-
return current.
|
|
350
|
+
if (callback(current.value)) {
|
|
351
|
+
return current.value;
|
|
352
352
|
}
|
|
353
353
|
current = current.next;
|
|
354
354
|
}
|
|
@@ -364,7 +364,7 @@ class SinglyLinkedList {
|
|
|
364
364
|
let index = 0;
|
|
365
365
|
let current = this.head;
|
|
366
366
|
while (current) {
|
|
367
|
-
if (current.
|
|
367
|
+
if (current.value === value) {
|
|
368
368
|
return index;
|
|
369
369
|
}
|
|
370
370
|
index++;
|
|
@@ -382,7 +382,7 @@ class SinglyLinkedList {
|
|
|
382
382
|
getNode(value) {
|
|
383
383
|
let current = this.head;
|
|
384
384
|
while (current) {
|
|
385
|
-
if (current.
|
|
385
|
+
if (current.value === value) {
|
|
386
386
|
return current;
|
|
387
387
|
}
|
|
388
388
|
current = current.next;
|
|
@@ -402,18 +402,18 @@ class SinglyLinkedList {
|
|
|
402
402
|
return false;
|
|
403
403
|
let existingValue;
|
|
404
404
|
if (existingValueOrNode instanceof SinglyLinkedListNode) {
|
|
405
|
-
existingValue = existingValueOrNode.
|
|
405
|
+
existingValue = existingValueOrNode.value;
|
|
406
406
|
}
|
|
407
407
|
else {
|
|
408
408
|
existingValue = existingValueOrNode;
|
|
409
409
|
}
|
|
410
|
-
if (this.head.
|
|
410
|
+
if (this.head.value === existingValue) {
|
|
411
411
|
this.unshift(newValue);
|
|
412
412
|
return true;
|
|
413
413
|
}
|
|
414
414
|
let current = this.head;
|
|
415
415
|
while (current.next) {
|
|
416
|
-
if (current.next.
|
|
416
|
+
if (current.next.value === existingValue) {
|
|
417
417
|
const newNode = new SinglyLinkedListNode(newValue);
|
|
418
418
|
newNode.next = current.next;
|
|
419
419
|
current.next = newNode;
|
|
@@ -461,7 +461,7 @@ class SinglyLinkedList {
|
|
|
461
461
|
let count = 0;
|
|
462
462
|
let current = this.head;
|
|
463
463
|
while (current) {
|
|
464
|
-
if (current.
|
|
464
|
+
if (current.value === value) {
|
|
465
465
|
count++;
|
|
466
466
|
}
|
|
467
467
|
current = current.next;
|
|
@@ -470,7 +470,7 @@ class SinglyLinkedList {
|
|
|
470
470
|
}
|
|
471
471
|
/**
|
|
472
472
|
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
473
|
-
* @param callback - The callback parameter is a function that takes two arguments:
|
|
473
|
+
* @param callback - The callback parameter is a function that takes two arguments: value and index. The value argument
|
|
474
474
|
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
475
475
|
* current node in the linked list.
|
|
476
476
|
*/
|
|
@@ -478,7 +478,7 @@ class SinglyLinkedList {
|
|
|
478
478
|
let current = this.head;
|
|
479
479
|
let index = 0;
|
|
480
480
|
while (current) {
|
|
481
|
-
callback(current.
|
|
481
|
+
callback(current.value, index);
|
|
482
482
|
current = current.next;
|
|
483
483
|
index++;
|
|
484
484
|
}
|
|
@@ -495,7 +495,7 @@ class SinglyLinkedList {
|
|
|
495
495
|
const mappedList = new SinglyLinkedList();
|
|
496
496
|
let current = this.head;
|
|
497
497
|
while (current) {
|
|
498
|
-
mappedList.push(callback(current.
|
|
498
|
+
mappedList.push(callback(current.value));
|
|
499
499
|
current = current.next;
|
|
500
500
|
}
|
|
501
501
|
return mappedList;
|
|
@@ -511,8 +511,8 @@ class SinglyLinkedList {
|
|
|
511
511
|
const filteredList = new SinglyLinkedList();
|
|
512
512
|
let current = this.head;
|
|
513
513
|
while (current) {
|
|
514
|
-
if (callback(current.
|
|
515
|
-
filteredList.push(current.
|
|
514
|
+
if (callback(current.value)) {
|
|
515
|
+
filteredList.push(current.value);
|
|
516
516
|
}
|
|
517
517
|
current = current.next;
|
|
518
518
|
}
|
|
@@ -521,7 +521,7 @@ class SinglyLinkedList {
|
|
|
521
521
|
/**
|
|
522
522
|
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
523
523
|
* single value.
|
|
524
|
-
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `
|
|
524
|
+
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `value`. It is
|
|
525
525
|
* used to perform a specific operation on each element of the linked list.
|
|
526
526
|
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
527
527
|
* point for the reduction operation.
|
|
@@ -532,7 +532,7 @@ class SinglyLinkedList {
|
|
|
532
532
|
let accumulator = initialValue;
|
|
533
533
|
let current = this.head;
|
|
534
534
|
while (current) {
|
|
535
|
-
accumulator = callback(accumulator, current.
|
|
535
|
+
accumulator = callback(accumulator, current.value);
|
|
536
536
|
current = current.next;
|
|
537
537
|
}
|
|
538
538
|
return accumulator;
|
|
@@ -543,7 +543,7 @@ class SinglyLinkedList {
|
|
|
543
543
|
*[Symbol.iterator]() {
|
|
544
544
|
let current = this.head;
|
|
545
545
|
while (current) {
|
|
546
|
-
yield current.
|
|
546
|
+
yield current.value;
|
|
547
547
|
current = current.next;
|
|
548
548
|
}
|
|
549
549
|
}
|
|
@@ -28,7 +28,7 @@ class SkipQueue extends linked_list_1.SinglyLinkedList {
|
|
|
28
28
|
*/
|
|
29
29
|
peek() {
|
|
30
30
|
var _a;
|
|
31
|
-
return (_a = this.head) === null || _a === void 0 ? void 0 : _a.
|
|
31
|
+
return (_a = this.head) === null || _a === void 0 ? void 0 : _a.value;
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
34
|
exports.SkipQueue = SkipQueue;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { BinaryTreeNode } from '../data-structures';
|
|
2
2
|
import { BinaryTreeDeletedResult, BTNKey, BinaryTreeNodeNested, BTNCallback } from '../types';
|
|
3
3
|
export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>> {
|
|
4
|
-
createNode(key: BTNKey,
|
|
5
|
-
add(keyOrNode: BTNKey | N | null,
|
|
4
|
+
createNode(key: BTNKey, value?: N['value']): N;
|
|
5
|
+
add(keyOrNode: BTNKey | N | null, value?: N['value']): N | null | undefined;
|
|
6
6
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeletedResult<N>[];
|
|
7
7
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { VertexKey } from '../types';
|
|
2
2
|
export interface IGraph<V, E, VO, EO> {
|
|
3
|
-
createVertex(key: VertexKey,
|
|
4
|
-
createEdge(srcOrV1: VertexKey
|
|
3
|
+
createVertex(key: VertexKey, value?: V): VO;
|
|
4
|
+
createEdge(srcOrV1: VertexKey, destOrV2: VertexKey, weight?: number, value?: E): EO;
|
|
5
5
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "graph-typed",
|
|
3
|
-
"version": "1.39.
|
|
3
|
+
"version": "1.39.6",
|
|
4
4
|
"description": "Graph. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -135,6 +135,6 @@
|
|
|
135
135
|
"typescript": "^4.9.5"
|
|
136
136
|
},
|
|
137
137
|
"dependencies": {
|
|
138
|
-
"data-structure-typed": "^1.39.
|
|
138
|
+
"data-structure-typed": "^1.39.6"
|
|
139
139
|
}
|
|
140
140
|
}
|
|
@@ -13,8 +13,8 @@ import {IBinaryTree} from '../../interfaces';
|
|
|
13
13
|
export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
14
14
|
height: number;
|
|
15
15
|
|
|
16
|
-
constructor(key: BTNKey,
|
|
17
|
-
super(key,
|
|
16
|
+
constructor(key: BTNKey, value?: V) {
|
|
17
|
+
super(key, value);
|
|
18
18
|
this.height = 0;
|
|
19
19
|
}
|
|
20
20
|
}
|
|
@@ -37,13 +37,13 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
37
37
|
* The function creates a new AVL tree node with the specified key and value.
|
|
38
38
|
* @param {BTNKey} key - The key parameter is the key value that will be associated with
|
|
39
39
|
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
40
|
-
* @param [
|
|
41
|
-
* type `V`, which means it can be any value that is assignable to the `
|
|
40
|
+
* @param [value] - The parameter `value` is an optional value that can be assigned to the node. It is of
|
|
41
|
+
* type `V`, which means it can be any value that is assignable to the `value` property of the
|
|
42
42
|
* node type `N`.
|
|
43
43
|
* @returns a new AVLTreeNode object with the specified key and value.
|
|
44
44
|
*/
|
|
45
|
-
override createNode(key: BTNKey,
|
|
46
|
-
return new AVLTreeNode<V, N>(key,
|
|
45
|
+
override createNode(key: BTNKey, value?: V): N {
|
|
46
|
+
return new AVLTreeNode<V, N>(key, value) as N;
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
/**
|
|
@@ -51,13 +51,13 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
51
51
|
* a new node.
|
|
52
52
|
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can accept either a
|
|
53
53
|
* `BTNKey` or a `N` (which represents a node in the binary tree) or `null`.
|
|
54
|
-
* @param [
|
|
54
|
+
* @param [value] - The `value` parameter is the value that you want to assign to the new node that you
|
|
55
55
|
* are adding to the binary search tree.
|
|
56
56
|
* @returns The method is returning the inserted node (`N`), `null`, or `undefined`.
|
|
57
57
|
*/
|
|
58
|
-
override add(keyOrNode: BTNKey | N | null,
|
|
58
|
+
override add(keyOrNode: BTNKey | N | null, value?: V): N | null | undefined {
|
|
59
59
|
// TODO support node as a param
|
|
60
|
-
const inserted = super.add(keyOrNode,
|
|
60
|
+
const inserted = super.add(keyOrNode, value);
|
|
61
61
|
if (inserted) this._balancePath(inserted);
|
|
62
62
|
return inserted;
|
|
63
63
|
}
|
|
@@ -97,18 +97,18 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
97
97
|
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
98
98
|
*/
|
|
99
99
|
protected override _swap(srcNode: N, destNode: N): N {
|
|
100
|
-
const {key,
|
|
101
|
-
const tempNode = this.createNode(key,
|
|
100
|
+
const {key, value, height} = destNode;
|
|
101
|
+
const tempNode = this.createNode(key, value);
|
|
102
102
|
|
|
103
103
|
if (tempNode) {
|
|
104
104
|
tempNode.height = height;
|
|
105
105
|
|
|
106
106
|
destNode.key = srcNode.key;
|
|
107
|
-
destNode.
|
|
107
|
+
destNode.value = srcNode.value;
|
|
108
108
|
destNode.height = srcNode.height;
|
|
109
109
|
|
|
110
110
|
srcNode.key = tempNode.key;
|
|
111
|
-
srcNode.
|
|
111
|
+
srcNode.value = tempNode.value;
|
|
112
112
|
srcNode.height = tempNode.height;
|
|
113
113
|
}
|
|
114
114
|
|
|
@@ -26,7 +26,7 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
|
|
|
26
26
|
/**
|
|
27
27
|
* The value stored in the node.
|
|
28
28
|
*/
|
|
29
|
-
|
|
29
|
+
value: V | undefined;
|
|
30
30
|
|
|
31
31
|
/**
|
|
32
32
|
* The parent node of the current node.
|
|
@@ -36,11 +36,11 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
|
|
|
36
36
|
/**
|
|
37
37
|
* Creates a new instance of BinaryTreeNode.
|
|
38
38
|
* @param {BTNKey} key - The key associated with the node.
|
|
39
|
-
* @param {V}
|
|
39
|
+
* @param {V} value - The value stored in the node.
|
|
40
40
|
*/
|
|
41
|
-
constructor(key: BTNKey,
|
|
41
|
+
constructor(key: BTNKey, value?: V) {
|
|
42
42
|
this.key = key;
|
|
43
|
-
this.
|
|
43
|
+
this.value = value;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
private _left: N | null | undefined;
|
|
@@ -159,11 +159,11 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
159
159
|
/**
|
|
160
160
|
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
161
161
|
* @param {BTNKey} key - The key for the new node.
|
|
162
|
-
* @param {V}
|
|
162
|
+
* @param {V} value - The value for the new node.
|
|
163
163
|
* @returns {N} - The newly created BinaryTreeNode.
|
|
164
164
|
*/
|
|
165
|
-
createNode(key: BTNKey,
|
|
166
|
-
return new BinaryTreeNode<V, N>(key,
|
|
165
|
+
createNode(key: BTNKey, value?: V): N {
|
|
166
|
+
return new BinaryTreeNode<V, N>(key, value) as N;
|
|
167
167
|
}
|
|
168
168
|
|
|
169
169
|
/**
|
|
@@ -185,10 +185,10 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
185
185
|
/**
|
|
186
186
|
* Add a node with the given key and value to the binary tree.
|
|
187
187
|
* @param {BTNKey | N | null} keyOrNode - The key or node to add to the binary tree.
|
|
188
|
-
* @param {V}
|
|
188
|
+
* @param {V} value - The value for the new node (optional).
|
|
189
189
|
* @returns {N | null | undefined} - The inserted node, or null if nothing was inserted, or undefined if the operation failed.
|
|
190
190
|
*/
|
|
191
|
-
add(keyOrNode: BTNKey | N | null,
|
|
191
|
+
add(keyOrNode: BTNKey | N | null, value?: V): N | null | undefined {
|
|
192
192
|
const _bfs = (root: N, newNode: N | null): N | undefined | null => {
|
|
193
193
|
const queue = new Queue<N | null>([root]);
|
|
194
194
|
while (queue.size > 0) {
|
|
@@ -209,7 +209,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
209
209
|
if (keyOrNode === null) {
|
|
210
210
|
needInsert = null;
|
|
211
211
|
} else if (typeof keyOrNode === 'number') {
|
|
212
|
-
needInsert = this.createNode(keyOrNode,
|
|
212
|
+
needInsert = this.createNode(keyOrNode, value);
|
|
213
213
|
} else if (keyOrNode instanceof BinaryTreeNode) {
|
|
214
214
|
needInsert = keyOrNode;
|
|
215
215
|
} else {
|
|
@@ -221,7 +221,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
221
221
|
|
|
222
222
|
if (this.root) {
|
|
223
223
|
if (existNode) {
|
|
224
|
-
existNode.
|
|
224
|
+
existNode.value = value;
|
|
225
225
|
inserted = existNode;
|
|
226
226
|
} else {
|
|
227
227
|
inserted = _bfs(this.root, needInsert);
|
|
@@ -252,15 +252,15 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
252
252
|
// TODO not sure addMany not be run multi times
|
|
253
253
|
return keysOrNodes.map((keyOrNode, i) => {
|
|
254
254
|
if (keyOrNode instanceof BinaryTreeNode) {
|
|
255
|
-
return this.add(keyOrNode.key, keyOrNode.
|
|
255
|
+
return this.add(keyOrNode.key, keyOrNode.value);
|
|
256
256
|
}
|
|
257
257
|
|
|
258
258
|
if (keyOrNode === null) {
|
|
259
259
|
return this.add(null);
|
|
260
260
|
}
|
|
261
261
|
|
|
262
|
-
const
|
|
263
|
-
return this.add(keyOrNode,
|
|
262
|
+
const value = values?.[i];
|
|
263
|
+
return this.add(keyOrNode, value);
|
|
264
264
|
});
|
|
265
265
|
}
|
|
266
266
|
|
|
@@ -1212,15 +1212,15 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|
|
1212
1212
|
* @returns {N} - The destination node after the swap.
|
|
1213
1213
|
*/
|
|
1214
1214
|
protected _swap(srcNode: N, destNode: N): N {
|
|
1215
|
-
const {key,
|
|
1216
|
-
const tempNode = this.createNode(key,
|
|
1215
|
+
const {key, value} = destNode;
|
|
1216
|
+
const tempNode = this.createNode(key, value);
|
|
1217
1217
|
|
|
1218
1218
|
if (tempNode) {
|
|
1219
1219
|
destNode.key = srcNode.key;
|
|
1220
|
-
destNode.
|
|
1220
|
+
destNode.value = srcNode.value;
|
|
1221
1221
|
|
|
1222
1222
|
srcNode.key = tempNode.key;
|
|
1223
|
-
srcNode.
|
|
1223
|
+
srcNode.value = tempNode.value;
|
|
1224
1224
|
}
|
|
1225
1225
|
|
|
1226
1226
|
return destNode;
|
|
@@ -12,8 +12,8 @@ import {IBinaryTree} from '../../interfaces';
|
|
|
12
12
|
import {Queue} from '../queue';
|
|
13
13
|
|
|
14
14
|
export class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extends BinaryTreeNode<V, N> {
|
|
15
|
-
constructor(key: BTNKey,
|
|
16
|
-
super(key,
|
|
15
|
+
constructor(key: BTNKey, value?: V) {
|
|
16
|
+
super(key, value);
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
|
|
@@ -41,12 +41,12 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
41
41
|
* The function creates a new binary search tree node with the given key and value.
|
|
42
42
|
* @param {BTNKey} key - The key parameter is the key value that will be associated with
|
|
43
43
|
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
44
|
-
* @param [
|
|
44
|
+
* @param [value] - The parameter `value` is an optional value that can be assigned to the node. It
|
|
45
45
|
* represents the value associated with the node in a binary search tree.
|
|
46
46
|
* @returns a new instance of the BSTNode class with the specified key and value.
|
|
47
47
|
*/
|
|
48
|
-
override createNode(key: BTNKey,
|
|
49
|
-
return new BSTNode<V, N>(key,
|
|
48
|
+
override createNode(key: BTNKey, value?: V): N {
|
|
49
|
+
return new BSTNode<V, N>(key, value) as N;
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
/**
|
|
@@ -54,19 +54,19 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
54
54
|
* into the tree.
|
|
55
55
|
* @param {BTNKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
|
56
56
|
* `BTNKey` (which can be a number or a string), a `BSTNode` object, or `null`.
|
|
57
|
-
* @param [
|
|
57
|
+
* @param [value] - The `value` parameter is the value to be assigned to the new node being added to the
|
|
58
58
|
* binary search tree.
|
|
59
59
|
* @returns the inserted node (N) if it was successfully added to the binary search tree. If the node
|
|
60
60
|
* was not added or if the parameters were invalid, it returns null or undefined.
|
|
61
61
|
*/
|
|
62
|
-
override add(keyOrNode: BTNKey | N | null,
|
|
62
|
+
override add(keyOrNode: BTNKey | N | null, value?: V): N | null | undefined {
|
|
63
63
|
// TODO support node as a parameter
|
|
64
64
|
let inserted: N | null = null;
|
|
65
65
|
let newNode: N | null = null;
|
|
66
66
|
if (keyOrNode instanceof BSTNode) {
|
|
67
67
|
newNode = keyOrNode;
|
|
68
68
|
} else if (typeof keyOrNode === 'number') {
|
|
69
|
-
newNode = this.createNode(keyOrNode,
|
|
69
|
+
newNode = this.createNode(keyOrNode, value);
|
|
70
70
|
} else if (keyOrNode === null) {
|
|
71
71
|
newNode = null;
|
|
72
72
|
}
|
|
@@ -81,7 +81,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
81
81
|
if (cur !== null && newNode !== null) {
|
|
82
82
|
if (this._compare(cur.key, newNode.key) === CP.eq) {
|
|
83
83
|
if (newNode) {
|
|
84
|
-
cur.
|
|
84
|
+
cur.value = newNode.value;
|
|
85
85
|
}
|
|
86
86
|
//Duplicates are not accepted.
|
|
87
87
|
traversing = false;
|
|
@@ -128,7 +128,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
128
128
|
/**
|
|
129
129
|
* The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
|
|
130
130
|
* maintaining balance.
|
|
131
|
-
* @param {[BTNKey | N, N['
|
|
131
|
+
* @param {[BTNKey | N, N['value']][]} keysOrNodes - The `arr` parameter in the `addMany` function
|
|
132
132
|
* represents an array of keys or nodes that need to be added to the binary search tree. It can be an
|
|
133
133
|
* array of `BTNKey` or `N` (which represents the node type in the binary search tree) or
|
|
134
134
|
* `null
|
|
@@ -154,15 +154,15 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
154
154
|
return super.addMany(keysOrNodes, data);
|
|
155
155
|
}
|
|
156
156
|
const inserted: (N | null | undefined)[] = [];
|
|
157
|
-
const combinedArr: [BTNKey | N, N['
|
|
157
|
+
const combinedArr: [BTNKey | N, N['value']][] = keysOrNodes.map((value, index) => [value, data?.[index]]);
|
|
158
158
|
let sorted = [];
|
|
159
159
|
|
|
160
|
-
function isNodeOrNullTuple(arr: [BTNKey | N, N['
|
|
160
|
+
function isNodeOrNullTuple(arr: [BTNKey | N, N['value']][]): arr is [N, N['value']][] {
|
|
161
161
|
for (const [keyOrNode] of arr) if (keyOrNode instanceof BSTNode) return true;
|
|
162
162
|
return false;
|
|
163
163
|
}
|
|
164
164
|
|
|
165
|
-
function isBinaryTreeKeyOrNullTuple(arr: [BTNKey | N, N['
|
|
165
|
+
function isBinaryTreeKeyOrNullTuple(arr: [BTNKey | N, N['value']][]): arr is [BTNKey, N['value']][] {
|
|
166
166
|
for (const [keyOrNode] of arr) if (typeof keyOrNode === 'number') return true;
|
|
167
167
|
return false;
|
|
168
168
|
}
|
|
@@ -178,7 +178,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
178
178
|
throw new Error('Invalid input keysOrNodes');
|
|
179
179
|
}
|
|
180
180
|
sortedKeysOrNodes = sorted.map(([keyOrNode]) => keyOrNode);
|
|
181
|
-
sortedData = sorted.map(([,
|
|
181
|
+
sortedData = sorted.map(([, value]) => value);
|
|
182
182
|
const recursive = (arr: (BTNKey | null | N)[], data?: (V | undefined)[]) => {
|
|
183
183
|
if (arr.length === 0) return;
|
|
184
184
|
|
|
@@ -426,7 +426,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
426
426
|
if (l > r) return;
|
|
427
427
|
const m = l + Math.floor((r - l) / 2);
|
|
428
428
|
const midNode = sorted[m];
|
|
429
|
-
this.add(midNode.key, midNode.
|
|
429
|
+
this.add(midNode.key, midNode.value);
|
|
430
430
|
buildBalanceBST(l, m - 1);
|
|
431
431
|
buildBalanceBST(m + 1, r);
|
|
432
432
|
};
|
|
@@ -442,7 +442,7 @@ export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>
|
|
|
442
442
|
if (l <= r) {
|
|
443
443
|
const m = l + Math.floor((r - l) / 2);
|
|
444
444
|
const midNode = sorted[m];
|
|
445
|
-
this.add(midNode.key, midNode.
|
|
445
|
+
this.add(midNode.key, midNode.value);
|
|
446
446
|
stack.push([m + 1, r]);
|
|
447
447
|
stack.push([l, m - 1]);
|
|
448
448
|
}
|