data-structure-typed 1.52.5 → 1.52.7
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 +1 -1
- package/README.md +33 -30
- package/benchmark/report.html +13 -13
- package/benchmark/report.json +156 -156
- package/dist/cjs/constants/index.d.ts +4 -0
- package/dist/cjs/constants/index.js +9 -0
- package/dist/cjs/constants/index.js.map +1 -0
- package/dist/cjs/data-structures/base/iterable-element-base.d.ts +8 -1
- package/dist/cjs/data-structures/base/iterable-element-base.js +10 -1
- package/dist/cjs/data-structures/base/iterable-element-base.js.map +1 -1
- package/dist/cjs/data-structures/base/iterable-entry-base.d.ts +8 -1
- package/dist/cjs/data-structures/base/iterable-entry-base.js +10 -10
- package/dist/cjs/data-structures/base/iterable-entry-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +31 -32
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +43 -44
- package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +23 -24
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +71 -64
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js +2 -2
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +534 -402
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +669 -598
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +72 -65
- package/dist/cjs/data-structures/binary-tree/bst.js +115 -113
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +21 -24
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +40 -39
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/segment-tree.d.ts +2 -2
- package/dist/cjs/data-structures/binary-tree/segment-tree.js +2 -2
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +28 -31
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +44 -43
- package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/cjs/data-structures/graph/abstract-graph.js +7 -4
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +2 -2
- package/dist/cjs/data-structures/graph/directed-graph.js +4 -2
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +2 -2
- package/dist/cjs/data-structures/hash/hash-map.d.ts +2 -2
- package/dist/cjs/data-structures/hash/hash-map.js +1 -1
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.js +3 -3
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +7 -7
- 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 +2 -2
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +6 -6
- 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 +2 -2
- package/dist/cjs/data-structures/matrix/matrix.d.ts +2 -2
- package/dist/cjs/data-structures/matrix/navigator.d.ts +2 -2
- package/dist/cjs/data-structures/matrix/navigator.js +4 -2
- package/dist/cjs/data-structures/matrix/navigator.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +3 -3
- package/dist/cjs/data-structures/queue/deque.js +29 -29
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +2 -2
- package/dist/cjs/data-structures/tree/tree.js +2 -0
- package/dist/cjs/data-structures/tree/tree.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +2 -2
- package/dist/cjs/data-structures/trie/trie.js +1 -1
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/interfaces/binary-tree.d.ts +2 -2
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.d.ts +2 -4
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.js +0 -6
- package/dist/cjs/types/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/mjs/constants/index.d.ts +4 -0
- package/dist/mjs/constants/index.js +5 -0
- package/dist/mjs/data-structures/base/iterable-element-base.d.ts +8 -1
- package/dist/mjs/data-structures/base/iterable-element-base.js +10 -1
- package/dist/mjs/data-structures/base/iterable-entry-base.d.ts +8 -1
- package/dist/mjs/data-structures/base/iterable-entry-base.js +10 -10
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +31 -32
- package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +44 -44
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +23 -24
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +72 -64
- package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.js +2 -2
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +534 -402
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +668 -592
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +72 -65
- package/dist/mjs/data-structures/binary-tree/bst.js +116 -112
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +21 -24
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +41 -38
- package/dist/mjs/data-structures/binary-tree/segment-tree.d.ts +2 -2
- package/dist/mjs/data-structures/binary-tree/segment-tree.js +2 -2
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +28 -31
- package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +45 -42
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +2 -2
- package/dist/mjs/data-structures/graph/abstract-graph.js +7 -4
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +2 -2
- package/dist/mjs/data-structures/graph/directed-graph.js +4 -2
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +2 -2
- package/dist/mjs/data-structures/hash/hash-map.d.ts +2 -2
- package/dist/mjs/data-structures/hash/hash-map.js +1 -1
- package/dist/mjs/data-structures/heap/heap.js +3 -3
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +7 -7
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +6 -6
- package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +2 -2
- package/dist/mjs/data-structures/matrix/matrix.d.ts +2 -2
- package/dist/mjs/data-structures/matrix/navigator.d.ts +2 -2
- package/dist/mjs/data-structures/matrix/navigator.js +4 -2
- package/dist/mjs/data-structures/queue/deque.d.ts +3 -3
- package/dist/mjs/data-structures/queue/deque.js +29 -29
- package/dist/mjs/data-structures/queue/queue.d.ts +1 -1
- package/dist/mjs/data-structures/stack/stack.d.ts +2 -2
- package/dist/mjs/data-structures/tree/tree.js +2 -0
- package/dist/mjs/data-structures/trie/trie.d.ts +2 -2
- package/dist/mjs/data-structures/trie/trie.js +1 -1
- package/dist/mjs/index.d.ts +1 -0
- package/dist/mjs/index.js +1 -0
- package/dist/mjs/interfaces/binary-tree.d.ts +2 -2
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.d.ts +2 -4
- package/dist/mjs/types/data-structures/binary-tree/binary-tree.js +1 -5
- package/dist/umd/data-structure-typed.js +1344 -1614
- package/dist/umd/data-structure-typed.min.js +10 -3
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/eslint.config.mjs +69 -0
- package/package.json +30 -28
- package/src/constants/index.ts +4 -0
- package/src/data-structures/base/iterable-element-base.ts +11 -1
- package/src/data-structures/base/iterable-entry-base.ts +11 -19
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +47 -50
- package/src/data-structures/binary-tree/avl-tree.ts +69 -71
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +698 -726
- package/src/data-structures/binary-tree/bst.ts +123 -129
- package/src/data-structures/binary-tree/rb-tree.ts +44 -46
- package/src/data-structures/binary-tree/segment-tree.ts +2 -2
- package/src/data-structures/binary-tree/tree-multi-map.ts +48 -49
- package/src/data-structures/graph/abstract-graph.ts +6 -6
- package/src/data-structures/graph/directed-graph.ts +4 -4
- package/src/data-structures/graph/undirected-graph.ts +2 -2
- package/src/data-structures/hash/hash-map.ts +3 -3
- package/src/data-structures/heap/heap.ts +3 -3
- package/src/data-structures/linked-list/doubly-linked-list.ts +9 -9
- package/src/data-structures/linked-list/singly-linked-list.ts +8 -8
- package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
- package/src/data-structures/matrix/matrix.ts +2 -2
- package/src/data-structures/matrix/navigator.ts +4 -4
- package/src/data-structures/queue/deque.ts +31 -31
- package/src/data-structures/queue/queue.ts +1 -1
- package/src/data-structures/stack/stack.ts +2 -2
- package/src/data-structures/trie/trie.ts +3 -3
- package/src/index.ts +1 -0
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +3 -5
- package/test/config.ts +1 -7
- package/test/integration/all-in-one.test.ts +2 -2
- package/test/integration/avl-tree.test.ts +3 -3
- package/test/integration/bst.test.ts +19 -18
- package/test/integration/heap.test.js +6 -1
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +39 -39
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +140 -112
- package/test/unit/data-structures/binary-tree/bst.test.ts +41 -13
- package/test/unit/data-structures/binary-tree/overall.test.ts +0 -6
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +6 -6
- package/test/unit/data-structures/binary-tree/segment-tree.test.ts +88 -34
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +42 -42
- package/test/unit/data-structures/graph/abstract-graph.test.ts +1 -1
- package/test/unit/data-structures/graph/directed-graph.test.ts +4 -4
- package/test/unit/data-structures/graph/undirected-graph.test.ts +14 -2
- package/test/unit/data-structures/hash/hash-map.test.ts +1 -1
- package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +1 -1
- package/test/unit/data-structures/matrix/navigator.test.ts +2 -2
- package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +1 -1
- package/test/unit/data-structures/priority-queue/priority-queue.test.ts +4 -4
- package/test/unit/data-structures/stack/stack.test.ts +6 -0
- package/test/unit/unrestricted-interconversion.test.ts +24 -24
- package/test/utils/big-o.ts +5 -4
- package/.eslintrc.js +0 -64
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* data-structure-typed
|
|
3
3
|
*
|
|
4
|
-
* @author
|
|
5
|
-
* @copyright Copyright (c) 2022
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { BST, BSTNode } from './bst';
|
|
@@ -49,7 +49,7 @@ export class AVLTree extends BST {
|
|
|
49
49
|
/**
|
|
50
50
|
* This is a constructor function for an AVLTree class that initializes the tree with keys, nodes,
|
|
51
51
|
* entries, or raw elements.
|
|
52
|
-
* @param
|
|
52
|
+
* @param keysOrNodesOrEntriesOrRaws - The `keysOrNodesOrEntriesOrRaws` parameter is an
|
|
53
53
|
* iterable object that can contain either keys, nodes, entries, or raw elements. These elements will
|
|
54
54
|
* be used to initialize the AVLTree.
|
|
55
55
|
* @param [options] - The `options` parameter is an optional object that can be used to customize the
|
|
@@ -57,10 +57,10 @@ export class AVLTree extends BST {
|
|
|
57
57
|
* keys), `allowDuplicates` (a boolean indicating whether duplicate keys are allowed), and
|
|
58
58
|
* `nodeBuilder` (
|
|
59
59
|
*/
|
|
60
|
-
constructor(
|
|
60
|
+
constructor(keysOrNodesOrEntriesOrRaws = [], options) {
|
|
61
61
|
super([], options);
|
|
62
|
-
if (
|
|
63
|
-
super.addMany(
|
|
62
|
+
if (keysOrNodesOrEntriesOrRaws)
|
|
63
|
+
super.addMany(keysOrNodesOrEntriesOrRaws);
|
|
64
64
|
}
|
|
65
65
|
/**
|
|
66
66
|
* The function creates a new AVL tree node with the given key and value.
|
|
@@ -84,19 +84,20 @@ export class AVLTree extends BST {
|
|
|
84
84
|
createTree(options) {
|
|
85
85
|
return new AVLTree([], {
|
|
86
86
|
iterationType: this.iterationType,
|
|
87
|
-
comparator: this.
|
|
87
|
+
comparator: this._comparator,
|
|
88
|
+
toEntryFn: this._toEntryFn,
|
|
88
89
|
...options
|
|
89
90
|
});
|
|
90
91
|
}
|
|
91
92
|
/**
|
|
92
93
|
* The function checks if the input is an instance of AVLTreeNode.
|
|
93
|
-
* @param {
|
|
94
|
-
* `
|
|
95
|
-
* @returns a boolean value indicating whether the input parameter `
|
|
94
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
95
|
+
* `keyOrNodeOrEntryOrRaw` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
96
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRaw` is
|
|
96
97
|
* an instance of the `AVLTreeNode` class.
|
|
97
98
|
*/
|
|
98
|
-
isNode(
|
|
99
|
-
return
|
|
99
|
+
isNode(keyOrNodeOrEntryOrRaw) {
|
|
100
|
+
return keyOrNodeOrEntryOrRaw instanceof AVLTreeNode;
|
|
100
101
|
}
|
|
101
102
|
/**
|
|
102
103
|
* Time Complexity: O(log n)
|
|
@@ -104,36 +105,36 @@ export class AVLTree extends BST {
|
|
|
104
105
|
*
|
|
105
106
|
* The function overrides the add method of a class and inserts a key-value pair into a data
|
|
106
107
|
* structure, then balances the path.
|
|
107
|
-
* @param {
|
|
108
|
-
* `
|
|
108
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} keyOrNodeOrEntryOrRaw - The parameter
|
|
109
|
+
* `keyOrNodeOrEntryOrRaw` can accept values of type `R`, `BTNKeyOrNodeOrEntry<K, V, NODE>`, or
|
|
109
110
|
* `RawElement`.
|
|
110
111
|
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
111
112
|
* the key or node being added to the data structure.
|
|
112
113
|
* @returns The method is returning a boolean value.
|
|
113
114
|
*/
|
|
114
|
-
add(
|
|
115
|
-
if (
|
|
115
|
+
add(keyOrNodeOrEntryOrRaw, value) {
|
|
116
|
+
if (keyOrNodeOrEntryOrRaw === null)
|
|
116
117
|
return false;
|
|
117
|
-
const inserted = super.add(
|
|
118
|
+
const inserted = super.add(keyOrNodeOrEntryOrRaw, value);
|
|
118
119
|
if (inserted)
|
|
119
|
-
this._balancePath(
|
|
120
|
+
this._balancePath(keyOrNodeOrEntryOrRaw);
|
|
120
121
|
return inserted;
|
|
121
122
|
}
|
|
122
123
|
/**
|
|
123
124
|
* Time Complexity: O(log n)
|
|
124
125
|
* Space Complexity: O(1)
|
|
125
126
|
*
|
|
126
|
-
* The function overrides the delete method
|
|
127
|
-
*
|
|
128
|
-
* @param
|
|
129
|
-
*
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
*
|
|
127
|
+
* The function overrides the delete method in a TypeScript class, performs deletion, and then
|
|
128
|
+
* balances the tree if necessary.
|
|
129
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R | BTNPredicate<NODE>} predicate - The `predicate`
|
|
130
|
+
* parameter in the `override delete` method can be one of the following types:
|
|
131
|
+
* @returns The `delete` method is being overridden in this code snippet. It first calls the `delete`
|
|
132
|
+
* method from the superclass (presumably a parent class) with the provided `predicate`, which could
|
|
133
|
+
* be a key, node, entry, or a custom predicate. The result of this deletion operation is stored in
|
|
134
|
+
* `deletedResults`, which is an array of `BinaryTreeDeleteResult` objects.
|
|
134
135
|
*/
|
|
135
|
-
delete(
|
|
136
|
-
const deletedResults = super.delete(
|
|
136
|
+
delete(predicate) {
|
|
137
|
+
const deletedResults = super.delete(predicate);
|
|
137
138
|
for (const { needBalanced } of deletedResults) {
|
|
138
139
|
if (needBalanced) {
|
|
139
140
|
this._balancePath(needBalanced);
|
|
@@ -299,8 +300,10 @@ export class AVLTree extends BST {
|
|
|
299
300
|
C.right = A;
|
|
300
301
|
}
|
|
301
302
|
this._updateHeight(A);
|
|
302
|
-
|
|
303
|
-
|
|
303
|
+
if (B)
|
|
304
|
+
this._updateHeight(B);
|
|
305
|
+
if (C)
|
|
306
|
+
this._updateHeight(C);
|
|
304
307
|
}
|
|
305
308
|
/**
|
|
306
309
|
* Time Complexity: O(1)
|
|
@@ -338,7 +341,8 @@ export class AVLTree extends BST {
|
|
|
338
341
|
B.left = A;
|
|
339
342
|
}
|
|
340
343
|
this._updateHeight(A);
|
|
341
|
-
|
|
344
|
+
if (B)
|
|
345
|
+
this._updateHeight(B);
|
|
342
346
|
}
|
|
343
347
|
/**
|
|
344
348
|
* Time Complexity: O(1)
|
|
@@ -389,8 +393,10 @@ export class AVLTree extends BST {
|
|
|
389
393
|
if (C)
|
|
390
394
|
C.right = B;
|
|
391
395
|
this._updateHeight(A);
|
|
392
|
-
|
|
393
|
-
|
|
396
|
+
if (B)
|
|
397
|
+
this._updateHeight(B);
|
|
398
|
+
if (C)
|
|
399
|
+
this._updateHeight(C);
|
|
394
400
|
}
|
|
395
401
|
/**
|
|
396
402
|
* Time Complexity: O(log n)
|
|
@@ -398,47 +404,49 @@ export class AVLTree extends BST {
|
|
|
398
404
|
*
|
|
399
405
|
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
400
406
|
* to restore balance in an AVL tree after inserting a node.
|
|
401
|
-
* @param {
|
|
407
|
+
* @param {BTNKeyOrNodeOrEntry<K, V, NODE> | R} node - The `node` parameter can be of type `R` or
|
|
402
408
|
* `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
403
409
|
*/
|
|
404
410
|
_balancePath(node) {
|
|
405
411
|
node = this.ensureNode(node);
|
|
406
|
-
const path = this.getPathToRoot(node, false); // first O(log n) + O(log n)
|
|
412
|
+
const path = this.getPathToRoot(node => node, node, false); // first O(log n) + O(log n)
|
|
407
413
|
for (let i = 0; i < path.length; i++) {
|
|
408
414
|
// second O(log n)
|
|
409
415
|
const A = path[i];
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
if (
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
416
|
+
if (A) {
|
|
417
|
+
// Update Heights: After inserting a node, backtrack from the insertion point to the root node, updating the height of each node along the way.
|
|
418
|
+
this._updateHeight(A); // first O(1)
|
|
419
|
+
// Check Balance: Simultaneously with height updates, check if each node violates the balance property of an AVL tree.
|
|
420
|
+
// 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:
|
|
421
|
+
switch (this._balanceFactor(A) // second O(1)
|
|
422
|
+
) {
|
|
423
|
+
case -2:
|
|
424
|
+
if (A && A.left) {
|
|
425
|
+
if (this._balanceFactor(A.left) <= 0) {
|
|
426
|
+
// second O(1)
|
|
427
|
+
// Left Rotation (LL Rotation): When the inserted node is in the left subtree of the left subtree, causing an imbalance.
|
|
428
|
+
this._balanceLL(A);
|
|
429
|
+
}
|
|
430
|
+
else {
|
|
431
|
+
// Left-Right Rotation (LR Rotation): When the inserted node is in the right subtree of the left subtree, causing an imbalance.
|
|
432
|
+
this._balanceLR(A);
|
|
433
|
+
}
|
|
426
434
|
}
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
435
|
+
break;
|
|
436
|
+
case +2:
|
|
437
|
+
if (A && A.right) {
|
|
438
|
+
if (this._balanceFactor(A.right) >= 0) {
|
|
439
|
+
// Right Rotation (RR Rotation): When the inserted node is in the right subtree of the right subtree, causing an imbalance.
|
|
440
|
+
this._balanceRR(A);
|
|
441
|
+
}
|
|
442
|
+
else {
|
|
443
|
+
// Right-Left Rotation (RL Rotation): When the inserted node is in the left subtree of the right subtree, causing an imbalance.
|
|
444
|
+
this._balanceRL(A);
|
|
445
|
+
}
|
|
434
446
|
}
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
this._balanceRL(A);
|
|
438
|
-
}
|
|
439
|
-
}
|
|
447
|
+
}
|
|
448
|
+
// 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.
|
|
440
449
|
}
|
|
441
|
-
// 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.
|
|
442
450
|
}
|
|
443
451
|
}
|
|
444
452
|
/**
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* data-structure-typed
|
|
3
3
|
*
|
|
4
|
-
* @author
|
|
5
|
-
* @copyright Copyright (c) 2022
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { getMSB } from '../../utils';
|