data-structure-typed 1.39.0 → 1.39.2
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 +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +2 -2
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +96 -32
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +46 -8
- 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 +2 -2
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multiset.d.ts +2 -2
- package/dist/cjs/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.d.ts +1 -1
- package/dist/cjs/data-structures/graph/map-graph.js +1 -1
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +46 -28
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +59 -49
- 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 +75 -7
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +110 -9
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix2d.d.ts +1 -2
- package/dist/cjs/data-structures/matrix/matrix2d.js +3 -7
- package/dist/cjs/data-structures/matrix/matrix2d.js.map +1 -1
- package/dist/cjs/data-structures/matrix/vector2d.d.ts +0 -1
- package/dist/cjs/data-structures/matrix/vector2d.js +0 -1
- package/dist/cjs/data-structures/matrix/vector2d.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +20 -20
- package/dist/cjs/data-structures/queue/deque.js +22 -22
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +3 -3
- package/dist/cjs/data-structures/queue/queue.js +3 -3
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +2 -2
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +0 -2
- package/dist/cjs/types/helpers.d.ts +1 -4
- package/dist/cjs/types/helpers.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +2 -2
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +96 -32
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +46 -8
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +6 -6
- package/dist/mjs/data-structures/binary-tree/bst.js +2 -2
- package/dist/mjs/data-structures/binary-tree/tree-multiset.d.ts +2 -2
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +1 -1
- package/dist/mjs/data-structures/graph/abstract-graph.js +1 -1
- package/dist/mjs/data-structures/graph/map-graph.d.ts +1 -1
- package/dist/mjs/data-structures/graph/map-graph.js +1 -1
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +46 -28
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +59 -49
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +75 -7
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +110 -9
- package/dist/mjs/data-structures/matrix/matrix2d.d.ts +1 -2
- package/dist/mjs/data-structures/matrix/matrix2d.js +3 -7
- package/dist/mjs/data-structures/matrix/vector2d.d.ts +0 -1
- package/dist/mjs/data-structures/matrix/vector2d.js +0 -1
- package/dist/mjs/data-structures/queue/deque.d.ts +20 -20
- package/dist/mjs/data-structures/queue/deque.js +22 -22
- package/dist/mjs/data-structures/queue/queue.d.ts +3 -3
- package/dist/mjs/data-structures/queue/queue.js +3 -3
- package/dist/mjs/interfaces/binary-tree.d.ts +2 -2
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +0 -2
- package/dist/mjs/types/helpers.d.ts +1 -4
- 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 +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +76 -90
- package/src/data-structures/binary-tree/bst.ts +9 -16
- package/src/data-structures/binary-tree/tree-multiset.ts +2 -2
- package/src/data-structures/graph/abstract-graph.ts +1 -1
- package/src/data-structures/graph/map-graph.ts +2 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +62 -56
- package/src/data-structures/linked-list/singly-linked-list.ts +118 -13
- package/src/data-structures/matrix/matrix2d.ts +1 -3
- package/src/data-structures/matrix/vector2d.ts +0 -2
- package/src/data-structures/queue/deque.ts +22 -22
- package/src/data-structures/queue/queue.ts +3 -3
- package/src/interfaces/binary-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/binary-tree.ts +0 -4
- package/src/types/helpers.ts +1 -7
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +141 -1
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +289 -47
- package/test/unit/data-structures/binary-tree/bst.test.ts +391 -1
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +73 -7
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +241 -186
- package/test/unit/data-structures/graph/directed-graph.test.ts +34 -4
- package/test/unit/data-structures/graph/map-graph.test.ts +82 -1
- package/test/unit/data-structures/graph/undirected-graph.test.ts +82 -0
- package/test/unit/data-structures/hash/coordinate-map.test.ts +20 -0
- package/test/unit/data-structures/hash/coordinate-set.test.ts +25 -0
- package/test/unit/data-structures/hash/hash-table.test.ts +3 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +69 -6
- package/test/unit/data-structures/linked-list/linked-list.test.ts +2 -2
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +59 -12
- package/test/unit/data-structures/matrix/matrix2d.test.ts +207 -0
- package/test/unit/data-structures/matrix/navigator.test.ts +166 -1
- package/test/unit/data-structures/matrix/vector2d.test.ts +171 -0
- package/test/unit/data-structures/queue/deque.test.ts +283 -20
- package/test/unit/data-structures/queue/queue.test.ts +10 -8
|
@@ -74,16 +74,13 @@ class SinglyLinkedList {
|
|
|
74
74
|
}
|
|
75
75
|
return singlyLinkedList;
|
|
76
76
|
}
|
|
77
|
-
getLength() {
|
|
78
|
-
return this._length;
|
|
79
|
-
}
|
|
80
77
|
/**
|
|
81
|
-
* The `push` function adds a new node with the given
|
|
82
|
-
* @param {E}
|
|
78
|
+
* The `push` function adds a new node with the given val to the end of a singly linked list.
|
|
79
|
+
* @param {E} val - The "val" parameter represents the value that you want to add to the linked list. It can be of
|
|
83
80
|
* any type (E) as specified in the generic type declaration of the class or function.
|
|
84
81
|
*/
|
|
85
|
-
push(
|
|
86
|
-
const newNode = new SinglyLinkedListNode(
|
|
82
|
+
push(val) {
|
|
83
|
+
const newNode = new SinglyLinkedListNode(val);
|
|
87
84
|
if (!this.head) {
|
|
88
85
|
this.head = newNode;
|
|
89
86
|
this.tail = newNode;
|
|
@@ -94,6 +91,14 @@ class SinglyLinkedList {
|
|
|
94
91
|
}
|
|
95
92
|
this._length++;
|
|
96
93
|
}
|
|
94
|
+
/**
|
|
95
|
+
* The `push` function adds a new node with the given val to the end of a singly linked list.
|
|
96
|
+
* @param {E} val - The "val" parameter represents the value that you want to add to the linked list. It can be of
|
|
97
|
+
* any type (E) as specified in the generic type declaration of the class or function.
|
|
98
|
+
*/
|
|
99
|
+
addLast(val) {
|
|
100
|
+
this.push(val);
|
|
101
|
+
}
|
|
97
102
|
/**
|
|
98
103
|
* The `pop()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
99
104
|
* pointers accordingly.
|
|
@@ -120,6 +125,15 @@ class SinglyLinkedList {
|
|
|
120
125
|
this._length--;
|
|
121
126
|
return val;
|
|
122
127
|
}
|
|
128
|
+
/**
|
|
129
|
+
* The `popLast()` function removes and returns the value of the last element in a linked list, updating the head and tail
|
|
130
|
+
* pointers accordingly.
|
|
131
|
+
* @returns The method `pop()` returns the value of the node that is being removed from the end of the linked list. If
|
|
132
|
+
* the linked list is empty, it returns `null`.
|
|
133
|
+
*/
|
|
134
|
+
popLast() {
|
|
135
|
+
return this.pop();
|
|
136
|
+
}
|
|
123
137
|
/**
|
|
124
138
|
* The `shift()` function removes and returns the value of the first node in a linked list.
|
|
125
139
|
* @returns The value of the node that is being removed from the beginning of the linked list.
|
|
@@ -132,6 +146,13 @@ class SinglyLinkedList {
|
|
|
132
146
|
this._length--;
|
|
133
147
|
return removedNode.val;
|
|
134
148
|
}
|
|
149
|
+
/**
|
|
150
|
+
* The `popFirst()` function removes and returns the value of the first node in a linked list.
|
|
151
|
+
* @returns The value of the node that is being removed from the beginning of the linked list.
|
|
152
|
+
*/
|
|
153
|
+
popFirst() {
|
|
154
|
+
return this.shift();
|
|
155
|
+
}
|
|
135
156
|
/**
|
|
136
157
|
* The unshift function adds a new node with the given value to the beginning of a singly linked list.
|
|
137
158
|
* @param {E} val - The parameter "val" represents the value of the new node that will be added to the beginning of the
|
|
@@ -149,6 +170,14 @@ class SinglyLinkedList {
|
|
|
149
170
|
}
|
|
150
171
|
this._length++;
|
|
151
172
|
}
|
|
173
|
+
/**
|
|
174
|
+
* The addFirst function adds a new node with the given value to the beginning of a singly linked list.
|
|
175
|
+
* @param {E} val - The parameter "val" represents the value of the new node that will be added to the beginning of the
|
|
176
|
+
* linked list.
|
|
177
|
+
*/
|
|
178
|
+
addFirst(val) {
|
|
179
|
+
this.unshift(val);
|
|
180
|
+
}
|
|
152
181
|
/**
|
|
153
182
|
* The function `getAt` returns the value at a specified index in a linked list, or null if the index is out of range.
|
|
154
183
|
* @param {number} index - The index parameter is a number that represents the position of the element we want to
|
|
@@ -355,7 +384,7 @@ class SinglyLinkedList {
|
|
|
355
384
|
* @returns a `SinglyLinkedListNode<E>` if a node with the specified value is found in the linked list. If no node with
|
|
356
385
|
* the specified value is found, the function returns `null`.
|
|
357
386
|
*/
|
|
358
|
-
|
|
387
|
+
getNode(value) {
|
|
359
388
|
let current = this.head;
|
|
360
389
|
while (current) {
|
|
361
390
|
if (current.val === value) {
|
|
@@ -414,7 +443,7 @@ class SinglyLinkedList {
|
|
|
414
443
|
existingNode = existingValueOrNode;
|
|
415
444
|
}
|
|
416
445
|
else {
|
|
417
|
-
existingNode = this.
|
|
446
|
+
existingNode = this.getNode(existingValueOrNode);
|
|
418
447
|
}
|
|
419
448
|
if (existingNode) {
|
|
420
449
|
const newNode = new SinglyLinkedListNode(newValue);
|
|
@@ -444,6 +473,78 @@ class SinglyLinkedList {
|
|
|
444
473
|
}
|
|
445
474
|
return count;
|
|
446
475
|
}
|
|
476
|
+
/**
|
|
477
|
+
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
|
|
478
|
+
* @param callback - The callback parameter is a function that takes two arguments: val and index. The val argument
|
|
479
|
+
* represents the value of the current node in the linked list, and the index argument represents the index of the
|
|
480
|
+
* current node in the linked list.
|
|
481
|
+
*/
|
|
482
|
+
forEach(callback) {
|
|
483
|
+
let current = this.head;
|
|
484
|
+
let index = 0;
|
|
485
|
+
while (current) {
|
|
486
|
+
callback(current.val, index);
|
|
487
|
+
current = current.next;
|
|
488
|
+
index++;
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
/**
|
|
492
|
+
* The `map` function takes a callback function and applies it to each element in the SinglyLinkedList, returning a new
|
|
493
|
+
* SinglyLinkedList with the transformed values.
|
|
494
|
+
* @param callback - The callback parameter is a function that takes a value of type E (the type of values stored in
|
|
495
|
+
* the original SinglyLinkedList) and returns a value of type U (the type of values that will be stored in the mapped
|
|
496
|
+
* SinglyLinkedList).
|
|
497
|
+
* @returns The `map` function is returning a new instance of `SinglyLinkedList<U>` that contains the mapped values.
|
|
498
|
+
*/
|
|
499
|
+
map(callback) {
|
|
500
|
+
const mappedList = new SinglyLinkedList();
|
|
501
|
+
let current = this.head;
|
|
502
|
+
while (current) {
|
|
503
|
+
mappedList.push(callback(current.val));
|
|
504
|
+
current = current.next;
|
|
505
|
+
}
|
|
506
|
+
return mappedList;
|
|
507
|
+
}
|
|
508
|
+
/**
|
|
509
|
+
* The `filter` function iterates through a SinglyLinkedList and returns a new SinglyLinkedList containing only the
|
|
510
|
+
* elements that satisfy the given callback function.
|
|
511
|
+
* @param callback - The `callback` parameter is a function that takes a value of type `E` and returns a boolean value.
|
|
512
|
+
* It is used to determine whether a value should be included in the filtered list or not.
|
|
513
|
+
* @returns The filtered list, which is an instance of the SinglyLinkedList class.
|
|
514
|
+
*/
|
|
515
|
+
filter(callback) {
|
|
516
|
+
const filteredList = new SinglyLinkedList();
|
|
517
|
+
let current = this.head;
|
|
518
|
+
while (current) {
|
|
519
|
+
if (callback(current.val)) {
|
|
520
|
+
filteredList.push(current.val);
|
|
521
|
+
}
|
|
522
|
+
current = current.next;
|
|
523
|
+
}
|
|
524
|
+
return filteredList;
|
|
525
|
+
}
|
|
526
|
+
/**
|
|
527
|
+
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
|
|
528
|
+
* single value.
|
|
529
|
+
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `val`. It is
|
|
530
|
+
* used to perform a specific operation on each element of the linked list.
|
|
531
|
+
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
|
|
532
|
+
* point for the reduction operation.
|
|
533
|
+
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
|
|
534
|
+
* elements in the linked list.
|
|
535
|
+
*/
|
|
536
|
+
reduce(callback, initialValue) {
|
|
537
|
+
let accumulator = initialValue;
|
|
538
|
+
let current = this.head;
|
|
539
|
+
while (current) {
|
|
540
|
+
accumulator = callback(accumulator, current.val);
|
|
541
|
+
current = current.next;
|
|
542
|
+
}
|
|
543
|
+
return accumulator;
|
|
544
|
+
}
|
|
545
|
+
/**
|
|
546
|
+
* The function returns an iterator that iterates over the values of a linked list.
|
|
547
|
+
*/
|
|
447
548
|
*[Symbol.iterator]() {
|
|
448
549
|
let current = this.head;
|
|
449
550
|
while (current) {
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import Vector2D from './vector2d';
|
|
8
|
+
import { Vector2D } from './vector2d';
|
|
9
9
|
export declare class Matrix2D {
|
|
10
10
|
private readonly _matrix;
|
|
11
11
|
/**
|
|
@@ -105,4 +105,3 @@ export declare class Matrix2D {
|
|
|
105
105
|
*/
|
|
106
106
|
toVector(): Vector2D;
|
|
107
107
|
}
|
|
108
|
-
export default Matrix2D;
|
|
@@ -1,7 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.Matrix2D = void 0;
|
|
7
4
|
/**
|
|
@@ -11,7 +8,7 @@ exports.Matrix2D = void 0;
|
|
|
11
8
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
12
9
|
* @license MIT License
|
|
13
10
|
*/
|
|
14
|
-
const vector2d_1 =
|
|
11
|
+
const vector2d_1 = require("./vector2d");
|
|
15
12
|
class Matrix2D {
|
|
16
13
|
_matrix;
|
|
17
14
|
/**
|
|
@@ -24,7 +21,7 @@ class Matrix2D {
|
|
|
24
21
|
if (typeof value === 'undefined') {
|
|
25
22
|
this._matrix = Matrix2D.identity;
|
|
26
23
|
}
|
|
27
|
-
else if (value instanceof vector2d_1.
|
|
24
|
+
else if (value instanceof vector2d_1.Vector2D) {
|
|
28
25
|
this._matrix = Matrix2D.identity;
|
|
29
26
|
this._matrix[0][0] = value.x;
|
|
30
27
|
this._matrix[1][0] = value.y;
|
|
@@ -197,8 +194,7 @@ class Matrix2D {
|
|
|
197
194
|
* the first column of the matrix.
|
|
198
195
|
*/
|
|
199
196
|
toVector() {
|
|
200
|
-
return new vector2d_1.
|
|
197
|
+
return new vector2d_1.Vector2D(this._matrix[0][0], this._matrix[1][0]);
|
|
201
198
|
}
|
|
202
199
|
}
|
|
203
200
|
exports.Matrix2D = Matrix2D;
|
|
204
|
-
exports.default = Matrix2D;
|
|
@@ -37,25 +37,25 @@ export declare class ObjectDeque<E = number> {
|
|
|
37
37
|
*/
|
|
38
38
|
addLast(value: E): void;
|
|
39
39
|
/**
|
|
40
|
-
* The function `
|
|
40
|
+
* The function `popFirst()` removes and returns the first element in a data structure.
|
|
41
41
|
* @returns The value of the first element in the data structure.
|
|
42
42
|
*/
|
|
43
|
-
|
|
43
|
+
popFirst(): E | undefined;
|
|
44
44
|
/**
|
|
45
|
-
* The `
|
|
45
|
+
* The `getFirst` function returns the first element in an array-like data structure if it exists.
|
|
46
46
|
* @returns The element at the first position of the `_nodes` array.
|
|
47
47
|
*/
|
|
48
|
-
|
|
48
|
+
getFirst(): E | undefined;
|
|
49
49
|
/**
|
|
50
|
-
* The `
|
|
50
|
+
* The `popLast()` function removes and returns the last element in a data structure.
|
|
51
51
|
* @returns The value that was removed from the data structure.
|
|
52
52
|
*/
|
|
53
|
-
|
|
53
|
+
popLast(): E | undefined;
|
|
54
54
|
/**
|
|
55
|
-
* The `
|
|
55
|
+
* The `getLast()` function returns the last element in an array-like data structure.
|
|
56
56
|
* @returns The last element in the array "_nodes" is being returned.
|
|
57
57
|
*/
|
|
58
|
-
|
|
58
|
+
getLast(): E | undefined;
|
|
59
59
|
/**
|
|
60
60
|
* The get function returns the element at the specified index in an array-like data structure.
|
|
61
61
|
* @param {number} index - The index parameter is a number that represents the position of the element you want to
|
|
@@ -87,16 +87,16 @@ export declare class ArrayDeque<E> {
|
|
|
87
87
|
*/
|
|
88
88
|
addLast(value: E): number;
|
|
89
89
|
/**
|
|
90
|
-
* The function "
|
|
91
|
-
* @returns The method `
|
|
90
|
+
* The function "popLast" returns and removes the last element from an array, or returns null if the array is empty.
|
|
91
|
+
* @returns The method `popLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
92
92
|
*/
|
|
93
|
-
|
|
93
|
+
popLast(): E | null;
|
|
94
94
|
/**
|
|
95
|
-
* The `
|
|
96
|
-
* @returns The `
|
|
95
|
+
* The `popFirst` function removes and returns the first element from an array, or returns null if the array is empty.
|
|
96
|
+
* @returns The `popFirst()` function returns the first element of the `_nodes` array, or `null` if the array is
|
|
97
97
|
* empty.
|
|
98
98
|
*/
|
|
99
|
-
|
|
99
|
+
popFirst(): E | null;
|
|
100
100
|
/**
|
|
101
101
|
* O(n) time complexity of adding at the beginning and the end
|
|
102
102
|
*/
|
|
@@ -108,16 +108,16 @@ export declare class ArrayDeque<E> {
|
|
|
108
108
|
*/
|
|
109
109
|
addFirst(value: E): number;
|
|
110
110
|
/**
|
|
111
|
-
* The `
|
|
112
|
-
* @returns The function `
|
|
111
|
+
* The `getFirst` function returns the first element of an array or null if the array is empty.
|
|
112
|
+
* @returns The function `getFirst()` is returning the first element (`E`) of the `_nodes` array. If the array is
|
|
113
113
|
* empty, it will return `null`.
|
|
114
114
|
*/
|
|
115
|
-
|
|
115
|
+
getFirst(): E | null;
|
|
116
116
|
/**
|
|
117
|
-
* The `
|
|
118
|
-
* @returns The method `
|
|
117
|
+
* The `getLast` function returns the last element of an array or null if the array is empty.
|
|
118
|
+
* @returns The method `getLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
119
119
|
*/
|
|
120
|
-
|
|
120
|
+
getLast(): E | null;
|
|
121
121
|
/**
|
|
122
122
|
* O(1) time complexity of obtaining the value
|
|
123
123
|
*/
|
|
@@ -85,44 +85,44 @@ class ObjectDeque {
|
|
|
85
85
|
this._size++;
|
|
86
86
|
}
|
|
87
87
|
/**
|
|
88
|
-
* The function `
|
|
88
|
+
* The function `popFirst()` removes and returns the first element in a data structure.
|
|
89
89
|
* @returns The value of the first element in the data structure.
|
|
90
90
|
*/
|
|
91
|
-
|
|
91
|
+
popFirst() {
|
|
92
92
|
if (!this._size)
|
|
93
93
|
return;
|
|
94
|
-
const value = this.
|
|
94
|
+
const value = this.getFirst();
|
|
95
95
|
delete this._nodes[this._first];
|
|
96
96
|
this._first++;
|
|
97
97
|
this._size--;
|
|
98
98
|
return value;
|
|
99
99
|
}
|
|
100
100
|
/**
|
|
101
|
-
* The `
|
|
101
|
+
* The `getFirst` function returns the first element in an array-like data structure if it exists.
|
|
102
102
|
* @returns The element at the first position of the `_nodes` array.
|
|
103
103
|
*/
|
|
104
|
-
|
|
104
|
+
getFirst() {
|
|
105
105
|
if (this._size)
|
|
106
106
|
return this._nodes[this._first];
|
|
107
107
|
}
|
|
108
108
|
/**
|
|
109
|
-
* The `
|
|
109
|
+
* The `popLast()` function removes and returns the last element in a data structure.
|
|
110
110
|
* @returns The value that was removed from the data structure.
|
|
111
111
|
*/
|
|
112
|
-
|
|
112
|
+
popLast() {
|
|
113
113
|
if (!this._size)
|
|
114
114
|
return;
|
|
115
|
-
const value = this.
|
|
115
|
+
const value = this.getLast();
|
|
116
116
|
delete this._nodes[this._last];
|
|
117
117
|
this._last--;
|
|
118
118
|
this._size--;
|
|
119
119
|
return value;
|
|
120
120
|
}
|
|
121
121
|
/**
|
|
122
|
-
* The `
|
|
122
|
+
* The `getLast()` function returns the last element in an array-like data structure.
|
|
123
123
|
* @returns The last element in the array "_nodes" is being returned.
|
|
124
124
|
*/
|
|
125
|
-
|
|
125
|
+
getLast() {
|
|
126
126
|
if (this._size)
|
|
127
127
|
return this._nodes[this._last];
|
|
128
128
|
}
|
|
@@ -170,18 +170,18 @@ class ArrayDeque {
|
|
|
170
170
|
return this._nodes.push(value);
|
|
171
171
|
}
|
|
172
172
|
/**
|
|
173
|
-
* The function "
|
|
174
|
-
* @returns The method `
|
|
173
|
+
* The function "popLast" returns and removes the last element from an array, or returns null if the array is empty.
|
|
174
|
+
* @returns The method `popLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
175
175
|
*/
|
|
176
|
-
|
|
176
|
+
popLast() {
|
|
177
177
|
return this._nodes.pop() ?? null;
|
|
178
178
|
}
|
|
179
179
|
/**
|
|
180
|
-
* The `
|
|
181
|
-
* @returns The `
|
|
180
|
+
* The `popFirst` function removes and returns the first element from an array, or returns null if the array is empty.
|
|
181
|
+
* @returns The `popFirst()` function returns the first element of the `_nodes` array, or `null` if the array is
|
|
182
182
|
* empty.
|
|
183
183
|
*/
|
|
184
|
-
|
|
184
|
+
popFirst() {
|
|
185
185
|
return this._nodes.shift() ?? null;
|
|
186
186
|
}
|
|
187
187
|
/**
|
|
@@ -197,18 +197,18 @@ class ArrayDeque {
|
|
|
197
197
|
return this._nodes.unshift(value);
|
|
198
198
|
}
|
|
199
199
|
/**
|
|
200
|
-
* The `
|
|
201
|
-
* @returns The function `
|
|
200
|
+
* The `getFirst` function returns the first element of an array or null if the array is empty.
|
|
201
|
+
* @returns The function `getFirst()` is returning the first element (`E`) of the `_nodes` array. If the array is
|
|
202
202
|
* empty, it will return `null`.
|
|
203
203
|
*/
|
|
204
|
-
|
|
204
|
+
getFirst() {
|
|
205
205
|
return this._nodes[0] ?? null;
|
|
206
206
|
}
|
|
207
207
|
/**
|
|
208
|
-
* The `
|
|
209
|
-
* @returns The method `
|
|
208
|
+
* The `getLast` function returns the last element of an array or null if the array is empty.
|
|
209
|
+
* @returns The method `getLast()` returns the last element of the `_nodes` array, or `null` if the array is empty.
|
|
210
210
|
*/
|
|
211
|
-
|
|
211
|
+
getLast() {
|
|
212
212
|
return this._nodes[this._nodes.length - 1] ?? null;
|
|
213
213
|
}
|
|
214
214
|
/**
|
|
@@ -68,11 +68,11 @@ export declare class Queue<E = any> {
|
|
|
68
68
|
*/
|
|
69
69
|
peek(): E | undefined;
|
|
70
70
|
/**
|
|
71
|
-
* The `
|
|
72
|
-
* @returns The method `
|
|
71
|
+
* The `getLast` function returns the last element in an array-like data structure, or null if the structure is empty.
|
|
72
|
+
* @returns The method `getLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
73
73
|
* array is empty, it returns `null`.
|
|
74
74
|
*/
|
|
75
|
-
|
|
75
|
+
getLast(): E | undefined;
|
|
76
76
|
/**
|
|
77
77
|
* The enqueue function adds a value to the end of a queue.
|
|
78
78
|
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
@@ -110,11 +110,11 @@ class Queue {
|
|
|
110
110
|
return this.size > 0 ? this.nodes[this.offset] : undefined;
|
|
111
111
|
}
|
|
112
112
|
/**
|
|
113
|
-
* The `
|
|
114
|
-
* @returns The method `
|
|
113
|
+
* The `getLast` function returns the last element in an array-like data structure, or null if the structure is empty.
|
|
114
|
+
* @returns The method `getLast()` returns the last element of the `_nodes` array if the array is not empty. If the
|
|
115
115
|
* array is empty, it returns `null`.
|
|
116
116
|
*/
|
|
117
|
-
|
|
117
|
+
getLast() {
|
|
118
118
|
return this.size > 0 ? this.nodes[this.nodes.length - 1] : undefined;
|
|
119
119
|
}
|
|
120
120
|
/**
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { BinaryTreeNode } from '../data-structures';
|
|
2
|
-
import { BinaryTreeDeletedResult, BinaryTreeNodeKey, BinaryTreeNodeNested,
|
|
2
|
+
import { BinaryTreeDeletedResult, BinaryTreeNodeKey, BinaryTreeNodeNested, OneParamCallback } from '../types';
|
|
3
3
|
export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>> {
|
|
4
4
|
createNode(key: BinaryTreeNodeKey, val?: N['val']): N;
|
|
5
5
|
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined;
|
|
6
|
-
delete<C extends
|
|
6
|
+
delete<C extends OneParamCallback<N>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeletedResult<N>[];
|
|
7
7
|
}
|
|
@@ -19,8 +19,6 @@ export declare enum FamilyPosition {
|
|
|
19
19
|
MAL_NODE = "MAL_NODE"
|
|
20
20
|
}
|
|
21
21
|
export type BinaryTreeNodeKey = number;
|
|
22
|
-
export type BFSCallback<N, D = any> = (node: N, level?: number) => D;
|
|
23
|
-
export type BFSCallbackReturn<N> = ReturnType<BFSCallback<N>>;
|
|
24
22
|
export type BinaryTreeDeletedResult<N> = {
|
|
25
23
|
deleted: N | null | undefined;
|
|
26
24
|
needBalanced: N | null;
|
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
import { BinaryTreeNodeKey } from './data-structures';
|
|
2
1
|
export type Comparator<T> = (a: T, b: T) => number;
|
|
3
2
|
export type DFSOrderPattern = 'pre' | 'in' | 'post';
|
|
4
|
-
export type
|
|
5
|
-
export type DefaultMapCallback<N, D = BinaryTreeNodeKey> = (node: N) => D;
|
|
6
|
-
export type MapCallbackReturn<N> = ReturnType<MapCallback<N>>;
|
|
3
|
+
export type OneParamCallback<N, D = any> = (node: N) => D;
|
|
7
4
|
export declare enum CP {
|
|
8
5
|
lt = "lt",
|
|
9
6
|
eq = "eq",
|