avl-tree-typed 1.48.4 → 1.48.6
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-base.d.ts +6 -6
- package/dist/data-structures/base/iterable-base.js +3 -3
- package/dist/data-structures/binary-tree/avl-tree.d.ts +5 -3
- package/dist/data-structures/binary-tree/avl-tree.js +6 -4
- package/dist/data-structures/binary-tree/binary-tree.d.ts +24 -22
- package/dist/data-structures/binary-tree/binary-tree.js +35 -22
- package/dist/data-structures/binary-tree/bst.d.ts +30 -22
- package/dist/data-structures/binary-tree/bst.js +38 -26
- package/dist/data-structures/binary-tree/rb-tree.d.ts +19 -13
- package/dist/data-structures/binary-tree/rb-tree.js +20 -14
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +21 -14
- package/dist/data-structures/binary-tree/tree-multimap.js +25 -18
- package/dist/data-structures/graph/abstract-graph.d.ts +52 -52
- package/dist/data-structures/graph/abstract-graph.js +78 -78
- package/dist/data-structures/graph/directed-graph.d.ts +47 -47
- package/dist/data-structures/graph/directed-graph.js +56 -56
- package/dist/data-structures/graph/map-graph.d.ts +5 -5
- package/dist/data-structures/graph/map-graph.js +8 -8
- package/dist/data-structures/graph/undirected-graph.d.ts +29 -29
- package/dist/data-structures/graph/undirected-graph.js +57 -57
- package/dist/data-structures/hash/hash-map.d.ts +8 -8
- package/dist/data-structures/hash/hash-map.js +2 -2
- package/dist/interfaces/binary-tree.d.ts +2 -2
- package/dist/types/data-structures/base/base.d.ts +3 -3
- package/package.json +2 -2
- package/src/data-structures/base/iterable-base.ts +6 -6
- package/src/data-structures/binary-tree/avl-tree.ts +7 -4
- package/src/data-structures/binary-tree/binary-tree.ts +47 -27
- package/src/data-structures/binary-tree/bst.ts +52 -32
- package/src/data-structures/binary-tree/rb-tree.ts +20 -14
- package/src/data-structures/binary-tree/tree-multimap.ts +26 -18
- package/src/data-structures/graph/abstract-graph.ts +82 -82
- package/src/data-structures/graph/directed-graph.ts +56 -56
- package/src/data-structures/graph/map-graph.ts +8 -8
- package/src/data-structures/graph/undirected-graph.ts +59 -59
- package/src/data-structures/hash/hash-map.ts +8 -8
- package/src/interfaces/binary-tree.ts +2 -2
- package/src/types/data-structures/base/base.ts +3 -3
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { ElementCallback,
|
|
2
|
-
export declare abstract class
|
|
1
|
+
import { ElementCallback, EntryCallback, ReduceElementCallback, ReduceEntryCallback } from "../../types";
|
|
2
|
+
export declare abstract class IterableEntryBase<K = any, V = any> {
|
|
3
3
|
/**
|
|
4
4
|
* Time Complexity: O(n)
|
|
5
5
|
* Space Complexity: O(1)
|
|
@@ -66,7 +66,7 @@ export declare abstract class IterablePairBase<K = any, V = any> {
|
|
|
66
66
|
* @returns The `every` method is returning a boolean value. It returns `true` if every element in
|
|
67
67
|
* the collection satisfies the provided predicate function, and `false` otherwise.
|
|
68
68
|
*/
|
|
69
|
-
every(predicate:
|
|
69
|
+
every(predicate: EntryCallback<K, V, boolean>, thisArg?: any): boolean;
|
|
70
70
|
/**
|
|
71
71
|
* Time Complexity: O(n)
|
|
72
72
|
* Space Complexity: O(1)
|
|
@@ -86,7 +86,7 @@ export declare abstract class IterablePairBase<K = any, V = any> {
|
|
|
86
86
|
* @returns a boolean value. It returns true if the predicate function returns true for any pair in
|
|
87
87
|
* the collection, and false otherwise.
|
|
88
88
|
*/
|
|
89
|
-
some(predicate:
|
|
89
|
+
some(predicate: EntryCallback<K, V, boolean>, thisArg?: any): boolean;
|
|
90
90
|
/**
|
|
91
91
|
* Time Complexity: O(n)
|
|
92
92
|
* Space Complexity: O(1)
|
|
@@ -104,7 +104,7 @@ export declare abstract class IterablePairBase<K = any, V = any> {
|
|
|
104
104
|
* specify the value of `this` within the callback function. If `thisArg` is provided, it will be
|
|
105
105
|
* used as the `this` value when calling the callback function. If `thisArg` is not provided, `
|
|
106
106
|
*/
|
|
107
|
-
forEach(callbackfn:
|
|
107
|
+
forEach(callbackfn: EntryCallback<K, V, void>, thisArg?: any): void;
|
|
108
108
|
/**
|
|
109
109
|
* Time Complexity: O(n)
|
|
110
110
|
* Space Complexity: O(1)
|
|
@@ -125,7 +125,7 @@ export declare abstract class IterablePairBase<K = any, V = any> {
|
|
|
125
125
|
* @returns The `reduce` method is returning the final value of the accumulator after iterating over
|
|
126
126
|
* all the elements in the collection.
|
|
127
127
|
*/
|
|
128
|
-
reduce<U>(callbackfn:
|
|
128
|
+
reduce<U>(callbackfn: ReduceEntryCallback<K, V, U>, initialValue: U): U;
|
|
129
129
|
protected abstract _getIterator(...args: any[]): IterableIterator<[K, V]>;
|
|
130
130
|
}
|
|
131
131
|
export declare abstract class IterableElementBase<V> {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.IterableElementBase = exports.
|
|
4
|
-
class
|
|
3
|
+
exports.IterableElementBase = exports.IterableEntryBase = void 0;
|
|
4
|
+
class IterableEntryBase {
|
|
5
5
|
/**
|
|
6
6
|
* Time Complexity: O(n)
|
|
7
7
|
* Space Complexity: O(1)
|
|
@@ -173,7 +173,7 @@ class IterablePairBase {
|
|
|
173
173
|
return accumulator;
|
|
174
174
|
}
|
|
175
175
|
}
|
|
176
|
-
exports.
|
|
176
|
+
exports.IterableEntryBase = IterableEntryBase;
|
|
177
177
|
class IterableElementBase {
|
|
178
178
|
/**
|
|
179
179
|
* Time Complexity: O(n)
|
|
@@ -68,11 +68,13 @@ export declare class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> =
|
|
|
68
68
|
*
|
|
69
69
|
* The function overrides the add method of a binary tree node and balances the tree after inserting
|
|
70
70
|
* a new node.
|
|
71
|
-
* @param keyOrNodeOrEntry - The
|
|
71
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
|
|
72
72
|
* entry.
|
|
73
|
-
* @
|
|
73
|
+
* @param {V} [value] - The `value` parameter represents the value associated with the key that is
|
|
74
|
+
* being added to the binary tree.
|
|
75
|
+
* @returns The method is returning either the inserted node or undefined.
|
|
74
76
|
*/
|
|
75
|
-
add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N
|
|
77
|
+
add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V): N | undefined;
|
|
76
78
|
/**
|
|
77
79
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
78
80
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -81,14 +81,16 @@ class AVLTree extends bst_1.BST {
|
|
|
81
81
|
*
|
|
82
82
|
* The function overrides the add method of a binary tree node and balances the tree after inserting
|
|
83
83
|
* a new node.
|
|
84
|
-
* @param keyOrNodeOrEntry - The
|
|
84
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
|
|
85
85
|
* entry.
|
|
86
|
-
* @
|
|
86
|
+
* @param {V} [value] - The `value` parameter represents the value associated with the key that is
|
|
87
|
+
* being added to the binary tree.
|
|
88
|
+
* @returns The method is returning either the inserted node or undefined.
|
|
87
89
|
*/
|
|
88
|
-
add(keyOrNodeOrEntry) {
|
|
90
|
+
add(keyOrNodeOrEntry, value) {
|
|
89
91
|
if (keyOrNodeOrEntry === null)
|
|
90
92
|
return undefined;
|
|
91
|
-
const inserted = super.add(keyOrNodeOrEntry);
|
|
93
|
+
const inserted = super.add(keyOrNodeOrEntry, value);
|
|
92
94
|
if (inserted)
|
|
93
95
|
this._balancePath(inserted);
|
|
94
96
|
return inserted;
|
|
@@ -6,9 +6,9 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { BinaryTreeNodeNested, BinaryTreeOptions, BTNCallback, BTNodeEntry, BTNodeExemplar, BTNodeKeyOrNode } from '../../types';
|
|
9
|
-
import { BinaryTreeNested, BinaryTreePrintOptions, BiTreeDeleteResult, DFSOrderPattern, FamilyPosition, IterationType, NodeDisplayLayout
|
|
9
|
+
import { BinaryTreeNested, BinaryTreePrintOptions, BiTreeDeleteResult, DFSOrderPattern, EntryCallback, FamilyPosition, IterationType, NodeDisplayLayout } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
|
-
import {
|
|
11
|
+
import { IterableEntryBase } from "../base";
|
|
12
12
|
/**
|
|
13
13
|
* Represents a node in a binary tree.
|
|
14
14
|
* @template V - The type of data stored in the node.
|
|
@@ -42,7 +42,7 @@ export declare class BinaryTreeNode<K = any, V = any, N extends BinaryTreeNode<K
|
|
|
42
42
|
* 8. Full Trees: Every node has either 0 or 2 children.
|
|
43
43
|
* 9. Complete Trees: All levels are fully filled except possibly the last, filled from left to right.
|
|
44
44
|
*/
|
|
45
|
-
export declare class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>, TREE extends BinaryTree<K, V, N, TREE> = BinaryTree<K, V, N, BinaryTreeNested<K, V, N>>> extends
|
|
45
|
+
export declare class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V, N> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>, TREE extends BinaryTree<K, V, N, TREE> = BinaryTree<K, V, N, BinaryTreeNested<K, V, N>>> extends IterableEntryBase<K, V | undefined> implements IBinaryTree<K, V, N, TREE> {
|
|
46
46
|
iterationType: IterationType;
|
|
47
47
|
/**
|
|
48
48
|
* The constructor function initializes a binary tree object with optional elements and options.
|
|
@@ -82,13 +82,14 @@ export declare class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V,
|
|
|
82
82
|
*/
|
|
83
83
|
isNode(exemplar: BTNodeExemplar<K, V, N>): exemplar is N;
|
|
84
84
|
/**
|
|
85
|
-
* The function `exemplarToNode` converts an exemplar
|
|
86
|
-
*
|
|
87
|
-
* @param
|
|
88
|
-
* function. It
|
|
89
|
-
*
|
|
85
|
+
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
86
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
|
|
87
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
88
|
+
* `exemplarToNode` function. It represents the value associated with the exemplar node. If no value
|
|
89
|
+
* is provided, it will be `undefined`.
|
|
90
|
+
* @returns a value of type N (node), or null, or undefined.
|
|
90
91
|
*/
|
|
91
|
-
exemplarToNode(exemplar: BTNodeExemplar<K, V, N
|
|
92
|
+
exemplarToNode(exemplar: BTNodeExemplar<K, V, N>, value?: V): N | null | undefined;
|
|
92
93
|
/**
|
|
93
94
|
* The function checks if a given value is an entry in a binary tree node.
|
|
94
95
|
* @param kne - BTNodeExemplar<K, V,N> - A generic type representing a node in a binary tree. It has
|
|
@@ -104,11 +105,13 @@ export declare class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V,
|
|
|
104
105
|
* Time Complexity O(log n) - O(n)
|
|
105
106
|
* Space Complexity O(1)
|
|
106
107
|
*
|
|
107
|
-
* The `add` function adds a new node to a binary tree, either by
|
|
108
|
-
*
|
|
109
|
-
* @
|
|
108
|
+
* The `add` function adds a new node to a binary tree, either by creating a new node or replacing an
|
|
109
|
+
* existing node with the same key.
|
|
110
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
|
|
111
|
+
* @param {V} [value] - The value to be inserted into the binary tree.
|
|
112
|
+
* @returns The function `add` returns either a node (`N`), `null`, or `undefined`.
|
|
110
113
|
*/
|
|
111
|
-
add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N
|
|
114
|
+
add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V): N | null | undefined;
|
|
112
115
|
/**
|
|
113
116
|
* Time Complexity: O(k log n) - O(k * n)
|
|
114
117
|
* Space Complexity: O(1)
|
|
@@ -118,14 +121,13 @@ export declare class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V,
|
|
|
118
121
|
* Time Complexity: O(k log n) - O(k * n)
|
|
119
122
|
* Space Complexity: O(1)
|
|
120
123
|
*
|
|
121
|
-
* The
|
|
122
|
-
*
|
|
123
|
-
* @param nodes -
|
|
124
|
-
*
|
|
125
|
-
* @returns The function `addMany` returns an array of
|
|
126
|
-
* `N`, `null`, or `undefined`.
|
|
124
|
+
* The `addMany` function takes in a collection of nodes and an optional collection of values, and
|
|
125
|
+
* adds each node with its corresponding value to the data structure.
|
|
126
|
+
* @param nodes - An iterable collection of BTNodeExemplar objects.
|
|
127
|
+
* @param [values] - An optional iterable of values that will be assigned to each node being added.
|
|
128
|
+
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
127
129
|
*/
|
|
128
|
-
addMany(nodes: Iterable<BTNodeExemplar<K, V, N
|
|
130
|
+
addMany(nodes: Iterable<BTNodeExemplar<K, V, N>>, values?: Iterable<V | undefined>): (N | null | undefined)[];
|
|
129
131
|
/**
|
|
130
132
|
* Time Complexity: O(k * n) "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted.
|
|
131
133
|
* Space Complexity: O(1)
|
|
@@ -495,7 +497,7 @@ export declare class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V,
|
|
|
495
497
|
* @returns The `filter` method is returning a new tree object that contains the key-value pairs that
|
|
496
498
|
* pass the given predicate function.
|
|
497
499
|
*/
|
|
498
|
-
filter(predicate:
|
|
500
|
+
filter(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: any): TREE;
|
|
499
501
|
/**
|
|
500
502
|
* Time Complexity: O(n)
|
|
501
503
|
* Space Complexity: O(n)
|
|
@@ -515,7 +517,7 @@ export declare class BinaryTree<K = any, V = any, N extends BinaryTreeNode<K, V,
|
|
|
515
517
|
* will be used as the `this` value when the callback function is called. If you don't pass a value
|
|
516
518
|
* @returns The `map` method is returning a new tree object.
|
|
517
519
|
*/
|
|
518
|
-
map(callback:
|
|
520
|
+
map(callback: EntryCallback<K, V | undefined, V>, thisArg?: any): TREE;
|
|
519
521
|
/**
|
|
520
522
|
* The `print` function is used to display a binary tree structure in a visually appealing way.
|
|
521
523
|
* @param {K | N | null | undefined} [beginRoot=this.root] - The `root` parameter is of type `K | N | null |
|
|
@@ -70,7 +70,7 @@ exports.BinaryTreeNode = BinaryTreeNode;
|
|
|
70
70
|
* 8. Full Trees: Every node has either 0 or 2 children.
|
|
71
71
|
* 9. Complete Trees: All levels are fully filled except possibly the last, filled from left to right.
|
|
72
72
|
*/
|
|
73
|
-
class BinaryTree extends base_1.
|
|
73
|
+
class BinaryTree extends base_1.IterableEntryBase {
|
|
74
74
|
/**
|
|
75
75
|
* The constructor function initializes a binary tree object with optional elements and options.
|
|
76
76
|
* @param [elements] - An optional iterable of BTNodeExemplar objects. These objects represent the
|
|
@@ -135,13 +135,14 @@ class BinaryTree extends base_1.IterablePairBase {
|
|
|
135
135
|
return exemplar instanceof BinaryTreeNode;
|
|
136
136
|
}
|
|
137
137
|
/**
|
|
138
|
-
* The function `exemplarToNode` converts an exemplar
|
|
139
|
-
*
|
|
140
|
-
* @param
|
|
141
|
-
* function. It
|
|
142
|
-
*
|
|
138
|
+
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
139
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`.
|
|
140
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
141
|
+
* `exemplarToNode` function. It represents the value associated with the exemplar node. If no value
|
|
142
|
+
* is provided, it will be `undefined`.
|
|
143
|
+
* @returns a value of type N (node), or null, or undefined.
|
|
143
144
|
*/
|
|
144
|
-
exemplarToNode(exemplar) {
|
|
145
|
+
exemplarToNode(exemplar, value) {
|
|
145
146
|
if (exemplar === undefined)
|
|
146
147
|
return;
|
|
147
148
|
let node;
|
|
@@ -164,7 +165,7 @@ class BinaryTree extends base_1.IterablePairBase {
|
|
|
164
165
|
node = exemplar;
|
|
165
166
|
}
|
|
166
167
|
else if (this.isNotNodeInstance(exemplar)) {
|
|
167
|
-
node = this.createNode(exemplar);
|
|
168
|
+
node = this.createNode(exemplar, value);
|
|
168
169
|
}
|
|
169
170
|
else {
|
|
170
171
|
return;
|
|
@@ -188,13 +189,15 @@ class BinaryTree extends base_1.IterablePairBase {
|
|
|
188
189
|
* Time Complexity O(log n) - O(n)
|
|
189
190
|
* Space Complexity O(1)
|
|
190
191
|
*
|
|
191
|
-
* The `add` function adds a new node to a binary tree, either by
|
|
192
|
-
*
|
|
193
|
-
* @
|
|
192
|
+
* The `add` function adds a new node to a binary tree, either by creating a new node or replacing an
|
|
193
|
+
* existing node with the same key.
|
|
194
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
|
|
195
|
+
* @param {V} [value] - The value to be inserted into the binary tree.
|
|
196
|
+
* @returns The function `add` returns either a node (`N`), `null`, or `undefined`.
|
|
194
197
|
*/
|
|
195
|
-
add(keyOrNodeOrEntry) {
|
|
198
|
+
add(keyOrNodeOrEntry, value) {
|
|
196
199
|
let inserted;
|
|
197
|
-
const newNode = this.exemplarToNode(keyOrNodeOrEntry);
|
|
200
|
+
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
198
201
|
if (newNode === undefined)
|
|
199
202
|
return;
|
|
200
203
|
const _bfs = (root, newNode) => {
|
|
@@ -238,18 +241,28 @@ class BinaryTree extends base_1.IterablePairBase {
|
|
|
238
241
|
* Time Complexity: O(k log n) - O(k * n)
|
|
239
242
|
* Space Complexity: O(1)
|
|
240
243
|
*
|
|
241
|
-
* The
|
|
242
|
-
*
|
|
243
|
-
* @param nodes -
|
|
244
|
-
*
|
|
245
|
-
* @returns The function `addMany` returns an array of
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
addMany(nodes) {
|
|
244
|
+
* The `addMany` function takes in a collection of nodes and an optional collection of values, and
|
|
245
|
+
* adds each node with its corresponding value to the data structure.
|
|
246
|
+
* @param nodes - An iterable collection of BTNodeExemplar objects.
|
|
247
|
+
* @param [values] - An optional iterable of values that will be assigned to each node being added.
|
|
248
|
+
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
249
|
+
*/
|
|
250
|
+
addMany(nodes, values) {
|
|
249
251
|
// TODO not sure addMany not be run multi times
|
|
250
252
|
const inserted = [];
|
|
253
|
+
let valuesIterator;
|
|
254
|
+
if (values) {
|
|
255
|
+
valuesIterator = values[Symbol.iterator]();
|
|
256
|
+
}
|
|
251
257
|
for (const kne of nodes) {
|
|
252
|
-
|
|
258
|
+
let value = undefined;
|
|
259
|
+
if (valuesIterator) {
|
|
260
|
+
const valueResult = valuesIterator.next();
|
|
261
|
+
if (!valueResult.done) {
|
|
262
|
+
value = valueResult.value;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
inserted.push(this.add(kne, value));
|
|
253
266
|
}
|
|
254
267
|
return inserted;
|
|
255
268
|
}
|
|
@@ -80,12 +80,14 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
80
80
|
*/
|
|
81
81
|
isNode(exemplar: BTNodeExemplar<K, V, N>): exemplar is N;
|
|
82
82
|
/**
|
|
83
|
-
* The function `exemplarToNode` takes an exemplar and returns a
|
|
84
|
-
*
|
|
85
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N
|
|
86
|
-
* @
|
|
83
|
+
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
|
|
84
|
+
* otherwise it returns undefined.
|
|
85
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, where:
|
|
86
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
87
|
+
* `exemplarToNode` function. It represents the value associated with the exemplar node.
|
|
88
|
+
* @returns a node of type N or undefined.
|
|
87
89
|
*/
|
|
88
|
-
exemplarToNode(exemplar: BTNodeExemplar<K, V, N
|
|
90
|
+
exemplarToNode(exemplar: BTNodeExemplar<K, V, N>, value?: V): N | undefined;
|
|
89
91
|
/**
|
|
90
92
|
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
91
93
|
* Space Complexity: O(1) - Constant space is used.
|
|
@@ -94,13 +96,15 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
94
96
|
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
95
97
|
* Space Complexity: O(1) - Constant space is used.
|
|
96
98
|
*
|
|
97
|
-
* The `add` function adds a new node to a binary
|
|
98
|
-
*
|
|
99
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can
|
|
100
|
-
* @
|
|
101
|
-
*
|
|
99
|
+
* The `add` function adds a new node to a binary tree, updating the value if the key already exists
|
|
100
|
+
* or inserting a new node if the key is unique.
|
|
101
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can accept three types of values:
|
|
102
|
+
* @param {V} [value] - The `value` parameter represents the value associated with the key that is
|
|
103
|
+
* being added to the binary tree.
|
|
104
|
+
* @returns The method `add` returns either the newly added node (`newNode`) or `undefined` if the
|
|
105
|
+
* node was not added.
|
|
102
106
|
*/
|
|
103
|
-
add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N
|
|
107
|
+
add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V): N | undefined;
|
|
104
108
|
/**
|
|
105
109
|
* Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
|
|
106
110
|
* Space Complexity: O(k) - Additional space is required for the sorted array.
|
|
@@ -109,19 +113,23 @@ export declare class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<
|
|
|
109
113
|
* Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
|
|
110
114
|
* Space Complexity: O(k) - Additional space is required for the sorted array.
|
|
111
115
|
*
|
|
112
|
-
* The `addMany` function in TypeScript adds multiple nodes to a binary tree,
|
|
113
|
-
*
|
|
114
|
-
* @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries to be added to
|
|
115
|
-
* binary tree.
|
|
116
|
-
* @param [
|
|
117
|
-
*
|
|
116
|
+
* The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
|
|
117
|
+
* balancing the tree after each addition.
|
|
118
|
+
* @param keysOrNodesOrEntries - An iterable containing the keys, nodes, or entries to be added to
|
|
119
|
+
* the binary tree.
|
|
120
|
+
* @param [values] - An optional iterable of values to be associated with the keys or nodes being
|
|
121
|
+
* added. If provided, the values will be assigned to the corresponding keys or nodes in the same
|
|
122
|
+
* order. If not provided, undefined will be assigned as the value for each key or node.
|
|
123
|
+
* @param [isBalanceAdd=true] - A boolean flag indicating whether the add operation should be
|
|
124
|
+
* balanced or not. If set to true, the add operation will be balanced using a binary search tree
|
|
125
|
+
* algorithm. If set to false, the add operation will not be balanced and the elements will be added
|
|
126
|
+
* in the order they appear in the input.
|
|
118
127
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
119
|
-
* type of iteration to use when adding multiple keys or nodes
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
* @returns The `addMany` function returns an array of `N` or `undefined` values.
|
|
128
|
+
* type of iteration to use when adding multiple keys or nodes. It has a default value of
|
|
129
|
+
* `this.iterationType`, which suggests that it is a property of the current object.
|
|
130
|
+
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
123
131
|
*/
|
|
124
|
-
addMany(keysOrNodesOrEntries: Iterable<BTNodeExemplar<K, V, N>>, isBalanceAdd?: boolean, iterationType?: IterationType): (N | undefined)[];
|
|
132
|
+
addMany(keysOrNodesOrEntries: Iterable<BTNodeExemplar<K, V, N>>, values?: Iterable<V | undefined>, isBalanceAdd?: boolean, iterationType?: IterationType): (N | undefined)[];
|
|
125
133
|
/**
|
|
126
134
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
127
135
|
* Space Complexity: O(1) - Constant space is used.
|
|
@@ -112,12 +112,14 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
112
112
|
return exemplar instanceof BSTNode;
|
|
113
113
|
}
|
|
114
114
|
/**
|
|
115
|
-
* The function `exemplarToNode` takes an exemplar and returns a
|
|
116
|
-
*
|
|
117
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N
|
|
118
|
-
* @
|
|
115
|
+
* The function `exemplarToNode` takes an exemplar and returns a node if the exemplar is valid,
|
|
116
|
+
* otherwise it returns undefined.
|
|
117
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, where:
|
|
118
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
119
|
+
* `exemplarToNode` function. It represents the value associated with the exemplar node.
|
|
120
|
+
* @returns a node of type N or undefined.
|
|
119
121
|
*/
|
|
120
|
-
exemplarToNode(exemplar) {
|
|
122
|
+
exemplarToNode(exemplar, value) {
|
|
121
123
|
let node;
|
|
122
124
|
if (exemplar === null || exemplar === undefined) {
|
|
123
125
|
return;
|
|
@@ -135,7 +137,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
135
137
|
}
|
|
136
138
|
}
|
|
137
139
|
else if (this.isNotNodeInstance(exemplar)) {
|
|
138
|
-
node = this.createNode(exemplar);
|
|
140
|
+
node = this.createNode(exemplar, value);
|
|
139
141
|
}
|
|
140
142
|
else {
|
|
141
143
|
return;
|
|
@@ -150,14 +152,16 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
150
152
|
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
151
153
|
* Space Complexity: O(1) - Constant space is used.
|
|
152
154
|
*
|
|
153
|
-
* The `add` function adds a new node to a binary
|
|
154
|
-
*
|
|
155
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can
|
|
156
|
-
* @
|
|
157
|
-
*
|
|
155
|
+
* The `add` function adds a new node to a binary tree, updating the value if the key already exists
|
|
156
|
+
* or inserting a new node if the key is unique.
|
|
157
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can accept three types of values:
|
|
158
|
+
* @param {V} [value] - The `value` parameter represents the value associated with the key that is
|
|
159
|
+
* being added to the binary tree.
|
|
160
|
+
* @returns The method `add` returns either the newly added node (`newNode`) or `undefined` if the
|
|
161
|
+
* node was not added.
|
|
158
162
|
*/
|
|
159
|
-
add(keyOrNodeOrEntry) {
|
|
160
|
-
const newNode = this.exemplarToNode(keyOrNodeOrEntry);
|
|
163
|
+
add(keyOrNodeOrEntry, value) {
|
|
164
|
+
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
161
165
|
if (newNode === undefined)
|
|
162
166
|
return;
|
|
163
167
|
if (this.root === undefined) {
|
|
@@ -207,23 +211,32 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
207
211
|
* Time Complexity: O(k log n) - Adding each element individually in a balanced tree.
|
|
208
212
|
* Space Complexity: O(k) - Additional space is required for the sorted array.
|
|
209
213
|
*
|
|
210
|
-
* The `addMany` function in TypeScript adds multiple nodes to a binary tree,
|
|
211
|
-
*
|
|
212
|
-
* @param keysOrNodesOrEntries - An iterable containing keys, nodes, or entries to be added to
|
|
213
|
-
* binary tree.
|
|
214
|
-
* @param [
|
|
215
|
-
*
|
|
214
|
+
* The `addMany` function in TypeScript adds multiple keys or nodes to a binary tree, optionally
|
|
215
|
+
* balancing the tree after each addition.
|
|
216
|
+
* @param keysOrNodesOrEntries - An iterable containing the keys, nodes, or entries to be added to
|
|
217
|
+
* the binary tree.
|
|
218
|
+
* @param [values] - An optional iterable of values to be associated with the keys or nodes being
|
|
219
|
+
* added. If provided, the values will be assigned to the corresponding keys or nodes in the same
|
|
220
|
+
* order. If not provided, undefined will be assigned as the value for each key or node.
|
|
221
|
+
* @param [isBalanceAdd=true] - A boolean flag indicating whether the add operation should be
|
|
222
|
+
* balanced or not. If set to true, the add operation will be balanced using a binary search tree
|
|
223
|
+
* algorithm. If set to false, the add operation will not be balanced and the elements will be added
|
|
224
|
+
* in the order they appear in the input.
|
|
216
225
|
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
217
|
-
* type of iteration to use when adding multiple keys or nodes
|
|
218
|
-
*
|
|
219
|
-
*
|
|
220
|
-
* @returns The `addMany` function returns an array of `N` or `undefined` values.
|
|
226
|
+
* type of iteration to use when adding multiple keys or nodes. It has a default value of
|
|
227
|
+
* `this.iterationType`, which suggests that it is a property of the current object.
|
|
228
|
+
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
221
229
|
*/
|
|
222
|
-
addMany(keysOrNodesOrEntries, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
230
|
+
addMany(keysOrNodesOrEntries, values, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
223
231
|
const inserted = [];
|
|
232
|
+
let valuesIterator;
|
|
233
|
+
if (values) {
|
|
234
|
+
valuesIterator = values[Symbol.iterator]();
|
|
235
|
+
}
|
|
224
236
|
if (!isBalanceAdd) {
|
|
225
237
|
for (const kve of keysOrNodesOrEntries) {
|
|
226
|
-
const
|
|
238
|
+
const value = valuesIterator === null || valuesIterator === void 0 ? void 0 : valuesIterator.next().value;
|
|
239
|
+
const nn = this.add(kve, value);
|
|
227
240
|
inserted.push(nn);
|
|
228
241
|
}
|
|
229
242
|
return inserted;
|
|
@@ -237,7 +250,6 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
237
250
|
for (const kve of keysOrNodesOrEntries) {
|
|
238
251
|
isRealBTNExemplar(kve) && realBTNExemplars.push(kve);
|
|
239
252
|
}
|
|
240
|
-
// TODO this addMany function is inefficient, it should be optimized
|
|
241
253
|
let sorted = [];
|
|
242
254
|
sorted = realBTNExemplars.sort((a, b) => {
|
|
243
255
|
let aR, bR;
|
|
@@ -66,25 +66,31 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
|
|
|
66
66
|
*/
|
|
67
67
|
isNode(exemplar: BTNodeExemplar<K, V, N>): exemplar is N;
|
|
68
68
|
/**
|
|
69
|
-
* The function `exemplarToNode` takes an exemplar and
|
|
70
|
-
*
|
|
71
|
-
* @param
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
* @returns a
|
|
69
|
+
* The function `exemplarToNode` takes an exemplar and converts it into a node object if possible.
|
|
70
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, where:
|
|
71
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
72
|
+
* `exemplarToNode` function. It represents the value associated with the exemplar node. If a value
|
|
73
|
+
* is provided, it will be used when creating the new node. If no value is provided, the new node
|
|
74
|
+
* @returns a node of type N or undefined.
|
|
75
75
|
*/
|
|
76
|
-
exemplarToNode(exemplar: BTNodeExemplar<K, V, N
|
|
76
|
+
exemplarToNode(exemplar: BTNodeExemplar<K, V, N>, value?: V): N | undefined;
|
|
77
77
|
/**
|
|
78
78
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
79
79
|
* Space Complexity: O(1)
|
|
80
80
|
*/
|
|
81
81
|
/**
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
* `
|
|
86
|
-
|
|
87
|
-
|
|
82
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
83
|
+
* Space Complexity: O(1)
|
|
84
|
+
*
|
|
85
|
+
* The `add` function adds a new node to a binary search tree and performs necessary rotations and
|
|
86
|
+
* color changes to maintain the red-black tree properties.
|
|
87
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
|
|
88
|
+
* entry.
|
|
89
|
+
* @param {V} [value] - The `value` parameter represents the value associated with the key that is
|
|
90
|
+
* being added to the binary search tree.
|
|
91
|
+
* @returns The method `add` returns either the newly added node (`N`) or `undefined`.
|
|
92
|
+
*/
|
|
93
|
+
add(keyOrNodeOrEntry: BTNodeExemplar<K, V, N>, value?: V): N | undefined;
|
|
88
94
|
/**
|
|
89
95
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
90
96
|
* Space Complexity: O(1)
|
|
@@ -86,14 +86,14 @@ class RedBlackTree extends bst_1.BST {
|
|
|
86
86
|
return exemplar instanceof RedBlackTreeNode;
|
|
87
87
|
}
|
|
88
88
|
/**
|
|
89
|
-
* The function `exemplarToNode` takes an exemplar and
|
|
90
|
-
*
|
|
91
|
-
* @param
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
* @returns a
|
|
89
|
+
* The function `exemplarToNode` takes an exemplar and converts it into a node object if possible.
|
|
90
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNodeExemplar<K, V, N>`, where:
|
|
91
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
92
|
+
* `exemplarToNode` function. It represents the value associated with the exemplar node. If a value
|
|
93
|
+
* is provided, it will be used when creating the new node. If no value is provided, the new node
|
|
94
|
+
* @returns a node of type N or undefined.
|
|
95
95
|
*/
|
|
96
|
-
exemplarToNode(exemplar) {
|
|
96
|
+
exemplarToNode(exemplar, value) {
|
|
97
97
|
let node;
|
|
98
98
|
if (exemplar === null || exemplar === undefined) {
|
|
99
99
|
return;
|
|
@@ -111,7 +111,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
111
111
|
}
|
|
112
112
|
}
|
|
113
113
|
else if (this.isNotNodeInstance(exemplar)) {
|
|
114
|
-
node = this.createNode(exemplar,
|
|
114
|
+
node = this.createNode(exemplar, value, types_1.RBTNColor.RED);
|
|
115
115
|
}
|
|
116
116
|
else {
|
|
117
117
|
return;
|
|
@@ -123,13 +123,19 @@ class RedBlackTree extends bst_1.BST {
|
|
|
123
123
|
* Space Complexity: O(1)
|
|
124
124
|
*/
|
|
125
125
|
/**
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
*
|
|
129
|
-
* `
|
|
126
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
127
|
+
* Space Complexity: O(1)
|
|
128
|
+
*
|
|
129
|
+
* The `add` function adds a new node to a binary search tree and performs necessary rotations and
|
|
130
|
+
* color changes to maintain the red-black tree properties.
|
|
131
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
|
|
132
|
+
* entry.
|
|
133
|
+
* @param {V} [value] - The `value` parameter represents the value associated with the key that is
|
|
134
|
+
* being added to the binary search tree.
|
|
135
|
+
* @returns The method `add` returns either the newly added node (`N`) or `undefined`.
|
|
130
136
|
*/
|
|
131
|
-
add(keyOrNodeOrEntry) {
|
|
132
|
-
const newNode = this.exemplarToNode(keyOrNodeOrEntry);
|
|
137
|
+
add(keyOrNodeOrEntry, value) {
|
|
138
|
+
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
133
139
|
if (newNode === undefined)
|
|
134
140
|
return;
|
|
135
141
|
newNode.left = this.Sentinel;
|