bst-typed 1.49.5 → 1.49.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/binary-tree/avl-tree.d.ts +53 -48
- package/dist/data-structures/binary-tree/avl-tree.js +55 -49
- package/dist/data-structures/binary-tree/binary-tree.d.ts +153 -130
- package/dist/data-structures/binary-tree/binary-tree.js +192 -149
- package/dist/data-structures/binary-tree/bst.d.ts +83 -71
- package/dist/data-structures/binary-tree/bst.js +113 -89
- package/dist/data-structures/binary-tree/rb-tree.d.ts +37 -35
- package/dist/data-structures/binary-tree/rb-tree.js +62 -59
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +46 -39
- package/dist/data-structures/binary-tree/tree-multimap.js +58 -51
- package/dist/interfaces/binary-tree.d.ts +3 -3
- package/dist/types/common.d.ts +3 -3
- package/dist/types/common.js +2 -2
- package/package.json +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +58 -53
- package/src/data-structures/binary-tree/binary-tree.ts +253 -205
- package/src/data-structures/binary-tree/bst.ts +125 -104
- package/src/data-structures/binary-tree/rb-tree.ts +66 -64
- package/src/data-structures/binary-tree/tree-multimap.ts +62 -56
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +3 -3
|
@@ -24,11 +24,11 @@ exports.TreeMultimapNode = TreeMultimapNode;
|
|
|
24
24
|
* The only distinction between a TreeMultimap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
25
25
|
*/
|
|
26
26
|
class TreeMultimap extends avl_tree_1.AVLTree {
|
|
27
|
-
constructor(
|
|
27
|
+
constructor(nodes, options) {
|
|
28
28
|
super([], options);
|
|
29
29
|
this._count = 0;
|
|
30
|
-
if (
|
|
31
|
-
this.addMany(
|
|
30
|
+
if (nodes)
|
|
31
|
+
this.addMany(nodes);
|
|
32
32
|
}
|
|
33
33
|
// TODO the _count is not accurate after nodes count modified
|
|
34
34
|
get count() {
|
|
@@ -52,26 +52,8 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
52
52
|
return new TreeMultimap([], Object.assign({ iterationType: this.iterationType, variant: this.variant }, options));
|
|
53
53
|
}
|
|
54
54
|
/**
|
|
55
|
-
* The function
|
|
56
|
-
* @param
|
|
57
|
-
* @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
|
|
58
|
-
* class.
|
|
59
|
-
*/
|
|
60
|
-
isNode(exemplar) {
|
|
61
|
-
return exemplar instanceof TreeMultimapNode;
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
65
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
66
|
-
* data type.
|
|
67
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
68
|
-
*/
|
|
69
|
-
isNotNodeInstance(potentialKey) {
|
|
70
|
-
return !(potentialKey instanceof TreeMultimapNode);
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
74
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, which means it
|
|
55
|
+
* The function `exemplarToNode` converts an keyOrNodeOrEntry object into a node object.
|
|
56
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, which means it
|
|
75
57
|
* can be one of the following:
|
|
76
58
|
* @param {V} [value] - The `value` parameter is an optional argument that represents the value
|
|
77
59
|
* associated with the node. It is of type `V`, which can be any data type. If no value is provided,
|
|
@@ -80,16 +62,16 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
80
62
|
* times the value should be added to the node. If not provided, it defaults to 1.
|
|
81
63
|
* @returns a node of type `N` or `undefined`.
|
|
82
64
|
*/
|
|
83
|
-
exemplarToNode(
|
|
65
|
+
exemplarToNode(keyOrNodeOrEntry, value, count = 1) {
|
|
84
66
|
let node;
|
|
85
|
-
if (
|
|
67
|
+
if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) {
|
|
86
68
|
return;
|
|
87
69
|
}
|
|
88
|
-
else if (this.isNode(
|
|
89
|
-
node =
|
|
70
|
+
else if (this.isNode(keyOrNodeOrEntry)) {
|
|
71
|
+
node = keyOrNodeOrEntry;
|
|
90
72
|
}
|
|
91
|
-
else if (this.isEntry(
|
|
92
|
-
const [key, value] =
|
|
73
|
+
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
74
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
93
75
|
if (key === undefined || key === null) {
|
|
94
76
|
return;
|
|
95
77
|
}
|
|
@@ -97,8 +79,8 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
97
79
|
node = this.createNode(key, value, count);
|
|
98
80
|
}
|
|
99
81
|
}
|
|
100
|
-
else if (this.isNotNodeInstance(
|
|
101
|
-
node = this.createNode(
|
|
82
|
+
else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
|
|
83
|
+
node = this.createNode(keyOrNodeOrEntry, value, count);
|
|
102
84
|
}
|
|
103
85
|
else {
|
|
104
86
|
return;
|
|
@@ -106,12 +88,31 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
106
88
|
return node;
|
|
107
89
|
}
|
|
108
90
|
/**
|
|
109
|
-
*
|
|
110
|
-
*
|
|
91
|
+
* The function checks if an keyOrNodeOrEntry is an instance of the TreeMultimapNode class.
|
|
92
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
93
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the TreeMultimapNode
|
|
94
|
+
* class.
|
|
111
95
|
*/
|
|
96
|
+
isNode(keyOrNodeOrEntry) {
|
|
97
|
+
return keyOrNodeOrEntry instanceof TreeMultimapNode;
|
|
98
|
+
}
|
|
112
99
|
/**
|
|
113
|
-
*
|
|
114
|
-
*
|
|
100
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
101
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
102
|
+
* data type.
|
|
103
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
104
|
+
*/
|
|
105
|
+
isNotNodeInstance(potentialKey) {
|
|
106
|
+
return !(potentialKey instanceof TreeMultimapNode);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Time Complexity: O(log n)
|
|
110
|
+
* Space Complexity: O(1)
|
|
111
|
+
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
|
|
112
|
+
*/
|
|
113
|
+
/**
|
|
114
|
+
* Time Complexity: O(log n)
|
|
115
|
+
* Space Complexity: O(1)
|
|
115
116
|
*
|
|
116
117
|
* The function overrides the add method of a binary tree node and adds a new node to the tree.
|
|
117
118
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
|
|
@@ -128,21 +129,22 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
128
129
|
add(keyOrNodeOrEntry, value, count = 1) {
|
|
129
130
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value, count);
|
|
130
131
|
if (newNode === undefined)
|
|
131
|
-
return;
|
|
132
|
+
return false;
|
|
132
133
|
const orgNodeCount = (newNode === null || newNode === void 0 ? void 0 : newNode.count) || 0;
|
|
133
134
|
const inserted = super.add(newNode);
|
|
134
135
|
if (inserted) {
|
|
135
136
|
this._count += orgNodeCount;
|
|
136
137
|
}
|
|
137
|
-
return
|
|
138
|
+
return true;
|
|
138
139
|
}
|
|
139
140
|
/**
|
|
140
|
-
* Time Complexity: O(k log n)
|
|
141
|
-
* Space Complexity: O(1)
|
|
141
|
+
* Time Complexity: O(k log n)
|
|
142
|
+
* Space Complexity: O(1)
|
|
143
|
+
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
|
|
142
144
|
*/
|
|
143
145
|
/**
|
|
144
|
-
* Time Complexity: O(k log n)
|
|
145
|
-
* Space Complexity: O(1)
|
|
146
|
+
* Time Complexity: O(k log n)
|
|
147
|
+
* Space Complexity: O(1)
|
|
146
148
|
*
|
|
147
149
|
* The function overrides the addMany method to add multiple keys, nodes, or entries to a data
|
|
148
150
|
* structure.
|
|
@@ -154,12 +156,13 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
154
156
|
return super.addMany(keysOrNodesOrEntries);
|
|
155
157
|
}
|
|
156
158
|
/**
|
|
157
|
-
* Time Complexity: O(
|
|
158
|
-
* Space Complexity: O(
|
|
159
|
+
* Time Complexity: O(n log n)
|
|
160
|
+
* Space Complexity: O(n)
|
|
161
|
+
* logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node. linear space, as it creates an array to store the sorted nodes.
|
|
159
162
|
*/
|
|
160
163
|
/**
|
|
161
|
-
* Time Complexity: O(n log n)
|
|
162
|
-
* Space Complexity: O(n)
|
|
164
|
+
* Time Complexity: O(n log n)
|
|
165
|
+
* Space Complexity: O(n)
|
|
163
166
|
*
|
|
164
167
|
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
|
|
165
168
|
* tree using either a recursive or iterative approach.
|
|
@@ -205,12 +208,13 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
205
208
|
}
|
|
206
209
|
}
|
|
207
210
|
/**
|
|
208
|
-
* Time Complexity: O(k log n)
|
|
209
|
-
* Space Complexity: O(1)
|
|
211
|
+
* Time Complexity: O(k log n)
|
|
212
|
+
* Space Complexity: O(1)
|
|
213
|
+
* logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each. constant space, as it doesn't use additional data structures that scale with input size.
|
|
210
214
|
*/
|
|
211
215
|
/**
|
|
212
|
-
* Time Complexity: O(log n)
|
|
213
|
-
* Space Complexity: O(1)
|
|
216
|
+
* Time Complexity: O(k log n)
|
|
217
|
+
* Space Complexity: O(1)
|
|
214
218
|
*
|
|
215
219
|
* The `delete` function in TypeScript is used to remove a node from a binary tree, taking into
|
|
216
220
|
* account the count of the node and balancing the tree if necessary.
|
|
@@ -286,10 +290,13 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
286
290
|
return deletedResult;
|
|
287
291
|
}
|
|
288
292
|
/**
|
|
289
|
-
* Time Complexity: O(
|
|
290
|
-
* Space Complexity: O(
|
|
293
|
+
* Time Complexity: O(1)
|
|
294
|
+
* Space Complexity: O(1)
|
|
291
295
|
*/
|
|
292
296
|
/**
|
|
297
|
+
* Time Complexity: O(1)
|
|
298
|
+
* Space Complexity: O(1)
|
|
299
|
+
*
|
|
293
300
|
* The clear() function clears the contents of a data structure and sets the count to zero.
|
|
294
301
|
*/
|
|
295
302
|
clear() {
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { BinaryTree, BinaryTreeNode } from '../data-structures';
|
|
2
|
-
import { BinaryTreeDeleteResult, BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BTNCallback,
|
|
2
|
+
import { BinaryTreeDeleteResult, BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BTNCallback, KeyOrNodeOrEntry } from '../types';
|
|
3
3
|
export interface IBinaryTree<K = number, V = any, N extends BinaryTreeNode<K, V, N> = BinaryTreeNodeNested<K, V>, TREE extends BinaryTree<K, V, N, TREE> = BinaryTreeNested<K, V, N>> {
|
|
4
4
|
createNode(key: K, value?: N['value']): N;
|
|
5
5
|
createTree(options?: Partial<BinaryTreeOptions<K>>): TREE;
|
|
6
|
-
add(keyOrNodeOrEntry:
|
|
7
|
-
addMany(nodes: Iterable<
|
|
6
|
+
add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V, count?: number): boolean;
|
|
7
|
+
addMany(nodes: Iterable<KeyOrNodeOrEntry<K, V, N>>, values?: Iterable<V | undefined>): boolean[];
|
|
8
8
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeleteResult<N>[];
|
|
9
9
|
}
|
package/dist/types/common.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export declare enum BSTVariant {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
STANDARD = "STANDARD",
|
|
3
|
+
INVERSE = "INVERSE"
|
|
4
4
|
}
|
|
5
5
|
export declare enum CP {
|
|
6
6
|
lt = "lt",
|
|
@@ -44,7 +44,7 @@ export type BinaryTreePrintOptions = {
|
|
|
44
44
|
};
|
|
45
45
|
export type BTNEntry<K, V> = [K | null | undefined, V | undefined];
|
|
46
46
|
export type BTNKeyOrNode<K, N> = K | null | undefined | N;
|
|
47
|
-
export type
|
|
47
|
+
export type KeyOrNodeOrEntry<K, V, N> = BTNEntry<K, V> | BTNKeyOrNode<K, N>;
|
|
48
48
|
export type BTNodePureKeyOrNode<K, N> = K | N;
|
|
49
49
|
export type BTNodePureExemplar<K, V, N> = [K, V | undefined] | BTNodePureKeyOrNode<K, N>;
|
|
50
50
|
export type BSTNKeyOrNode<K, N> = K | undefined | N;
|
package/dist/types/common.js
CHANGED
|
@@ -3,8 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.FamilyPosition = exports.IterationType = exports.CP = exports.BSTVariant = void 0;
|
|
4
4
|
var BSTVariant;
|
|
5
5
|
(function (BSTVariant) {
|
|
6
|
-
BSTVariant["
|
|
7
|
-
BSTVariant["
|
|
6
|
+
BSTVariant["STANDARD"] = "STANDARD";
|
|
7
|
+
BSTVariant["INVERSE"] = "INVERSE";
|
|
8
8
|
})(BSTVariant = exports.BSTVariant || (exports.BSTVariant = {}));
|
|
9
9
|
var CP;
|
|
10
10
|
(function (CP) {
|
package/package.json
CHANGED
|
@@ -13,8 +13,7 @@ import type {
|
|
|
13
13
|
BinaryTreeDeleteResult,
|
|
14
14
|
BSTNKeyOrNode,
|
|
15
15
|
BTNCallback,
|
|
16
|
-
|
|
17
|
-
BTNKeyOrNode
|
|
16
|
+
KeyOrNodeOrEntry
|
|
18
17
|
} from '../../types';
|
|
19
18
|
import { IBinaryTree } from '../../interfaces';
|
|
20
19
|
|
|
@@ -49,17 +48,17 @@ export class AVLTree<
|
|
|
49
48
|
extends BST<K, V, N, TREE>
|
|
50
49
|
implements IBinaryTree<K, V, N, TREE> {
|
|
51
50
|
/**
|
|
52
|
-
* The constructor function initializes an AVLTree object with optional
|
|
53
|
-
* @param [
|
|
54
|
-
* objects. It represents a collection of
|
|
51
|
+
* The constructor function initializes an AVLTree object with optional nodes and options.
|
|
52
|
+
* @param [nodes] - The `nodes` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>`
|
|
53
|
+
* objects. It represents a collection of nodes that will be added to the AVL tree during
|
|
55
54
|
* initialization.
|
|
56
55
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
57
56
|
* behavior of the AVL tree. It is of type `Partial<AVLTreeOptions>`, which means that you can
|
|
58
57
|
* provide only a subset of the properties defined in the `AVLTreeOptions` interface.
|
|
59
58
|
*/
|
|
60
|
-
constructor(
|
|
59
|
+
constructor(nodes?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: Partial<AVLTreeOptions<K>>) {
|
|
61
60
|
super([], options);
|
|
62
|
-
if (
|
|
61
|
+
if (nodes) super.addMany(nodes);
|
|
63
62
|
}
|
|
64
63
|
|
|
65
64
|
/**
|
|
@@ -91,12 +90,12 @@ export class AVLTree<
|
|
|
91
90
|
}
|
|
92
91
|
|
|
93
92
|
/**
|
|
94
|
-
* The function checks if an
|
|
95
|
-
* @param
|
|
96
|
-
* @returns a boolean value indicating whether the
|
|
93
|
+
* The function checks if an keyOrNodeOrEntry is an instance of AVLTreeNode.
|
|
94
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
95
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeNode class.
|
|
97
96
|
*/
|
|
98
|
-
override isNode(
|
|
99
|
-
return
|
|
97
|
+
override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N {
|
|
98
|
+
return keyOrNodeOrEntry instanceof AVLTreeNode;
|
|
100
99
|
}
|
|
101
100
|
|
|
102
101
|
/**
|
|
@@ -105,18 +104,19 @@ export class AVLTree<
|
|
|
105
104
|
* data type.
|
|
106
105
|
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
107
106
|
*/
|
|
108
|
-
override isNotNodeInstance(potentialKey:
|
|
107
|
+
override isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K {
|
|
109
108
|
return !(potentialKey instanceof AVLTreeNode);
|
|
110
109
|
}
|
|
111
110
|
|
|
112
111
|
/**
|
|
113
|
-
* Time Complexity: O(log n)
|
|
114
|
-
* Space Complexity: O(1)
|
|
112
|
+
* Time Complexity: O(log n)
|
|
113
|
+
* Space Complexity: O(1)
|
|
114
|
+
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
|
|
115
115
|
*/
|
|
116
116
|
|
|
117
117
|
/**
|
|
118
|
-
* Time Complexity: O(log n)
|
|
119
|
-
* Space Complexity: O(1)
|
|
118
|
+
* Time Complexity: O(log n)
|
|
119
|
+
* Space Complexity: O(1)
|
|
120
120
|
*
|
|
121
121
|
* The function overrides the add method of a binary tree node and balances the tree after inserting
|
|
122
122
|
* a new node.
|
|
@@ -126,21 +126,21 @@ export class AVLTree<
|
|
|
126
126
|
* being added to the binary tree.
|
|
127
127
|
* @returns The method is returning either the inserted node or undefined.
|
|
128
128
|
*/
|
|
129
|
-
override add(keyOrNodeOrEntry:
|
|
130
|
-
if (keyOrNodeOrEntry === null) return
|
|
129
|
+
override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): boolean {
|
|
130
|
+
if (keyOrNodeOrEntry === null) return false;
|
|
131
131
|
const inserted = super.add(keyOrNodeOrEntry, value);
|
|
132
|
-
if (inserted) this._balancePath(
|
|
132
|
+
if (inserted) this._balancePath(keyOrNodeOrEntry);
|
|
133
133
|
return inserted;
|
|
134
134
|
}
|
|
135
135
|
|
|
136
136
|
/**
|
|
137
|
-
* Time Complexity: O(log n)
|
|
138
|
-
* Space Complexity: O(1)
|
|
137
|
+
* Time Complexity: O(log n)
|
|
138
|
+
* Space Complexity: O(1)
|
|
139
139
|
*/
|
|
140
140
|
|
|
141
141
|
/**
|
|
142
|
-
* Time Complexity: O(log n)
|
|
143
|
-
* Space Complexity: O(1)
|
|
142
|
+
* Time Complexity: O(log n)
|
|
143
|
+
* Space Complexity: O(1)
|
|
144
144
|
*
|
|
145
145
|
* The function overrides the delete method of a binary tree, performs the deletion, and then
|
|
146
146
|
* balances the tree if necessary.
|
|
@@ -203,13 +203,14 @@ export class AVLTree<
|
|
|
203
203
|
}
|
|
204
204
|
|
|
205
205
|
/**
|
|
206
|
-
* Time Complexity: O(1)
|
|
207
|
-
* Space Complexity: O(1)
|
|
206
|
+
* Time Complexity: O(1)
|
|
207
|
+
* Space Complexity: O(1)
|
|
208
|
+
* constant time, as it performs a fixed number of operations. constant space, as it only uses a constant amount of memory.
|
|
208
209
|
*/
|
|
209
210
|
|
|
210
211
|
/**
|
|
211
|
-
* Time Complexity: O(1)
|
|
212
|
-
* Space Complexity: O(1)
|
|
212
|
+
* Time Complexity: O(1)
|
|
213
|
+
* Space Complexity: O(1)
|
|
213
214
|
*
|
|
214
215
|
* The function calculates the balance factor of a node in a binary tree.
|
|
215
216
|
* @param {N} node - The parameter "node" represents a node in a binary tree data structure.
|
|
@@ -227,13 +228,14 @@ export class AVLTree<
|
|
|
227
228
|
}
|
|
228
229
|
|
|
229
230
|
/**
|
|
230
|
-
* Time Complexity: O(1)
|
|
231
|
-
* Space Complexity: O(1)
|
|
231
|
+
* Time Complexity: O(1)
|
|
232
|
+
* Space Complexity: O(1)
|
|
233
|
+
* constant time, as it performs a fixed number of operations. constant space, as it only uses a constant amount of memory.
|
|
232
234
|
*/
|
|
233
235
|
|
|
234
236
|
/**
|
|
235
|
-
* Time Complexity: O(1)
|
|
236
|
-
* Space Complexity: O(1)
|
|
237
|
+
* Time Complexity: O(1)
|
|
238
|
+
* Space Complexity: O(1)
|
|
237
239
|
*
|
|
238
240
|
* The function updates the height of a node in a binary tree based on the heights of its left and
|
|
239
241
|
* right children.
|
|
@@ -249,20 +251,22 @@ export class AVLTree<
|
|
|
249
251
|
}
|
|
250
252
|
|
|
251
253
|
/**
|
|
252
|
-
* Time Complexity: O(log n)
|
|
253
|
-
* Space Complexity: O(1)
|
|
254
|
+
* Time Complexity: O(log n)
|
|
255
|
+
* Space Complexity: O(1)
|
|
256
|
+
* logarithmic time, where "n" is the number of nodes in the tree. The method traverses the path from the inserted node to the root. constant space, as it doesn't use additional data structures that scale with input size.
|
|
254
257
|
*/
|
|
255
258
|
|
|
256
259
|
/**
|
|
257
|
-
* Time Complexity: O(log n)
|
|
258
|
-
* Space Complexity: O(1)
|
|
260
|
+
* Time Complexity: O(log n)
|
|
261
|
+
* Space Complexity: O(1)
|
|
259
262
|
*
|
|
260
263
|
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
261
264
|
* to restore balance in an AVL tree after inserting a node.
|
|
262
265
|
* @param {N} node - The `node` parameter in the `_balancePath` function represents the node in the
|
|
263
266
|
* AVL tree that needs to be balanced.
|
|
264
267
|
*/
|
|
265
|
-
protected _balancePath(node: N): void {
|
|
268
|
+
protected _balancePath(node: KeyOrNodeOrEntry<K, V, N>): void {
|
|
269
|
+
node = this.ensureNode(node);
|
|
266
270
|
const path = this.getPathToRoot(node, false); // first O(log n) + O(log n)
|
|
267
271
|
for (let i = 0; i < path.length; i++) {
|
|
268
272
|
// second O(log n)
|
|
@@ -302,13 +306,14 @@ export class AVLTree<
|
|
|
302
306
|
}
|
|
303
307
|
|
|
304
308
|
/**
|
|
305
|
-
* Time Complexity: O(1)
|
|
306
|
-
* Space Complexity: O(1)
|
|
309
|
+
* Time Complexity: O(1)
|
|
310
|
+
* Space Complexity: O(1)
|
|
311
|
+
* constant time, as these methods perform a fixed number of operations. constant space, as they only use a constant amount of memory.
|
|
307
312
|
*/
|
|
308
313
|
|
|
309
314
|
/**
|
|
310
|
-
* Time Complexity: O(1)
|
|
311
|
-
* Space Complexity: O(1)
|
|
315
|
+
* Time Complexity: O(1)
|
|
316
|
+
* Space Complexity: O(1)
|
|
312
317
|
*
|
|
313
318
|
* The function `_balanceLL` performs a left-left rotation to balance a binary tree.
|
|
314
319
|
* @param {N} A - A is a node in a binary tree.
|
|
@@ -340,13 +345,13 @@ export class AVLTree<
|
|
|
340
345
|
}
|
|
341
346
|
|
|
342
347
|
/**
|
|
343
|
-
* Time Complexity: O(1)
|
|
344
|
-
* Space Complexity: O(1)
|
|
348
|
+
* Time Complexity: O(1)
|
|
349
|
+
* Space Complexity: O(1)
|
|
345
350
|
*/
|
|
346
351
|
|
|
347
352
|
/**
|
|
348
|
-
* Time Complexity: O(1)
|
|
349
|
-
* Space Complexity: O(1)
|
|
353
|
+
* Time Complexity: O(1)
|
|
354
|
+
* Space Complexity: O(1)
|
|
350
355
|
*
|
|
351
356
|
* The `_balanceLR` function performs a left-right rotation to balance a binary tree.
|
|
352
357
|
* @param {N} A - A is a node in a binary tree.
|
|
@@ -396,13 +401,13 @@ export class AVLTree<
|
|
|
396
401
|
}
|
|
397
402
|
|
|
398
403
|
/**
|
|
399
|
-
* Time Complexity: O(1)
|
|
400
|
-
* Space Complexity: O(1)
|
|
404
|
+
* Time Complexity: O(1)
|
|
405
|
+
* Space Complexity: O(1)
|
|
401
406
|
*/
|
|
402
407
|
|
|
403
408
|
/**
|
|
404
|
-
* Time Complexity: O(1)
|
|
405
|
-
* Space Complexity: O(1)
|
|
409
|
+
* Time Complexity: O(1)
|
|
410
|
+
* Space Complexity: O(1)
|
|
406
411
|
*
|
|
407
412
|
* The function `_balanceRR` performs a right-right rotation to balance a binary tree.
|
|
408
413
|
* @param {N} A - A is a node in a binary tree.
|
|
@@ -439,13 +444,13 @@ export class AVLTree<
|
|
|
439
444
|
}
|
|
440
445
|
|
|
441
446
|
/**
|
|
442
|
-
* Time Complexity: O(1)
|
|
443
|
-
* Space Complexity: O(1)
|
|
447
|
+
* Time Complexity: O(1)
|
|
448
|
+
* Space Complexity: O(1)
|
|
444
449
|
*/
|
|
445
450
|
|
|
446
451
|
/**
|
|
447
|
-
* Time Complexity: O(1)
|
|
448
|
-
* Space Complexity: O(1)
|
|
452
|
+
* Time Complexity: O(1)
|
|
453
|
+
* Space Complexity: O(1)
|
|
449
454
|
*
|
|
450
455
|
* The function `_balanceRL` performs a right-left rotation to balance a binary tree.
|
|
451
456
|
* @param {N} A - A is a node in a binary tree.
|