undirected-graph-typed 1.53.8 → 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/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 +64 -35
- 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 +75 -71
- package/dist/data-structures/binary-tree/bst.d.ts +35 -30
- package/dist/data-structures/binary-tree/bst.js +54 -40
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +34 -10
- package/dist/data-structures/binary-tree/red-black-tree.js +41 -43
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +33 -17
- package/dist/data-structures/binary-tree/tree-multi-map.js +62 -36
- 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 +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/data-structures/base/iterable-entry-base.ts +4 -4
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +81 -54
- package/src/data-structures/binary-tree/avl-tree.ts +32 -15
- package/src/data-structures/binary-tree/binary-tree.ts +88 -83
- package/src/data-structures/binary-tree/bst.ts +87 -74
- package/src/data-structures/binary-tree/red-black-tree.ts +55 -54
- package/src/data-structures/binary-tree/tree-multi-map.ts +79 -49
- 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 +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
|
@@ -113,6 +113,9 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
113
113
|
return this._toEntryFn;
|
|
114
114
|
}
|
|
115
115
|
/**
|
|
116
|
+
* Time Complexity: O(1)
|
|
117
|
+
* Space Complexity: O(1)
|
|
118
|
+
*
|
|
116
119
|
* The function creates a new binary tree node with a specified key and optional value.
|
|
117
120
|
* @param {K} key - The `key` parameter is the key of the node being created in the binary tree.
|
|
118
121
|
* @param {V} [value] - The `value` parameter in the `createNode` function is optional, meaning it is
|
|
@@ -125,57 +128,22 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
125
128
|
return new BinaryTreeNode(key, this._isMapMode ? undefined : value);
|
|
126
129
|
}
|
|
127
130
|
/**
|
|
128
|
-
*
|
|
129
|
-
*
|
|
130
|
-
*
|
|
131
|
-
* `
|
|
132
|
-
* of
|
|
133
|
-
*
|
|
131
|
+
* Time Complexity: O(1)
|
|
132
|
+
* Space Complexity: O(1)
|
|
133
|
+
*
|
|
134
|
+
* The `createTree` function creates a new binary tree based on the provided options.
|
|
135
|
+
* @param [options] - The `options` parameter in the `createTree` method is of type
|
|
136
|
+
* `BinaryTreeOptions<K, V, R>`. This type likely contains configuration options for creating a
|
|
137
|
+
* binary tree, such as the iteration type, whether the tree is in map mode, and functions for
|
|
138
|
+
* converting entries.
|
|
139
|
+
* @returns The `createTree` method is returning an instance of the `BinaryTree` class with the
|
|
140
|
+
* provided options. The method is creating a new `BinaryTree` object with an empty array as the
|
|
141
|
+
* initial data, and then setting various options such as `iterationType`, `isMapMode`, and
|
|
142
|
+
* `toEntryFn` based on the current object's properties and the provided `options`. Finally, it
|
|
134
143
|
*/
|
|
135
144
|
createTree(options) {
|
|
136
145
|
return new BinaryTree([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, toEntryFn: this._toEntryFn }, options));
|
|
137
146
|
}
|
|
138
|
-
/**
|
|
139
|
-
* The function `keyValueNodeEntryRawToNodeAndValue` converts various input types into a node object
|
|
140
|
-
* or returns null.
|
|
141
|
-
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
|
|
142
|
-
* `keyValueNodeEntryRawToNodeAndValue` function takes in a parameter `keyNodeEntryOrRaw`, which
|
|
143
|
-
* can be of type `BTNRep<K, V, NODE>` or `R`. This parameter represents either a key, a
|
|
144
|
-
* node, an entry
|
|
145
|
-
* @param {V} [value] - The `value` parameter in the `keyValueNodeEntryRawToNodeAndValue` function is
|
|
146
|
-
* an optional parameter of type `V`. It represents the value associated with the key in the node
|
|
147
|
-
* being created. If a `value` is provided, it will be used when creating the node. If
|
|
148
|
-
* @returns The `keyValueNodeEntryRawToNodeAndValue` function returns an optional node
|
|
149
|
-
* (`OptNodeOrNull<NODE>`) based on the input parameters provided. The function checks the type of the
|
|
150
|
-
* input parameter (`keyNodeEntryOrRaw`) and processes it accordingly to return a node or null
|
|
151
|
-
* value.
|
|
152
|
-
*/
|
|
153
|
-
_keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value) {
|
|
154
|
-
if (keyNodeEntryOrRaw === undefined)
|
|
155
|
-
return [undefined, undefined];
|
|
156
|
-
if (keyNodeEntryOrRaw === null)
|
|
157
|
-
return [null, undefined];
|
|
158
|
-
if (this.isNode(keyNodeEntryOrRaw))
|
|
159
|
-
return [keyNodeEntryOrRaw, value];
|
|
160
|
-
if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
161
|
-
const [key, entryValue] = keyNodeEntryOrRaw;
|
|
162
|
-
if (key === undefined)
|
|
163
|
-
return [undefined, undefined];
|
|
164
|
-
else if (key === null)
|
|
165
|
-
return [null, undefined];
|
|
166
|
-
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
167
|
-
return [this.createNode(key, finalValue), finalValue];
|
|
168
|
-
}
|
|
169
|
-
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
170
|
-
const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
171
|
-
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
172
|
-
if (this.isKey(key))
|
|
173
|
-
return [this.createNode(key, finalValue), finalValue];
|
|
174
|
-
}
|
|
175
|
-
if (this.isKey(keyNodeEntryOrRaw))
|
|
176
|
-
return [this.createNode(keyNodeEntryOrRaw, value), value];
|
|
177
|
-
return [undefined, undefined];
|
|
178
|
-
}
|
|
179
147
|
/**
|
|
180
148
|
* Time Complexity: O(n)
|
|
181
149
|
* Space Complexity: O(log n)
|
|
@@ -1499,7 +1467,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1499
1467
|
const newTree = this.createTree();
|
|
1500
1468
|
let index = 0;
|
|
1501
1469
|
for (const [key, value] of this) {
|
|
1502
|
-
if (predicate.call(thisArg,
|
|
1470
|
+
if (predicate.call(thisArg, key, value, index++, this)) {
|
|
1503
1471
|
newTree.add([key, value]);
|
|
1504
1472
|
}
|
|
1505
1473
|
}
|
|
@@ -1509,34 +1477,29 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1509
1477
|
* Time Complexity: O(n)
|
|
1510
1478
|
* Space Complexity: O(n)
|
|
1511
1479
|
*
|
|
1512
|
-
* The `map` function
|
|
1513
|
-
*
|
|
1514
|
-
* @param callback -
|
|
1515
|
-
*
|
|
1516
|
-
*
|
|
1517
|
-
*
|
|
1518
|
-
*
|
|
1519
|
-
*
|
|
1520
|
-
*
|
|
1521
|
-
*
|
|
1480
|
+
* The `map` function in TypeScript creates a new BinaryTree by applying a callback function to each
|
|
1481
|
+
* entry in the original BinaryTree.
|
|
1482
|
+
* @param callback - A function that will be called for each entry in the current binary tree. It
|
|
1483
|
+
* takes the key, value (which can be undefined), and an array containing the mapped key and value as
|
|
1484
|
+
* arguments.
|
|
1485
|
+
* @param [options] - The `options` parameter in the `map` method is of type `BinaryTreeOptions<MK,
|
|
1486
|
+
* MV, MR>`. It is an optional parameter that allows you to specify additional options for the binary
|
|
1487
|
+
* tree being created during the mapping process. These options could include things like custom
|
|
1488
|
+
* comparators, initial
|
|
1489
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `map` method is used to specify the value
|
|
1490
|
+
* of `this` when executing the `callback` function. It allows you to set the context (value of
|
|
1491
|
+
* `this`) within the callback function. If `thisArg` is provided, it will be passed
|
|
1492
|
+
* @returns The `map` function is returning a new `BinaryTree` instance filled with entries that are
|
|
1493
|
+
* the result of applying the provided `callback` function to each entry in the original tree.
|
|
1522
1494
|
*/
|
|
1523
|
-
map(callback, thisArg) {
|
|
1524
|
-
const newTree =
|
|
1495
|
+
map(callback, options, thisArg) {
|
|
1496
|
+
const newTree = new BinaryTree([], options);
|
|
1525
1497
|
let index = 0;
|
|
1526
1498
|
for (const [key, value] of this) {
|
|
1527
|
-
newTree.add(
|
|
1499
|
+
newTree.add(callback.call(thisArg, key, value, index++, this));
|
|
1528
1500
|
}
|
|
1529
1501
|
return newTree;
|
|
1530
1502
|
}
|
|
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
1503
|
/**
|
|
1541
1504
|
* Time Complexity: O(n)
|
|
1542
1505
|
* Space Complexity: O(n)
|
|
@@ -1568,7 +1531,7 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1568
1531
|
if (opts.isShowRedBlackNIL)
|
|
1569
1532
|
output += `S for Sentinel Node(NIL)\n`;
|
|
1570
1533
|
const display = (root) => {
|
|
1571
|
-
const [lines, ,
|
|
1534
|
+
const [lines, ,] = this._displayAux(root, opts);
|
|
1572
1535
|
let paragraph = '';
|
|
1573
1536
|
for (const line of lines) {
|
|
1574
1537
|
paragraph += line + '\n';
|
|
@@ -1596,6 +1559,47 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1596
1559
|
print(options, startNode = this._root) {
|
|
1597
1560
|
console.log(this.toVisual(startNode, options));
|
|
1598
1561
|
}
|
|
1562
|
+
/**
|
|
1563
|
+
* The function `keyValueNodeEntryRawToNodeAndValue` converts various input types into a node object
|
|
1564
|
+
* or returns null.
|
|
1565
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
|
|
1566
|
+
* `keyValueNodeEntryRawToNodeAndValue` function takes in a parameter `keyNodeEntryOrRaw`, which
|
|
1567
|
+
* can be of type `BTNRep<K, V, NODE>` or `R`. This parameter represents either a key, a
|
|
1568
|
+
* node, an entry
|
|
1569
|
+
* @param {V} [value] - The `value` parameter in the `keyValueNodeEntryRawToNodeAndValue` function is
|
|
1570
|
+
* an optional parameter of type `V`. It represents the value associated with the key in the node
|
|
1571
|
+
* being created. If a `value` is provided, it will be used when creating the node. If
|
|
1572
|
+
* @returns The `keyValueNodeEntryRawToNodeAndValue` function returns an optional node
|
|
1573
|
+
* (`OptNodeOrNull<NODE>`) based on the input parameters provided. The function checks the type of the
|
|
1574
|
+
* input parameter (`keyNodeEntryOrRaw`) and processes it accordingly to return a node or null
|
|
1575
|
+
* value.
|
|
1576
|
+
*/
|
|
1577
|
+
_keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value) {
|
|
1578
|
+
if (keyNodeEntryOrRaw === undefined)
|
|
1579
|
+
return [undefined, undefined];
|
|
1580
|
+
if (keyNodeEntryOrRaw === null)
|
|
1581
|
+
return [null, undefined];
|
|
1582
|
+
if (this.isNode(keyNodeEntryOrRaw))
|
|
1583
|
+
return [keyNodeEntryOrRaw, value];
|
|
1584
|
+
if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
1585
|
+
const [key, entryValue] = keyNodeEntryOrRaw;
|
|
1586
|
+
if (key === undefined)
|
|
1587
|
+
return [undefined, undefined];
|
|
1588
|
+
else if (key === null)
|
|
1589
|
+
return [null, undefined];
|
|
1590
|
+
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
1591
|
+
return [this.createNode(key, finalValue), finalValue];
|
|
1592
|
+
}
|
|
1593
|
+
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
1594
|
+
const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
1595
|
+
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
1596
|
+
if (this.isKey(key))
|
|
1597
|
+
return [this.createNode(key, finalValue), finalValue];
|
|
1598
|
+
}
|
|
1599
|
+
if (this.isKey(keyNodeEntryOrRaw))
|
|
1600
|
+
return [this.createNode(keyNodeEntryOrRaw, value), value];
|
|
1601
|
+
return [undefined, undefined];
|
|
1602
|
+
}
|
|
1599
1603
|
/**
|
|
1600
1604
|
* Time complexity: O(n)
|
|
1601
1605
|
* Space complexity: O(n)
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import
|
|
8
|
+
import { BSTNodeNested, BSTNOptKeyOrNode, BSTOptions, BTNRep, Comparable, Comparator, CP, DFSOrderPattern, EntryCallback, IterationType, NodeCallback, NodePredicate, OptNode } from '../../types';
|
|
9
9
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
import { Range } from '../../common';
|
|
@@ -103,7 +103,7 @@ export declare class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE>
|
|
|
103
103
|
* console.log(findLCA(5, 35)); // 15
|
|
104
104
|
* console.log(findLCA(20, 30)); // 25
|
|
105
105
|
*/
|
|
106
|
-
export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V, NODE> = BSTNode<K, V, BSTNodeNested<K, V
|
|
106
|
+
export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V, NODE> = BSTNode<K, V, BSTNodeNested<K, V>>> extends BinaryTree<K, V, R, NODE> implements IBinaryTree<K, V, R, NODE> {
|
|
107
107
|
/**
|
|
108
108
|
* This is the constructor function for a Binary Search Tree class in TypeScript.
|
|
109
109
|
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
|
|
@@ -126,6 +126,19 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
|
|
|
126
126
|
* @returns The `isReverse` property of the object, which is a boolean value.
|
|
127
127
|
*/
|
|
128
128
|
get isReverse(): boolean;
|
|
129
|
+
protected _comparator: Comparator<K>;
|
|
130
|
+
/**
|
|
131
|
+
* The function returns the value of the _comparator property.
|
|
132
|
+
* @returns The `_comparator` property is being returned.
|
|
133
|
+
*/
|
|
134
|
+
get comparator(): Comparator<K>;
|
|
135
|
+
protected _specifyComparable?: (key: K) => Comparable;
|
|
136
|
+
/**
|
|
137
|
+
* This function returns the value of the `_specifyComparable` property.
|
|
138
|
+
* @returns The method `specifyComparable()` is being returned, which is a getter method for the
|
|
139
|
+
* `_specifyComparable` property.
|
|
140
|
+
*/
|
|
141
|
+
get specifyComparable(): ((key: K) => Comparable) | undefined;
|
|
129
142
|
/**
|
|
130
143
|
* The function creates a new BSTNode with the given key and value and returns it.
|
|
131
144
|
* @param {K} key - The key parameter is of type K, which represents the type of the key for the node
|
|
@@ -136,23 +149,17 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
|
|
|
136
149
|
*/
|
|
137
150
|
createNode(key: K, value?: V): NODE;
|
|
138
151
|
/**
|
|
139
|
-
*
|
|
140
|
-
*
|
|
141
|
-
*
|
|
142
|
-
*
|
|
143
|
-
*
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
*
|
|
148
|
-
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - A variable that can be of
|
|
149
|
-
* type R or BTNRep<K, V, NODE>. It represents either a key, a node, an entry, or a raw
|
|
150
|
-
* element.
|
|
151
|
-
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
152
|
-
* value associated with a key in a key-value pair.
|
|
153
|
-
* @returns either a NODE object or undefined.
|
|
152
|
+
* Time Complexity: O(1)
|
|
153
|
+
* Space Complexity: O(1)
|
|
154
|
+
*
|
|
155
|
+
* The `createTree` function in TypeScript overrides the default options with the provided options to
|
|
156
|
+
* create a new Binary Search Tree.
|
|
157
|
+
* @param [options] - The `options` parameter in the `createTree` method is an optional object that
|
|
158
|
+
* can contain the following properties:
|
|
159
|
+
* @returns A new instance of a Binary Search Tree (BST) is being returned with the specified options
|
|
160
|
+
* and properties inherited from the current instance.
|
|
154
161
|
*/
|
|
155
|
-
|
|
162
|
+
createTree(options?: BSTOptions<K, V, R>): BST<K, V, R, BSTNode<K, V, BSTNodeNested<K, V>>>;
|
|
156
163
|
/**
|
|
157
164
|
* Time Complexity: O(log n)
|
|
158
165
|
* Space Complexity: O(log n)
|
|
@@ -229,7 +236,7 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
|
|
|
229
236
|
* @param anotherTree - `anotherTree` is an instance of a Binary Search Tree (BST) with key type `K`,
|
|
230
237
|
* value type `V`, return type `R`, node type `NODE`, and tree type `TREE`.
|
|
231
238
|
*/
|
|
232
|
-
merge(anotherTree:
|
|
239
|
+
merge(anotherTree: this): void;
|
|
233
240
|
/**
|
|
234
241
|
* Time Complexity: O(log n)
|
|
235
242
|
* Space Complexity: O(k + log n)
|
|
@@ -408,19 +415,17 @@ export declare class BST<K = any, V = any, R = object, NODE extends BSTNode<K, V
|
|
|
408
415
|
* @returns a boolean value.
|
|
409
416
|
*/
|
|
410
417
|
isAVLBalanced(iterationType?: IterationType): boolean;
|
|
411
|
-
|
|
412
|
-
/**
|
|
413
|
-
* The function returns the value of the _comparator property.
|
|
414
|
-
* @returns The `_comparator` property is being returned.
|
|
415
|
-
*/
|
|
416
|
-
get comparator(): Comparator<K>;
|
|
417
|
-
protected _extractComparable?: (key: K) => Comparable;
|
|
418
|
+
map<MK, MV, MR>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: BSTOptions<MK, MV, MR>, thisArg?: any): BST<MK, MV, MR, BSTNode<MK, MV, BSTNodeNested<MK, MV>>>;
|
|
418
419
|
/**
|
|
419
|
-
*
|
|
420
|
-
* @
|
|
421
|
-
*
|
|
420
|
+
* The function overrides a method and converts a key, value pair or entry or raw element to a node.
|
|
421
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - A variable that can be of
|
|
422
|
+
* type R or BTNRep<K, V, NODE>. It represents either a key, a node, an entry, or a raw
|
|
423
|
+
* element.
|
|
424
|
+
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
425
|
+
* value associated with a key in a key-value pair.
|
|
426
|
+
* @returns either a NODE object or undefined.
|
|
422
427
|
*/
|
|
423
|
-
|
|
428
|
+
protected _keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): [OptNode<NODE>, V | undefined];
|
|
424
429
|
/**
|
|
425
430
|
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
426
431
|
* root.
|
|
@@ -137,22 +137,22 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
137
137
|
return -1;
|
|
138
138
|
return 0;
|
|
139
139
|
}
|
|
140
|
-
if (this.
|
|
141
|
-
if (this.
|
|
140
|
+
if (this._specifyComparable) {
|
|
141
|
+
if (this._specifyComparable(a) > this._specifyComparable(b))
|
|
142
142
|
return 1;
|
|
143
|
-
if (this.
|
|
143
|
+
if (this._specifyComparable(a) < this._specifyComparable(b))
|
|
144
144
|
return -1;
|
|
145
145
|
return 0;
|
|
146
146
|
}
|
|
147
147
|
if (typeof a === 'object' || typeof b === 'object') {
|
|
148
|
-
throw TypeError(`When comparing object types, a custom
|
|
148
|
+
throw TypeError(`When comparing object types, a custom specifyComparable must be defined in the constructor's options parameter.`);
|
|
149
149
|
}
|
|
150
150
|
return 0;
|
|
151
151
|
};
|
|
152
152
|
if (options) {
|
|
153
|
-
const {
|
|
154
|
-
if (typeof
|
|
155
|
-
this.
|
|
153
|
+
const { specifyComparable, isReverse } = options;
|
|
154
|
+
if (typeof specifyComparable === 'function')
|
|
155
|
+
this._specifyComparable = specifyComparable;
|
|
156
156
|
if (isReverse !== undefined)
|
|
157
157
|
this._isReverse = isReverse;
|
|
158
158
|
}
|
|
@@ -174,6 +174,21 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
174
174
|
get isReverse() {
|
|
175
175
|
return this._isReverse;
|
|
176
176
|
}
|
|
177
|
+
/**
|
|
178
|
+
* The function returns the value of the _comparator property.
|
|
179
|
+
* @returns The `_comparator` property is being returned.
|
|
180
|
+
*/
|
|
181
|
+
get comparator() {
|
|
182
|
+
return this._comparator;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* This function returns the value of the `_specifyComparable` property.
|
|
186
|
+
* @returns The method `specifyComparable()` is being returned, which is a getter method for the
|
|
187
|
+
* `_specifyComparable` property.
|
|
188
|
+
*/
|
|
189
|
+
get specifyComparable() {
|
|
190
|
+
return this._specifyComparable;
|
|
191
|
+
}
|
|
177
192
|
/**
|
|
178
193
|
* The function creates a new BSTNode with the given key and value and returns it.
|
|
179
194
|
* @param {K} key - The key parameter is of type K, which represents the type of the key for the node
|
|
@@ -186,29 +201,19 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
186
201
|
return new BSTNode(key, this._isMapMode ? undefined : value);
|
|
187
202
|
}
|
|
188
203
|
/**
|
|
189
|
-
*
|
|
190
|
-
*
|
|
191
|
-
*
|
|
192
|
-
*
|
|
193
|
-
*
|
|
204
|
+
* Time Complexity: O(1)
|
|
205
|
+
* Space Complexity: O(1)
|
|
206
|
+
*
|
|
207
|
+
* The `createTree` function in TypeScript overrides the default options with the provided options to
|
|
208
|
+
* create a new Binary Search Tree.
|
|
209
|
+
* @param [options] - The `options` parameter in the `createTree` method is an optional object that
|
|
210
|
+
* can contain the following properties:
|
|
211
|
+
* @returns A new instance of a Binary Search Tree (BST) is being returned with the specified options
|
|
212
|
+
* and properties inherited from the current instance.
|
|
194
213
|
*/
|
|
214
|
+
// @ts-ignore
|
|
195
215
|
createTree(options) {
|
|
196
|
-
return new BST([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode,
|
|
197
|
-
}
|
|
198
|
-
/**
|
|
199
|
-
* The function overrides a method and converts a key, value pair or entry or raw element to a node.
|
|
200
|
-
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - A variable that can be of
|
|
201
|
-
* type R or BTNRep<K, V, NODE>. It represents either a key, a node, an entry, or a raw
|
|
202
|
-
* element.
|
|
203
|
-
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
204
|
-
* value associated with a key in a key-value pair.
|
|
205
|
-
* @returns either a NODE object or undefined.
|
|
206
|
-
*/
|
|
207
|
-
_keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value) {
|
|
208
|
-
const [node, entryValue] = super._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
|
|
209
|
-
if (node === null)
|
|
210
|
-
return [undefined, undefined];
|
|
211
|
-
return [node, value !== null && value !== void 0 ? value : entryValue];
|
|
216
|
+
return new BST([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options));
|
|
212
217
|
}
|
|
213
218
|
/**
|
|
214
219
|
* Time Complexity: O(log n)
|
|
@@ -248,7 +253,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
248
253
|
* this._DEFAULT_COMPARATOR`.
|
|
249
254
|
*/
|
|
250
255
|
isKey(key) {
|
|
251
|
-
return (0, utils_1.isComparable)(key, this.
|
|
256
|
+
return (0, utils_1.isComparable)(key, this._specifyComparable !== undefined);
|
|
252
257
|
}
|
|
253
258
|
/**
|
|
254
259
|
* Time Complexity: O(log n)
|
|
@@ -840,20 +845,29 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
840
845
|
}
|
|
841
846
|
return balanced;
|
|
842
847
|
}
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
848
|
+
// @ts-ignore
|
|
849
|
+
map(callback, options, thisArg) {
|
|
850
|
+
const newTree = new BST([], options);
|
|
851
|
+
let index = 0;
|
|
852
|
+
for (const [key, value] of this) {
|
|
853
|
+
newTree.add(callback.call(thisArg, key, value, index++, this));
|
|
854
|
+
}
|
|
855
|
+
return newTree;
|
|
849
856
|
}
|
|
850
857
|
/**
|
|
851
|
-
*
|
|
852
|
-
* @
|
|
853
|
-
*
|
|
858
|
+
* The function overrides a method and converts a key, value pair or entry or raw element to a node.
|
|
859
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - A variable that can be of
|
|
860
|
+
* type R or BTNRep<K, V, NODE>. It represents either a key, a node, an entry, or a raw
|
|
861
|
+
* element.
|
|
862
|
+
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
863
|
+
* value associated with a key in a key-value pair.
|
|
864
|
+
* @returns either a NODE object or undefined.
|
|
854
865
|
*/
|
|
855
|
-
|
|
856
|
-
|
|
866
|
+
_keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value) {
|
|
867
|
+
const [node, entryValue] = super._keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value);
|
|
868
|
+
if (node === null)
|
|
869
|
+
return [undefined, undefined];
|
|
870
|
+
return [node, value !== null && value !== void 0 ? value : entryValue];
|
|
857
871
|
}
|
|
858
872
|
/**
|
|
859
873
|
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { BinaryTreeDeleteResult, BTNRep, CRUD,
|
|
1
|
+
import type { BinaryTreeDeleteResult, BTNRep, CRUD, EntryCallback, RBTNColor, RedBlackTreeOptions, RedBlackTreeNodeNested } from '../../types';
|
|
2
2
|
import { BST, BSTNode } from './bst';
|
|
3
3
|
import { IBinaryTree } from '../../interfaces';
|
|
4
4
|
export declare class RedBlackTreeNode<K = any, V = any, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNodeNested<K, V>> extends BSTNode<K, V, NODE> {
|
|
@@ -79,18 +79,18 @@ export declare class RedBlackTreeNode<K = any, V = any, NODE extends RedBlackTre
|
|
|
79
79
|
* );
|
|
80
80
|
* console.log(stocksInRange); // ['GOOGL', 'MSFT', 'META']
|
|
81
81
|
*/
|
|
82
|
-
export declare class RedBlackTree<K = any, V = any, R = object, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V
|
|
82
|
+
export declare class RedBlackTree<K = any, V = any, R = object, NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>> extends BST<K, V, R, NODE> implements IBinaryTree<K, V, R, NODE> {
|
|
83
83
|
/**
|
|
84
84
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript.
|
|
85
85
|
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
|
|
86
86
|
* iterable object that can contain either keys, nodes, entries, or raw elements. It is used to
|
|
87
|
-
* initialize the
|
|
87
|
+
* initialize the RedBlackTree with the provided elements.
|
|
88
88
|
* @param [options] - The `options` parameter is an optional object that can be passed to the
|
|
89
|
-
* constructor. It is of type `
|
|
89
|
+
* constructor. It is of type `RedBlackTreeOptions<K, V, R>`. This object can contain various options for
|
|
90
90
|
* configuring the behavior of the Red-Black Tree. The specific properties and their meanings would
|
|
91
91
|
* depend on the implementation
|
|
92
92
|
*/
|
|
93
|
-
constructor(keysNodesEntriesOrRaws?: Iterable<R | BTNRep<K, V, NODE>>, options?:
|
|
93
|
+
constructor(keysNodesEntriesOrRaws?: Iterable<R | BTNRep<K, V, NODE>>, options?: RedBlackTreeOptions<K, V, R>);
|
|
94
94
|
protected _root: NODE | undefined;
|
|
95
95
|
/**
|
|
96
96
|
* The function returns the root node of a tree or undefined if there is no root.
|
|
@@ -113,12 +113,15 @@ export declare class RedBlackTree<K = any, V = any, R = object, NODE extends Red
|
|
|
113
113
|
*/
|
|
114
114
|
createNode(key: K, value?: V, color?: RBTNColor): NODE;
|
|
115
115
|
/**
|
|
116
|
-
* The function
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
*
|
|
116
|
+
* The function `createTree` overrides the default implementation to create a Red-Black Tree with
|
|
117
|
+
* specified options in TypeScript.
|
|
118
|
+
* @param [options] - The `options` parameter in the `createTree` method is of type `RedBlackTreeOptions<K,
|
|
119
|
+
* V, R>`, which is a generic type with three type parameters `K`, `V`, and `R`. This parameter
|
|
120
|
+
* allows you to pass additional configuration options when creating a new Red-
|
|
121
|
+
* @returns A new instance of a RedBlackTree with the specified options and properties from the
|
|
122
|
+
* current object is being returned.
|
|
120
123
|
*/
|
|
121
|
-
createTree(options?:
|
|
124
|
+
createTree(options?: RedBlackTreeOptions<K, V, R>): RedBlackTree<K, V, R, RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>>;
|
|
122
125
|
/**
|
|
123
126
|
* Time Complexity: O(1)
|
|
124
127
|
* Space Complexity: O(1)
|
|
@@ -169,6 +172,27 @@ export declare class RedBlackTree<K = any, V = any, R = object, NODE extends Red
|
|
|
169
172
|
* balancing is needed.
|
|
170
173
|
*/
|
|
171
174
|
delete(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): BinaryTreeDeleteResult<NODE>[];
|
|
175
|
+
/**
|
|
176
|
+
* Time Complexity: O(n)
|
|
177
|
+
* Space Complexity: O(n)
|
|
178
|
+
*
|
|
179
|
+
* The `map` function in TypeScript overrides the default behavior to create a new Red-Black Tree by
|
|
180
|
+
* applying a callback to each entry in the original tree.
|
|
181
|
+
* @param callback - A function that will be called for each entry in the tree, with parameters
|
|
182
|
+
* representing the key, value, index, and the tree itself. It should return an entry for the new
|
|
183
|
+
* tree.
|
|
184
|
+
* @param [options] - The `options` parameter in the `map` method is of type `RedBlackTreeOptions<MK, MV,
|
|
185
|
+
* MR>`. This parameter allows you to specify additional options or configurations for the Red-Black
|
|
186
|
+
* Tree that will be created during the mapping process. These options could include things like
|
|
187
|
+
* custom comparators
|
|
188
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
|
|
189
|
+
* the value of `this` when executing the `callback` function. It allows you to set the context
|
|
190
|
+
* (value of `this`) for the callback function. This can be useful when you want to access properties
|
|
191
|
+
* or
|
|
192
|
+
* @returns A new Red-Black Tree is being returned, where each entry has been transformed using the
|
|
193
|
+
* provided callback function.
|
|
194
|
+
*/
|
|
195
|
+
map<MK, MV, MR>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: RedBlackTreeOptions<MK, MV, MR>, thisArg?: any): RedBlackTree<MK, MV, MR, RedBlackTreeNode<MK, MV, RedBlackTreeNodeNested<MK, MV>>>;
|
|
172
196
|
/**
|
|
173
197
|
* Time Complexity: O(1)
|
|
174
198
|
* Space Complexity: O(1)
|