stack-typed 1.53.7 → 1.53.9
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/common/index.js +5 -0
- package/dist/data-structures/base/iterable-entry-base.js +4 -4
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +36 -17
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +65 -36
- package/dist/data-structures/binary-tree/avl-tree.d.ts +12 -8
- package/dist/data-structures/binary-tree/avl-tree.js +19 -6
- package/dist/data-structures/binary-tree/binary-tree.d.ts +53 -40
- package/dist/data-structures/binary-tree/binary-tree.js +76 -72
- package/dist/data-structures/binary-tree/bst.d.ts +87 -52
- package/dist/data-structures/binary-tree/bst.js +111 -63
- package/dist/data-structures/binary-tree/index.d.ts +1 -1
- package/dist/data-structures/binary-tree/index.js +1 -1
- package/dist/data-structures/binary-tree/{rb-tree.d.ts → red-black-tree.d.ts} +83 -10
- package/dist/data-structures/binary-tree/{rb-tree.js → red-black-tree.js} +91 -44
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +34 -18
- package/dist/data-structures/binary-tree/tree-multi-map.js +66 -40
- package/dist/data-structures/graph/abstract-graph.js +2 -2
- package/dist/data-structures/hash/hash-map.d.ts +31 -1
- package/dist/data-structures/hash/hash-map.js +35 -5
- package/dist/data-structures/heap/heap.d.ts +20 -3
- package/dist/data-structures/heap/heap.js +31 -11
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +46 -11
- package/dist/data-structures/linked-list/doubly-linked-list.js +68 -21
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +44 -11
- package/dist/data-structures/linked-list/singly-linked-list.js +70 -26
- package/dist/data-structures/queue/deque.d.ts +37 -8
- package/dist/data-structures/queue/deque.js +73 -29
- package/dist/data-structures/queue/queue.d.ts +41 -1
- package/dist/data-structures/queue/queue.js +51 -9
- package/dist/data-structures/stack/stack.d.ts +27 -10
- package/dist/data-structures/stack/stack.js +39 -20
- package/dist/data-structures/trie/trie.d.ts +8 -3
- package/dist/data-structures/trie/trie.js +8 -3
- package/dist/interfaces/binary-tree.d.ts +3 -4
- 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 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -3
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -3
- package/dist/types/data-structures/binary-tree/bst.d.ts +3 -4
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +4 -5
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +4 -5
- package/package.json +2 -2
- package/src/common/index.ts +7 -1
- package/src/data-structures/base/iterable-entry-base.ts +4 -4
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +82 -55
- package/src/data-structures/binary-tree/avl-tree.ts +32 -15
- package/src/data-structures/binary-tree/binary-tree.ts +89 -84
- package/src/data-structures/binary-tree/bst.ts +149 -97
- package/src/data-structures/binary-tree/index.ts +1 -1
- package/src/data-structures/binary-tree/{rb-tree.ts → red-black-tree.ts} +105 -55
- package/src/data-structures/binary-tree/tree-multi-map.ts +81 -51
- package/src/data-structures/graph/abstract-graph.ts +2 -2
- package/src/data-structures/hash/hash-map.ts +37 -7
- package/src/data-structures/heap/heap.ts +33 -10
- package/src/data-structures/linked-list/doubly-linked-list.ts +75 -21
- package/src/data-structures/linked-list/singly-linked-list.ts +77 -27
- package/src/data-structures/queue/deque.ts +72 -28
- package/src/data-structures/queue/queue.ts +50 -7
- package/src/data-structures/stack/stack.ts +39 -20
- package/src/data-structures/trie/trie.ts +8 -3
- package/src/interfaces/binary-tree.ts +3 -13
- package/src/types/data-structures/base/base.ts +1 -1
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +2 -4
- package/src/types/data-structures/binary-tree/avl-tree.ts +2 -4
- package/src/types/data-structures/binary-tree/binary-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/bst.ts +3 -5
- package/src/types/data-structures/binary-tree/rb-tree.ts +4 -6
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +4 -6
package/dist/common/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Range = exports.DFSOperation = void 0;
|
|
4
|
+
const utils_1 = require("../utils");
|
|
4
5
|
var DFSOperation;
|
|
5
6
|
(function (DFSOperation) {
|
|
6
7
|
DFSOperation[DFSOperation["VISIT"] = 0] = "VISIT";
|
|
@@ -12,6 +13,10 @@ class Range {
|
|
|
12
13
|
this.high = high;
|
|
13
14
|
this.includeLow = includeLow;
|
|
14
15
|
this.includeHigh = includeHigh;
|
|
16
|
+
if (!((0, utils_1.isComparable)(low) && (0, utils_1.isComparable)(high)))
|
|
17
|
+
throw new RangeError('low or high is not comparable');
|
|
18
|
+
if (low > high)
|
|
19
|
+
throw new RangeError('low must be less than or equal to high');
|
|
15
20
|
}
|
|
16
21
|
// Determine whether a key is within the range
|
|
17
22
|
isInRange(key, comparator) {
|
|
@@ -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 {
|
|
8
|
+
import type { 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> {
|
|
@@ -36,7 +36,7 @@ export declare class AVLTreeMultiMapNode<K = any, V = any, NODE extends AVLTreeM
|
|
|
36
36
|
/**
|
|
37
37
|
* 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
38
|
*/
|
|
39
|
-
export declare class AVLTreeMultiMap<K = any, V = any, R = object, NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNodeNested<K, V
|
|
39
|
+
export declare class AVLTreeMultiMap<K = any, V = any, R = object, NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNodeNested<K, V>>> extends AVLTree<K, V, R, NODE> implements IBinaryTree<K, V, R, NODE> {
|
|
40
40
|
/**
|
|
41
41
|
* The constructor initializes a new AVLTreeMultiMap object with optional initial elements.
|
|
42
42
|
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
|
|
@@ -81,7 +81,7 @@ export declare class AVLTreeMultiMap<K = any, V = any, R = object, NODE extends
|
|
|
81
81
|
* @returns a new instance of the AVLTreeMultiMap class, with the specified options, as a TREE
|
|
82
82
|
* object.
|
|
83
83
|
*/
|
|
84
|
-
createTree(options?: AVLTreeMultiMapOptions<K, V, R>):
|
|
84
|
+
createTree(options?: AVLTreeMultiMapOptions<K, V, R>): AVLTreeMultiMap<K, V, R, NODE>;
|
|
85
85
|
/**
|
|
86
86
|
* The function checks if the input is an instance of AVLTreeMultiMapNode.
|
|
87
87
|
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
@@ -90,19 +90,6 @@ export declare class AVLTreeMultiMap<K = any, V = any, R = object, NODE extends
|
|
|
90
90
|
* an instance of the `AVLTreeMultiMapNode` class.
|
|
91
91
|
*/
|
|
92
92
|
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
|
-
keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V, count?: number): [NODE | undefined, V | undefined];
|
|
106
93
|
/**
|
|
107
94
|
* Time Complexity: O(log n)
|
|
108
95
|
* Space Complexity: O(1)
|
|
@@ -169,7 +156,39 @@ export declare class AVLTreeMultiMap<K = any, V = any, R = object, NODE extends
|
|
|
169
156
|
* The function overrides the clone method to create a deep copy of a tree object.
|
|
170
157
|
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
171
158
|
*/
|
|
172
|
-
clone():
|
|
159
|
+
clone(): AVLTreeMultiMap<K, V, R, NODE>;
|
|
160
|
+
/**
|
|
161
|
+
* The `map` function in TypeScript overrides the default behavior to create a new AVLTreeMultiMap
|
|
162
|
+
* with modified entries based on a provided callback.
|
|
163
|
+
* @param callback - The `callback` parameter is a function that will be called for each entry in the
|
|
164
|
+
* AVLTreeMultiMap. It takes four arguments:
|
|
165
|
+
* @param [options] - The `options` parameter in the `override map` function is of type
|
|
166
|
+
* `AVLTreeMultiMapOptions<MK, MV, MR>`. This parameter allows you to provide additional
|
|
167
|
+
* configuration options when creating a new `AVLTreeMultiMap` instance within the `map` function.
|
|
168
|
+
* These options
|
|
169
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
|
|
170
|
+
* the value of `this` when executing the `callback` function. It allows you to set the context
|
|
171
|
+
* (value of `this`) for the callback function. This can be useful when you want to access properties
|
|
172
|
+
* or
|
|
173
|
+
* @returns The `map` method is returning a new `AVLTreeMultiMap` instance with the entries
|
|
174
|
+
* transformed by the provided `callback` function. Each entry in the original tree is passed to the
|
|
175
|
+
* `callback` function along with the index and the original tree itself. The transformed entries are
|
|
176
|
+
* then added to the new `AVLTreeMultiMap` instance, which is returned at the end.
|
|
177
|
+
*/
|
|
178
|
+
map<MK, MV, MR>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: AVLTreeMultiMapOptions<MK, MV, MR>, thisArg?: any): AVLTreeMultiMap<MK, MV, MR, AVLTreeMultiMapNode<MK, MV, AVLTreeMultiMapNodeNested<MK, MV>>>;
|
|
179
|
+
/**
|
|
180
|
+
* The function `keyValueNodeEntryRawToNodeAndValue` converts a key, value, entry, or raw element into
|
|
181
|
+
* a node object.
|
|
182
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
|
|
183
|
+
* `keyNodeEntryOrRaw` parameter can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
184
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
185
|
+
* `override` function. It represents the value associated with the key in the data structure. If no
|
|
186
|
+
* value is provided, it will default to `undefined`.
|
|
187
|
+
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
188
|
+
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
189
|
+
* @returns either a NODE object or undefined.
|
|
190
|
+
*/
|
|
191
|
+
protected _keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V, count?: number): [NODE | undefined, V | undefined];
|
|
173
192
|
/**
|
|
174
193
|
* Time Complexity: O(1)
|
|
175
194
|
* Space Complexity: O(1)
|
|
@@ -95,8 +95,9 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
95
95
|
* @returns a new instance of the AVLTreeMultiMap class, with the specified options, as a TREE
|
|
96
96
|
* object.
|
|
97
97
|
*/
|
|
98
|
+
// @ts-ignore
|
|
98
99
|
createTree(options) {
|
|
99
|
-
return new AVLTreeMultiMap([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode,
|
|
100
|
+
return new AVLTreeMultiMap([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options));
|
|
100
101
|
}
|
|
101
102
|
/**
|
|
102
103
|
* The function checks if the input is an instance of AVLTreeMultiMapNode.
|
|
@@ -108,40 +109,6 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
108
109
|
isNode(keyNodeEntryOrRaw) {
|
|
109
110
|
return keyNodeEntryOrRaw instanceof AVLTreeMultiMapNode;
|
|
110
111
|
}
|
|
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
112
|
/**
|
|
146
113
|
* Time Complexity: O(log n)
|
|
147
114
|
* Space Complexity: O(1)
|
|
@@ -160,7 +127,7 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
160
127
|
* @returns a boolean value.
|
|
161
128
|
*/
|
|
162
129
|
add(keyNodeEntryOrRaw, value, count = 1) {
|
|
163
|
-
const [newNode, newValue] = this.
|
|
130
|
+
const [newNode, newValue] = this._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count);
|
|
164
131
|
if (newNode === undefined)
|
|
165
132
|
return false;
|
|
166
133
|
const orgNodeCount = (newNode === null || newNode === void 0 ? void 0 : newNode.count) || 0;
|
|
@@ -319,6 +286,7 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
319
286
|
* The function overrides the clone method to create a deep copy of a tree object.
|
|
320
287
|
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
321
288
|
*/
|
|
289
|
+
// @ts-ignore
|
|
322
290
|
clone() {
|
|
323
291
|
const cloned = this.createTree();
|
|
324
292
|
if (this._isMapMode)
|
|
@@ -329,6 +297,67 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
329
297
|
cloned._store = this._store;
|
|
330
298
|
return cloned;
|
|
331
299
|
}
|
|
300
|
+
/**
|
|
301
|
+
* The `map` function in TypeScript overrides the default behavior to create a new AVLTreeMultiMap
|
|
302
|
+
* with modified entries based on a provided callback.
|
|
303
|
+
* @param callback - The `callback` parameter is a function that will be called for each entry in the
|
|
304
|
+
* AVLTreeMultiMap. It takes four arguments:
|
|
305
|
+
* @param [options] - The `options` parameter in the `override map` function is of type
|
|
306
|
+
* `AVLTreeMultiMapOptions<MK, MV, MR>`. This parameter allows you to provide additional
|
|
307
|
+
* configuration options when creating a new `AVLTreeMultiMap` instance within the `map` function.
|
|
308
|
+
* These options
|
|
309
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
|
|
310
|
+
* the value of `this` when executing the `callback` function. It allows you to set the context
|
|
311
|
+
* (value of `this`) for the callback function. This can be useful when you want to access properties
|
|
312
|
+
* or
|
|
313
|
+
* @returns The `map` method is returning a new `AVLTreeMultiMap` instance with the entries
|
|
314
|
+
* transformed by the provided `callback` function. Each entry in the original tree is passed to the
|
|
315
|
+
* `callback` function along with the index and the original tree itself. The transformed entries are
|
|
316
|
+
* then added to the new `AVLTreeMultiMap` instance, which is returned at the end.
|
|
317
|
+
*/
|
|
318
|
+
// @ts-ignore
|
|
319
|
+
map(callback, options, thisArg) {
|
|
320
|
+
const newTree = new AVLTreeMultiMap([], options);
|
|
321
|
+
let index = 0;
|
|
322
|
+
for (const [key, value] of this) {
|
|
323
|
+
newTree.add(callback.call(thisArg, key, value, index++, this));
|
|
324
|
+
}
|
|
325
|
+
return newTree;
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* The function `keyValueNodeEntryRawToNodeAndValue` converts a key, value, entry, or raw element into
|
|
329
|
+
* a node object.
|
|
330
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
|
|
331
|
+
* `keyNodeEntryOrRaw` parameter can be of type `R` or `BTNRep<K, V, NODE>`.
|
|
332
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
333
|
+
* `override` function. It represents the value associated with the key in the data structure. If no
|
|
334
|
+
* value is provided, it will default to `undefined`.
|
|
335
|
+
* @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
|
|
336
|
+
* times the key-value pair should be added to the data structure. If not provided, it defaults to 1.
|
|
337
|
+
* @returns either a NODE object or undefined.
|
|
338
|
+
*/
|
|
339
|
+
_keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value, count = 1) {
|
|
340
|
+
if (keyNodeEntryOrRaw === undefined || keyNodeEntryOrRaw === null)
|
|
341
|
+
return [undefined, undefined];
|
|
342
|
+
if (this.isNode(keyNodeEntryOrRaw))
|
|
343
|
+
return [keyNodeEntryOrRaw, value];
|
|
344
|
+
if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
345
|
+
const [key, entryValue] = keyNodeEntryOrRaw;
|
|
346
|
+
if (key === undefined || key === null)
|
|
347
|
+
return [undefined, undefined];
|
|
348
|
+
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
349
|
+
return [this.createNode(key, finalValue, count), finalValue];
|
|
350
|
+
}
|
|
351
|
+
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
352
|
+
const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
353
|
+
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
354
|
+
if (this.isKey(key))
|
|
355
|
+
return [this.createNode(key, finalValue, count), finalValue];
|
|
356
|
+
}
|
|
357
|
+
if (this.isKey(keyNodeEntryOrRaw))
|
|
358
|
+
return [this.createNode(keyNodeEntryOrRaw, value, count), value];
|
|
359
|
+
return [undefined, undefined];
|
|
360
|
+
}
|
|
332
361
|
/**
|
|
333
362
|
* Time Complexity: O(1)
|
|
334
363
|
* 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 {
|
|
9
|
+
import type { 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
|
/**
|
|
@@ -40,7 +40,7 @@ export declare class AVLTreeNode<K = any, V = any, NODE extends AVLTreeNode<K, V
|
|
|
40
40
|
* 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
|
|
41
41
|
* 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
42
|
*/
|
|
43
|
-
export declare class AVLTree<K = any, V = any, R = object, NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V
|
|
43
|
+
export declare class AVLTree<K = any, V = any, R = object, NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>> extends BST<K, V, R, NODE> implements IBinaryTree<K, V, R, NODE> {
|
|
44
44
|
/**
|
|
45
45
|
* This is a constructor function for an AVLTree class that initializes the tree with keys, nodes,
|
|
46
46
|
* entries, or raw elements.
|
|
@@ -64,13 +64,16 @@ export declare class AVLTree<K = any, V = any, R = object, NODE extends AVLTreeN
|
|
|
64
64
|
*/
|
|
65
65
|
createNode(key: K, value?: V): NODE;
|
|
66
66
|
/**
|
|
67
|
-
* The function
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
67
|
+
* The function `createTree` in TypeScript overrides the default AVLTree creation with the provided
|
|
68
|
+
* options.
|
|
69
|
+
* @param [options] - The `options` parameter in the `createTree` function is an object that contains
|
|
70
|
+
* configuration options for creating an AVL tree. These options can include properties such as
|
|
71
|
+
* `iterationType`, `isMapMode`, `specifyComparable`, `toEntryFn`, and `isReverse`. The function
|
|
72
|
+
* creates a
|
|
73
|
+
* @returns An AVLTree object is being returned with the specified options and properties inherited
|
|
74
|
+
* from the current object.
|
|
72
75
|
*/
|
|
73
|
-
createTree(options?: AVLTreeOptions<K, V, R>):
|
|
76
|
+
createTree(options?: AVLTreeOptions<K, V, R>): AVLTree<K, V, R, NODE>;
|
|
74
77
|
/**
|
|
75
78
|
* The function checks if the input is an instance of AVLTreeNode.
|
|
76
79
|
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
@@ -107,6 +110,7 @@ export declare class AVLTree<K = any, V = any, R = object, NODE extends AVLTreeN
|
|
|
107
110
|
* `deletedResults`, which is an array of `BinaryTreeDeleteResult` objects.
|
|
108
111
|
*/
|
|
109
112
|
delete(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): BinaryTreeDeleteResult<NODE>[];
|
|
113
|
+
map<MK, MV, MR>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: AVLTreeOptions<MK, MV, MR>, thisArg?: any): AVLTree<MK, MV, MR, AVLTreeNode<MK, MV, AVLTreeNodeNested<MK, MV>>>;
|
|
110
114
|
/**
|
|
111
115
|
* Time Complexity: O(1)
|
|
112
116
|
* Space Complexity: O(1)
|
|
@@ -78,14 +78,18 @@ class AVLTree extends bst_1.BST {
|
|
|
78
78
|
return new AVLTreeNode(key, this._isMapMode ? undefined : value);
|
|
79
79
|
}
|
|
80
80
|
/**
|
|
81
|
-
* The function
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
81
|
+
* The function `createTree` in TypeScript overrides the default AVLTree creation with the provided
|
|
82
|
+
* options.
|
|
83
|
+
* @param [options] - The `options` parameter in the `createTree` function is an object that contains
|
|
84
|
+
* configuration options for creating an AVL tree. These options can include properties such as
|
|
85
|
+
* `iterationType`, `isMapMode`, `specifyComparable`, `toEntryFn`, and `isReverse`. The function
|
|
86
|
+
* creates a
|
|
87
|
+
* @returns An AVLTree object is being returned with the specified options and properties inherited
|
|
88
|
+
* from the current object.
|
|
86
89
|
*/
|
|
90
|
+
// @ts-ignore
|
|
87
91
|
createTree(options) {
|
|
88
|
-
return new AVLTree([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode,
|
|
92
|
+
return new AVLTree([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options));
|
|
89
93
|
}
|
|
90
94
|
/**
|
|
91
95
|
* The function checks if the input is an instance of AVLTreeNode.
|
|
@@ -140,6 +144,15 @@ class AVLTree extends bst_1.BST {
|
|
|
140
144
|
}
|
|
141
145
|
return deletedResults;
|
|
142
146
|
}
|
|
147
|
+
// @ts-ignore
|
|
148
|
+
map(callback, options, thisArg) {
|
|
149
|
+
const newTree = new AVLTree([], options);
|
|
150
|
+
let index = 0;
|
|
151
|
+
for (const [key, value] of this) {
|
|
152
|
+
newTree.add(callback.call(thisArg, key, value, index++, this));
|
|
153
|
+
}
|
|
154
|
+
return newTree;
|
|
155
|
+
}
|
|
143
156
|
/**
|
|
144
157
|
* Time Complexity: O(1)
|
|
145
158
|
* Space Complexity: O(1)
|
|
@@ -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,
|
|
8
|
+
import { BinaryTreeDeleteResult, BinaryTreeNodeNested, BinaryTreeOptions, BinaryTreePrintOptions, BTNEntry, BTNRep, DFSOrderPattern, EntryCallback, FamilyPosition, IterationType, NodeCallback, NodeDisplayLayout, NodePredicate, OptNodeOrNull, ToEntryFn } from '../../types';
|
|
9
9
|
import { IBinaryTree } from '../../interfaces';
|
|
10
10
|
import { IterableEntryBase } from '../base';
|
|
11
11
|
import { Range } from '../../common';
|
|
@@ -34,7 +34,7 @@ export declare class BinaryTreeNode<K = any, V = any, NODE extends BinaryTreeNod
|
|
|
34
34
|
* 4. Subtrees: Each child of a node forms the root of a subtree.
|
|
35
35
|
* 5. Leaf Nodes: Nodes without children are leaves.
|
|
36
36
|
*/
|
|
37
|
-
export declare class BinaryTree<K = any, V = any, R = object, NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V
|
|
37
|
+
export declare class BinaryTree<K = any, V = any, R = object, NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>> extends IterableEntryBase<K, V | undefined> implements IBinaryTree<K, V, R, NODE> {
|
|
38
38
|
iterationType: IterationType;
|
|
39
39
|
/**
|
|
40
40
|
* The constructor initializes a binary tree with optional options and adds keys, nodes, entries, or
|
|
@@ -59,6 +59,9 @@ export declare class BinaryTree<K = any, V = any, R = object, NODE extends Binar
|
|
|
59
59
|
protected _toEntryFn?: ToEntryFn<K, V, R>;
|
|
60
60
|
get toEntryFn(): ToEntryFn<K, V, R> | undefined;
|
|
61
61
|
/**
|
|
62
|
+
* Time Complexity: O(1)
|
|
63
|
+
* Space Complexity: O(1)
|
|
64
|
+
*
|
|
62
65
|
* The function creates a new binary tree node with a specified key and optional value.
|
|
63
66
|
* @param {K} key - The `key` parameter is the key of the node being created in the binary tree.
|
|
64
67
|
* @param {V} [value] - The `value` parameter in the `createNode` function is optional, meaning it is
|
|
@@ -69,30 +72,20 @@ export declare class BinaryTree<K = any, V = any, R = object, NODE extends Binar
|
|
|
69
72
|
*/
|
|
70
73
|
createNode(key: K, value?: V): NODE;
|
|
71
74
|
/**
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
* `
|
|
76
|
-
* of
|
|
77
|
-
*
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
* The
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
* `keyValueNodeEntryRawToNodeAndValue` function takes in a parameter `keyNodeEntryOrRaw`, which
|
|
85
|
-
* can be of type `BTNRep<K, V, NODE>` or `R`. This parameter represents either a key, a
|
|
86
|
-
* node, an entry
|
|
87
|
-
* @param {V} [value] - The `value` parameter in the `keyValueNodeEntryRawToNodeAndValue` function is
|
|
88
|
-
* an optional parameter of type `V`. It represents the value associated with the key in the node
|
|
89
|
-
* being created. If a `value` is provided, it will be used when creating the node. If
|
|
90
|
-
* @returns The `keyValueNodeEntryRawToNodeAndValue` function returns an optional node
|
|
91
|
-
* (`OptNodeOrNull<NODE>`) based on the input parameters provided. The function checks the type of the
|
|
92
|
-
* input parameter (`keyNodeEntryOrRaw`) and processes it accordingly to return a node or null
|
|
93
|
-
* value.
|
|
75
|
+
* Time Complexity: O(1)
|
|
76
|
+
* Space Complexity: O(1)
|
|
77
|
+
*
|
|
78
|
+
* The `createTree` function creates a new binary tree based on the provided options.
|
|
79
|
+
* @param [options] - The `options` parameter in the `createTree` method is of type
|
|
80
|
+
* `BinaryTreeOptions<K, V, R>`. This type likely contains configuration options for creating a
|
|
81
|
+
* binary tree, such as the iteration type, whether the tree is in map mode, and functions for
|
|
82
|
+
* converting entries.
|
|
83
|
+
* @returns The `createTree` method is returning an instance of the `BinaryTree` class with the
|
|
84
|
+
* provided options. The method is creating a new `BinaryTree` object with an empty array as the
|
|
85
|
+
* initial data, and then setting various options such as `iterationType`, `isMapMode`, and
|
|
86
|
+
* `toEntryFn` based on the current object's properties and the provided `options`. Finally, it
|
|
94
87
|
*/
|
|
95
|
-
|
|
88
|
+
createTree(options?: BinaryTreeOptions<K, V, R>): typeof this;
|
|
96
89
|
/**
|
|
97
90
|
* Time Complexity: O(n)
|
|
98
91
|
* Space Complexity: O(log n)
|
|
@@ -238,7 +231,7 @@ export declare class BinaryTree<K = any, V = any, R = object, NODE extends Binar
|
|
|
238
231
|
* elements from the other tree.
|
|
239
232
|
* @param anotherTree - `BinaryTree<K, V, R, NODE, TREE>`
|
|
240
233
|
*/
|
|
241
|
-
merge(anotherTree:
|
|
234
|
+
merge(anotherTree: this): void;
|
|
242
235
|
/**
|
|
243
236
|
* Time Complexity: O(k * n)
|
|
244
237
|
* Space Complexity: O(1)
|
|
@@ -638,7 +631,7 @@ export declare class BinaryTree<K = any, V = any, R = object, NODE extends Binar
|
|
|
638
631
|
* original tree using breadth-first search (bfs), and adds the nodes to the new tree. If a node in
|
|
639
632
|
* the original tree is null, a null node is added to the cloned tree. If a node
|
|
640
633
|
*/
|
|
641
|
-
clone():
|
|
634
|
+
clone(): this;
|
|
642
635
|
/**
|
|
643
636
|
* Time Complexity: O(n)
|
|
644
637
|
* Space Complexity: O(n)
|
|
@@ -655,23 +648,27 @@ export declare class BinaryTree<K = any, V = any, R = object, NODE extends Binar
|
|
|
655
648
|
* @returns The `filter` method is returning a new tree that contains entries that pass the provided
|
|
656
649
|
* predicate function.
|
|
657
650
|
*/
|
|
658
|
-
filter(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: any):
|
|
651
|
+
filter(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: any): this;
|
|
659
652
|
/**
|
|
660
653
|
* Time Complexity: O(n)
|
|
661
654
|
* Space Complexity: O(n)
|
|
662
655
|
*
|
|
663
|
-
* The `map` function
|
|
664
|
-
*
|
|
665
|
-
* @param callback -
|
|
666
|
-
*
|
|
667
|
-
*
|
|
668
|
-
*
|
|
669
|
-
*
|
|
670
|
-
*
|
|
671
|
-
*
|
|
672
|
-
*
|
|
673
|
-
|
|
674
|
-
|
|
656
|
+
* The `map` function in TypeScript creates a new BinaryTree by applying a callback function to each
|
|
657
|
+
* entry in the original BinaryTree.
|
|
658
|
+
* @param callback - A function that will be called for each entry in the current binary tree. It
|
|
659
|
+
* takes the key, value (which can be undefined), and an array containing the mapped key and value as
|
|
660
|
+
* arguments.
|
|
661
|
+
* @param [options] - The `options` parameter in the `map` method is of type `BinaryTreeOptions<MK,
|
|
662
|
+
* MV, MR>`. It is an optional parameter that allows you to specify additional options for the binary
|
|
663
|
+
* tree being created during the mapping process. These options could include things like custom
|
|
664
|
+
* comparators, initial
|
|
665
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `map` method is used to specify the value
|
|
666
|
+
* of `this` when executing the `callback` function. It allows you to set the context (value of
|
|
667
|
+
* `this`) within the callback function. If `thisArg` is provided, it will be passed
|
|
668
|
+
* @returns The `map` function is returning a new `BinaryTree` instance filled with entries that are
|
|
669
|
+
* the result of applying the provided `callback` function to each entry in the original tree.
|
|
670
|
+
*/
|
|
671
|
+
map<MK, MV, MR>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: BinaryTreeOptions<MK, MV, MR>, thisArg?: any): BinaryTree<MK, MV, MR, BinaryTreeNode<MK, MV, BinaryTreeNodeNested<MK, MV>>>;
|
|
675
672
|
/**
|
|
676
673
|
* Time Complexity: O(n)
|
|
677
674
|
* Space Complexity: O(n)
|
|
@@ -707,6 +704,22 @@ export declare class BinaryTree<K = any, V = any, R = object, NODE extends Binar
|
|
|
707
704
|
* provided, the default value is set to
|
|
708
705
|
*/
|
|
709
706
|
print(options?: BinaryTreePrintOptions, startNode?: BTNRep<K, V, NODE> | R): void;
|
|
707
|
+
/**
|
|
708
|
+
* The function `keyValueNodeEntryRawToNodeAndValue` converts various input types into a node object
|
|
709
|
+
* or returns null.
|
|
710
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
|
|
711
|
+
* `keyValueNodeEntryRawToNodeAndValue` function takes in a parameter `keyNodeEntryOrRaw`, which
|
|
712
|
+
* can be of type `BTNRep<K, V, NODE>` or `R`. This parameter represents either a key, a
|
|
713
|
+
* node, an entry
|
|
714
|
+
* @param {V} [value] - The `value` parameter in the `keyValueNodeEntryRawToNodeAndValue` function is
|
|
715
|
+
* an optional parameter of type `V`. It represents the value associated with the key in the node
|
|
716
|
+
* being created. If a `value` is provided, it will be used when creating the node. If
|
|
717
|
+
* @returns The `keyValueNodeEntryRawToNodeAndValue` function returns an optional node
|
|
718
|
+
* (`OptNodeOrNull<NODE>`) based on the input parameters provided. The function checks the type of the
|
|
719
|
+
* input parameter (`keyNodeEntryOrRaw`) and processes it accordingly to return a node or null
|
|
720
|
+
* value.
|
|
721
|
+
*/
|
|
722
|
+
protected _keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): [OptNodeOrNull<NODE>, V | undefined];
|
|
710
723
|
/**
|
|
711
724
|
* Time complexity: O(n)
|
|
712
725
|
* Space complexity: O(n)
|