bst-typed 1.54.2 → 1.54.3
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/README.md +3 -3
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +21 -20
- package/dist/data-structures/binary-tree/avl-tree-counter.js +8 -7
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +12 -12
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +2 -2
- package/dist/data-structures/binary-tree/avl-tree.d.ts +25 -21
- package/dist/data-structures/binary-tree/avl-tree.js +12 -8
- package/dist/data-structures/binary-tree/binary-tree.d.ts +173 -225
- package/dist/data-structures/binary-tree/binary-tree.js +239 -144
- package/dist/data-structures/binary-tree/bst.d.ts +62 -56
- package/dist/data-structures/binary-tree/bst.js +78 -122
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +19 -25
- package/dist/data-structures/binary-tree/red-black-tree.js +7 -13
- package/dist/data-structures/binary-tree/tree-counter.d.ts +19 -19
- package/dist/data-structures/binary-tree/tree-counter.js +12 -12
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +14 -14
- package/dist/data-structures/binary-tree/tree-multi-map.js +4 -4
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -0
- package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/utils/utils.d.ts +2 -2
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-counter.ts +30 -23
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +25 -15
- package/src/data-structures/binary-tree/avl-tree.ts +35 -29
- package/src/data-structures/binary-tree/binary-tree.ts +469 -252
- package/src/data-structures/binary-tree/bst.ts +141 -143
- package/src/data-structures/binary-tree/red-black-tree.ts +27 -35
- package/src/data-structures/binary-tree/tree-counter.ts +33 -27
- package/src/data-structures/binary-tree/tree-multi-map.ts +25 -17
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -0
- package/src/types/data-structures/binary-tree/bst.ts +1 -1
- package/src/utils/utils.ts +2 -2
|
@@ -6,17 +6,12 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { BST, BSTNode } from './bst';
|
|
9
|
-
import type {
|
|
10
|
-
AVLTreeOptions,
|
|
11
|
-
BinaryTreeDeleteResult,
|
|
12
|
-
BSTNOptKeyOrNode,
|
|
13
|
-
BTNRep,
|
|
14
|
-
EntryCallback,
|
|
15
|
-
OptNodeOrNull
|
|
16
|
-
} from '../../types';
|
|
9
|
+
import type { AVLTreeOptions, BinaryTreeDeleteResult, BSTNOptKeyOrNode, EntryCallback } from '../../types';
|
|
17
10
|
import { IBinaryTree } from '../../interfaces';
|
|
18
11
|
|
|
19
12
|
export class AVLTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
13
|
+
override parent?: AVLTreeNode<K, V> = undefined;
|
|
14
|
+
|
|
20
15
|
/**
|
|
21
16
|
* This TypeScript constructor function initializes an instance with a key and an optional value.
|
|
22
17
|
* @param {K} key - The `key` parameter is typically used to uniquely identify an object or element
|
|
@@ -30,28 +25,26 @@ export class AVLTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
30
25
|
super(key, value);
|
|
31
26
|
}
|
|
32
27
|
|
|
33
|
-
override
|
|
34
|
-
|
|
35
|
-
override _left?: OptNodeOrNull<AVLTreeNode<K, V>> = undefined;
|
|
28
|
+
override _left?: AVLTreeNode<K, V> | null | undefined = undefined;
|
|
36
29
|
|
|
37
|
-
override get left():
|
|
30
|
+
override get left(): AVLTreeNode<K, V> | null | undefined {
|
|
38
31
|
return this._left;
|
|
39
32
|
}
|
|
40
33
|
|
|
41
|
-
override set left(v:
|
|
34
|
+
override set left(v: AVLTreeNode<K, V> | null | undefined) {
|
|
42
35
|
if (v) {
|
|
43
36
|
v.parent = this;
|
|
44
37
|
}
|
|
45
38
|
this._left = v;
|
|
46
39
|
}
|
|
47
40
|
|
|
48
|
-
override _right?:
|
|
41
|
+
override _right?: AVLTreeNode<K, V> | null | undefined = undefined;
|
|
49
42
|
|
|
50
|
-
override get right():
|
|
43
|
+
override get right(): AVLTreeNode<K, V> | null | undefined {
|
|
51
44
|
return this._right;
|
|
52
45
|
}
|
|
53
46
|
|
|
54
|
-
override set right(v:
|
|
47
|
+
override set right(v: AVLTreeNode<K, V> | null | undefined) {
|
|
55
48
|
if (v) {
|
|
56
49
|
v.parent = this;
|
|
57
50
|
}
|
|
@@ -76,7 +69,8 @@ export class AVLTree<K = any, V = any, R = object, MK = any, MV = any, MR = obje
|
|
|
76
69
|
* This TypeScript constructor initializes an AVLTree with keys, nodes, entries, or raw data provided
|
|
77
70
|
* in an iterable format.
|
|
78
71
|
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
|
|
79
|
-
* iterable that can contain either `
|
|
72
|
+
* iterable that can contain either `
|
|
73
|
+
K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` objects or `R` objects. It is
|
|
80
74
|
* used to initialize the AVLTree with key-value pairs or raw data entries. If provided
|
|
81
75
|
* @param [options] - The `options` parameter in the constructor is of type `AVLTreeOptions<K, V,
|
|
82
76
|
* R>`. It is an optional parameter that allows you to specify additional options for configuring the
|
|
@@ -84,7 +78,9 @@ export class AVLTree<K = any, V = any, R = object, MK = any, MV = any, MR = obje
|
|
|
84
78
|
* other configuration settings specific
|
|
85
79
|
*/
|
|
86
80
|
constructor(
|
|
87
|
-
keysNodesEntriesOrRaws: Iterable<
|
|
81
|
+
keysNodesEntriesOrRaws: Iterable<
|
|
82
|
+
K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R
|
|
83
|
+
> = [],
|
|
88
84
|
options?: AVLTreeOptions<K, V, R>
|
|
89
85
|
) {
|
|
90
86
|
super([], options);
|
|
@@ -133,12 +129,15 @@ export class AVLTree<K = any, V = any, R = object, MK = any, MV = any, MR = obje
|
|
|
133
129
|
* Space Complexity: O(1)
|
|
134
130
|
*
|
|
135
131
|
* The function checks if the input is an instance of AVLTreeNode.
|
|
136
|
-
* @param {
|
|
137
|
-
* `keyNodeOrEntry` can be of type `R` or `
|
|
132
|
+
* @param {K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter
|
|
133
|
+
* `keyNodeOrEntry` can be of type `R` or `
|
|
134
|
+
K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `.
|
|
138
135
|
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
|
|
139
136
|
* an instance of the `AVLTreeNode` class.
|
|
140
137
|
*/
|
|
141
|
-
override isNode(
|
|
138
|
+
override isNode(
|
|
139
|
+
keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
|
|
140
|
+
): keyNodeOrEntry is AVLTreeNode<K, V> {
|
|
142
141
|
return keyNodeOrEntry instanceof AVLTreeNode;
|
|
143
142
|
}
|
|
144
143
|
|
|
@@ -148,13 +147,17 @@ export class AVLTree<K = any, V = any, R = object, MK = any, MV = any, MR = obje
|
|
|
148
147
|
*
|
|
149
148
|
* The function overrides the add method of a class and inserts a key-value pair into a data
|
|
150
149
|
* structure, then balances the path.
|
|
151
|
-
* @param {
|
|
152
|
-
* `keyNodeOrEntry` can accept values of type `R`, `
|
|
150
|
+
* @param { K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter
|
|
151
|
+
* `keyNodeOrEntry` can accept values of type `R`, `
|
|
152
|
+
K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `
|
|
153
153
|
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
154
154
|
* the key or node being added to the data structure.
|
|
155
155
|
* @returns The method is returning a boolean value.
|
|
156
156
|
*/
|
|
157
|
-
override add(
|
|
157
|
+
override add(
|
|
158
|
+
keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
|
|
159
|
+
value?: V
|
|
160
|
+
): boolean {
|
|
158
161
|
if (keyNodeOrEntry === null) return false;
|
|
159
162
|
const inserted = super.add(keyNodeOrEntry, value);
|
|
160
163
|
if (inserted) this._balancePath(keyNodeOrEntry);
|
|
@@ -167,14 +170,16 @@ export class AVLTree<K = any, V = any, R = object, MK = any, MV = any, MR = obje
|
|
|
167
170
|
*
|
|
168
171
|
* The function overrides the delete method in a TypeScript class, performs deletion, and then
|
|
169
172
|
* balances the tree if necessary.
|
|
170
|
-
* @param {
|
|
173
|
+
* @param { K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `keyNodeOrEntry`
|
|
171
174
|
* parameter in the `override delete` method can be one of the following types:
|
|
172
175
|
* @returns The `delete` method is being overridden in this code snippet. It first calls the `delete`
|
|
173
176
|
* method from the superclass (presumably a parent class) with the provided `predicate`, which could
|
|
174
177
|
* be a key, node, entry, or a custom predicate. The result of this deletion operation is stored in
|
|
175
178
|
* `deletedResults`, which is an array of `BinaryTreeDeleteResult` objects.
|
|
176
179
|
*/
|
|
177
|
-
override delete(
|
|
180
|
+
override delete(
|
|
181
|
+
keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
|
|
182
|
+
): BinaryTreeDeleteResult<AVLTreeNode<K, V>>[] {
|
|
178
183
|
const deletedResults = super.delete(keyNodeOrEntry);
|
|
179
184
|
for (const { needBalanced } of deletedResults) {
|
|
180
185
|
if (needBalanced) {
|
|
@@ -487,10 +492,11 @@ export class AVLTree<K = any, V = any, R = object, MK = any, MV = any, MR = obje
|
|
|
487
492
|
*
|
|
488
493
|
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
489
494
|
* to restore balance in an AVL tree after inserting a node.
|
|
490
|
-
* @param {
|
|
491
|
-
* `
|
|
495
|
+
* @param { K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } node - The `node` parameter can be of type `R` or
|
|
496
|
+
* `
|
|
497
|
+
K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `.
|
|
492
498
|
*/
|
|
493
|
-
protected _balancePath(node:
|
|
499
|
+
protected _balancePath(node: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): void {
|
|
494
500
|
node = this.ensureNode(node);
|
|
495
501
|
const path = this.getPathToRoot(node, node => node, false); // first O(log n) + O(log n)
|
|
496
502
|
for (let i = 0; i < path.length; i++) {
|