data-structure-typed 1.53.8 → 1.54.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/CONTRIBUTING.md +1 -1
- package/dist/cjs/data-structures/base/iterable-entry-base.js +4 -4
- package/dist/cjs/data-structures/base/iterable-entry-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +34 -27
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +62 -52
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +3 -14
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +22 -25
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +32 -16
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +43 -24
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +14 -23
- package/dist/cjs/data-structures/binary-tree/bst.js +28 -29
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/red-black-tree.d.ts +35 -28
- package/dist/cjs/data-structures/binary-tree/red-black-tree.js +52 -71
- package/dist/cjs/data-structures/binary-tree/red-black-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +18 -14
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +36 -23
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.js +2 -2
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +1 -1
- package/dist/cjs/data-structures/hash/hash-map.js +5 -5
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +10 -10
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +12 -12
- 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 +10 -10
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +16 -16
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +1 -1
- package/dist/cjs/types/data-structures/base/base.d.ts +1 -1
- package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
- package/dist/cjs/types/data-structures/binary-tree/avl-tree.d.ts +2 -2
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +2 -2
- package/dist/cjs/types/data-structures/binary-tree/bst.d.ts +3 -3
- package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.d.ts +4 -4
- package/dist/mjs/data-structures/base/iterable-entry-base.js +4 -4
- package/dist/mjs/data-structures/base/iterable-entry-base.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +34 -27
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +62 -52
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +3 -14
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +22 -26
- package/dist/mjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +32 -16
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +43 -24
- package/dist/mjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +14 -23
- package/dist/mjs/data-structures/binary-tree/bst.js +29 -30
- package/dist/mjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/red-black-tree.d.ts +35 -28
- package/dist/mjs/data-structures/binary-tree/red-black-tree.js +51 -71
- package/dist/mjs/data-structures/binary-tree/red-black-tree.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +18 -14
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +36 -23
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/mjs/data-structures/graph/abstract-graph.js +2 -2
- package/dist/mjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/mjs/data-structures/hash/hash-map.d.ts +1 -1
- package/dist/mjs/data-structures/hash/hash-map.js +5 -5
- package/dist/mjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +10 -10
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +12 -12
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +10 -10
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +16 -16
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/mjs/interfaces/binary-tree.d.ts +1 -1
- package/dist/mjs/types/data-structures/base/base.d.ts +1 -1
- package/dist/mjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
- package/dist/mjs/types/data-structures/binary-tree/avl-tree.d.ts +2 -2
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +2 -2
- package/dist/mjs/types/data-structures/binary-tree/bst.d.ts +3 -3
- package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/mjs/types/data-structures/binary-tree/tree-multi-map.d.ts +4 -4
- package/dist/umd/data-structure-typed.js +258 -249
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +6 -6
- package/src/data-structures/base/iterable-entry-base.ts +4 -4
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +83 -64
- package/src/data-structures/binary-tree/avl-tree.ts +40 -34
- package/src/data-structures/binary-tree/binary-tree.ts +74 -30
- package/src/data-structures/binary-tree/bst.ts +57 -43
- package/src/data-structures/binary-tree/red-black-tree.ts +66 -70
- package/src/data-structures/binary-tree/tree-multi-map.ts +56 -30
- package/src/data-structures/graph/abstract-graph.ts +2 -2
- package/src/data-structures/hash/hash-map.ts +7 -7
- package/src/data-structures/linked-list/doubly-linked-list.ts +13 -13
- package/src/data-structures/linked-list/singly-linked-list.ts +17 -17
- package/src/interfaces/binary-tree.ts +4 -1
- package/src/types/data-structures/base/base.ts +1 -1
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +2 -2
- package/src/types/data-structures/binary-tree/avl-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/binary-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/bst.ts +3 -3
- package/src/types/data-structures/binary-tree/rb-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +4 -4
- package/test/integration/index.html +3 -3
- package/test/performance/data-structures/binary-tree/binary-tree-overall.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +12 -12
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +10 -10
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +10 -10
- package/test/unit/data-structures/binary-tree/bst.test.ts +11 -11
- package/test/unit/data-structures/binary-tree/data/cost-of-living-by-country.ts +259 -0
- package/test/unit/data-structures/binary-tree/overall.test.ts +2 -0
- package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +30 -10
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +10 -10
- package/test/unit/data-structures/graph/directed-graph.test.ts +4 -4
- package/test/unit/data-structures/hash/hash-map.test.ts +12 -12
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "data-structure-typed",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.54.0",
|
|
4
4
|
"description": "Javascript Data Structure. Heap, Binary Tree, Red Black Tree, Linked List, Deque, Trie, HashMap, Directed Graph, Undirected Graph, Binary Search Tree(BST), AVL Tree, Priority Queue, Graph, Queue, Tree Multiset, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue, Stack. Benchmark compared with C++ STL. API aligned with ES6 and Java.util. Usability is comparable to Python",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/mjs/index.js",
|
|
@@ -70,11 +70,11 @@
|
|
|
70
70
|
"@typescript-eslint/eslint-plugin": "^8.12.1",
|
|
71
71
|
"@typescript-eslint/parser": "^8.12.1",
|
|
72
72
|
"auto-changelog": "^2.5.0",
|
|
73
|
-
"avl-tree-typed": "^1.53.
|
|
73
|
+
"avl-tree-typed": "^1.53.9",
|
|
74
74
|
"benchmark": "^2.1.4",
|
|
75
|
-
"binary-tree-typed": "^1.53.
|
|
76
|
-
"bst-typed": "^1.53.
|
|
77
|
-
"data-structure-typed": "^1.53.
|
|
75
|
+
"binary-tree-typed": "^1.53.8",
|
|
76
|
+
"bst-typed": "^1.53.9",
|
|
77
|
+
"data-structure-typed": "^1.53.9",
|
|
78
78
|
"dependency-cruiser": "^16.5.0",
|
|
79
79
|
"doctoc": "^2.2.1",
|
|
80
80
|
"eslint": "^9.13.0",
|
|
@@ -83,7 +83,7 @@
|
|
|
83
83
|
"eslint-import-resolver-typescript": "^3.6.3",
|
|
84
84
|
"eslint-plugin-import": "^2.31.0",
|
|
85
85
|
"fast-glob": "^3.3.2",
|
|
86
|
-
"heap-typed": "^1.53.
|
|
86
|
+
"heap-typed": "^1.53.9",
|
|
87
87
|
"istanbul-badges-readme": "^1.9.0",
|
|
88
88
|
"jest": "^29.7.0",
|
|
89
89
|
"js-sdsl": "^4.4.2",
|
|
@@ -70,7 +70,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
70
70
|
every(predicate: EntryCallback<K, V, boolean>, thisArg?: any): boolean {
|
|
71
71
|
let index = 0;
|
|
72
72
|
for (const item of this) {
|
|
73
|
-
if (!predicate.call(thisArg, item[
|
|
73
|
+
if (!predicate.call(thisArg, item[0], item[1], index++, this)) {
|
|
74
74
|
return false;
|
|
75
75
|
}
|
|
76
76
|
}
|
|
@@ -95,7 +95,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
95
95
|
some(predicate: EntryCallback<K, V, boolean>, thisArg?: any): boolean {
|
|
96
96
|
let index = 0;
|
|
97
97
|
for (const item of this) {
|
|
98
|
-
if (predicate.call(thisArg, item[
|
|
98
|
+
if (predicate.call(thisArg, item[0], item[1], index++, this)) {
|
|
99
99
|
return true;
|
|
100
100
|
}
|
|
101
101
|
}
|
|
@@ -119,7 +119,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
119
119
|
let index = 0;
|
|
120
120
|
for (const item of this) {
|
|
121
121
|
const [key, value] = item;
|
|
122
|
-
callbackfn.call(thisArg,
|
|
122
|
+
callbackfn.call(thisArg, key, value, index++, this);
|
|
123
123
|
}
|
|
124
124
|
}
|
|
125
125
|
|
|
@@ -144,7 +144,7 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
144
144
|
let index = 0;
|
|
145
145
|
for (const item of this) {
|
|
146
146
|
const [key, value] = item;
|
|
147
|
-
if (callbackfn.call(thisArg,
|
|
147
|
+
if (callbackfn.call(thisArg, key, value, index++, this)) return item;
|
|
148
148
|
}
|
|
149
149
|
return;
|
|
150
150
|
}
|
|
@@ -12,6 +12,7 @@ import type {
|
|
|
12
12
|
BinaryTreeDeleteResult,
|
|
13
13
|
BSTNOptKeyOrNode,
|
|
14
14
|
BTNRep,
|
|
15
|
+
EntryCallback,
|
|
15
16
|
IterationType
|
|
16
17
|
} from '../../types';
|
|
17
18
|
import { IBinaryTree } from '../../interfaces';
|
|
@@ -36,25 +37,6 @@ export class AVLTreeMultiMapNode<
|
|
|
36
37
|
super(key, value);
|
|
37
38
|
this.count = count;
|
|
38
39
|
}
|
|
39
|
-
|
|
40
|
-
protected _count: number = 1;
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* The function returns the value of the protected variable _count.
|
|
44
|
-
* @returns The count property of the object, which is of type number.
|
|
45
|
-
*/
|
|
46
|
-
get count(): number {
|
|
47
|
-
return this._count;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* The above function sets the value of the count property.
|
|
52
|
-
* @param {number} value - The value parameter is of type number, which means it can accept any
|
|
53
|
-
* numeric value.
|
|
54
|
-
*/
|
|
55
|
-
set count(value: number) {
|
|
56
|
-
this._count = value;
|
|
57
|
-
}
|
|
58
40
|
}
|
|
59
41
|
|
|
60
42
|
/**
|
|
@@ -64,17 +46,23 @@ export class AVLTreeMultiMap<
|
|
|
64
46
|
K = any,
|
|
65
47
|
V = any,
|
|
66
48
|
R = object,
|
|
49
|
+
MK = any,
|
|
50
|
+
MV = any,
|
|
51
|
+
MR = object,
|
|
67
52
|
NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNodeNested<K, V>>,
|
|
68
|
-
TREE extends AVLTreeMultiMap<K, V, R, NODE, TREE> = AVLTreeMultiMap<
|
|
53
|
+
TREE extends AVLTreeMultiMap<K, V, R, MK, MV, MR, NODE, TREE> = AVLTreeMultiMap<
|
|
69
54
|
K,
|
|
70
55
|
V,
|
|
71
56
|
R,
|
|
57
|
+
MK,
|
|
58
|
+
MV,
|
|
59
|
+
MR,
|
|
72
60
|
NODE,
|
|
73
|
-
AVLTreeMultiMapNested<K, V, R, NODE>
|
|
61
|
+
AVLTreeMultiMapNested<K, V, R, MK, MV, MR, NODE>
|
|
74
62
|
>
|
|
75
63
|
>
|
|
76
|
-
extends AVLTree<K, V, R, NODE, TREE>
|
|
77
|
-
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
64
|
+
extends AVLTree<K, V, R, MK, MV, MR, NODE, TREE>
|
|
65
|
+
implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE>
|
|
78
66
|
{
|
|
79
67
|
/**
|
|
80
68
|
* The constructor initializes a new AVLTreeMultiMap object with optional initial elements.
|
|
@@ -140,10 +128,10 @@ export class AVLTreeMultiMap<
|
|
|
140
128
|
* object.
|
|
141
129
|
*/
|
|
142
130
|
override createTree(options?: AVLTreeMultiMapOptions<K, V, R>): TREE {
|
|
143
|
-
return new AVLTreeMultiMap<K, V, R, NODE, TREE>([], {
|
|
131
|
+
return new AVLTreeMultiMap<K, V, R, MK, MV, MR, NODE, TREE>([], {
|
|
144
132
|
iterationType: this.iterationType,
|
|
145
133
|
isMapMode: this._isMapMode,
|
|
146
|
-
|
|
134
|
+
specifyComparable: this._specifyComparable,
|
|
147
135
|
toEntryFn: this._toEntryFn,
|
|
148
136
|
isReverse: this._isReverse,
|
|
149
137
|
...options
|
|
@@ -161,44 +149,6 @@ export class AVLTreeMultiMap<
|
|
|
161
149
|
return keyNodeEntryOrRaw instanceof AVLTreeMultiMapNode;
|
|
162
150
|
}
|
|
163
151
|
|
|
164
|
-
/**
|
|
165
|
-
* The function `keyValueNodeEntryRawToNodeAndValue` converts a key, value, entry, or raw element into
|
|
166
|
-
* a node object.
|
|
167
|
-
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
|
|
168
|
-
* `keyNodeEntryOrRaw` parameter can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
169
|
-
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
170
|
-
* `override` function. It represents the value associated with the key in the data structure. If no
|
|
171
|
-
* value is provided, it will default to `undefined`.
|
|
172
|
-
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
173
|
-
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
174
|
-
* @returns either a NODE object or undefined.
|
|
175
|
-
*/
|
|
176
|
-
protected override _keyValueNodeEntryRawToNodeAndValue(
|
|
177
|
-
keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
|
|
178
|
-
value?: V,
|
|
179
|
-
count = 1
|
|
180
|
-
): [NODE | undefined, V | undefined] {
|
|
181
|
-
if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null) return [undefined, undefined];
|
|
182
|
-
if (this.isNode(keyNodeEntryOrRaw)) return [keyNodeEntryOrRaw, value];
|
|
183
|
-
|
|
184
|
-
if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
185
|
-
const [key, entryValue] = keyNodeEntryOrRaw;
|
|
186
|
-
if (key === undefined || key === null) return [undefined, undefined];
|
|
187
|
-
const finalValue = value ?? entryValue;
|
|
188
|
-
return [this.createNode(key, finalValue, count), finalValue];
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
192
|
-
const [key, entryValue] = this._toEntryFn!(keyNodeEntryOrRaw);
|
|
193
|
-
const finalValue = value ?? entryValue;
|
|
194
|
-
if (this.isKey(key)) return [this.createNode(key, finalValue, count), finalValue];
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value, count), value];
|
|
198
|
-
|
|
199
|
-
return [undefined, undefined];
|
|
200
|
-
}
|
|
201
|
-
|
|
202
152
|
/**
|
|
203
153
|
* Time Complexity: O(log n)
|
|
204
154
|
* Space Complexity: O(1)
|
|
@@ -264,7 +214,7 @@ export class AVLTreeMultiMap<
|
|
|
264
214
|
} else {
|
|
265
215
|
if (!curr.left) {
|
|
266
216
|
if (!parent) {
|
|
267
|
-
if (curr.right !== undefined) this._setRoot(curr.right);
|
|
217
|
+
if (curr.right !== undefined && curr.right !== null) this._setRoot(curr.right);
|
|
268
218
|
} else {
|
|
269
219
|
const { familyPosition: fp } = curr;
|
|
270
220
|
if (fp === 'LEFT' || fp === 'ROOT_LEFT') {
|
|
@@ -382,6 +332,75 @@ export class AVLTreeMultiMap<
|
|
|
382
332
|
return cloned;
|
|
383
333
|
}
|
|
384
334
|
|
|
335
|
+
/**
|
|
336
|
+
* The `map` function in TypeScript overrides the default behavior to create a new AVLTreeMultiMap
|
|
337
|
+
* with modified entries based on a provided callback.
|
|
338
|
+
* @param callback - The `callback` parameter is a function that will be called for each entry in the
|
|
339
|
+
* AVLTreeMultiMap. It takes four arguments:
|
|
340
|
+
* @param [options] - The `options` parameter in the `override map` function is of type
|
|
341
|
+
* `AVLTreeMultiMapOptions<MK, MV, MR>`. This parameter allows you to provide additional
|
|
342
|
+
* configuration options when creating a new `AVLTreeMultiMap` instance within the `map` function.
|
|
343
|
+
* These options
|
|
344
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
|
|
345
|
+
* the value of `this` when executing the `callback` function. It allows you to set the context
|
|
346
|
+
* (value of `this`) for the callback function. This can be useful when you want to access properties
|
|
347
|
+
* or
|
|
348
|
+
* @returns The `map` method is returning a new `AVLTreeMultiMap` instance with the entries
|
|
349
|
+
* transformed by the provided `callback` function. Each entry in the original tree is passed to the
|
|
350
|
+
* `callback` function along with the index and the original tree itself. The transformed entries are
|
|
351
|
+
* then added to the new `AVLTreeMultiMap` instance, which is returned at the end.
|
|
352
|
+
*/
|
|
353
|
+
override map<MK, MV, MR>(
|
|
354
|
+
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
355
|
+
options?: AVLTreeMultiMapOptions<MK, MV, MR>,
|
|
356
|
+
thisArg?: any
|
|
357
|
+
): AVLTreeMultiMap<MK, MV, MR> {
|
|
358
|
+
const newTree = new AVLTreeMultiMap<MK, MV, MR>([], options);
|
|
359
|
+
let index = 0;
|
|
360
|
+
for (const [key, value] of this) {
|
|
361
|
+
newTree.add(callback.call(thisArg, key, value, index++, this));
|
|
362
|
+
}
|
|
363
|
+
return newTree;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* The function `keyValueNodeEntryRawToNodeAndValue` converts a key, value, entry, or raw element into
|
|
368
|
+
* a node object.
|
|
369
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
|
|
370
|
+
* `keyNodeEntryOrRaw` parameter can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
371
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
372
|
+
* `override` function. It represents the value associated with the key in the data structure. If no
|
|
373
|
+
* value is provided, it will default to `undefined`.
|
|
374
|
+
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
375
|
+
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
376
|
+
* @returns either a NODE object or undefined.
|
|
377
|
+
*/
|
|
378
|
+
protected override _keyValueNodeEntryRawToNodeAndValue(
|
|
379
|
+
keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R,
|
|
380
|
+
value?: V,
|
|
381
|
+
count = 1
|
|
382
|
+
): [NODE | undefined, V | undefined] {
|
|
383
|
+
if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null) return [undefined, undefined];
|
|
384
|
+
if (this.isNode(keyNodeEntryOrRaw)) return [keyNodeEntryOrRaw, value];
|
|
385
|
+
|
|
386
|
+
if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
387
|
+
const [key, entryValue] = keyNodeEntryOrRaw;
|
|
388
|
+
if (key === undefined || key === null) return [undefined, undefined];
|
|
389
|
+
const finalValue = value ?? entryValue;
|
|
390
|
+
return [this.createNode(key, finalValue, count), finalValue];
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
394
|
+
const [key, entryValue] = this._toEntryFn!(keyNodeEntryOrRaw);
|
|
395
|
+
const finalValue = value ?? entryValue;
|
|
396
|
+
if (this.isKey(key)) return [this.createNode(key, finalValue, count), finalValue];
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
if (this.isKey(keyNodeEntryOrRaw)) return [this.createNode(keyNodeEntryOrRaw, value, count), value];
|
|
400
|
+
|
|
401
|
+
return [undefined, undefined];
|
|
402
|
+
}
|
|
403
|
+
|
|
385
404
|
/**
|
|
386
405
|
* Time Complexity: O(1)
|
|
387
406
|
* Space Complexity: O(1)
|
|
@@ -12,7 +12,8 @@ import type {
|
|
|
12
12
|
AVLTreeOptions,
|
|
13
13
|
BinaryTreeDeleteResult,
|
|
14
14
|
BSTNOptKeyOrNode,
|
|
15
|
-
BTNRep
|
|
15
|
+
BTNRep,
|
|
16
|
+
EntryCallback
|
|
16
17
|
} from '../../types';
|
|
17
18
|
import { IBinaryTree } from '../../interfaces';
|
|
18
19
|
|
|
@@ -31,26 +32,6 @@ export class AVLTreeNode<
|
|
|
31
32
|
*/
|
|
32
33
|
constructor(key: K, value?: V) {
|
|
33
34
|
super(key, value);
|
|
34
|
-
this._height = 0;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
protected _height: number;
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* The function returns the value of the height property.
|
|
41
|
-
* @returns The height of the object.
|
|
42
|
-
*/
|
|
43
|
-
get height(): number {
|
|
44
|
-
return this._height;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* The above function sets the value of the height property.
|
|
49
|
-
* @param {number} value - The value parameter is a number that represents the new height value to be
|
|
50
|
-
* set.
|
|
51
|
-
*/
|
|
52
|
-
set height(value: number) {
|
|
53
|
-
this._height = value;
|
|
54
35
|
}
|
|
55
36
|
}
|
|
56
37
|
|
|
@@ -67,11 +48,23 @@ export class AVLTree<
|
|
|
67
48
|
K = any,
|
|
68
49
|
V = any,
|
|
69
50
|
R = object,
|
|
51
|
+
MK = any,
|
|
52
|
+
MV = any,
|
|
53
|
+
MR = object,
|
|
70
54
|
NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>,
|
|
71
|
-
TREE extends AVLTree<K, V, R,
|
|
55
|
+
TREE extends AVLTree<K, V, R, MK, MV, MR, NODE, TREE> = AVLTree<
|
|
56
|
+
K,
|
|
57
|
+
V,
|
|
58
|
+
R,
|
|
59
|
+
MK,
|
|
60
|
+
MV,
|
|
61
|
+
MR,
|
|
62
|
+
NODE,
|
|
63
|
+
AVLTreeNested<K, V, R, MK, MV, MR, NODE>
|
|
64
|
+
>
|
|
72
65
|
>
|
|
73
|
-
extends BST<K, V, R, NODE, TREE>
|
|
74
|
-
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
66
|
+
extends BST<K, V, R, MK, MV, MR, NODE, TREE>
|
|
67
|
+
implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE>
|
|
75
68
|
{
|
|
76
69
|
/**
|
|
77
70
|
* This is a constructor function for an AVLTree class that initializes the tree with keys, nodes,
|
|
@@ -110,10 +103,10 @@ export class AVLTree<
|
|
|
110
103
|
* @returns a new AVLTree object.
|
|
111
104
|
*/
|
|
112
105
|
override createTree(options?: AVLTreeOptions<K, V, R>): TREE {
|
|
113
|
-
return new AVLTree<K, V, R, NODE, TREE>([], {
|
|
106
|
+
return new AVLTree<K, V, R, MK, MV, MR, NODE, TREE>([], {
|
|
114
107
|
iterationType: this.iterationType,
|
|
115
108
|
isMapMode: this._isMapMode,
|
|
116
|
-
|
|
109
|
+
specifyComparable: this._specifyComparable,
|
|
117
110
|
toEntryFn: this._toEntryFn,
|
|
118
111
|
isReverse: this._isReverse,
|
|
119
112
|
...options
|
|
@@ -174,6 +167,19 @@ export class AVLTree<
|
|
|
174
167
|
return deletedResults;
|
|
175
168
|
}
|
|
176
169
|
|
|
170
|
+
override map(
|
|
171
|
+
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
172
|
+
options?: AVLTreeOptions<MK, MV, MR>,
|
|
173
|
+
thisArg?: any
|
|
174
|
+
): AVLTree<MK, MV, MR> {
|
|
175
|
+
const newTree = new AVLTree<MK, MV, MR>([], options);
|
|
176
|
+
let index = 0;
|
|
177
|
+
for (const [key, value] of this) {
|
|
178
|
+
newTree.add(callback.call(thisArg, key, value, index++, this));
|
|
179
|
+
}
|
|
180
|
+
return newTree;
|
|
181
|
+
}
|
|
182
|
+
|
|
177
183
|
/**
|
|
178
184
|
* Time Complexity: O(1)
|
|
179
185
|
* Space Complexity: O(1)
|
|
@@ -262,7 +268,7 @@ export class AVLTree<
|
|
|
262
268
|
protected _balanceLL(A: NODE): void {
|
|
263
269
|
const parentOfA = A.parent;
|
|
264
270
|
const B = A.left;
|
|
265
|
-
A.parent = B;
|
|
271
|
+
if (B !== null) A.parent = B;
|
|
266
272
|
if (B && B.right) {
|
|
267
273
|
B.right.parent = A;
|
|
268
274
|
}
|
|
@@ -299,12 +305,12 @@ export class AVLTree<
|
|
|
299
305
|
if (B) {
|
|
300
306
|
C = B.right;
|
|
301
307
|
}
|
|
302
|
-
if (A) A.parent = C;
|
|
303
|
-
if (B) B.parent = C;
|
|
308
|
+
if (A && C !== null) A.parent = C;
|
|
309
|
+
if (B && C !== null) B.parent = C;
|
|
304
310
|
|
|
305
311
|
if (C) {
|
|
306
312
|
if (C.left) {
|
|
307
|
-
C.left.parent = B;
|
|
313
|
+
if (B !== null) C.left.parent = B;
|
|
308
314
|
}
|
|
309
315
|
if (C.right) {
|
|
310
316
|
C.right.parent = A;
|
|
@@ -346,7 +352,7 @@ export class AVLTree<
|
|
|
346
352
|
protected _balanceRR(A: NODE): void {
|
|
347
353
|
const parentOfA = A.parent;
|
|
348
354
|
const B = A.right;
|
|
349
|
-
A.parent = B;
|
|
355
|
+
if (B !== null) A.parent = B;
|
|
350
356
|
if (B) {
|
|
351
357
|
if (B.left) {
|
|
352
358
|
B.left.parent = A;
|
|
@@ -389,15 +395,15 @@ export class AVLTree<
|
|
|
389
395
|
C = B.left;
|
|
390
396
|
}
|
|
391
397
|
|
|
392
|
-
A.parent = C;
|
|
393
|
-
if (B) B.parent = C;
|
|
398
|
+
if (C !== null) A.parent = C;
|
|
399
|
+
if (B && C !== null) B.parent = C;
|
|
394
400
|
|
|
395
401
|
if (C) {
|
|
396
402
|
if (C.left) {
|
|
397
403
|
C.left.parent = A;
|
|
398
404
|
}
|
|
399
405
|
if (C.right) {
|
|
400
|
-
C.right.parent = B;
|
|
406
|
+
if (B !== null) C.right.parent = B;
|
|
401
407
|
}
|
|
402
408
|
C.parent = parentOfA;
|
|
403
409
|
}
|
|
@@ -23,6 +23,7 @@ import {
|
|
|
23
23
|
NodeDisplayLayout,
|
|
24
24
|
NodePredicate,
|
|
25
25
|
OptNodeOrNull,
|
|
26
|
+
type RBTNColor,
|
|
26
27
|
ToEntryFn
|
|
27
28
|
} from '../../types';
|
|
28
29
|
import { IBinaryTree } from '../../interfaces';
|
|
@@ -52,7 +53,7 @@ export class BinaryTreeNode<
|
|
|
52
53
|
this.value = value;
|
|
53
54
|
}
|
|
54
55
|
|
|
55
|
-
|
|
56
|
+
_left?: OptNodeOrNull<NODE>;
|
|
56
57
|
|
|
57
58
|
get left(): OptNodeOrNull<NODE> {
|
|
58
59
|
return this._left;
|
|
@@ -65,7 +66,7 @@ export class BinaryTreeNode<
|
|
|
65
66
|
this._left = v;
|
|
66
67
|
}
|
|
67
68
|
|
|
68
|
-
|
|
69
|
+
_right?: OptNodeOrNull<NODE>;
|
|
69
70
|
|
|
70
71
|
get right(): OptNodeOrNull<NODE> {
|
|
71
72
|
return this._right;
|
|
@@ -78,6 +79,36 @@ export class BinaryTreeNode<
|
|
|
78
79
|
this._right = v;
|
|
79
80
|
}
|
|
80
81
|
|
|
82
|
+
_height: number = 0;
|
|
83
|
+
|
|
84
|
+
get height(): number {
|
|
85
|
+
return this._height;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
set height(value: number) {
|
|
89
|
+
this._height = value;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
_color: RBTNColor = 'BLACK';
|
|
93
|
+
|
|
94
|
+
get color(): RBTNColor {
|
|
95
|
+
return this._color;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
set color(value: RBTNColor) {
|
|
99
|
+
this._color = value;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
_count: number = 1;
|
|
103
|
+
|
|
104
|
+
get count(): number {
|
|
105
|
+
return this._count;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
set count(value: number) {
|
|
109
|
+
this._count = value;
|
|
110
|
+
}
|
|
111
|
+
|
|
81
112
|
get familyPosition(): FamilyPosition {
|
|
82
113
|
const that = this as unknown as NODE;
|
|
83
114
|
if (!this.parent) {
|
|
@@ -105,11 +136,23 @@ export class BinaryTree<
|
|
|
105
136
|
K = any,
|
|
106
137
|
V = any,
|
|
107
138
|
R = object,
|
|
139
|
+
MK = any,
|
|
140
|
+
MV = any,
|
|
141
|
+
MR = object,
|
|
108
142
|
NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>,
|
|
109
|
-
TREE extends BinaryTree<K, V, R,
|
|
143
|
+
TREE extends BinaryTree<K, V, R, MK, MV, MR, NODE, TREE> = BinaryTree<
|
|
144
|
+
K,
|
|
145
|
+
V,
|
|
146
|
+
R,
|
|
147
|
+
MK,
|
|
148
|
+
MV,
|
|
149
|
+
MR,
|
|
150
|
+
NODE,
|
|
151
|
+
BinaryTreeNested<K, V, R, MK, MV, MR, NODE>
|
|
152
|
+
>
|
|
110
153
|
>
|
|
111
154
|
extends IterableEntryBase<K, V | undefined>
|
|
112
|
-
implements IBinaryTree<K, V, R, NODE, TREE>
|
|
155
|
+
implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE>
|
|
113
156
|
{
|
|
114
157
|
iterationType: IterationType = 'ITERATIVE';
|
|
115
158
|
|
|
@@ -172,6 +215,9 @@ export class BinaryTree<
|
|
|
172
215
|
}
|
|
173
216
|
|
|
174
217
|
/**
|
|
218
|
+
* Time Complexity: O(1)
|
|
219
|
+
* Space Complexity: O(1)
|
|
220
|
+
*
|
|
175
221
|
* The function creates a new binary tree node with a specified key and optional value.
|
|
176
222
|
* @param {K} key - The `key` parameter is the key of the node being created in the binary tree.
|
|
177
223
|
* @param {V} [value] - The `value` parameter in the `createNode` function is optional, meaning it is
|
|
@@ -193,7 +239,7 @@ export class BinaryTree<
|
|
|
193
239
|
* @returns A new instance of a binary tree with the specified options is being returned.
|
|
194
240
|
*/
|
|
195
241
|
createTree(options?: BinaryTreeOptions<K, V, R>): TREE {
|
|
196
|
-
return new BinaryTree<K, V, R, NODE, TREE>([], {
|
|
242
|
+
return new BinaryTree<K, V, R, MK, MV, MR, NODE, TREE>([], {
|
|
197
243
|
iterationType: this.iterationType,
|
|
198
244
|
isMapMode: this._isMapMode,
|
|
199
245
|
toEntryFn: this._toEntryFn,
|
|
@@ -1673,7 +1719,7 @@ export class BinaryTree<
|
|
|
1673
1719
|
const newTree = this.createTree();
|
|
1674
1720
|
let index = 0;
|
|
1675
1721
|
for (const [key, value] of this) {
|
|
1676
|
-
if (predicate.call(thisArg,
|
|
1722
|
+
if (predicate.call(thisArg, key, value, index++, this)) {
|
|
1677
1723
|
newTree.add([key, value]);
|
|
1678
1724
|
}
|
|
1679
1725
|
}
|
|
@@ -1684,36 +1730,34 @@ export class BinaryTree<
|
|
|
1684
1730
|
* Time Complexity: O(n)
|
|
1685
1731
|
* Space Complexity: O(n)
|
|
1686
1732
|
*
|
|
1687
|
-
* The `map` function
|
|
1688
|
-
*
|
|
1689
|
-
* @param callback -
|
|
1690
|
-
*
|
|
1691
|
-
*
|
|
1692
|
-
*
|
|
1693
|
-
*
|
|
1694
|
-
*
|
|
1695
|
-
*
|
|
1696
|
-
*
|
|
1733
|
+
* The `map` function in TypeScript creates a new BinaryTree by applying a callback function to each
|
|
1734
|
+
* entry in the original BinaryTree.
|
|
1735
|
+
* @param callback - A function that will be called for each entry in the current binary tree. It
|
|
1736
|
+
* takes the key, value (which can be undefined), and an array containing the mapped key and value as
|
|
1737
|
+
* arguments.
|
|
1738
|
+
* @param [options] - The `options` parameter in the `map` method is of type `BinaryTreeOptions<MK,
|
|
1739
|
+
* MV, MR>`. It is an optional parameter that allows you to specify additional options for the binary
|
|
1740
|
+
* tree being created during the mapping process. These options could include things like custom
|
|
1741
|
+
* comparators, initial
|
|
1742
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `map` method is used to specify the value
|
|
1743
|
+
* of `this` when executing the `callback` function. It allows you to set the context (value of
|
|
1744
|
+
* `this`) within the callback function. If `thisArg` is provided, it will be passed
|
|
1745
|
+
* @returns The `map` function is returning a new `BinaryTree` instance filled with entries that are
|
|
1746
|
+
* the result of applying the provided `callback` function to each entry in the original tree.
|
|
1697
1747
|
*/
|
|
1698
|
-
map(
|
|
1699
|
-
|
|
1748
|
+
map(
|
|
1749
|
+
callback: EntryCallback<K, V | undefined, [MK, MV]>,
|
|
1750
|
+
options?: BinaryTreeOptions<MK, MV, MR>,
|
|
1751
|
+
thisArg?: any
|
|
1752
|
+
): BinaryTree<MK, MV, MR> {
|
|
1753
|
+
const newTree = new BinaryTree<MK, MV, MR>([], options);
|
|
1700
1754
|
let index = 0;
|
|
1701
1755
|
for (const [key, value] of this) {
|
|
1702
|
-
newTree.add(
|
|
1756
|
+
newTree.add(callback.call(thisArg, key, value, index++, this));
|
|
1703
1757
|
}
|
|
1704
1758
|
return newTree;
|
|
1705
1759
|
}
|
|
1706
1760
|
|
|
1707
|
-
// // TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
|
|
1708
|
-
// // map<NV>(callback: (entry: [K, V | undefined], tree: this) => NV) {
|
|
1709
|
-
// // const newTree = this.createTree();
|
|
1710
|
-
// // for (const [key, value] of this) {
|
|
1711
|
-
// // newTree.add(key, callback([key, value], this));
|
|
1712
|
-
// // }
|
|
1713
|
-
// // return newTree;
|
|
1714
|
-
// // }
|
|
1715
|
-
//
|
|
1716
|
-
|
|
1717
1761
|
/**
|
|
1718
1762
|
* Time Complexity: O(n)
|
|
1719
1763
|
* Space Complexity: O(n)
|
|
@@ -1743,7 +1787,7 @@ export class BinaryTree<
|
|
|
1743
1787
|
if (opts.isShowRedBlackNIL) output += `S for Sentinel Node(NIL)\n`;
|
|
1744
1788
|
|
|
1745
1789
|
const display = (root: OptNodeOrNull<NODE>): void => {
|
|
1746
|
-
const [lines, ,
|
|
1790
|
+
const [lines, ,] = this._displayAux(root, opts);
|
|
1747
1791
|
let paragraph = '';
|
|
1748
1792
|
for (const line of lines) {
|
|
1749
1793
|
paragraph += line + '\n';
|