bst-typed 1.51.8 → 1.51.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +104 -66
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +119 -87
- package/dist/data-structures/binary-tree/avl-tree.d.ts +80 -60
- package/dist/data-structures/binary-tree/avl-tree.js +78 -59
- package/dist/data-structures/binary-tree/binary-tree.d.ts +316 -224
- package/dist/data-structures/binary-tree/binary-tree.js +471 -361
- package/dist/data-structures/binary-tree/bst.d.ts +198 -200
- package/dist/data-structures/binary-tree/bst.js +215 -249
- package/dist/data-structures/binary-tree/rb-tree.d.ts +71 -72
- package/dist/data-structures/binary-tree/rb-tree.js +107 -98
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +90 -73
- package/dist/data-structures/binary-tree/tree-multi-map.js +105 -93
- package/dist/data-structures/graph/abstract-graph.d.ts +10 -15
- package/dist/data-structures/graph/abstract-graph.js +10 -15
- package/dist/data-structures/hash/hash-map.d.ts +31 -38
- package/dist/data-structures/hash/hash-map.js +40 -55
- package/dist/data-structures/queue/deque.d.ts +2 -3
- package/dist/data-structures/queue/deque.js +2 -3
- package/dist/data-structures/trie/trie.d.ts +1 -1
- package/dist/data-structures/trie/trie.js +1 -1
- package/dist/interfaces/binary-tree.d.ts +6 -6
- package/dist/types/common.d.ts +1 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +5 -4
- package/dist/types/data-structures/binary-tree/bst.d.ts +4 -4
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -3
- package/dist/utils/utils.js +3 -5
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +142 -92
- package/src/data-structures/binary-tree/avl-tree.ts +94 -66
- package/src/data-structures/binary-tree/binary-tree.ts +530 -398
- package/src/data-structures/binary-tree/bst.ts +251 -270
- package/src/data-structures/binary-tree/rb-tree.ts +121 -100
- package/src/data-structures/binary-tree/tree-multi-map.ts +125 -99
- package/src/data-structures/graph/abstract-graph.ts +10 -10
- package/src/data-structures/hash/hash-map.ts +42 -49
- package/src/data-structures/queue/deque.ts +2 -2
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/data-structures/trie/trie.ts +2 -2
- package/src/interfaces/binary-tree.ts +8 -7
- package/src/types/common.ts +1 -2
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +2 -2
- package/src/types/data-structures/binary-tree/avl-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/binary-tree.ts +5 -4
- package/src/types/data-structures/binary-tree/bst.ts +4 -4
- package/src/types/data-structures/binary-tree/rb-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -3
- package/src/utils/utils.ts +3 -3
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { BST, BSTNode } from './bst';
|
|
9
9
|
import type { AVLTreeNested, AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, Comparable, KeyOrNodeOrEntry } from '../../types';
|
|
10
|
+
import { BTNEntry } from '../../types';
|
|
10
11
|
import { IBinaryTree } from '../../interfaces';
|
|
11
12
|
export declare class AVLTreeNode<K extends Comparable, V = any, NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNodeNested<K, V>> extends BSTNode<K, V, NODE> {
|
|
12
13
|
/**
|
|
@@ -40,25 +41,27 @@ export declare class AVLTreeNode<K extends Comparable, V = any, NODE extends AVL
|
|
|
40
41
|
* 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
|
|
41
42
|
* 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.
|
|
42
43
|
*/
|
|
43
|
-
export declare class AVLTree<K extends Comparable, V = any, NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>, TREE extends AVLTree<K, V, NODE, TREE> = AVLTree<K, V, NODE, AVLTreeNested<K, V, NODE>>> extends BST<K, V, NODE, TREE> implements IBinaryTree<K, V, NODE, TREE> {
|
|
44
|
-
/**
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
*
|
|
58
|
-
* @param
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
44
|
+
export declare class AVLTree<K extends Comparable, V = any, R = BTNEntry<K, V>, NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>, TREE extends AVLTree<K, V, R, NODE, TREE> = AVLTree<K, V, R, NODE, AVLTreeNested<K, V, R, NODE>>> extends BST<K, V, R, NODE, TREE> implements IBinaryTree<K, V, R, NODE, TREE> {
|
|
45
|
+
/**
|
|
46
|
+
* This is a constructor function for an AVLTree class that initializes the tree with keys, nodes,
|
|
47
|
+
* entries, or raw elements.
|
|
48
|
+
* @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
|
|
49
|
+
* iterable object that can contain either keys, nodes, entries, or raw elements. These elements will
|
|
50
|
+
* be used to initialize the AVLTree.
|
|
51
|
+
* @param [options] - The `options` parameter is an optional object that can be used to customize the
|
|
52
|
+
* behavior of the AVLTree. It can include properties such as `compareFn` (a function used to compare
|
|
53
|
+
* keys), `allowDuplicates` (a boolean indicating whether duplicate keys are allowed), and
|
|
54
|
+
* `nodeBuilder` (
|
|
55
|
+
*/
|
|
56
|
+
constructor(keysOrNodesOrEntriesOrRawElements?: Iterable<R | KeyOrNodeOrEntry<K, V, NODE>>, options?: AVLTreeOptions<K, V, R>);
|
|
57
|
+
/**
|
|
58
|
+
* The function creates a new AVL tree node with the given key and value.
|
|
59
|
+
* @param {K} key - The key parameter is of type K, which represents the key of the node being
|
|
60
|
+
* created.
|
|
61
|
+
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
|
|
62
|
+
* value associated with the key in the node being created.
|
|
63
|
+
* @returns The method is returning a new instance of the AVLTreeNode class, casted as the generic
|
|
64
|
+
* type NODE.
|
|
62
65
|
*/
|
|
63
66
|
createNode(key: K, value?: V): NODE;
|
|
64
67
|
/**
|
|
@@ -68,13 +71,15 @@ export declare class AVLTree<K extends Comparable, V = any, NODE extends AVLTree
|
|
|
68
71
|
* being created.
|
|
69
72
|
* @returns a new AVLTree object.
|
|
70
73
|
*/
|
|
71
|
-
createTree(options?: AVLTreeOptions<K>): TREE;
|
|
74
|
+
createTree(options?: AVLTreeOptions<K, V, R>): TREE;
|
|
72
75
|
/**
|
|
73
|
-
* The function checks if
|
|
74
|
-
* @param
|
|
75
|
-
*
|
|
76
|
+
* The function checks if the input is an instance of AVLTreeNode.
|
|
77
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
78
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
79
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
80
|
+
* an instance of the `AVLTreeNode` class.
|
|
76
81
|
*/
|
|
77
|
-
isNode(
|
|
82
|
+
isNode(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntryOrRawElement is NODE;
|
|
78
83
|
/**
|
|
79
84
|
* Time Complexity: O(log n)
|
|
80
85
|
* Space Complexity: O(1)
|
|
@@ -84,15 +89,16 @@ export declare class AVLTree<K extends Comparable, V = any, NODE extends AVLTree
|
|
|
84
89
|
* Time Complexity: O(log n)
|
|
85
90
|
* Space Complexity: O(1)
|
|
86
91
|
*
|
|
87
|
-
* The function overrides the add method of a
|
|
88
|
-
*
|
|
89
|
-
* @param
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
92
|
+
* The function overrides the add method of a class and inserts a key-value pair into a data
|
|
93
|
+
* structure, then balances the path.
|
|
94
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
95
|
+
* `keyOrNodeOrEntryOrRawElement` can accept values of type `R`, `KeyOrNodeOrEntry<K, V, NODE>`, or
|
|
96
|
+
* `RawElement`.
|
|
97
|
+
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
98
|
+
* the key or node being added to the data structure.
|
|
99
|
+
* @returns The method is returning a boolean value.
|
|
94
100
|
*/
|
|
95
|
-
add(
|
|
101
|
+
add(keyOrNodeOrEntryOrRawElement: R | KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean;
|
|
96
102
|
/**
|
|
97
103
|
* Time Complexity: O(log n)
|
|
98
104
|
* Space Complexity: O(1)
|
|
@@ -101,29 +107,34 @@ export declare class AVLTree<K extends Comparable, V = any, NODE extends AVLTree
|
|
|
101
107
|
* Time Complexity: O(log n)
|
|
102
108
|
* Space Complexity: O(1)
|
|
103
109
|
*
|
|
104
|
-
* The function overrides the delete method of a binary tree
|
|
105
|
-
*
|
|
110
|
+
* The function overrides the delete method of a binary tree class and performs additional operations
|
|
111
|
+
* to balance the tree after deletion.
|
|
106
112
|
* @param identifier - The `identifier` parameter is the value or condition used to identify the
|
|
107
|
-
* node(s) to be deleted from the binary tree. It can be of any type
|
|
108
|
-
*
|
|
109
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
110
|
-
*
|
|
111
|
-
*
|
|
112
|
-
* parameter of type `NODE
|
|
113
|
-
* @returns The method is returning an array of `BinaryTreeDeleteResult<NODE>`.
|
|
113
|
+
* node(s) to be deleted from the binary tree. It can be of any type that is compatible with the
|
|
114
|
+
* binary tree's node type.
|
|
115
|
+
* @param {C} callback - The `callback` parameter is a function that will be used to determine if a
|
|
116
|
+
* node should be deleted or not. It is optional and has a default value of `this._DEFAULT_CALLBACK`.
|
|
117
|
+
* @returns The method is returning an array of BinaryTreeDeleteResult<NODE> objects.
|
|
114
118
|
*/
|
|
115
119
|
delete<C extends BTNCallback<NODE>>(identifier: ReturnType<C>, callback?: C): BinaryTreeDeleteResult<NODE>[];
|
|
116
120
|
/**
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
* @param {K | NODE | undefined} srcNode - The `srcNode` parameter represents the source node that
|
|
120
|
-
* needs to be swapped with the destination node. It can be of type `K`, `NODE`, or `undefined`.
|
|
121
|
-
* @param {K | NODE | undefined} destNode - The `destNode` parameter represents the destination
|
|
122
|
-
* node where the values from the source node will be swapped to.
|
|
123
|
-
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
124
|
-
* if either `srcNode` or `destNode` is undefined.
|
|
121
|
+
* Time Complexity: O(1)
|
|
122
|
+
* Space Complexity: O(1)
|
|
125
123
|
*/
|
|
126
|
-
|
|
124
|
+
/**
|
|
125
|
+
* Time Complexity: O(1)
|
|
126
|
+
* Space Complexity: O(1)
|
|
127
|
+
*
|
|
128
|
+
* The `_swapProperties` function swaps the key, value, and height properties between two nodes in a
|
|
129
|
+
* binary search tree.
|
|
130
|
+
* @param {R | BSTNKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents either a node
|
|
131
|
+
* object (`NODE`) or a key-value pair (`R`) that is being swapped with another node.
|
|
132
|
+
* @param {R | BSTNKeyOrNode<K, NODE>} destNode - The `destNode` parameter is either an instance of
|
|
133
|
+
* `R` or an instance of `BSTNKeyOrNode<K, NODE>`.
|
|
134
|
+
* @returns The method is returning the `destNodeEnsured` object if both `srcNodeEnsured` and
|
|
135
|
+
* `destNodeEnsured` are truthy. Otherwise, it returns `undefined`.
|
|
136
|
+
*/
|
|
137
|
+
protected _swapProperties(srcNode: R | BSTNKeyOrNode<K, NODE>, destNode: R | BSTNKeyOrNode<K, NODE>): NODE | undefined;
|
|
127
138
|
/**
|
|
128
139
|
* Time Complexity: O(1)
|
|
129
140
|
* Space Complexity: O(1)
|
|
@@ -133,7 +144,8 @@ export declare class AVLTree<K extends Comparable, V = any, NODE extends AVLTree
|
|
|
133
144
|
* Space Complexity: O(1)
|
|
134
145
|
*
|
|
135
146
|
* The function calculates the balance factor of a node in a binary tree.
|
|
136
|
-
* @param {NODE} node - The parameter "node" represents a node in a
|
|
147
|
+
* @param {NODE} node - The parameter "node" is of type "NODE", which likely represents a node in a
|
|
148
|
+
* binary tree data structure.
|
|
137
149
|
* @returns the balance factor of a given node. The balance factor is calculated by subtracting the
|
|
138
150
|
* height of the left subtree from the height of the right subtree.
|
|
139
151
|
*/
|
|
@@ -159,7 +171,7 @@ export declare class AVLTree<K extends Comparable, V = any, NODE extends AVLTree
|
|
|
159
171
|
* Time Complexity: O(1)
|
|
160
172
|
* Space Complexity: O(1)
|
|
161
173
|
*
|
|
162
|
-
* The
|
|
174
|
+
* The `_balanceLL` function performs a left-left rotation to balance a binary search tree.
|
|
163
175
|
* @param {NODE} A - A is a node in a binary tree.
|
|
164
176
|
*/
|
|
165
177
|
protected _balanceLL(A: NODE): void;
|
|
@@ -210,18 +222,26 @@ export declare class AVLTree<K extends Comparable, V = any, NODE extends AVLTree
|
|
|
210
222
|
*
|
|
211
223
|
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
212
224
|
* to restore balance in an AVL tree after inserting a node.
|
|
213
|
-
* @param {NODE} node - The `node` parameter
|
|
214
|
-
*
|
|
225
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} node - The `node` parameter can be of type `R` or
|
|
226
|
+
* `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
215
227
|
*/
|
|
216
|
-
protected _balancePath(node: KeyOrNodeOrEntry<K, V, NODE>): void;
|
|
228
|
+
protected _balancePath(node: R | KeyOrNodeOrEntry<K, V, NODE>): void;
|
|
217
229
|
/**
|
|
218
|
-
*
|
|
219
|
-
*
|
|
220
|
-
|
|
230
|
+
* Time Complexity: O(1)
|
|
231
|
+
* Space Complexity: O(1)
|
|
232
|
+
*/
|
|
233
|
+
/**
|
|
234
|
+
* Time Complexity: O(1)
|
|
235
|
+
* Space Complexity: O(1)
|
|
236
|
+
*
|
|
237
|
+
* The function replaces an old node with a new node and sets the height of the new node to be the
|
|
238
|
+
* same as the old node.
|
|
239
|
+
* @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
|
|
240
|
+
* the data structure.
|
|
221
241
|
* @param {NODE} newNode - The `newNode` parameter is the new node that will replace the `oldNode` in
|
|
222
242
|
* the data structure.
|
|
223
|
-
* @returns the result of calling the `_replaceNode` method
|
|
224
|
-
* `oldNode` and `newNode` as arguments.
|
|
243
|
+
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
244
|
+
* superclass, with the `oldNode` and `newNode` as arguments.
|
|
225
245
|
*/
|
|
226
246
|
protected _replaceNode(oldNode: NODE, newNode: NODE): NODE;
|
|
227
247
|
}
|
|
@@ -50,27 +50,29 @@ exports.AVLTreeNode = AVLTreeNode;
|
|
|
50
50
|
*/
|
|
51
51
|
class AVLTree extends bst_1.BST {
|
|
52
52
|
/**
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
53
|
+
* This is a constructor function for an AVLTree class that initializes the tree with keys, nodes,
|
|
54
|
+
* entries, or raw elements.
|
|
55
|
+
* @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
|
|
56
|
+
* iterable object that can contain either keys, nodes, entries, or raw elements. These elements will
|
|
57
|
+
* be used to initialize the AVLTree.
|
|
58
|
+
* @param [options] - The `options` parameter is an optional object that can be used to customize the
|
|
59
|
+
* behavior of the AVLTree. It can include properties such as `compareFn` (a function used to compare
|
|
60
|
+
* keys), `allowDuplicates` (a boolean indicating whether duplicate keys are allowed), and
|
|
61
|
+
* `nodeBuilder` (
|
|
60
62
|
*/
|
|
61
|
-
constructor(
|
|
63
|
+
constructor(keysOrNodesOrEntriesOrRawElements = [], options) {
|
|
62
64
|
super([], options);
|
|
63
|
-
if (
|
|
64
|
-
super.addMany(
|
|
65
|
+
if (keysOrNodesOrEntriesOrRawElements)
|
|
66
|
+
super.addMany(keysOrNodesOrEntriesOrRawElements);
|
|
65
67
|
}
|
|
66
68
|
/**
|
|
67
|
-
* The function creates a new AVL tree node with the
|
|
68
|
-
* @param {K} key - The key parameter is the key
|
|
69
|
-
*
|
|
70
|
-
* @param [value] - The parameter
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
69
|
+
* The function creates a new AVL tree node with the given key and value.
|
|
70
|
+
* @param {K} key - The key parameter is of type K, which represents the key of the node being
|
|
71
|
+
* created.
|
|
72
|
+
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
|
|
73
|
+
* value associated with the key in the node being created.
|
|
74
|
+
* @returns The method is returning a new instance of the AVLTreeNode class, casted as the generic
|
|
75
|
+
* type NODE.
|
|
74
76
|
*/
|
|
75
77
|
createNode(key, value) {
|
|
76
78
|
return new AVLTreeNode(key, value);
|
|
@@ -86,12 +88,14 @@ class AVLTree extends bst_1.BST {
|
|
|
86
88
|
return new AVLTree([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
|
|
87
89
|
}
|
|
88
90
|
/**
|
|
89
|
-
* The function checks if
|
|
90
|
-
* @param
|
|
91
|
-
*
|
|
91
|
+
* The function checks if the input is an instance of AVLTreeNode.
|
|
92
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
93
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
94
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
95
|
+
* an instance of the `AVLTreeNode` class.
|
|
92
96
|
*/
|
|
93
|
-
isNode(
|
|
94
|
-
return
|
|
97
|
+
isNode(keyOrNodeOrEntryOrRawElement) {
|
|
98
|
+
return keyOrNodeOrEntryOrRawElement instanceof AVLTreeNode;
|
|
95
99
|
}
|
|
96
100
|
/**
|
|
97
101
|
* Time Complexity: O(log n)
|
|
@@ -102,20 +106,21 @@ class AVLTree extends bst_1.BST {
|
|
|
102
106
|
* Time Complexity: O(log n)
|
|
103
107
|
* Space Complexity: O(1)
|
|
104
108
|
*
|
|
105
|
-
* The function overrides the add method of a
|
|
106
|
-
*
|
|
107
|
-
* @param
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
*
|
|
109
|
+
* The function overrides the add method of a class and inserts a key-value pair into a data
|
|
110
|
+
* structure, then balances the path.
|
|
111
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
112
|
+
* `keyOrNodeOrEntryOrRawElement` can accept values of type `R`, `KeyOrNodeOrEntry<K, V, NODE>`, or
|
|
113
|
+
* `RawElement`.
|
|
114
|
+
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
115
|
+
* the key or node being added to the data structure.
|
|
116
|
+
* @returns The method is returning a boolean value.
|
|
112
117
|
*/
|
|
113
|
-
add(
|
|
114
|
-
if (
|
|
118
|
+
add(keyOrNodeOrEntryOrRawElement, value) {
|
|
119
|
+
if (keyOrNodeOrEntryOrRawElement === null)
|
|
115
120
|
return false;
|
|
116
|
-
const inserted = super.add(
|
|
121
|
+
const inserted = super.add(keyOrNodeOrEntryOrRawElement, value);
|
|
117
122
|
if (inserted)
|
|
118
|
-
this._balancePath(
|
|
123
|
+
this._balancePath(keyOrNodeOrEntryOrRawElement);
|
|
119
124
|
return inserted;
|
|
120
125
|
}
|
|
121
126
|
/**
|
|
@@ -126,16 +131,14 @@ class AVLTree extends bst_1.BST {
|
|
|
126
131
|
* Time Complexity: O(log n)
|
|
127
132
|
* Space Complexity: O(1)
|
|
128
133
|
*
|
|
129
|
-
* The function overrides the delete method of a binary tree
|
|
130
|
-
*
|
|
134
|
+
* The function overrides the delete method of a binary tree class and performs additional operations
|
|
135
|
+
* to balance the tree after deletion.
|
|
131
136
|
* @param identifier - The `identifier` parameter is the value or condition used to identify the
|
|
132
|
-
* node(s) to be deleted from the binary tree. It can be of any type
|
|
133
|
-
*
|
|
134
|
-
* @param {C} callback - The `callback` parameter is a function that will be
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
* parameter of type `NODE
|
|
138
|
-
* @returns The method is returning an array of `BinaryTreeDeleteResult<NODE>`.
|
|
137
|
+
* node(s) to be deleted from the binary tree. It can be of any type that is compatible with the
|
|
138
|
+
* binary tree's node type.
|
|
139
|
+
* @param {C} callback - The `callback` parameter is a function that will be used to determine if a
|
|
140
|
+
* node should be deleted or not. It is optional and has a default value of `this._DEFAULT_CALLBACK`.
|
|
141
|
+
* @returns The method is returning an array of BinaryTreeDeleteResult<NODE> objects.
|
|
139
142
|
*/
|
|
140
143
|
delete(identifier, callback = this._DEFAULT_CALLBACK) {
|
|
141
144
|
const deletedResults = super.delete(identifier, callback);
|
|
@@ -147,14 +150,21 @@ class AVLTree extends bst_1.BST {
|
|
|
147
150
|
return deletedResults;
|
|
148
151
|
}
|
|
149
152
|
/**
|
|
150
|
-
*
|
|
151
|
-
*
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
*
|
|
155
|
-
*
|
|
156
|
-
*
|
|
157
|
-
*
|
|
153
|
+
* Time Complexity: O(1)
|
|
154
|
+
* Space Complexity: O(1)
|
|
155
|
+
*/
|
|
156
|
+
/**
|
|
157
|
+
* Time Complexity: O(1)
|
|
158
|
+
* Space Complexity: O(1)
|
|
159
|
+
*
|
|
160
|
+
* The `_swapProperties` function swaps the key, value, and height properties between two nodes in a
|
|
161
|
+
* binary search tree.
|
|
162
|
+
* @param {R | BSTNKeyOrNode<K, NODE>} srcNode - The `srcNode` parameter represents either a node
|
|
163
|
+
* object (`NODE`) or a key-value pair (`R`) that is being swapped with another node.
|
|
164
|
+
* @param {R | BSTNKeyOrNode<K, NODE>} destNode - The `destNode` parameter is either an instance of
|
|
165
|
+
* `R` or an instance of `BSTNKeyOrNode<K, NODE>`.
|
|
166
|
+
* @returns The method is returning the `destNodeEnsured` object if both `srcNodeEnsured` and
|
|
167
|
+
* `destNodeEnsured` are truthy. Otherwise, it returns `undefined`.
|
|
158
168
|
*/
|
|
159
169
|
_swapProperties(srcNode, destNode) {
|
|
160
170
|
const srcNodeEnsured = this.ensureNode(srcNode);
|
|
@@ -184,7 +194,8 @@ class AVLTree extends bst_1.BST {
|
|
|
184
194
|
* Space Complexity: O(1)
|
|
185
195
|
*
|
|
186
196
|
* The function calculates the balance factor of a node in a binary tree.
|
|
187
|
-
* @param {NODE} node - The parameter "node" represents a node in a
|
|
197
|
+
* @param {NODE} node - The parameter "node" is of type "NODE", which likely represents a node in a
|
|
198
|
+
* binary tree data structure.
|
|
188
199
|
* @returns the balance factor of a given node. The balance factor is calculated by subtracting the
|
|
189
200
|
* height of the left subtree from the height of the right subtree.
|
|
190
201
|
*/
|
|
@@ -230,7 +241,7 @@ class AVLTree extends bst_1.BST {
|
|
|
230
241
|
* Time Complexity: O(1)
|
|
231
242
|
* Space Complexity: O(1)
|
|
232
243
|
*
|
|
233
|
-
* The
|
|
244
|
+
* The `_balanceLL` function performs a left-left rotation to balance a binary search tree.
|
|
234
245
|
* @param {NODE} A - A is a node in a binary tree.
|
|
235
246
|
*/
|
|
236
247
|
_balanceLL(A) {
|
|
@@ -428,8 +439,8 @@ class AVLTree extends bst_1.BST {
|
|
|
428
439
|
*
|
|
429
440
|
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
430
441
|
* to restore balance in an AVL tree after inserting a node.
|
|
431
|
-
* @param {NODE} node - The `node` parameter
|
|
432
|
-
*
|
|
442
|
+
* @param {R | KeyOrNodeOrEntry<K, V, NODE>} node - The `node` parameter can be of type `R` or
|
|
443
|
+
* `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
433
444
|
*/
|
|
434
445
|
_balancePath(node) {
|
|
435
446
|
node = this.ensureNode(node);
|
|
@@ -472,13 +483,21 @@ class AVLTree extends bst_1.BST {
|
|
|
472
483
|
}
|
|
473
484
|
}
|
|
474
485
|
/**
|
|
475
|
-
*
|
|
476
|
-
*
|
|
477
|
-
|
|
486
|
+
* Time Complexity: O(1)
|
|
487
|
+
* Space Complexity: O(1)
|
|
488
|
+
*/
|
|
489
|
+
/**
|
|
490
|
+
* Time Complexity: O(1)
|
|
491
|
+
* Space Complexity: O(1)
|
|
492
|
+
*
|
|
493
|
+
* The function replaces an old node with a new node and sets the height of the new node to be the
|
|
494
|
+
* same as the old node.
|
|
495
|
+
* @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
|
|
496
|
+
* the data structure.
|
|
478
497
|
* @param {NODE} newNode - The `newNode` parameter is the new node that will replace the `oldNode` in
|
|
479
498
|
* the data structure.
|
|
480
|
-
* @returns the result of calling the `_replaceNode` method
|
|
481
|
-
* `oldNode` and `newNode` as arguments.
|
|
499
|
+
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
500
|
+
* superclass, with the `oldNode` and `newNode` as arguments.
|
|
482
501
|
*/
|
|
483
502
|
_replaceNode(oldNode, newNode) {
|
|
484
503
|
newNode.height = oldNode.height;
|