data-structure-typed 1.50.0 → 1.50.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/CHANGELOG.md +2 -1
- package/README.md +204 -212
- package/benchmark/report.html +37 -1
- package/benchmark/report.json +370 -22
- package/dist/cjs/data-structures/base/iterable-base.d.ts +114 -9
- package/dist/cjs/data-structures/base/iterable-base.js +143 -7
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +43 -46
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +68 -71
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +244 -199
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +484 -376
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +54 -74
- package/dist/cjs/data-structures/binary-tree/bst.js +30 -71
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +78 -60
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +84 -89
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +37 -56
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +64 -85
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +1 -0
- package/dist/cjs/data-structures/graph/abstract-graph.js +3 -0
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +14 -0
- package/dist/cjs/data-structures/graph/directed-graph.js +26 -0
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/cjs/data-structures/graph/map-graph.js +14 -0
- package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +16 -0
- package/dist/cjs/data-structures/graph/undirected-graph.js +25 -0
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +121 -15
- package/dist/cjs/data-structures/hash/hash-map.js +160 -25
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +66 -6
- package/dist/cjs/data-structures/heap/heap.js +66 -6
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +67 -50
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +70 -64
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +128 -103
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +130 -112
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +63 -36
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/cjs/data-structures/matrix/matrix.js +50 -11
- package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +49 -19
- package/dist/cjs/data-structures/queue/deque.js +101 -47
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +39 -5
- package/dist/cjs/data-structures/queue/queue.js +47 -5
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +16 -0
- package/dist/cjs/data-structures/stack/stack.js +22 -0
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +38 -1
- package/dist/cjs/data-structures/trie/trie.js +41 -0
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/cjs/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/cjs/types/utils/utils.d.ts +1 -0
- package/dist/mjs/data-structures/base/iterable-base.d.ts +114 -9
- package/dist/mjs/data-structures/base/iterable-base.js +143 -7
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +43 -46
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +68 -71
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +244 -199
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +483 -375
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +54 -74
- package/dist/mjs/data-structures/binary-tree/bst.js +30 -71
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +78 -60
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +84 -89
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +37 -56
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +64 -85
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +1 -0
- package/dist/mjs/data-structures/graph/abstract-graph.js +3 -0
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +14 -0
- package/dist/mjs/data-structures/graph/directed-graph.js +26 -0
- package/dist/mjs/data-structures/graph/map-graph.d.ts +8 -0
- package/dist/mjs/data-structures/graph/map-graph.js +14 -0
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +16 -0
- package/dist/mjs/data-structures/graph/undirected-graph.js +25 -0
- package/dist/mjs/data-structures/hash/hash-map.d.ts +121 -15
- package/dist/mjs/data-structures/hash/hash-map.js +160 -25
- package/dist/mjs/data-structures/heap/heap.d.ts +66 -6
- package/dist/mjs/data-structures/heap/heap.js +66 -6
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +67 -50
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +70 -64
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +128 -103
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +130 -112
- package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +63 -36
- package/dist/mjs/data-structures/linked-list/skip-linked-list.js +63 -36
- package/dist/mjs/data-structures/matrix/matrix.d.ts +35 -4
- package/dist/mjs/data-structures/matrix/matrix.js +50 -11
- package/dist/mjs/data-structures/queue/deque.d.ts +49 -19
- package/dist/mjs/data-structures/queue/deque.js +101 -47
- package/dist/mjs/data-structures/queue/queue.d.ts +39 -5
- package/dist/mjs/data-structures/queue/queue.js +47 -5
- package/dist/mjs/data-structures/stack/stack.d.ts +16 -0
- package/dist/mjs/data-structures/stack/stack.js +22 -0
- package/dist/mjs/data-structures/trie/trie.d.ts +38 -1
- package/dist/mjs/data-structures/trie/trie.js +41 -0
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/mjs/types/data-structures/hash/hash-map.d.ts +4 -3
- package/dist/mjs/types/utils/utils.d.ts +1 -0
- package/dist/umd/data-structure-typed.js +1730 -1042
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +16 -40
- package/src/data-structures/base/iterable-base.ts +172 -19
- package/src/data-structures/binary-tree/avl-tree.ts +97 -97
- package/src/data-structures/binary-tree/binary-tree.ts +674 -671
- package/src/data-structures/binary-tree/bst.ts +89 -131
- package/src/data-structures/binary-tree/rb-tree.ts +127 -155
- package/src/data-structures/binary-tree/tree-multimap.ts +96 -112
- package/src/data-structures/graph/abstract-graph.ts +4 -0
- package/src/data-structures/graph/directed-graph.ts +30 -0
- package/src/data-structures/graph/map-graph.ts +15 -0
- package/src/data-structures/graph/undirected-graph.ts +28 -0
- package/src/data-structures/hash/hash-map.ts +175 -34
- package/src/data-structures/heap/heap.ts +66 -6
- package/src/data-structures/linked-list/doubly-linked-list.ts +72 -66
- package/src/data-structures/linked-list/singly-linked-list.ts +132 -114
- package/src/data-structures/linked-list/skip-linked-list.ts +63 -37
- package/src/data-structures/matrix/matrix.ts +52 -12
- package/src/data-structures/queue/deque.ts +108 -49
- package/src/data-structures/queue/queue.ts +51 -5
- package/src/data-structures/stack/stack.ts +24 -0
- package/src/data-structures/trie/trie.ts +45 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +4 -3
- package/src/types/utils/utils.ts +2 -0
- package/test/performance/data-structures/graph/directed-graph.test.ts +3 -3
- package/test/performance/data-structures/queue/deque.test.ts +26 -25
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +37 -0
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +46 -17
- package/test/unit/data-structures/binary-tree/bst.test.ts +65 -1
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +38 -1
- package/test/unit/data-structures/binary-tree/tree-multimap.test.ts +37 -32
- package/test/unit/data-structures/graph/abstract-graph.test.ts +8 -0
- package/test/unit/data-structures/graph/directed-graph.test.ts +249 -0
- package/test/unit/data-structures/hash/hash-map.test.ts +376 -353
- package/test/unit/data-structures/heap/heap.test.ts +18 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +24 -5
- package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +20 -2
- package/test/unit/data-structures/linked-list/skip-list.test.ts +1 -1
- package/test/unit/data-structures/queue/deque.test.ts +71 -10
- package/test/unit/data-structures/queue/queue.test.ts +23 -6
- package/test/unit/data-structures/stack/stack.test.ts +17 -0
- package/test/unit/data-structures/trie/trie.test.ts +17 -0
- package/test/unit/unrestricted-interconversion.test.ts +143 -10
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
import { BST, BSTNode } from './bst';
|
|
9
9
|
import type { AVLTreeNested, AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, KeyOrNodeOrEntry } 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, NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNodeNested<K, V>> extends BSTNode<K, V, NODE> {
|
|
12
12
|
height: number;
|
|
13
13
|
constructor(key: K, value?: V);
|
|
14
14
|
}
|
|
@@ -21,27 +21,27 @@ export declare class AVLTreeNode<K = any, V = any, N extends AVLTreeNode<K, V, N
|
|
|
21
21
|
* 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
|
|
22
22
|
* 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.
|
|
23
23
|
*/
|
|
24
|
-
export declare class AVLTree<K = any, V = any,
|
|
24
|
+
export declare class AVLTree<K = any, 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> {
|
|
25
25
|
/**
|
|
26
26
|
* The constructor function initializes an AVLTree object with optional keysOrNodesOrEntries and options.
|
|
27
|
-
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V,
|
|
27
|
+
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, NODE>`
|
|
28
28
|
* objects. It represents a collection of nodes that will be added to the AVL tree during
|
|
29
29
|
* initialization.
|
|
30
30
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
31
31
|
* behavior of the AVL tree. It is of type `Partial<AVLTreeOptions>`, which means that you can
|
|
32
32
|
* provide only a subset of the properties defined in the `AVLTreeOptions` interface.
|
|
33
33
|
*/
|
|
34
|
-
constructor(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V,
|
|
34
|
+
constructor(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V, NODE>>, options?: AVLTreeOptions<K>);
|
|
35
35
|
/**
|
|
36
36
|
* The function creates a new AVL tree node with the specified key and value.
|
|
37
37
|
* @param {K} key - The key parameter is the key value that will be associated with
|
|
38
38
|
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
39
39
|
* @param [value] - The parameter `value` is an optional value that can be assigned to the node. It is of
|
|
40
40
|
* type `V`, which means it can be any value that is assignable to the `value` property of the
|
|
41
|
-
* node type `
|
|
41
|
+
* node type `NODE`.
|
|
42
42
|
* @returns a new AVLTreeNode object with the specified key and value.
|
|
43
43
|
*/
|
|
44
|
-
createNode(key: K, value?: V):
|
|
44
|
+
createNode(key: K, value?: V): NODE;
|
|
45
45
|
/**
|
|
46
46
|
* The function creates a new AVL tree with the specified options and returns it.
|
|
47
47
|
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
@@ -52,10 +52,10 @@ export declare class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> =
|
|
|
52
52
|
createTree(options?: AVLTreeOptions<K>): TREE;
|
|
53
53
|
/**
|
|
54
54
|
* The function checks if an keyOrNodeOrEntry is an instance of AVLTreeNode.
|
|
55
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V,
|
|
55
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
56
56
|
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeNode class.
|
|
57
57
|
*/
|
|
58
|
-
isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V,
|
|
58
|
+
isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE;
|
|
59
59
|
/**
|
|
60
60
|
* Time Complexity: O(log n)
|
|
61
61
|
* Space Complexity: O(1)
|
|
@@ -73,7 +73,7 @@ export declare class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> =
|
|
|
73
73
|
* being added to the binary tree.
|
|
74
74
|
* @returns The method is returning either the inserted node or undefined.
|
|
75
75
|
*/
|
|
76
|
-
add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V,
|
|
76
|
+
add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean;
|
|
77
77
|
/**
|
|
78
78
|
* Time Complexity: O(log n)
|
|
79
79
|
* Space Complexity: O(1)
|
|
@@ -90,40 +90,38 @@ export declare class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> =
|
|
|
90
90
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
91
91
|
* that is deleted from the binary tree. It is an optional parameter and if not provided, it will
|
|
92
92
|
* default to the `_defaultOneParamCallback` function. The `callback` function should have a single
|
|
93
|
-
* parameter of type `
|
|
94
|
-
* @returns The method is returning an array of `BinaryTreeDeleteResult<
|
|
93
|
+
* parameter of type `NODE
|
|
94
|
+
* @returns The method is returning an array of `BinaryTreeDeleteResult<NODE>`.
|
|
95
95
|
*/
|
|
96
|
-
delete<C extends BTNCallback<
|
|
96
|
+
delete<C extends BTNCallback<NODE>>(identifier: ReturnType<C>, callback?: C): BinaryTreeDeleteResult<NODE>[];
|
|
97
97
|
/**
|
|
98
98
|
* The `_swapProperties` function swaps the key, value, and height properties between two nodes in a binary
|
|
99
99
|
* tree.
|
|
100
|
-
* @param {K |
|
|
101
|
-
* needs to be swapped with the destination node. It can be of type `K`, `
|
|
102
|
-
* @param {K |
|
|
100
|
+
* @param {K | NODE | undefined} srcNode - The `srcNode` parameter represents the source node that
|
|
101
|
+
* needs to be swapped with the destination node. It can be of type `K`, `NODE`, or `undefined`.
|
|
102
|
+
* @param {K | NODE | undefined} destNode - The `destNode` parameter represents the destination
|
|
103
103
|
* node where the values from the source node will be swapped to.
|
|
104
104
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
105
105
|
* if either `srcNode` or `destNode` is undefined.
|
|
106
106
|
*/
|
|
107
|
-
protected _swapProperties(srcNode: BSTNKeyOrNode<K,
|
|
107
|
+
protected _swapProperties(srcNode: BSTNKeyOrNode<K, NODE>, destNode: BSTNKeyOrNode<K, NODE>): NODE | undefined;
|
|
108
108
|
/**
|
|
109
109
|
* Time Complexity: O(1)
|
|
110
110
|
* Space Complexity: O(1)
|
|
111
|
-
* constant time, as it performs a fixed number of operations. constant space, as it only uses a constant amount of memory.
|
|
112
111
|
*/
|
|
113
112
|
/**
|
|
114
113
|
* Time Complexity: O(1)
|
|
115
114
|
* Space Complexity: O(1)
|
|
116
115
|
*
|
|
117
116
|
* The function calculates the balance factor of a node in a binary tree.
|
|
118
|
-
* @param {
|
|
117
|
+
* @param {NODE} node - The parameter "node" represents a node in a binary tree data structure.
|
|
119
118
|
* @returns the balance factor of a given node. The balance factor is calculated by subtracting the
|
|
120
119
|
* height of the left subtree from the height of the right subtree.
|
|
121
120
|
*/
|
|
122
|
-
protected _balanceFactor(node:
|
|
121
|
+
protected _balanceFactor(node: NODE): number;
|
|
123
122
|
/**
|
|
124
123
|
* Time Complexity: O(1)
|
|
125
124
|
* Space Complexity: O(1)
|
|
126
|
-
* constant time, as it performs a fixed number of operations. constant space, as it only uses a constant amount of memory.
|
|
127
125
|
*/
|
|
128
126
|
/**
|
|
129
127
|
* Time Complexity: O(1)
|
|
@@ -131,37 +129,33 @@ export declare class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> =
|
|
|
131
129
|
*
|
|
132
130
|
* The function updates the height of a node in a binary tree based on the heights of its left and
|
|
133
131
|
* right children.
|
|
134
|
-
* @param {
|
|
132
|
+
* @param {NODE} node - The parameter "node" represents a node in a binary tree data structure.
|
|
135
133
|
*/
|
|
136
|
-
protected _updateHeight(node:
|
|
134
|
+
protected _updateHeight(node: NODE): void;
|
|
137
135
|
/**
|
|
138
|
-
* Time Complexity: O(
|
|
136
|
+
* Time Complexity: O(1)
|
|
139
137
|
* Space Complexity: O(1)
|
|
140
|
-
* 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.
|
|
141
138
|
*/
|
|
142
139
|
/**
|
|
143
|
-
* Time Complexity: O(
|
|
140
|
+
* Time Complexity: O(1)
|
|
144
141
|
* Space Complexity: O(1)
|
|
145
142
|
*
|
|
146
|
-
* The `
|
|
147
|
-
*
|
|
148
|
-
* @param {N} node - The `node` parameter in the `_balancePath` function represents the node in the
|
|
149
|
-
* AVL tree that needs to be balanced.
|
|
143
|
+
* The function `_balanceLL` performs a left-left rotation to balance a binary tree.
|
|
144
|
+
* @param {NODE} A - A is a node in a binary tree.
|
|
150
145
|
*/
|
|
151
|
-
protected
|
|
146
|
+
protected _balanceLL(A: NODE): void;
|
|
152
147
|
/**
|
|
153
148
|
* Time Complexity: O(1)
|
|
154
149
|
* Space Complexity: O(1)
|
|
155
|
-
* constant time, as these methods perform a fixed number of operations. constant space, as they only use a constant amount of memory.
|
|
156
150
|
*/
|
|
157
151
|
/**
|
|
158
152
|
* Time Complexity: O(1)
|
|
159
153
|
* Space Complexity: O(1)
|
|
160
154
|
*
|
|
161
|
-
* The
|
|
162
|
-
* @param {
|
|
155
|
+
* The `_balanceLR` function performs a left-right rotation to balance a binary tree.
|
|
156
|
+
* @param {NODE} A - A is a node in a binary tree.
|
|
163
157
|
*/
|
|
164
|
-
protected
|
|
158
|
+
protected _balanceLR(A: NODE): void;
|
|
165
159
|
/**
|
|
166
160
|
* Time Complexity: O(1)
|
|
167
161
|
* Space Complexity: O(1)
|
|
@@ -170,10 +164,10 @@ export declare class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> =
|
|
|
170
164
|
* Time Complexity: O(1)
|
|
171
165
|
* Space Complexity: O(1)
|
|
172
166
|
*
|
|
173
|
-
* The `
|
|
174
|
-
* @param {
|
|
167
|
+
* The function `_balanceRR` performs a right-right rotation to balance a binary tree.
|
|
168
|
+
* @param {NODE} A - A is a node in a binary tree.
|
|
175
169
|
*/
|
|
176
|
-
protected
|
|
170
|
+
protected _balanceRR(A: NODE): void;
|
|
177
171
|
/**
|
|
178
172
|
* Time Complexity: O(1)
|
|
179
173
|
* Space Complexity: O(1)
|
|
@@ -182,21 +176,24 @@ export declare class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> =
|
|
|
182
176
|
* Time Complexity: O(1)
|
|
183
177
|
* Space Complexity: O(1)
|
|
184
178
|
*
|
|
185
|
-
* The function `
|
|
186
|
-
* @param {
|
|
179
|
+
* The function `_balanceRL` performs a right-left rotation to balance a binary tree.
|
|
180
|
+
* @param {NODE} A - A is a node in a binary tree.
|
|
187
181
|
*/
|
|
188
|
-
protected
|
|
182
|
+
protected _balanceRL(A: NODE): void;
|
|
189
183
|
/**
|
|
190
|
-
* Time Complexity: O(
|
|
184
|
+
* Time Complexity: O(log n)
|
|
191
185
|
* Space Complexity: O(1)
|
|
186
|
+
* 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.
|
|
192
187
|
*/
|
|
193
188
|
/**
|
|
194
|
-
* Time Complexity: O(
|
|
189
|
+
* Time Complexity: O(log n)
|
|
195
190
|
* Space Complexity: O(1)
|
|
196
191
|
*
|
|
197
|
-
* The
|
|
198
|
-
*
|
|
192
|
+
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
193
|
+
* to restore balance in an AVL tree after inserting a node.
|
|
194
|
+
* @param {NODE} node - The `node` parameter in the `_balancePath` function represents the node in the
|
|
195
|
+
* AVL tree that needs to be balanced.
|
|
199
196
|
*/
|
|
200
|
-
protected
|
|
201
|
-
protected _replaceNode(oldNode:
|
|
197
|
+
protected _balancePath(node: KeyOrNodeOrEntry<K, V, NODE>): void;
|
|
198
|
+
protected _replaceNode(oldNode: NODE, newNode: NODE): NODE;
|
|
202
199
|
}
|
|
@@ -25,7 +25,7 @@ export class AVLTreeNode extends BSTNode {
|
|
|
25
25
|
export class AVLTree extends BST {
|
|
26
26
|
/**
|
|
27
27
|
* The constructor function initializes an AVLTree object with optional keysOrNodesOrEntries and options.
|
|
28
|
-
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V,
|
|
28
|
+
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, NODE>`
|
|
29
29
|
* objects. It represents a collection of nodes that will be added to the AVL tree during
|
|
30
30
|
* initialization.
|
|
31
31
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
@@ -43,7 +43,7 @@ export class AVLTree extends BST {
|
|
|
43
43
|
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
44
44
|
* @param [value] - The parameter `value` is an optional value that can be assigned to the node. It is of
|
|
45
45
|
* type `V`, which means it can be any value that is assignable to the `value` property of the
|
|
46
|
-
* node type `
|
|
46
|
+
* node type `NODE`.
|
|
47
47
|
* @returns a new AVLTreeNode object with the specified key and value.
|
|
48
48
|
*/
|
|
49
49
|
createNode(key, value) {
|
|
@@ -65,7 +65,7 @@ export class AVLTree extends BST {
|
|
|
65
65
|
}
|
|
66
66
|
/**
|
|
67
67
|
* The function checks if an keyOrNodeOrEntry is an instance of AVLTreeNode.
|
|
68
|
-
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V,
|
|
68
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
69
69
|
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeNode class.
|
|
70
70
|
*/
|
|
71
71
|
isNode(keyOrNodeOrEntry) {
|
|
@@ -112,8 +112,8 @@ export class AVLTree extends BST {
|
|
|
112
112
|
* @param {C} callback - The `callback` parameter is a function that will be called for each node
|
|
113
113
|
* that is deleted from the binary tree. It is an optional parameter and if not provided, it will
|
|
114
114
|
* default to the `_defaultOneParamCallback` function. The `callback` function should have a single
|
|
115
|
-
* parameter of type `
|
|
116
|
-
* @returns The method is returning an array of `BinaryTreeDeleteResult<
|
|
115
|
+
* parameter of type `NODE
|
|
116
|
+
* @returns The method is returning an array of `BinaryTreeDeleteResult<NODE>`.
|
|
117
117
|
*/
|
|
118
118
|
delete(identifier, callback = this._defaultOneParamCallback) {
|
|
119
119
|
if (identifier instanceof AVLTreeNode)
|
|
@@ -129,9 +129,9 @@ export class AVLTree extends BST {
|
|
|
129
129
|
/**
|
|
130
130
|
* The `_swapProperties` function swaps the key, value, and height properties between two nodes in a binary
|
|
131
131
|
* tree.
|
|
132
|
-
* @param {K |
|
|
133
|
-
* needs to be swapped with the destination node. It can be of type `K`, `
|
|
134
|
-
* @param {K |
|
|
132
|
+
* @param {K | NODE | undefined} srcNode - The `srcNode` parameter represents the source node that
|
|
133
|
+
* needs to be swapped with the destination node. It can be of type `K`, `NODE`, or `undefined`.
|
|
134
|
+
* @param {K | NODE | undefined} destNode - The `destNode` parameter represents the destination
|
|
135
135
|
* node where the values from the source node will be swapped to.
|
|
136
136
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
137
137
|
* if either `srcNode` or `destNode` is undefined.
|
|
@@ -158,14 +158,13 @@ export class AVLTree extends BST {
|
|
|
158
158
|
/**
|
|
159
159
|
* Time Complexity: O(1)
|
|
160
160
|
* Space Complexity: O(1)
|
|
161
|
-
* constant time, as it performs a fixed number of operations. constant space, as it only uses a constant amount of memory.
|
|
162
161
|
*/
|
|
163
162
|
/**
|
|
164
163
|
* Time Complexity: O(1)
|
|
165
164
|
* Space Complexity: O(1)
|
|
166
165
|
*
|
|
167
166
|
* The function calculates the balance factor of a node in a binary tree.
|
|
168
|
-
* @param {
|
|
167
|
+
* @param {NODE} node - The parameter "node" represents a node in a binary tree data structure.
|
|
169
168
|
* @returns the balance factor of a given node. The balance factor is calculated by subtracting the
|
|
170
169
|
* height of the left subtree from the height of the right subtree.
|
|
171
170
|
*/
|
|
@@ -182,7 +181,6 @@ export class AVLTree extends BST {
|
|
|
182
181
|
/**
|
|
183
182
|
* Time Complexity: O(1)
|
|
184
183
|
* Space Complexity: O(1)
|
|
185
|
-
* constant time, as it performs a fixed number of operations. constant space, as it only uses a constant amount of memory.
|
|
186
184
|
*/
|
|
187
185
|
/**
|
|
188
186
|
* Time Complexity: O(1)
|
|
@@ -190,7 +188,7 @@ export class AVLTree extends BST {
|
|
|
190
188
|
*
|
|
191
189
|
* The function updates the height of a node in a binary tree based on the heights of its left and
|
|
192
190
|
* right children.
|
|
193
|
-
* @param {
|
|
191
|
+
* @param {NODE} node - The parameter "node" represents a node in a binary tree data structure.
|
|
194
192
|
*/
|
|
195
193
|
_updateHeight(node) {
|
|
196
194
|
if (!node.left && !node.right)
|
|
@@ -204,71 +202,16 @@ export class AVLTree extends BST {
|
|
|
204
202
|
else
|
|
205
203
|
node.height = 1 + Math.max(node.right.height, node.left.height);
|
|
206
204
|
}
|
|
207
|
-
/**
|
|
208
|
-
* Time Complexity: O(log n)
|
|
209
|
-
* Space Complexity: O(1)
|
|
210
|
-
* 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.
|
|
211
|
-
*/
|
|
212
|
-
/**
|
|
213
|
-
* Time Complexity: O(log n)
|
|
214
|
-
* Space Complexity: O(1)
|
|
215
|
-
*
|
|
216
|
-
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
217
|
-
* to restore balance in an AVL tree after inserting a node.
|
|
218
|
-
* @param {N} node - The `node` parameter in the `_balancePath` function represents the node in the
|
|
219
|
-
* AVL tree that needs to be balanced.
|
|
220
|
-
*/
|
|
221
|
-
_balancePath(node) {
|
|
222
|
-
node = this.ensureNode(node);
|
|
223
|
-
const path = this.getPathToRoot(node, false); // first O(log n) + O(log n)
|
|
224
|
-
for (let i = 0; i < path.length; i++) {
|
|
225
|
-
// second O(log n)
|
|
226
|
-
const A = path[i];
|
|
227
|
-
// Update Heights: After inserting a node, backtrack from the insertion point to the root node, updating the height of each node along the way.
|
|
228
|
-
this._updateHeight(A); // first O(1)
|
|
229
|
-
// Check Balance: Simultaneously with height updates, check if each node violates the balance property of an AVL tree.
|
|
230
|
-
// Balance Restoration: If a balance issue is discovered after inserting a node, it requires balance restoration operations. Balance restoration includes four basic cases where rotation operations need to be performed to fix the balance:
|
|
231
|
-
switch (this._balanceFactor(A) // second O(1)
|
|
232
|
-
) {
|
|
233
|
-
case -2:
|
|
234
|
-
if (A && A.left) {
|
|
235
|
-
if (this._balanceFactor(A.left) <= 0) {
|
|
236
|
-
// second O(1)
|
|
237
|
-
// Left Rotation (LL Rotation): When the inserted node is in the left subtree of the left subtree, causing an imbalance.
|
|
238
|
-
this._balanceLL(A);
|
|
239
|
-
}
|
|
240
|
-
else {
|
|
241
|
-
// Left-Right Rotation (LR Rotation): When the inserted node is in the right subtree of the left subtree, causing an imbalance.
|
|
242
|
-
this._balanceLR(A);
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
break;
|
|
246
|
-
case +2:
|
|
247
|
-
if (A && A.right) {
|
|
248
|
-
if (this._balanceFactor(A.right) >= 0) {
|
|
249
|
-
// Right Rotation (RR Rotation): When the inserted node is in the right subtree of the right subtree, causing an imbalance.
|
|
250
|
-
this._balanceRR(A);
|
|
251
|
-
}
|
|
252
|
-
else {
|
|
253
|
-
// Right-Left Rotation (RL Rotation): When the inserted node is in the left subtree of the right subtree, causing an imbalance.
|
|
254
|
-
this._balanceRL(A);
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
// TODO So far, no sure if this is necessary that Recursive Repair: Once rotation operations are executed, it may cause imbalance issues at higher levels of the tree. Therefore, you need to recursively check and repair imbalance problems upwards until you reach the root node.
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
205
|
/**
|
|
262
206
|
* Time Complexity: O(1)
|
|
263
207
|
* Space Complexity: O(1)
|
|
264
|
-
* constant time, as these methods perform a fixed number of operations. constant space, as they only use a constant amount of memory.
|
|
265
208
|
*/
|
|
266
209
|
/**
|
|
267
210
|
* Time Complexity: O(1)
|
|
268
211
|
* Space Complexity: O(1)
|
|
269
212
|
*
|
|
270
213
|
* The function `_balanceLL` performs a left-left rotation to balance a binary tree.
|
|
271
|
-
* @param {
|
|
214
|
+
* @param {NODE} A - A is a node in a binary tree.
|
|
272
215
|
*/
|
|
273
216
|
_balanceLL(A) {
|
|
274
217
|
const parentOfA = A.parent;
|
|
@@ -309,7 +252,7 @@ export class AVLTree extends BST {
|
|
|
309
252
|
* Space Complexity: O(1)
|
|
310
253
|
*
|
|
311
254
|
* The `_balanceLR` function performs a left-right rotation to balance a binary tree.
|
|
312
|
-
* @param {
|
|
255
|
+
* @param {NODE} A - A is a node in a binary tree.
|
|
313
256
|
*/
|
|
314
257
|
_balanceLR(A) {
|
|
315
258
|
const parentOfA = A.parent;
|
|
@@ -365,7 +308,7 @@ export class AVLTree extends BST {
|
|
|
365
308
|
* Space Complexity: O(1)
|
|
366
309
|
*
|
|
367
310
|
* The function `_balanceRR` performs a right-right rotation to balance a binary tree.
|
|
368
|
-
* @param {
|
|
311
|
+
* @param {NODE} A - A is a node in a binary tree.
|
|
369
312
|
*/
|
|
370
313
|
_balanceRR(A) {
|
|
371
314
|
const parentOfA = A.parent;
|
|
@@ -407,7 +350,7 @@ export class AVLTree extends BST {
|
|
|
407
350
|
* Space Complexity: O(1)
|
|
408
351
|
*
|
|
409
352
|
* The function `_balanceRL` performs a right-left rotation to balance a binary tree.
|
|
410
|
-
* @param {
|
|
353
|
+
* @param {NODE} A - A is a node in a binary tree.
|
|
411
354
|
*/
|
|
412
355
|
_balanceRL(A) {
|
|
413
356
|
const parentOfA = A.parent;
|
|
@@ -454,6 +397,60 @@ export class AVLTree extends BST {
|
|
|
454
397
|
B && this._updateHeight(B);
|
|
455
398
|
C && this._updateHeight(C);
|
|
456
399
|
}
|
|
400
|
+
/**
|
|
401
|
+
* Time Complexity: O(log n)
|
|
402
|
+
* Space Complexity: O(1)
|
|
403
|
+
* 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.
|
|
404
|
+
*/
|
|
405
|
+
/**
|
|
406
|
+
* Time Complexity: O(log n)
|
|
407
|
+
* Space Complexity: O(1)
|
|
408
|
+
*
|
|
409
|
+
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
410
|
+
* to restore balance in an AVL tree after inserting a node.
|
|
411
|
+
* @param {NODE} node - The `node` parameter in the `_balancePath` function represents the node in the
|
|
412
|
+
* AVL tree that needs to be balanced.
|
|
413
|
+
*/
|
|
414
|
+
_balancePath(node) {
|
|
415
|
+
node = this.ensureNode(node);
|
|
416
|
+
const path = this.getPathToRoot(node, false); // first O(log n) + O(log n)
|
|
417
|
+
for (let i = 0; i < path.length; i++) {
|
|
418
|
+
// second O(log n)
|
|
419
|
+
const A = path[i];
|
|
420
|
+
// Update Heights: After inserting a node, backtrack from the insertion point to the root node, updating the height of each node along the way.
|
|
421
|
+
this._updateHeight(A); // first O(1)
|
|
422
|
+
// Check Balance: Simultaneously with height updates, check if each node violates the balance property of an AVL tree.
|
|
423
|
+
// Balance Restoration: If a balance issue is discovered after inserting a node, it requires balance restoration operations. Balance restoration includes four basic cases where rotation operations need to be performed to fix the balance:
|
|
424
|
+
switch (this._balanceFactor(A) // second O(1)
|
|
425
|
+
) {
|
|
426
|
+
case -2:
|
|
427
|
+
if (A && A.left) {
|
|
428
|
+
if (this._balanceFactor(A.left) <= 0) {
|
|
429
|
+
// second O(1)
|
|
430
|
+
// Left Rotation (LL Rotation): When the inserted node is in the left subtree of the left subtree, causing an imbalance.
|
|
431
|
+
this._balanceLL(A);
|
|
432
|
+
}
|
|
433
|
+
else {
|
|
434
|
+
// Left-Right Rotation (LR Rotation): When the inserted node is in the right subtree of the left subtree, causing an imbalance.
|
|
435
|
+
this._balanceLR(A);
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
break;
|
|
439
|
+
case +2:
|
|
440
|
+
if (A && A.right) {
|
|
441
|
+
if (this._balanceFactor(A.right) >= 0) {
|
|
442
|
+
// Right Rotation (RR Rotation): When the inserted node is in the right subtree of the right subtree, causing an imbalance.
|
|
443
|
+
this._balanceRR(A);
|
|
444
|
+
}
|
|
445
|
+
else {
|
|
446
|
+
// Right-Left Rotation (RL Rotation): When the inserted node is in the left subtree of the right subtree, causing an imbalance.
|
|
447
|
+
this._balanceRL(A);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
// TODO So far, no sure if this is necessary that Recursive Repair: Once rotation operations are executed, it may cause imbalance issues at higher levels of the tree. Therefore, you need to recursively check and repair imbalance problems upwards until you reach the root node.
|
|
452
|
+
}
|
|
453
|
+
}
|
|
457
454
|
_replaceNode(oldNode, newNode) {
|
|
458
455
|
newNode.height = oldNode.height;
|
|
459
456
|
return super._replaceNode(oldNode, newNode);
|