bst-typed 1.54.0 → 1.54.2
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/LICENSE +1 -1
- package/coverage/lcov-report/index.ts.html +2 -2
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +213 -0
- package/dist/data-structures/binary-tree/avl-tree-counter.js +407 -0
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +71 -177
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -340
- package/dist/data-structures/binary-tree/avl-tree.d.ts +102 -57
- package/dist/data-structures/binary-tree/avl-tree.js +110 -47
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +3 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +3 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +240 -190
- package/dist/data-structures/binary-tree/binary-tree.js +269 -240
- package/dist/data-structures/binary-tree/bst.d.ts +145 -112
- package/dist/data-structures/binary-tree/bst.js +180 -129
- package/dist/data-structures/binary-tree/index.d.ts +2 -0
- package/dist/data-structures/binary-tree/index.js +2 -0
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +100 -82
- package/dist/data-structures/binary-tree/red-black-tree.js +115 -79
- package/dist/data-structures/binary-tree/tree-counter.d.ts +212 -0
- package/dist/data-structures/binary-tree/tree-counter.js +444 -0
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +78 -174
- package/dist/data-structures/binary-tree/tree-multi-map.js +142 -377
- package/dist/data-structures/graph/directed-graph.d.ts +3 -0
- package/dist/data-structures/graph/directed-graph.js +3 -0
- package/dist/data-structures/graph/map-graph.d.ts +3 -0
- package/dist/data-structures/graph/map-graph.js +3 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +3 -0
- package/dist/data-structures/graph/undirected-graph.js +3 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +3 -0
- package/dist/data-structures/linked-list/singly-linked-list.js +3 -0
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +3 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +3 -0
- package/dist/data-structures/matrix/matrix.d.ts +3 -0
- package/dist/data-structures/matrix/matrix.js +3 -0
- package/dist/data-structures/matrix/navigator.d.ts +3 -0
- package/dist/data-structures/matrix/navigator.js +3 -0
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +3 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +3 -0
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +3 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +3 -0
- package/dist/data-structures/trie/trie.d.ts +0 -4
- package/dist/data-structures/trie/trie.js +0 -4
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/interfaces/binary-tree.d.ts +8 -8
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -4
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -3
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -3
- package/dist/types/data-structures/binary-tree/bst.d.ts +3 -3
- package/dist/types/data-structures/binary-tree/index.d.ts +3 -1
- package/dist/types/data-structures/binary-tree/index.js +3 -1
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +3 -0
- package/dist/types/data-structures/binary-tree/red-black-tree.js +2 -0
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
- package/dist/types/data-structures/binary-tree/tree-counter.js +2 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -5
- package/package.json +3 -3
- package/src/data-structures/binary-tree/avl-tree-counter.ts +463 -0
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +155 -393
- package/src/data-structures/binary-tree/avl-tree.ts +144 -93
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +3 -0
- package/src/data-structures/binary-tree/binary-tree.ts +433 -405
- package/src/data-structures/binary-tree/bst.ts +261 -239
- package/src/data-structures/binary-tree/index.ts +2 -0
- package/src/data-structures/binary-tree/red-black-tree.ts +163 -134
- package/src/data-structures/binary-tree/tree-counter.ts +504 -0
- package/src/data-structures/binary-tree/tree-multi-map.ts +161 -429
- package/src/data-structures/graph/directed-graph.ts +3 -0
- package/src/data-structures/graph/map-graph.ts +3 -0
- package/src/data-structures/graph/undirected-graph.ts +3 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +3 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
- package/src/data-structures/matrix/matrix.ts +3 -0
- package/src/data-structures/matrix/navigator.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +3 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +3 -0
- package/src/data-structures/trie/trie.ts +0 -4
- package/src/index.ts +2 -2
- package/src/interfaces/binary-tree.ts +10 -24
- package/src/types/data-structures/binary-tree/avl-tree-counter.ts +3 -0
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -6
- package/src/types/data-structures/binary-tree/avl-tree.ts +0 -5
- package/src/types/data-structures/binary-tree/binary-tree.ts +0 -5
- package/src/types/data-structures/binary-tree/bst.ts +5 -5
- package/src/types/data-structures/binary-tree/index.ts +3 -1
- package/src/types/data-structures/binary-tree/red-black-tree.ts +5 -0
- package/src/types/data-structures/binary-tree/tree-counter.ts +3 -0
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +2 -7
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +0 -6
- package/src/types/data-structures/binary-tree/rb-tree.ts +0 -10
- /package/dist/types/data-structures/binary-tree/{rb-tree.js → avl-tree-counter.js} +0 -0
|
@@ -6,18 +6,26 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { BST, BSTNode } from './bst';
|
|
9
|
-
import type {
|
|
9
|
+
import type { AVLTreeOptions, BinaryTreeDeleteResult, BSTNOptKeyOrNode, BTNRep, EntryCallback, OptNodeOrNull } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
|
-
export declare class AVLTreeNode<K = any, V = any
|
|
11
|
+
export declare class AVLTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
12
12
|
/**
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
* @param {V} [value] - The
|
|
18
|
-
*
|
|
13
|
+
* This TypeScript constructor function initializes an instance with a key and an optional value.
|
|
14
|
+
* @param {K} key - The `key` parameter is typically used to uniquely identify an object or element
|
|
15
|
+
* within a data structure. It serves as a reference or identifier for accessing or manipulating the
|
|
16
|
+
* associated value or data.
|
|
17
|
+
* @param {V} [value] - The `value` parameter in the constructor is optional, meaning it does not
|
|
18
|
+
* have to be provided when creating an instance of the class. If a value is not provided, it will
|
|
19
|
+
* default to `undefined`.
|
|
19
20
|
*/
|
|
20
21
|
constructor(key: K, value?: V);
|
|
22
|
+
parent?: AVLTreeNode<K, V>;
|
|
23
|
+
_left?: OptNodeOrNull<AVLTreeNode<K, V>>;
|
|
24
|
+
get left(): OptNodeOrNull<AVLTreeNode<K, V>>;
|
|
25
|
+
set left(v: OptNodeOrNull<AVLTreeNode<K, V>>);
|
|
26
|
+
_right?: OptNodeOrNull<AVLTreeNode<K, V>>;
|
|
27
|
+
get right(): OptNodeOrNull<AVLTreeNode<K, V>>;
|
|
28
|
+
set right(v: OptNodeOrNull<AVLTreeNode<K, V>>);
|
|
21
29
|
}
|
|
22
30
|
/**
|
|
23
31
|
* 1. Height-Balanced: Each node's left and right subtrees differ in height by no more than one.
|
|
@@ -28,162 +36,199 @@ export declare class AVLTreeNode<K = any, V = any, NODE extends AVLTreeNode<K, V
|
|
|
28
36
|
* 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
|
|
29
37
|
* 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.
|
|
30
38
|
*/
|
|
31
|
-
export declare class AVLTree<K = any, V = any, R = object, MK = any, MV = any, MR = object
|
|
39
|
+
export declare class AVLTree<K = any, V = any, R = object, MK = any, MV = any, MR = object> extends BST<K, V, R, MK, MV, MR> implements IBinaryTree<K, V, R, MK, MV, MR> {
|
|
32
40
|
/**
|
|
33
|
-
* This
|
|
34
|
-
*
|
|
35
|
-
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
|
|
36
|
-
* iterable
|
|
37
|
-
*
|
|
38
|
-
* @param [options] - The `options` parameter
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
41
|
+
* This TypeScript constructor initializes an AVLTree with keys, nodes, entries, or raw data provided
|
|
42
|
+
* in an iterable format.
|
|
43
|
+
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
|
|
44
|
+
* iterable that can contain either `BTNRep<K, V, AVLTreeNode<K, V>>` objects or `R` objects. It is
|
|
45
|
+
* used to initialize the AVLTree with key-value pairs or raw data entries. If provided
|
|
46
|
+
* @param [options] - The `options` parameter in the constructor is of type `AVLTreeOptions<K, V,
|
|
47
|
+
* R>`. It is an optional parameter that allows you to specify additional options for configuring the
|
|
48
|
+
* AVL tree. These options could include things like custom comparators, initial capacity, or any
|
|
49
|
+
* other configuration settings specific
|
|
42
50
|
*/
|
|
43
|
-
constructor(keysNodesEntriesOrRaws?: Iterable<
|
|
51
|
+
constructor(keysNodesEntriesOrRaws?: Iterable<BTNRep<K, V, AVLTreeNode<K, V>> | R>, options?: AVLTreeOptions<K, V, R>);
|
|
44
52
|
/**
|
|
53
|
+
* Time Complexity: O(1)
|
|
54
|
+
* Space Complexity: O(1)
|
|
55
|
+
*
|
|
45
56
|
* The function creates a new AVL tree node with the given key and value.
|
|
46
57
|
* @param {K} key - The key parameter is of type K, which represents the key of the node being
|
|
47
58
|
* created.
|
|
48
59
|
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
|
|
49
60
|
* value associated with the key in the node being created.
|
|
50
61
|
* @returns The method is returning a new instance of the AVLTreeNode class, casted as the generic
|
|
51
|
-
* type
|
|
62
|
+
* type AVLTreeNode<K, V>.
|
|
52
63
|
*/
|
|
53
|
-
createNode(key: K, value?: V):
|
|
64
|
+
createNode(key: K, value?: V): AVLTreeNode<K, V>;
|
|
54
65
|
/**
|
|
66
|
+
* Time Complexity: O(1)
|
|
67
|
+
* Space Complexity: O(1)
|
|
68
|
+
*
|
|
55
69
|
* The function creates a new AVL tree with the specified options and returns it.
|
|
56
70
|
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
57
71
|
* passed to the `createTree` function. It is used to customize the behavior of the AVL tree that is
|
|
58
72
|
* being created.
|
|
59
73
|
* @returns a new AVLTree object.
|
|
60
74
|
*/
|
|
61
|
-
createTree(options?: AVLTreeOptions<K, V, R>):
|
|
75
|
+
createTree(options?: AVLTreeOptions<K, V, R>): AVLTree<K, V, R, MK, MV, MR>;
|
|
62
76
|
/**
|
|
77
|
+
* Time Complexity: O(1)
|
|
78
|
+
* Space Complexity: O(1)
|
|
79
|
+
*
|
|
63
80
|
* The function checks if the input is an instance of AVLTreeNode.
|
|
64
|
-
* @param {BTNRep<K, V,
|
|
65
|
-
* `
|
|
66
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
81
|
+
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The parameter
|
|
82
|
+
* `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, AVLTreeNode<K, V>>`.
|
|
83
|
+
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
|
|
67
84
|
* an instance of the `AVLTreeNode` class.
|
|
68
85
|
*/
|
|
69
|
-
isNode(
|
|
86
|
+
isNode(keyNodeOrEntry: BTNRep<K, V, AVLTreeNode<K, V>>): keyNodeOrEntry is AVLTreeNode<K, V>;
|
|
70
87
|
/**
|
|
71
88
|
* Time Complexity: O(log n)
|
|
72
|
-
* Space Complexity: O(
|
|
89
|
+
* Space Complexity: O(log n)
|
|
73
90
|
*
|
|
74
91
|
* The function overrides the add method of a class and inserts a key-value pair into a data
|
|
75
92
|
* structure, then balances the path.
|
|
76
|
-
* @param {BTNRep<K, V,
|
|
77
|
-
* `
|
|
78
|
-
* `RawElement`.
|
|
93
|
+
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The parameter
|
|
94
|
+
* `keyNodeOrEntry` can accept values of type `R`, `BTNRep<K, V, AVLTreeNode<K, V>>`
|
|
79
95
|
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
80
96
|
* the key or node being added to the data structure.
|
|
81
97
|
* @returns The method is returning a boolean value.
|
|
82
98
|
*/
|
|
83
|
-
add(
|
|
99
|
+
add(keyNodeOrEntry: BTNRep<K, V, AVLTreeNode<K, V>>, value?: V): boolean;
|
|
84
100
|
/**
|
|
85
101
|
* Time Complexity: O(log n)
|
|
86
|
-
* Space Complexity: O(
|
|
102
|
+
* Space Complexity: O(log n)
|
|
87
103
|
*
|
|
88
104
|
* The function overrides the delete method in a TypeScript class, performs deletion, and then
|
|
89
105
|
* balances the tree if necessary.
|
|
90
|
-
* @param {BTNRep<K, V,
|
|
106
|
+
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry`
|
|
91
107
|
* parameter in the `override delete` method can be one of the following types:
|
|
92
108
|
* @returns The `delete` method is being overridden in this code snippet. It first calls the `delete`
|
|
93
109
|
* method from the superclass (presumably a parent class) with the provided `predicate`, which could
|
|
94
110
|
* be a key, node, entry, or a custom predicate. The result of this deletion operation is stored in
|
|
95
111
|
* `deletedResults`, which is an array of `BinaryTreeDeleteResult` objects.
|
|
96
112
|
*/
|
|
97
|
-
delete(
|
|
113
|
+
delete(keyNodeOrEntry: BTNRep<K, V, AVLTreeNode<K, V>>): BinaryTreeDeleteResult<AVLTreeNode<K, V>>[];
|
|
114
|
+
/**
|
|
115
|
+
* Time Complexity: O(n)
|
|
116
|
+
* Space Complexity: O(n)
|
|
117
|
+
*
|
|
118
|
+
* The `map` function in TypeScript overrides the default map behavior of an AVLTree data structure
|
|
119
|
+
* by applying a callback function to each entry and creating a new AVLTree with the results.
|
|
120
|
+
* @param callback - A function that will be called for each entry in the AVLTree. It takes four
|
|
121
|
+
* arguments: the key, the value (which can be undefined), the index of the entry, and a reference to
|
|
122
|
+
* the AVLTree itself.
|
|
123
|
+
* @param [options] - The `options` parameter in the `override map` function is of type
|
|
124
|
+
* `AVLTreeOptions<MK, MV, MR>`. It is an optional parameter that allows you to specify additional
|
|
125
|
+
* options for the AVL tree being created during the mapping process. These options could include
|
|
126
|
+
* custom comparators, initial
|
|
127
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
|
|
128
|
+
* the value of `this` when executing the `callback` function. It allows you to set the context
|
|
129
|
+
* (value of `this`) within the callback function. This can be useful when you want to access
|
|
130
|
+
* properties or
|
|
131
|
+
* @returns The `map` method is returning a new AVLTree instance (`newTree`) with the entries
|
|
132
|
+
* modified by the provided callback function.
|
|
133
|
+
*/
|
|
98
134
|
map(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: AVLTreeOptions<MK, MV, MR>, thisArg?: any): AVLTree<MK, MV, MR>;
|
|
135
|
+
/**
|
|
136
|
+
* Time Complexity: O(n)
|
|
137
|
+
* Space Complexity: O(n)
|
|
138
|
+
*
|
|
139
|
+
* The function `clone` overrides the default cloning behavior to create a deep copy of a tree
|
|
140
|
+
* structure.
|
|
141
|
+
* @returns A cloned tree object is being returned.
|
|
142
|
+
*/
|
|
143
|
+
clone(): AVLTree<K, V, R, MK, MV, MR>;
|
|
99
144
|
/**
|
|
100
145
|
* Time Complexity: O(1)
|
|
101
146
|
* Space Complexity: O(1)
|
|
102
147
|
*
|
|
103
148
|
* The `_swapProperties` function swaps the key, value, and height properties between two nodes in a
|
|
104
149
|
* binary search tree.
|
|
105
|
-
* @param {
|
|
106
|
-
* object (`
|
|
107
|
-
* @param {
|
|
108
|
-
* `R` or an instance of `BSTNOptKeyOrNode<K,
|
|
150
|
+
* @param {BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>} srcNode - The `srcNode` parameter represents either a node
|
|
151
|
+
* object (`AVLTreeNode<K, V>`) or a key-value pair (`R`) that is being swapped with another node.
|
|
152
|
+
* @param {BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>} destNode - The `destNode` parameter is either an instance of
|
|
153
|
+
* `R` or an instance of `BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>`.
|
|
109
154
|
* @returns The method is returning the `destNodeEnsured` object if both `srcNodeEnsured` and
|
|
110
155
|
* `destNodeEnsured` are truthy. Otherwise, it returns `undefined`.
|
|
111
156
|
*/
|
|
112
|
-
protected _swapProperties(srcNode:
|
|
157
|
+
protected _swapProperties(srcNode: BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>, destNode: BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>): AVLTreeNode<K, V> | undefined;
|
|
113
158
|
/**
|
|
114
159
|
* Time Complexity: O(1)
|
|
115
160
|
* Space Complexity: O(1)
|
|
116
161
|
*
|
|
117
162
|
* The function calculates the balance factor of a node in a binary tree.
|
|
118
|
-
* @param {
|
|
163
|
+
* @param {AVLTreeNode<K, V>} node - The parameter "node" is of type "AVLTreeNode<K, V>", which likely represents a node in a
|
|
119
164
|
* binary tree data structure.
|
|
120
165
|
* @returns the balance factor of a given node. The balance factor is calculated by subtracting the
|
|
121
166
|
* height of the left subtree from the height of the right subtree.
|
|
122
167
|
*/
|
|
123
|
-
protected _balanceFactor(node:
|
|
168
|
+
protected _balanceFactor(node: AVLTreeNode<K, V>): number;
|
|
124
169
|
/**
|
|
125
170
|
* Time Complexity: O(1)
|
|
126
171
|
* Space Complexity: O(1)
|
|
127
172
|
*
|
|
128
173
|
* The function updates the height of a node in a binary tree based on the heights of its left and
|
|
129
174
|
* right children.
|
|
130
|
-
* @param {
|
|
175
|
+
* @param {AVLTreeNode<K, V>} node - The parameter "node" represents a node in a binary tree data structure.
|
|
131
176
|
*/
|
|
132
|
-
protected _updateHeight(node:
|
|
177
|
+
protected _updateHeight(node: AVLTreeNode<K, V>): void;
|
|
133
178
|
/**
|
|
134
179
|
* Time Complexity: O(1)
|
|
135
180
|
* Space Complexity: O(1)
|
|
136
181
|
*
|
|
137
182
|
* The `_balanceLL` function performs a left-left rotation to balance a binary search tree.
|
|
138
|
-
* @param {
|
|
183
|
+
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree.
|
|
139
184
|
*/
|
|
140
|
-
protected _balanceLL(A:
|
|
185
|
+
protected _balanceLL(A: AVLTreeNode<K, V>): void;
|
|
141
186
|
/**
|
|
142
187
|
* Time Complexity: O(1)
|
|
143
188
|
* Space Complexity: O(1)
|
|
144
189
|
*
|
|
145
190
|
* The `_balanceLR` function performs a left-right rotation to balance a binary tree.
|
|
146
|
-
* @param {
|
|
191
|
+
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree.
|
|
147
192
|
*/
|
|
148
|
-
protected _balanceLR(A:
|
|
193
|
+
protected _balanceLR(A: AVLTreeNode<K, V>): void;
|
|
149
194
|
/**
|
|
150
195
|
* Time Complexity: O(1)
|
|
151
196
|
* Space Complexity: O(1)
|
|
152
197
|
*
|
|
153
198
|
* The function `_balanceRR` performs a right-right rotation to balance a binary tree.
|
|
154
|
-
* @param {
|
|
199
|
+
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree.
|
|
155
200
|
*/
|
|
156
|
-
protected _balanceRR(A:
|
|
201
|
+
protected _balanceRR(A: AVLTreeNode<K, V>): void;
|
|
157
202
|
/**
|
|
158
203
|
* Time Complexity: O(1)
|
|
159
204
|
* Space Complexity: O(1)
|
|
160
205
|
*
|
|
161
206
|
* The function `_balanceRL` performs a right-left rotation to balance a binary tree.
|
|
162
|
-
* @param {
|
|
207
|
+
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree.
|
|
163
208
|
*/
|
|
164
|
-
protected _balanceRL(A:
|
|
209
|
+
protected _balanceRL(A: AVLTreeNode<K, V>): void;
|
|
165
210
|
/**
|
|
166
211
|
* Time Complexity: O(log n)
|
|
167
212
|
* Space Complexity: O(1)
|
|
168
213
|
*
|
|
169
214
|
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
170
215
|
* to restore balance in an AVL tree after inserting a node.
|
|
171
|
-
* @param {BTNRep<K, V,
|
|
172
|
-
* `BTNRep<K, V,
|
|
216
|
+
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} node - The `node` parameter can be of type `R` or
|
|
217
|
+
* `BTNRep<K, V, AVLTreeNode<K, V>>`.
|
|
173
218
|
*/
|
|
174
|
-
protected _balancePath(node: BTNRep<K, V,
|
|
219
|
+
protected _balancePath(node: BTNRep<K, V, AVLTreeNode<K, V>>): void;
|
|
175
220
|
/**
|
|
176
221
|
* Time Complexity: O(1)
|
|
177
222
|
* Space Complexity: O(1)
|
|
178
223
|
*
|
|
179
224
|
* The function replaces an old node with a new node and sets the height of the new node to be the
|
|
180
225
|
* same as the old node.
|
|
181
|
-
* @param {
|
|
226
|
+
* @param {AVLTreeNode<K, V>} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
|
|
182
227
|
* the data structure.
|
|
183
|
-
* @param {
|
|
228
|
+
* @param {AVLTreeNode<K, V>} newNode - The `newNode` parameter is the new node that will replace the `oldNode` in
|
|
184
229
|
* the data structure.
|
|
185
230
|
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
186
231
|
* superclass, with the `oldNode` and `newNode` as arguments.
|
|
187
232
|
*/
|
|
188
|
-
protected _replaceNode(oldNode:
|
|
233
|
+
protected _replaceNode(oldNode: AVLTreeNode<K, V>, newNode: AVLTreeNode<K, V>): AVLTreeNode<K, V>;
|
|
189
234
|
}
|
|
@@ -11,15 +11,37 @@ exports.AVLTree = exports.AVLTreeNode = void 0;
|
|
|
11
11
|
const bst_1 = require("./bst");
|
|
12
12
|
class AVLTreeNode extends bst_1.BSTNode {
|
|
13
13
|
/**
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* @param {V} [value] - The
|
|
19
|
-
*
|
|
14
|
+
* This TypeScript constructor function initializes an instance with a key and an optional value.
|
|
15
|
+
* @param {K} key - The `key` parameter is typically used to uniquely identify an object or element
|
|
16
|
+
* within a data structure. It serves as a reference or identifier for accessing or manipulating the
|
|
17
|
+
* associated value or data.
|
|
18
|
+
* @param {V} [value] - The `value` parameter in the constructor is optional, meaning it does not
|
|
19
|
+
* have to be provided when creating an instance of the class. If a value is not provided, it will
|
|
20
|
+
* default to `undefined`.
|
|
20
21
|
*/
|
|
21
22
|
constructor(key, value) {
|
|
22
23
|
super(key, value);
|
|
24
|
+
this.parent = undefined;
|
|
25
|
+
this._left = undefined;
|
|
26
|
+
this._right = undefined;
|
|
27
|
+
}
|
|
28
|
+
get left() {
|
|
29
|
+
return this._left;
|
|
30
|
+
}
|
|
31
|
+
set left(v) {
|
|
32
|
+
if (v) {
|
|
33
|
+
v.parent = this;
|
|
34
|
+
}
|
|
35
|
+
this._left = v;
|
|
36
|
+
}
|
|
37
|
+
get right() {
|
|
38
|
+
return this._right;
|
|
39
|
+
}
|
|
40
|
+
set right(v) {
|
|
41
|
+
if (v) {
|
|
42
|
+
v.parent = this;
|
|
43
|
+
}
|
|
44
|
+
this._right = v;
|
|
23
45
|
}
|
|
24
46
|
}
|
|
25
47
|
exports.AVLTreeNode = AVLTreeNode;
|
|
@@ -34,15 +56,15 @@ exports.AVLTreeNode = AVLTreeNode;
|
|
|
34
56
|
*/
|
|
35
57
|
class AVLTree extends bst_1.BST {
|
|
36
58
|
/**
|
|
37
|
-
* This
|
|
38
|
-
*
|
|
39
|
-
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
|
|
40
|
-
* iterable
|
|
41
|
-
*
|
|
42
|
-
* @param [options] - The `options` parameter
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
59
|
+
* This TypeScript constructor initializes an AVLTree with keys, nodes, entries, or raw data provided
|
|
60
|
+
* in an iterable format.
|
|
61
|
+
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
|
|
62
|
+
* iterable that can contain either `BTNRep<K, V, AVLTreeNode<K, V>>` objects or `R` objects. It is
|
|
63
|
+
* used to initialize the AVLTree with key-value pairs or raw data entries. If provided
|
|
64
|
+
* @param [options] - The `options` parameter in the constructor is of type `AVLTreeOptions<K, V,
|
|
65
|
+
* R>`. It is an optional parameter that allows you to specify additional options for configuring the
|
|
66
|
+
* AVL tree. These options could include things like custom comparators, initial capacity, or any
|
|
67
|
+
* other configuration settings specific
|
|
46
68
|
*/
|
|
47
69
|
constructor(keysNodesEntriesOrRaws = [], options) {
|
|
48
70
|
super([], options);
|
|
@@ -50,18 +72,24 @@ class AVLTree extends bst_1.BST {
|
|
|
50
72
|
super.addMany(keysNodesEntriesOrRaws);
|
|
51
73
|
}
|
|
52
74
|
/**
|
|
75
|
+
* Time Complexity: O(1)
|
|
76
|
+
* Space Complexity: O(1)
|
|
77
|
+
*
|
|
53
78
|
* The function creates a new AVL tree node with the given key and value.
|
|
54
79
|
* @param {K} key - The key parameter is of type K, which represents the key of the node being
|
|
55
80
|
* created.
|
|
56
81
|
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
|
|
57
82
|
* value associated with the key in the node being created.
|
|
58
83
|
* @returns The method is returning a new instance of the AVLTreeNode class, casted as the generic
|
|
59
|
-
* type
|
|
84
|
+
* type AVLTreeNode<K, V>.
|
|
60
85
|
*/
|
|
61
86
|
createNode(key, value) {
|
|
62
87
|
return new AVLTreeNode(key, this._isMapMode ? undefined : value);
|
|
63
88
|
}
|
|
64
89
|
/**
|
|
90
|
+
* Time Complexity: O(1)
|
|
91
|
+
* Space Complexity: O(1)
|
|
92
|
+
*
|
|
65
93
|
* The function creates a new AVL tree with the specified options and returns it.
|
|
66
94
|
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
67
95
|
* passed to the `createTree` function. It is used to customize the behavior of the AVL tree that is
|
|
@@ -72,51 +100,53 @@ class AVLTree extends bst_1.BST {
|
|
|
72
100
|
return new AVLTree([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options));
|
|
73
101
|
}
|
|
74
102
|
/**
|
|
103
|
+
* Time Complexity: O(1)
|
|
104
|
+
* Space Complexity: O(1)
|
|
105
|
+
*
|
|
75
106
|
* The function checks if the input is an instance of AVLTreeNode.
|
|
76
|
-
* @param {BTNRep<K, V,
|
|
77
|
-
* `
|
|
78
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
107
|
+
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The parameter
|
|
108
|
+
* `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, AVLTreeNode<K, V>>`.
|
|
109
|
+
* @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
|
|
79
110
|
* an instance of the `AVLTreeNode` class.
|
|
80
111
|
*/
|
|
81
|
-
isNode(
|
|
82
|
-
return
|
|
112
|
+
isNode(keyNodeOrEntry) {
|
|
113
|
+
return keyNodeOrEntry instanceof AVLTreeNode;
|
|
83
114
|
}
|
|
84
115
|
/**
|
|
85
116
|
* Time Complexity: O(log n)
|
|
86
|
-
* Space Complexity: O(
|
|
117
|
+
* Space Complexity: O(log n)
|
|
87
118
|
*
|
|
88
119
|
* The function overrides the add method of a class and inserts a key-value pair into a data
|
|
89
120
|
* structure, then balances the path.
|
|
90
|
-
* @param {BTNRep<K, V,
|
|
91
|
-
* `
|
|
92
|
-
* `RawElement`.
|
|
121
|
+
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The parameter
|
|
122
|
+
* `keyNodeOrEntry` can accept values of type `R`, `BTNRep<K, V, AVLTreeNode<K, V>>`
|
|
93
123
|
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
94
124
|
* the key or node being added to the data structure.
|
|
95
125
|
* @returns The method is returning a boolean value.
|
|
96
126
|
*/
|
|
97
|
-
add(
|
|
98
|
-
if (
|
|
127
|
+
add(keyNodeOrEntry, value) {
|
|
128
|
+
if (keyNodeOrEntry === null)
|
|
99
129
|
return false;
|
|
100
|
-
const inserted = super.add(
|
|
130
|
+
const inserted = super.add(keyNodeOrEntry, value);
|
|
101
131
|
if (inserted)
|
|
102
|
-
this._balancePath(
|
|
132
|
+
this._balancePath(keyNodeOrEntry);
|
|
103
133
|
return inserted;
|
|
104
134
|
}
|
|
105
135
|
/**
|
|
106
136
|
* Time Complexity: O(log n)
|
|
107
|
-
* Space Complexity: O(
|
|
137
|
+
* Space Complexity: O(log n)
|
|
108
138
|
*
|
|
109
139
|
* The function overrides the delete method in a TypeScript class, performs deletion, and then
|
|
110
140
|
* balances the tree if necessary.
|
|
111
|
-
* @param {BTNRep<K, V,
|
|
141
|
+
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry`
|
|
112
142
|
* parameter in the `override delete` method can be one of the following types:
|
|
113
143
|
* @returns The `delete` method is being overridden in this code snippet. It first calls the `delete`
|
|
114
144
|
* method from the superclass (presumably a parent class) with the provided `predicate`, which could
|
|
115
145
|
* be a key, node, entry, or a custom predicate. The result of this deletion operation is stored in
|
|
116
146
|
* `deletedResults`, which is an array of `BinaryTreeDeleteResult` objects.
|
|
117
147
|
*/
|
|
118
|
-
delete(
|
|
119
|
-
const deletedResults = super.delete(
|
|
148
|
+
delete(keyNodeOrEntry) {
|
|
149
|
+
const deletedResults = super.delete(keyNodeOrEntry);
|
|
120
150
|
for (const { needBalanced } of deletedResults) {
|
|
121
151
|
if (needBalanced) {
|
|
122
152
|
this._balancePath(needBalanced);
|
|
@@ -124,6 +154,26 @@ class AVLTree extends bst_1.BST {
|
|
|
124
154
|
}
|
|
125
155
|
return deletedResults;
|
|
126
156
|
}
|
|
157
|
+
/**
|
|
158
|
+
* Time Complexity: O(n)
|
|
159
|
+
* Space Complexity: O(n)
|
|
160
|
+
*
|
|
161
|
+
* The `map` function in TypeScript overrides the default map behavior of an AVLTree data structure
|
|
162
|
+
* by applying a callback function to each entry and creating a new AVLTree with the results.
|
|
163
|
+
* @param callback - A function that will be called for each entry in the AVLTree. It takes four
|
|
164
|
+
* arguments: the key, the value (which can be undefined), the index of the entry, and a reference to
|
|
165
|
+
* the AVLTree itself.
|
|
166
|
+
* @param [options] - The `options` parameter in the `override map` function is of type
|
|
167
|
+
* `AVLTreeOptions<MK, MV, MR>`. It is an optional parameter that allows you to specify additional
|
|
168
|
+
* options for the AVL tree being created during the mapping process. These options could include
|
|
169
|
+
* custom comparators, initial
|
|
170
|
+
* @param {any} [thisArg] - The `thisArg` parameter in the `override map` function is used to specify
|
|
171
|
+
* the value of `this` when executing the `callback` function. It allows you to set the context
|
|
172
|
+
* (value of `this`) within the callback function. This can be useful when you want to access
|
|
173
|
+
* properties or
|
|
174
|
+
* @returns The `map` method is returning a new AVLTree instance (`newTree`) with the entries
|
|
175
|
+
* modified by the provided callback function.
|
|
176
|
+
*/
|
|
127
177
|
map(callback, options, thisArg) {
|
|
128
178
|
const newTree = new AVLTree([], options);
|
|
129
179
|
let index = 0;
|
|
@@ -132,16 +182,29 @@ class AVLTree extends bst_1.BST {
|
|
|
132
182
|
}
|
|
133
183
|
return newTree;
|
|
134
184
|
}
|
|
185
|
+
/**
|
|
186
|
+
* Time Complexity: O(n)
|
|
187
|
+
* Space Complexity: O(n)
|
|
188
|
+
*
|
|
189
|
+
* The function `clone` overrides the default cloning behavior to create a deep copy of a tree
|
|
190
|
+
* structure.
|
|
191
|
+
* @returns A cloned tree object is being returned.
|
|
192
|
+
*/
|
|
193
|
+
clone() {
|
|
194
|
+
const cloned = this.createTree();
|
|
195
|
+
this._clone(cloned);
|
|
196
|
+
return cloned;
|
|
197
|
+
}
|
|
135
198
|
/**
|
|
136
199
|
* Time Complexity: O(1)
|
|
137
200
|
* Space Complexity: O(1)
|
|
138
201
|
*
|
|
139
202
|
* The `_swapProperties` function swaps the key, value, and height properties between two nodes in a
|
|
140
203
|
* binary search tree.
|
|
141
|
-
* @param {
|
|
142
|
-
* object (`
|
|
143
|
-
* @param {
|
|
144
|
-
* `R` or an instance of `BSTNOptKeyOrNode<K,
|
|
204
|
+
* @param {BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>} srcNode - The `srcNode` parameter represents either a node
|
|
205
|
+
* object (`AVLTreeNode<K, V>`) or a key-value pair (`R`) that is being swapped with another node.
|
|
206
|
+
* @param {BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>} destNode - The `destNode` parameter is either an instance of
|
|
207
|
+
* `R` or an instance of `BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>`.
|
|
145
208
|
* @returns The method is returning the `destNodeEnsured` object if both `srcNodeEnsured` and
|
|
146
209
|
* `destNodeEnsured` are truthy. Otherwise, it returns `undefined`.
|
|
147
210
|
*/
|
|
@@ -171,7 +234,7 @@ class AVLTree extends bst_1.BST {
|
|
|
171
234
|
* Space Complexity: O(1)
|
|
172
235
|
*
|
|
173
236
|
* The function calculates the balance factor of a node in a binary tree.
|
|
174
|
-
* @param {
|
|
237
|
+
* @param {AVLTreeNode<K, V>} node - The parameter "node" is of type "AVLTreeNode<K, V>", which likely represents a node in a
|
|
175
238
|
* binary tree data structure.
|
|
176
239
|
* @returns the balance factor of a given node. The balance factor is calculated by subtracting the
|
|
177
240
|
* height of the left subtree from the height of the right subtree.
|
|
@@ -192,7 +255,7 @@ class AVLTree extends bst_1.BST {
|
|
|
192
255
|
*
|
|
193
256
|
* The function updates the height of a node in a binary tree based on the heights of its left and
|
|
194
257
|
* right children.
|
|
195
|
-
* @param {
|
|
258
|
+
* @param {AVLTreeNode<K, V>} node - The parameter "node" represents a node in a binary tree data structure.
|
|
196
259
|
*/
|
|
197
260
|
_updateHeight(node) {
|
|
198
261
|
if (!node.left && !node.right)
|
|
@@ -211,7 +274,7 @@ class AVLTree extends bst_1.BST {
|
|
|
211
274
|
* Space Complexity: O(1)
|
|
212
275
|
*
|
|
213
276
|
* The `_balanceLL` function performs a left-left rotation to balance a binary search tree.
|
|
214
|
-
* @param {
|
|
277
|
+
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree.
|
|
215
278
|
*/
|
|
216
279
|
_balanceLL(A) {
|
|
217
280
|
const parentOfA = A.parent;
|
|
@@ -249,7 +312,7 @@ class AVLTree extends bst_1.BST {
|
|
|
249
312
|
* Space Complexity: O(1)
|
|
250
313
|
*
|
|
251
314
|
* The `_balanceLR` function performs a left-right rotation to balance a binary tree.
|
|
252
|
-
* @param {
|
|
315
|
+
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree.
|
|
253
316
|
*/
|
|
254
317
|
_balanceLR(A) {
|
|
255
318
|
const parentOfA = A.parent;
|
|
@@ -304,7 +367,7 @@ class AVLTree extends bst_1.BST {
|
|
|
304
367
|
* Space Complexity: O(1)
|
|
305
368
|
*
|
|
306
369
|
* The function `_balanceRR` performs a right-right rotation to balance a binary tree.
|
|
307
|
-
* @param {
|
|
370
|
+
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree.
|
|
308
371
|
*/
|
|
309
372
|
_balanceRR(A) {
|
|
310
373
|
const parentOfA = A.parent;
|
|
@@ -344,7 +407,7 @@ class AVLTree extends bst_1.BST {
|
|
|
344
407
|
* Space Complexity: O(1)
|
|
345
408
|
*
|
|
346
409
|
* The function `_balanceRL` performs a right-left rotation to balance a binary tree.
|
|
347
|
-
* @param {
|
|
410
|
+
* @param {AVLTreeNode<K, V>} A - A is a node in a binary tree.
|
|
348
411
|
*/
|
|
349
412
|
_balanceRL(A) {
|
|
350
413
|
const parentOfA = A.parent;
|
|
@@ -401,8 +464,8 @@ class AVLTree extends bst_1.BST {
|
|
|
401
464
|
*
|
|
402
465
|
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
403
466
|
* to restore balance in an AVL tree after inserting a node.
|
|
404
|
-
* @param {BTNRep<K, V,
|
|
405
|
-
* `BTNRep<K, V,
|
|
467
|
+
* @param {BTNRep<K, V, AVLTreeNode<K, V>>} node - The `node` parameter can be of type `R` or
|
|
468
|
+
* `BTNRep<K, V, AVLTreeNode<K, V>>`.
|
|
406
469
|
*/
|
|
407
470
|
_balancePath(node) {
|
|
408
471
|
node = this.ensureNode(node);
|
|
@@ -452,9 +515,9 @@ class AVLTree extends bst_1.BST {
|
|
|
452
515
|
*
|
|
453
516
|
* The function replaces an old node with a new node and sets the height of the new node to be the
|
|
454
517
|
* same as the old node.
|
|
455
|
-
* @param {
|
|
518
|
+
* @param {AVLTreeNode<K, V>} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
|
|
456
519
|
* the data structure.
|
|
457
|
-
* @param {
|
|
520
|
+
* @param {AVLTreeNode<K, V>} newNode - The `newNode` parameter is the new node that will replace the `oldNode` in
|
|
458
521
|
* the data structure.
|
|
459
522
|
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
460
523
|
* superclass, with the `oldNode` and `newNode` as arguments.
|
|
@@ -9,6 +9,9 @@ exports.BinaryIndexedTree = void 0;
|
|
|
9
9
|
* @license MIT License
|
|
10
10
|
*/
|
|
11
11
|
const utils_1 = require("../../utils");
|
|
12
|
+
/**
|
|
13
|
+
*
|
|
14
|
+
*/
|
|
12
15
|
class BinaryIndexedTree {
|
|
13
16
|
/**
|
|
14
17
|
* The constructor initializes the properties of an object, including default frequency, maximum
|