bst-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/dist/data-structures/base/iterable-entry-base.js +4 -4
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +34 -27
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +62 -52
- package/dist/data-structures/binary-tree/avl-tree.d.ts +3 -14
- package/dist/data-structures/binary-tree/avl-tree.js +22 -25
- package/dist/data-structures/binary-tree/binary-tree.d.ts +32 -16
- package/dist/data-structures/binary-tree/binary-tree.js +43 -24
- package/dist/data-structures/binary-tree/bst.d.ts +14 -23
- package/dist/data-structures/binary-tree/bst.js +28 -29
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +26 -16
- package/dist/data-structures/binary-tree/red-black-tree.js +43 -59
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +18 -14
- package/dist/data-structures/binary-tree/tree-multi-map.js +36 -23
- package/dist/data-structures/graph/abstract-graph.js +2 -2
- package/dist/data-structures/hash/hash-map.d.ts +1 -1
- package/dist/data-structures/hash/hash-map.js +5 -5
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +10 -10
- package/dist/data-structures/linked-list/doubly-linked-list.js +12 -12
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +10 -10
- package/dist/data-structures/linked-list/singly-linked-list.js +16 -16
- package/dist/interfaces/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/base/base.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/bst.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +4 -4
- package/package.json +2 -2
- 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
|
@@ -65,7 +65,7 @@ class IterableEntryBase {
|
|
|
65
65
|
every(predicate, thisArg) {
|
|
66
66
|
let index = 0;
|
|
67
67
|
for (const item of this) {
|
|
68
|
-
if (!predicate.call(thisArg, item[
|
|
68
|
+
if (!predicate.call(thisArg, item[0], item[1], index++, this)) {
|
|
69
69
|
return false;
|
|
70
70
|
}
|
|
71
71
|
}
|
|
@@ -89,7 +89,7 @@ class IterableEntryBase {
|
|
|
89
89
|
some(predicate, thisArg) {
|
|
90
90
|
let index = 0;
|
|
91
91
|
for (const item of this) {
|
|
92
|
-
if (predicate.call(thisArg, item[
|
|
92
|
+
if (predicate.call(thisArg, item[0], item[1], index++, this)) {
|
|
93
93
|
return true;
|
|
94
94
|
}
|
|
95
95
|
}
|
|
@@ -112,7 +112,7 @@ class IterableEntryBase {
|
|
|
112
112
|
let index = 0;
|
|
113
113
|
for (const item of this) {
|
|
114
114
|
const [key, value] = item;
|
|
115
|
-
callbackfn.call(thisArg,
|
|
115
|
+
callbackfn.call(thisArg, key, value, index++, this);
|
|
116
116
|
}
|
|
117
117
|
}
|
|
118
118
|
/**
|
|
@@ -136,7 +136,7 @@ class IterableEntryBase {
|
|
|
136
136
|
let index = 0;
|
|
137
137
|
for (const item of this) {
|
|
138
138
|
const [key, value] = item;
|
|
139
|
-
if (callbackfn.call(thisArg,
|
|
139
|
+
if (callbackfn.call(thisArg, key, value, index++, this))
|
|
140
140
|
return item;
|
|
141
141
|
}
|
|
142
142
|
return;
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { AVLTreeMultiMapNested, AVLTreeMultiMapNodeNested, AVLTreeMultiMapOptions, BinaryTreeDeleteResult, BSTNOptKeyOrNode, BTNRep, IterationType } from '../../types';
|
|
8
|
+
import type { AVLTreeMultiMapNested, AVLTreeMultiMapNodeNested, AVLTreeMultiMapOptions, BinaryTreeDeleteResult, BSTNOptKeyOrNode, BTNRep, EntryCallback, IterationType } from '../../types';
|
|
9
9
|
import { IBinaryTree } from '../../interfaces';
|
|
10
10
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
11
11
|
export declare class AVLTreeMultiMapNode<K = any, V = any, NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNodeNested<K, V>> extends AVLTreeNode<K, V, NODE> {
|
|
@@ -20,23 +20,11 @@ export declare class AVLTreeMultiMapNode<K = any, V = any, NODE extends AVLTreeM
|
|
|
20
20
|
* parameter when creating a new instance of the `BinaryTreeNode` class.
|
|
21
21
|
*/
|
|
22
22
|
constructor(key: K, value?: V, count?: number);
|
|
23
|
-
protected _count: number;
|
|
24
|
-
/**
|
|
25
|
-
* The function returns the value of the protected variable _count.
|
|
26
|
-
* @returns The count property of the object, which is of type number.
|
|
27
|
-
*/
|
|
28
|
-
get count(): number;
|
|
29
|
-
/**
|
|
30
|
-
* The above function sets the value of the count property.
|
|
31
|
-
* @param {number} value - The value parameter is of type number, which means it can accept any
|
|
32
|
-
* numeric value.
|
|
33
|
-
*/
|
|
34
|
-
set count(value: number);
|
|
35
23
|
}
|
|
36
24
|
/**
|
|
37
25
|
* The only distinction between a AVLTreeMultiMap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
38
26
|
*/
|
|
39
|
-
export declare class AVLTreeMultiMap<K = any, V = any, R = object, NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNodeNested<K, V>>, TREE extends AVLTreeMultiMap<K, V, R, NODE, TREE> = AVLTreeMultiMap<K, V, R, NODE, AVLTreeMultiMapNested<K, V, R, NODE>>> extends AVLTree<K, V, R, NODE, TREE> implements IBinaryTree<K, V, R, NODE, TREE> {
|
|
27
|
+
export declare class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR = object, NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNodeNested<K, V>>, TREE extends AVLTreeMultiMap<K, V, R, MK, MV, MR, NODE, TREE> = AVLTreeMultiMap<K, V, R, MK, MV, MR, NODE, AVLTreeMultiMapNested<K, V, R, MK, MV, MR, NODE>>> extends AVLTree<K, V, R, MK, MV, MR, NODE, TREE> implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE> {
|
|
40
28
|
/**
|
|
41
29
|
* The constructor initializes a new AVLTreeMultiMap object with optional initial elements.
|
|
42
30
|
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
|
|
@@ -90,19 +78,6 @@ export declare class AVLTreeMultiMap<K = any, V = any, R = object, NODE extends
|
|
|
90
78
|
* an instance of the `AVLTreeMultiMapNode` class.
|
|
91
79
|
*/
|
|
92
80
|
isNode(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): keyNodeEntryOrRaw is NODE;
|
|
93
|
-
/**
|
|
94
|
-
* The function `keyValueNodeEntryRawToNodeAndValue` converts a key, value, entry, or raw element into
|
|
95
|
-
* a node object.
|
|
96
|
-
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
|
|
97
|
-
* `keyNodeEntryOrRaw` parameter can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
98
|
-
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
99
|
-
* `override` function. It represents the value associated with the key in the data structure. If no
|
|
100
|
-
* value is provided, it will default to `undefined`.
|
|
101
|
-
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
102
|
-
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
103
|
-
* @returns either a NODE object or undefined.
|
|
104
|
-
*/
|
|
105
|
-
protected _keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V, count?: number): [NODE | undefined, V | undefined];
|
|
106
81
|
/**
|
|
107
82
|
* Time Complexity: O(log n)
|
|
108
83
|
* Space Complexity: O(1)
|
|
@@ -170,6 +145,38 @@ export declare class AVLTreeMultiMap<K = any, V = any, R = object, NODE extends
|
|
|
170
145
|
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
171
146
|
*/
|
|
172
147
|
clone(): TREE;
|
|
148
|
+
/**
|
|
149
|
+
* The `map` function in TypeScript overrides the default behavior to create a new AVLTreeMultiMap
|
|
150
|
+
* with modified entries based on a provided callback.
|
|
151
|
+
* @param callback - The `callback` parameter is a function that will be called for each entry in the
|
|
152
|
+
* AVLTreeMultiMap. It takes four arguments:
|
|
153
|
+
* @param [options] - The `options` parameter in the `override map` function is of type
|
|
154
|
+
* `AVLTreeMultiMapOptions<MK, MV, MR>`. This parameter allows you to provide additional
|
|
155
|
+
* configuration options when creating a new `AVLTreeMultiMap` instance within the `map` function.
|
|
156
|
+
* These options
|
|
157
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
|
|
158
|
+
* the value of `this` when executing the `callback` function. It allows you to set the context
|
|
159
|
+
* (value of `this`) for the callback function. This can be useful when you want to access properties
|
|
160
|
+
* or
|
|
161
|
+
* @returns The `map` method is returning a new `AVLTreeMultiMap` instance with the entries
|
|
162
|
+
* transformed by the provided `callback` function. Each entry in the original tree is passed to the
|
|
163
|
+
* `callback` function along with the index and the original tree itself. The transformed entries are
|
|
164
|
+
* then added to the new `AVLTreeMultiMap` instance, which is returned at the end.
|
|
165
|
+
*/
|
|
166
|
+
map<MK, MV, MR>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: AVLTreeMultiMapOptions<MK, MV, MR>, thisArg?: any): AVLTreeMultiMap<MK, MV, MR>;
|
|
167
|
+
/**
|
|
168
|
+
* The function `keyValueNodeEntryRawToNodeAndValue` converts a key, value, entry, or raw element into
|
|
169
|
+
* a node object.
|
|
170
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
|
|
171
|
+
* `keyNodeEntryOrRaw` parameter can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
172
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
173
|
+
* `override` function. It represents the value associated with the key in the data structure. If no
|
|
174
|
+
* value is provided, it will default to `undefined`.
|
|
175
|
+
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
176
|
+
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
177
|
+
* @returns either a NODE object or undefined.
|
|
178
|
+
*/
|
|
179
|
+
protected _keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V, count?: number): [NODE | undefined, V | undefined];
|
|
173
180
|
/**
|
|
174
181
|
* Time Complexity: O(1)
|
|
175
182
|
* Space Complexity: O(1)
|
|
@@ -15,24 +15,8 @@ class AVLTreeMultiMapNode extends avl_tree_1.AVLTreeNode {
|
|
|
15
15
|
*/
|
|
16
16
|
constructor(key, value, count = 1) {
|
|
17
17
|
super(key, value);
|
|
18
|
-
this._count = 1;
|
|
19
18
|
this.count = count;
|
|
20
19
|
}
|
|
21
|
-
/**
|
|
22
|
-
* The function returns the value of the protected variable _count.
|
|
23
|
-
* @returns The count property of the object, which is of type number.
|
|
24
|
-
*/
|
|
25
|
-
get count() {
|
|
26
|
-
return this._count;
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* The above function sets the value of the count property.
|
|
30
|
-
* @param {number} value - The value parameter is of type number, which means it can accept any
|
|
31
|
-
* numeric value.
|
|
32
|
-
*/
|
|
33
|
-
set count(value) {
|
|
34
|
-
this._count = value;
|
|
35
|
-
}
|
|
36
20
|
}
|
|
37
21
|
exports.AVLTreeMultiMapNode = AVLTreeMultiMapNode;
|
|
38
22
|
/**
|
|
@@ -96,7 +80,7 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
96
80
|
* object.
|
|
97
81
|
*/
|
|
98
82
|
createTree(options) {
|
|
99
|
-
return new AVLTreeMultiMap([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode,
|
|
83
|
+
return new AVLTreeMultiMap([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options));
|
|
100
84
|
}
|
|
101
85
|
/**
|
|
102
86
|
* The function checks if the input is an instance of AVLTreeMultiMapNode.
|
|
@@ -108,40 +92,6 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
108
92
|
isNode(keyNodeEntryOrRaw) {
|
|
109
93
|
return keyNodeEntryOrRaw instanceof AVLTreeMultiMapNode;
|
|
110
94
|
}
|
|
111
|
-
/**
|
|
112
|
-
* The function `keyValueNodeEntryRawToNodeAndValue` converts a key, value, entry, or raw element into
|
|
113
|
-
* a node object.
|
|
114
|
-
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
|
|
115
|
-
* `keyNodeEntryOrRaw` parameter can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
116
|
-
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
117
|
-
* `override` function. It represents the value associated with the key in the data structure. If no
|
|
118
|
-
* value is provided, it will default to `undefined`.
|
|
119
|
-
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
120
|
-
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
121
|
-
* @returns either a NODE object or undefined.
|
|
122
|
-
*/
|
|
123
|
-
_keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count = 1) {
|
|
124
|
-
if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null)
|
|
125
|
-
return [undefined, undefined];
|
|
126
|
-
if (this.isNode(keyNodeEntryOrRaw))
|
|
127
|
-
return [keyNodeEntryOrRaw, value];
|
|
128
|
-
if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
129
|
-
const [key, entryValue] = keyNodeEntryOrRaw;
|
|
130
|
-
if (key === undefined || key === null)
|
|
131
|
-
return [undefined, undefined];
|
|
132
|
-
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
133
|
-
return [this.createNode(key, finalValue, count), finalValue];
|
|
134
|
-
}
|
|
135
|
-
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
136
|
-
const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
137
|
-
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
138
|
-
if (this.isKey(key))
|
|
139
|
-
return [this.createNode(key, finalValue, count), finalValue];
|
|
140
|
-
}
|
|
141
|
-
if (this.isKey(keyNodeEntryOrRaw))
|
|
142
|
-
return [this.createNode(keyNodeEntryOrRaw, value, count), value];
|
|
143
|
-
return [undefined, undefined];
|
|
144
|
-
}
|
|
145
95
|
/**
|
|
146
96
|
* Time Complexity: O(log n)
|
|
147
97
|
* Space Complexity: O(1)
|
|
@@ -206,7 +156,7 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
206
156
|
else {
|
|
207
157
|
if (!curr.left) {
|
|
208
158
|
if (!parent) {
|
|
209
|
-
if (curr.right !== undefined)
|
|
159
|
+
if (curr.right !== undefined && curr.right !== null)
|
|
210
160
|
this._setRoot(curr.right);
|
|
211
161
|
}
|
|
212
162
|
else {
|
|
@@ -329,6 +279,66 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
329
279
|
cloned._store = this._store;
|
|
330
280
|
return cloned;
|
|
331
281
|
}
|
|
282
|
+
/**
|
|
283
|
+
* The `map` function in TypeScript overrides the default behavior to create a new AVLTreeMultiMap
|
|
284
|
+
* with modified entries based on a provided callback.
|
|
285
|
+
* @param callback - The `callback` parameter is a function that will be called for each entry in the
|
|
286
|
+
* AVLTreeMultiMap. It takes four arguments:
|
|
287
|
+
* @param [options] - The `options` parameter in the `override map` function is of type
|
|
288
|
+
* `AVLTreeMultiMapOptions<MK, MV, MR>`. This parameter allows you to provide additional
|
|
289
|
+
* configuration options when creating a new `AVLTreeMultiMap` instance within the `map` function.
|
|
290
|
+
* These options
|
|
291
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
|
|
292
|
+
* the value of `this` when executing the `callback` function. It allows you to set the context
|
|
293
|
+
* (value of `this`) for the callback function. This can be useful when you want to access properties
|
|
294
|
+
* or
|
|
295
|
+
* @returns The `map` method is returning a new `AVLTreeMultiMap` instance with the entries
|
|
296
|
+
* transformed by the provided `callback` function. Each entry in the original tree is passed to the
|
|
297
|
+
* `callback` function along with the index and the original tree itself. The transformed entries are
|
|
298
|
+
* then added to the new `AVLTreeMultiMap` instance, which is returned at the end.
|
|
299
|
+
*/
|
|
300
|
+
map(callback, options, thisArg) {
|
|
301
|
+
const newTree = new AVLTreeMultiMap([], options);
|
|
302
|
+
let index = 0;
|
|
303
|
+
for (const [key, value] of this) {
|
|
304
|
+
newTree.add(callback.call(thisArg, key, value, index++, this));
|
|
305
|
+
}
|
|
306
|
+
return newTree;
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* The function `keyValueNodeEntryRawToNodeAndValue` converts a key, value, entry, or raw element into
|
|
310
|
+
* a node object.
|
|
311
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
|
|
312
|
+
* `keyNodeEntryOrRaw` parameter can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
313
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
314
|
+
* `override` function. It represents the value associated with the key in the data structure. If no
|
|
315
|
+
* value is provided, it will default to `undefined`.
|
|
316
|
+
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
317
|
+
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
318
|
+
* @returns either a NODE object or undefined.
|
|
319
|
+
*/
|
|
320
|
+
_keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count = 1) {
|
|
321
|
+
if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null)
|
|
322
|
+
return [undefined, undefined];
|
|
323
|
+
if (this.isNode(keyNodeEntryOrRaw))
|
|
324
|
+
return [keyNodeEntryOrRaw, value];
|
|
325
|
+
if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
326
|
+
const [key, entryValue] = keyNodeEntryOrRaw;
|
|
327
|
+
if (key === undefined || key === null)
|
|
328
|
+
return [undefined, undefined];
|
|
329
|
+
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
330
|
+
return [this.createNode(key, finalValue, count), finalValue];
|
|
331
|
+
}
|
|
332
|
+
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
333
|
+
const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
334
|
+
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
335
|
+
if (this.isKey(key))
|
|
336
|
+
return [this.createNode(key, finalValue, count), finalValue];
|
|
337
|
+
}
|
|
338
|
+
if (this.isKey(keyNodeEntryOrRaw))
|
|
339
|
+
return [this.createNode(keyNodeEntryOrRaw, value, count), value];
|
|
340
|
+
return [undefined, undefined];
|
|
341
|
+
}
|
|
332
342
|
/**
|
|
333
343
|
* Time Complexity: O(1)
|
|
334
344
|
* Space Complexity: O(1)
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { BST, BSTNode } from './bst';
|
|
9
|
-
import type { AVLTreeNested, AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeleteResult, BSTNOptKeyOrNode, BTNRep } from '../../types';
|
|
9
|
+
import type { AVLTreeNested, AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeleteResult, BSTNOptKeyOrNode, BTNRep, EntryCallback } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
export declare class AVLTreeNode<K = any, V = any, NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNodeNested<K, V>> extends BSTNode<K, V, NODE> {
|
|
12
12
|
/**
|
|
@@ -18,18 +18,6 @@ export declare class AVLTreeNode<K = any, V = any, NODE extends AVLTreeNode<K, V
|
|
|
18
18
|
* value associated with the key in the constructor.
|
|
19
19
|
*/
|
|
20
20
|
constructor(key: K, value?: V);
|
|
21
|
-
protected _height: number;
|
|
22
|
-
/**
|
|
23
|
-
* The function returns the value of the height property.
|
|
24
|
-
* @returns The height of the object.
|
|
25
|
-
*/
|
|
26
|
-
get height(): number;
|
|
27
|
-
/**
|
|
28
|
-
* The above function sets the value of the height property.
|
|
29
|
-
* @param {number} value - The value parameter is a number that represents the new height value to be
|
|
30
|
-
* set.
|
|
31
|
-
*/
|
|
32
|
-
set height(value: number);
|
|
33
21
|
}
|
|
34
22
|
/**
|
|
35
23
|
* 1. Height-Balanced: Each node's left and right subtrees differ in height by no more than one.
|
|
@@ -40,7 +28,7 @@ export declare class AVLTreeNode<K = any, V = any, NODE extends AVLTreeNode<K, V
|
|
|
40
28
|
* 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
|
|
41
29
|
* 7. Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes.
|
|
42
30
|
*/
|
|
43
|
-
export declare class AVLTree<K = any, V = any, R = object, NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>, TREE extends AVLTree<K, V, R, NODE, TREE> = AVLTree<K, V, R, NODE, AVLTreeNested<K, V, R, NODE>>> extends BST<K, V, R, NODE, TREE> implements IBinaryTree<K, V, R, NODE, TREE> {
|
|
31
|
+
export declare class AVLTree<K = any, V = any, R = object, MK = any, MV = any, MR = object, NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>, TREE extends AVLTree<K, V, R, MK, MV, MR, NODE, TREE> = AVLTree<K, V, R, MK, MV, MR, NODE, AVLTreeNested<K, V, R, MK, MV, MR, NODE>>> extends BST<K, V, R, MK, MV, MR, NODE, TREE> implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE> {
|
|
44
32
|
/**
|
|
45
33
|
* This is a constructor function for an AVLTree class that initializes the tree with keys, nodes,
|
|
46
34
|
* entries, or raw elements.
|
|
@@ -107,6 +95,7 @@ export declare class AVLTree<K = any, V = any, R = object, NODE extends AVLTreeN
|
|
|
107
95
|
* `deletedResults`, which is an array of `BinaryTreeDeleteResult` objects.
|
|
108
96
|
*/
|
|
109
97
|
delete(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): BinaryTreeDeleteResult<NODE>[];
|
|
98
|
+
map(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: AVLTreeOptions<MK, MV, MR>, thisArg?: any): AVLTree<MK, MV, MR>;
|
|
110
99
|
/**
|
|
111
100
|
* Time Complexity: O(1)
|
|
112
101
|
* Space Complexity: O(1)
|
|
@@ -20,22 +20,6 @@ class AVLTreeNode extends bst_1.BSTNode {
|
|
|
20
20
|
*/
|
|
21
21
|
constructor(key, value) {
|
|
22
22
|
super(key, value);
|
|
23
|
-
this._height = 0;
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* The function returns the value of the height property.
|
|
27
|
-
* @returns The height of the object.
|
|
28
|
-
*/
|
|
29
|
-
get height() {
|
|
30
|
-
return this._height;
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* The above function sets the value of the height property.
|
|
34
|
-
* @param {number} value - The value parameter is a number that represents the new height value to be
|
|
35
|
-
* set.
|
|
36
|
-
*/
|
|
37
|
-
set height(value) {
|
|
38
|
-
this._height = value;
|
|
39
23
|
}
|
|
40
24
|
}
|
|
41
25
|
exports.AVLTreeNode = AVLTreeNode;
|
|
@@ -85,7 +69,7 @@ class AVLTree extends bst_1.BST {
|
|
|
85
69
|
* @returns a new AVLTree object.
|
|
86
70
|
*/
|
|
87
71
|
createTree(options) {
|
|
88
|
-
return new AVLTree([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode,
|
|
72
|
+
return new AVLTree([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options));
|
|
89
73
|
}
|
|
90
74
|
/**
|
|
91
75
|
* The function checks if the input is an instance of AVLTreeNode.
|
|
@@ -140,6 +124,14 @@ class AVLTree extends bst_1.BST {
|
|
|
140
124
|
}
|
|
141
125
|
return deletedResults;
|
|
142
126
|
}
|
|
127
|
+
map(callback, options, thisArg) {
|
|
128
|
+
const newTree = new AVLTree([], options);
|
|
129
|
+
let index = 0;
|
|
130
|
+
for (const [key, value] of this) {
|
|
131
|
+
newTree.add(callback.call(thisArg, key, value, index++, this));
|
|
132
|
+
}
|
|
133
|
+
return newTree;
|
|
134
|
+
}
|
|
143
135
|
/**
|
|
144
136
|
* Time Complexity: O(1)
|
|
145
137
|
* Space Complexity: O(1)
|
|
@@ -224,7 +216,8 @@ class AVLTree extends bst_1.BST {
|
|
|
224
216
|
_balanceLL(A) {
|
|
225
217
|
const parentOfA = A.parent;
|
|
226
218
|
const B = A.left;
|
|
227
|
-
|
|
219
|
+
if (B !== null)
|
|
220
|
+
A.parent = B;
|
|
228
221
|
if (B && B.right) {
|
|
229
222
|
B.right.parent = A;
|
|
230
223
|
}
|
|
@@ -265,13 +258,14 @@ class AVLTree extends bst_1.BST {
|
|
|
265
258
|
if (B) {
|
|
266
259
|
C = B.right;
|
|
267
260
|
}
|
|
268
|
-
if (A)
|
|
261
|
+
if (A && C !== null)
|
|
269
262
|
A.parent = C;
|
|
270
|
-
if (B)
|
|
263
|
+
if (B && C !== null)
|
|
271
264
|
B.parent = C;
|
|
272
265
|
if (C) {
|
|
273
266
|
if (C.left) {
|
|
274
|
-
|
|
267
|
+
if (B !== null)
|
|
268
|
+
C.left.parent = B;
|
|
275
269
|
}
|
|
276
270
|
if (C.right) {
|
|
277
271
|
C.right.parent = A;
|
|
@@ -315,7 +309,8 @@ class AVLTree extends bst_1.BST {
|
|
|
315
309
|
_balanceRR(A) {
|
|
316
310
|
const parentOfA = A.parent;
|
|
317
311
|
const B = A.right;
|
|
318
|
-
|
|
312
|
+
if (B !== null)
|
|
313
|
+
A.parent = B;
|
|
319
314
|
if (B) {
|
|
320
315
|
if (B.left) {
|
|
321
316
|
B.left.parent = A;
|
|
@@ -358,15 +353,17 @@ class AVLTree extends bst_1.BST {
|
|
|
358
353
|
if (B) {
|
|
359
354
|
C = B.left;
|
|
360
355
|
}
|
|
361
|
-
|
|
362
|
-
|
|
356
|
+
if (C !== null)
|
|
357
|
+
A.parent = C;
|
|
358
|
+
if (B && C !== null)
|
|
363
359
|
B.parent = C;
|
|
364
360
|
if (C) {
|
|
365
361
|
if (C.left) {
|
|
366
362
|
C.left.parent = A;
|
|
367
363
|
}
|
|
368
364
|
if (C.right) {
|
|
369
|
-
|
|
365
|
+
if (B !== null)
|
|
366
|
+
C.right.parent = B;
|
|
370
367
|
}
|
|
371
368
|
C.parent = parentOfA;
|
|
372
369
|
}
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { BinaryTreeDeleteResult, BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BinaryTreePrintOptions, BTNEntry, BTNRep, DFSOrderPattern, EntryCallback, FamilyPosition, IterationType, NodeCallback, NodeDisplayLayout, NodePredicate, OptNodeOrNull, ToEntryFn } from '../../types';
|
|
8
|
+
import { BinaryTreeDeleteResult, BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BinaryTreePrintOptions, BTNEntry, BTNRep, DFSOrderPattern, EntryCallback, FamilyPosition, IterationType, NodeCallback, NodeDisplayLayout, NodePredicate, OptNodeOrNull, type RBTNColor, ToEntryFn } from '../../types';
|
|
9
9
|
import { IBinaryTree } from '../../interfaces';
|
|
10
10
|
import { IterableEntryBase } from '../base';
|
|
11
11
|
import { Range } from '../../common';
|
|
@@ -19,12 +19,21 @@ export declare class BinaryTreeNode<K = any, V = any, NODE extends BinaryTreeNod
|
|
|
19
19
|
value?: V;
|
|
20
20
|
parent?: NODE;
|
|
21
21
|
constructor(key: K, value?: V);
|
|
22
|
-
|
|
22
|
+
_left?: OptNodeOrNull<NODE>;
|
|
23
23
|
get left(): OptNodeOrNull<NODE>;
|
|
24
24
|
set left(v: OptNodeOrNull<NODE>);
|
|
25
|
-
|
|
25
|
+
_right?: OptNodeOrNull<NODE>;
|
|
26
26
|
get right(): OptNodeOrNull<NODE>;
|
|
27
27
|
set right(v: OptNodeOrNull<NODE>);
|
|
28
|
+
_height: number;
|
|
29
|
+
get height(): number;
|
|
30
|
+
set height(value: number);
|
|
31
|
+
_color: RBTNColor;
|
|
32
|
+
get color(): RBTNColor;
|
|
33
|
+
set color(value: RBTNColor);
|
|
34
|
+
_count: number;
|
|
35
|
+
get count(): number;
|
|
36
|
+
set count(value: number);
|
|
28
37
|
get familyPosition(): FamilyPosition;
|
|
29
38
|
}
|
|
30
39
|
/**
|
|
@@ -34,7 +43,7 @@ export declare class BinaryTreeNode<K = any, V = any, NODE extends BinaryTreeNod
|
|
|
34
43
|
* 4. Subtrees: Each child of a node forms the root of a subtree.
|
|
35
44
|
* 5. Leaf Nodes: Nodes without children are leaves.
|
|
36
45
|
*/
|
|
37
|
-
export declare class BinaryTree<K = any, V = any, R = object, NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>, TREE extends BinaryTree<K, V, R, NODE, TREE> = BinaryTree<K, V, R, NODE, BinaryTreeNested<K, V, R, NODE>>> extends IterableEntryBase<K, V | undefined> implements IBinaryTree<K, V, R, NODE, TREE> {
|
|
46
|
+
export declare class BinaryTree<K = any, V = any, R = object, MK = any, MV = any, MR = object, NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>, TREE extends BinaryTree<K, V, R, MK, MV, MR, NODE, TREE> = BinaryTree<K, V, R, MK, MV, MR, NODE, BinaryTreeNested<K, V, R, MK, MV, MR, NODE>>> extends IterableEntryBase<K, V | undefined> implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE> {
|
|
38
47
|
iterationType: IterationType;
|
|
39
48
|
/**
|
|
40
49
|
* The constructor initializes a binary tree with optional options and adds keys, nodes, entries, or
|
|
@@ -59,6 +68,9 @@ export declare class BinaryTree<K = any, V = any, R = object, NODE extends Binar
|
|
|
59
68
|
protected _toEntryFn?: ToEntryFn<K, V, R>;
|
|
60
69
|
get toEntryFn(): ToEntryFn<K, V, R> | undefined;
|
|
61
70
|
/**
|
|
71
|
+
* Time Complexity: O(1)
|
|
72
|
+
* Space Complexity: O(1)
|
|
73
|
+
*
|
|
62
74
|
* The function creates a new binary tree node with a specified key and optional value.
|
|
63
75
|
* @param {K} key - The `key` parameter is the key of the node being created in the binary tree.
|
|
64
76
|
* @param {V} [value] - The `value` parameter in the `createNode` function is optional, meaning it is
|
|
@@ -660,18 +672,22 @@ export declare class BinaryTree<K = any, V = any, R = object, NODE extends Binar
|
|
|
660
672
|
* Time Complexity: O(n)
|
|
661
673
|
* Space Complexity: O(n)
|
|
662
674
|
*
|
|
663
|
-
* The `map` function
|
|
664
|
-
*
|
|
665
|
-
* @param callback -
|
|
666
|
-
*
|
|
667
|
-
*
|
|
668
|
-
*
|
|
669
|
-
*
|
|
670
|
-
*
|
|
671
|
-
*
|
|
672
|
-
*
|
|
673
|
-
|
|
674
|
-
|
|
675
|
+
* The `map` function in TypeScript creates a new BinaryTree by applying a callback function to each
|
|
676
|
+
* entry in the original BinaryTree.
|
|
677
|
+
* @param callback - A function that will be called for each entry in the current binary tree. It
|
|
678
|
+
* takes the key, value (which can be undefined), and an array containing the mapped key and value as
|
|
679
|
+
* arguments.
|
|
680
|
+
* @param [options] - The `options` parameter in the `map` method is of type `BinaryTreeOptions<MK,
|
|
681
|
+
* MV, MR>`. It is an optional parameter that allows you to specify additional options for the binary
|
|
682
|
+
* tree being created during the mapping process. These options could include things like custom
|
|
683
|
+
* comparators, initial
|
|
684
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `map` method is used to specify the value
|
|
685
|
+
* of `this` when executing the `callback` function. It allows you to set the context (value of
|
|
686
|
+
* `this`) within the callback function. If `thisArg` is provided, it will be passed
|
|
687
|
+
* @returns The `map` function is returning a new `BinaryTree` instance filled with entries that are
|
|
688
|
+
* the result of applying the provided `callback` function to each entry in the original tree.
|
|
689
|
+
*/
|
|
690
|
+
map(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: BinaryTreeOptions<MK, MV, MR>, thisArg?: any): BinaryTree<MK, MV, MR>;
|
|
675
691
|
/**
|
|
676
692
|
* Time Complexity: O(n)
|
|
677
693
|
* Space Complexity: O(n)
|
|
@@ -19,6 +19,9 @@ const common_1 = require("../../common");
|
|
|
19
19
|
*/
|
|
20
20
|
class BinaryTreeNode {
|
|
21
21
|
constructor(key, value) {
|
|
22
|
+
this._height = 0;
|
|
23
|
+
this._color = 'BLACK';
|
|
24
|
+
this._count = 1;
|
|
22
25
|
this.key = key;
|
|
23
26
|
this.value = value;
|
|
24
27
|
}
|
|
@@ -40,6 +43,24 @@ class BinaryTreeNode {
|
|
|
40
43
|
}
|
|
41
44
|
this._right = v;
|
|
42
45
|
}
|
|
46
|
+
get height() {
|
|
47
|
+
return this._height;
|
|
48
|
+
}
|
|
49
|
+
set height(value) {
|
|
50
|
+
this._height = value;
|
|
51
|
+
}
|
|
52
|
+
get color() {
|
|
53
|
+
return this._color;
|
|
54
|
+
}
|
|
55
|
+
set color(value) {
|
|
56
|
+
this._color = value;
|
|
57
|
+
}
|
|
58
|
+
get count() {
|
|
59
|
+
return this._count;
|
|
60
|
+
}
|
|
61
|
+
set count(value) {
|
|
62
|
+
this._count = value;
|
|
63
|
+
}
|
|
43
64
|
get familyPosition() {
|
|
44
65
|
const that = this;
|
|
45
66
|
if (!this.parent) {
|
|
@@ -113,6 +134,9 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
113
134
|
return this._toEntryFn;
|
|
114
135
|
}
|
|
115
136
|
/**
|
|
137
|
+
* Time Complexity: O(1)
|
|
138
|
+
* Space Complexity: O(1)
|
|
139
|
+
*
|
|
116
140
|
* The function creates a new binary tree node with a specified key and optional value.
|
|
117
141
|
* @param {K} key - The `key` parameter is the key of the node being created in the binary tree.
|
|
118
142
|
* @param {V} [value] - The `value` parameter in the `createNode` function is optional, meaning it is
|
|
@@ -1499,7 +1523,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1499
1523
|
const newTree = this.createTree();
|
|
1500
1524
|
let index = 0;
|
|
1501
1525
|
for (const [key, value] of this) {
|
|
1502
|
-
if (predicate.call(thisArg,
|
|
1526
|
+
if (predicate.call(thisArg, key, value, index++, this)) {
|
|
1503
1527
|
newTree.add([key, value]);
|
|
1504
1528
|
}
|
|
1505
1529
|
}
|
|
@@ -1509,34 +1533,29 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1509
1533
|
* Time Complexity: O(n)
|
|
1510
1534
|
* Space Complexity: O(n)
|
|
1511
1535
|
*
|
|
1512
|
-
* The `map` function
|
|
1513
|
-
*
|
|
1514
|
-
* @param callback -
|
|
1515
|
-
*
|
|
1516
|
-
*
|
|
1517
|
-
*
|
|
1518
|
-
*
|
|
1519
|
-
*
|
|
1520
|
-
*
|
|
1521
|
-
*
|
|
1536
|
+
* The `map` function in TypeScript creates a new BinaryTree by applying a callback function to each
|
|
1537
|
+
* entry in the original BinaryTree.
|
|
1538
|
+
* @param callback - A function that will be called for each entry in the current binary tree. It
|
|
1539
|
+
* takes the key, value (which can be undefined), and an array containing the mapped key and value as
|
|
1540
|
+
* arguments.
|
|
1541
|
+
* @param [options] - The `options` parameter in the `map` method is of type `BinaryTreeOptions<MK,
|
|
1542
|
+
* MV, MR>`. It is an optional parameter that allows you to specify additional options for the binary
|
|
1543
|
+
* tree being created during the mapping process. These options could include things like custom
|
|
1544
|
+
* comparators, initial
|
|
1545
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `map` method is used to specify the value
|
|
1546
|
+
* of `this` when executing the `callback` function. It allows you to set the context (value of
|
|
1547
|
+
* `this`) within the callback function. If `thisArg` is provided, it will be passed
|
|
1548
|
+
* @returns The `map` function is returning a new `BinaryTree` instance filled with entries that are
|
|
1549
|
+
* the result of applying the provided `callback` function to each entry in the original tree.
|
|
1522
1550
|
*/
|
|
1523
|
-
map(callback, thisArg) {
|
|
1524
|
-
const newTree =
|
|
1551
|
+
map(callback, options, thisArg) {
|
|
1552
|
+
const newTree = new BinaryTree([], options);
|
|
1525
1553
|
let index = 0;
|
|
1526
1554
|
for (const [key, value] of this) {
|
|
1527
|
-
newTree.add(
|
|
1555
|
+
newTree.add(callback.call(thisArg, key, value, index++, this));
|
|
1528
1556
|
}
|
|
1529
1557
|
return newTree;
|
|
1530
1558
|
}
|
|
1531
|
-
// // TODO Type error, need to return a TREE<NV> that is a value type only for callback function.
|
|
1532
|
-
// // map<NV>(callback: (entry: [K, V | undefined], tree: this) => NV) {
|
|
1533
|
-
// // const newTree = this.createTree();
|
|
1534
|
-
// // for (const [key, value] of this) {
|
|
1535
|
-
// // newTree.add(key, callback([key, value], this));
|
|
1536
|
-
// // }
|
|
1537
|
-
// // return newTree;
|
|
1538
|
-
// // }
|
|
1539
|
-
//
|
|
1540
1559
|
/**
|
|
1541
1560
|
* Time Complexity: O(n)
|
|
1542
1561
|
* Space Complexity: O(n)
|
|
@@ -1568,7 +1587,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1568
1587
|
if (opts.isShowRedBlackNIL)
|
|
1569
1588
|
output += `S for Sentinel Node(NIL)\n`;
|
|
1570
1589
|
const display = (root) => {
|
|
1571
|
-
const [lines, ,
|
|
1590
|
+
const [lines, ,] = this._displayAux(root, opts);
|
|
1572
1591
|
let paragraph = '';
|
|
1573
1592
|
for (const line of lines) {
|
|
1574
1593
|
paragraph += line + '\n';
|