data-structure-typed 1.18.7 → 1.18.8
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/README.md +154 -366
- package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +29 -32
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +179 -136
- package/dist/data-structures/binary-tree/avl-tree.d.ts +4 -4
- package/dist/data-structures/binary-tree/avl-tree.js +2 -2
- package/dist/data-structures/binary-tree/binary-tree.d.ts +3 -3
- package/dist/data-structures/binary-tree/binary-tree.js +4 -4
- package/dist/data-structures/binary-tree/bst.d.ts +13 -25
- package/dist/data-structures/binary-tree/bst.js +20 -74
- package/dist/data-structures/binary-tree/rb-tree.d.ts +6 -6
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +6 -6
- package/dist/data-structures/binary-tree/tree-multiset.js +7 -14
- package/dist/data-structures/graph/abstract-graph.d.ts +11 -9
- package/dist/data-structures/graph/abstract-graph.js +39 -22
- package/dist/data-structures/graph/directed-graph.d.ts +17 -15
- package/dist/data-structures/graph/directed-graph.js +47 -42
- package/dist/data-structures/graph/undirected-graph.d.ts +7 -7
- package/dist/data-structures/graph/undirected-graph.js +48 -48
- package/dist/data-structures/interfaces/abstract-binary-tree.d.ts +2 -3
- package/dist/data-structures/interfaces/abstract-graph.d.ts +2 -4
- package/dist/data-structures/interfaces/avl-tree.d.ts +1 -1
- package/dist/data-structures/interfaces/bst.d.ts +2 -2
- package/dist/data-structures/interfaces/rb-tree.d.ts +2 -2
- package/dist/data-structures/interfaces/tree-multiset.d.ts +7 -1
- package/dist/data-structures/tree/tree.d.ts +1 -4
- package/dist/data-structures/tree/tree.js +1 -12
- package/dist/data-structures/types/abstract-binary-tree.d.ts +9 -5
- package/dist/data-structures/types/abstract-binary-tree.js +9 -5
- package/dist/data-structures/types/avl-tree.d.ts +1 -1
- package/dist/data-structures/types/binary-tree.d.ts +1 -1
- package/dist/data-structures/types/bst.d.ts +1 -1
- package/dist/data-structures/types/bst.js +1 -1
- package/dist/data-structures/types/directed-graph.js +1 -1
- package/dist/data-structures/types/helpers.d.ts +1 -8
- package/dist/data-structures/types/rb-tree.d.ts +2 -0
- package/dist/data-structures/types/rb-tree.js +1 -1
- package/dist/data-structures/types/tree-multiset.d.ts +4 -4
- package/dist/utils/types/utils.d.ts +18 -0
- package/dist/utils/utils.d.ts +6 -1
- package/dist/utils/utils.js +21 -1
- package/package.json +2 -2
- package/backup/recursive-type/src/assets/complexities-diff.jpg +0 -0
- package/backup/recursive-type/src/assets/data-structure-complexities.jpg +0 -0
- package/backup/recursive-type/src/assets/logo.png +0 -0
- package/backup/recursive-type/src/assets/overview-diagram-of-data-structures.png +0 -0
- package/backup/recursive-type/src/data-structures/binary-tree/aa-tree.ts +0 -3
- package/backup/recursive-type/src/data-structures/binary-tree/avl-tree.ts +0 -288
- package/backup/recursive-type/src/data-structures/binary-tree/b-tree.ts +0 -3
- package/backup/recursive-type/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -78
- package/backup/recursive-type/src/data-structures/binary-tree/binary-tree.ts +0 -1502
- package/backup/recursive-type/src/data-structures/binary-tree/bst.ts +0 -503
- package/backup/recursive-type/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
- package/backup/recursive-type/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
- package/backup/recursive-type/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
- package/backup/recursive-type/src/data-structures/binary-tree/index.ts +0 -11
- package/backup/recursive-type/src/data-structures/binary-tree/rb-tree.ts +0 -110
- package/backup/recursive-type/src/data-structures/binary-tree/segment-tree.ts +0 -243
- package/backup/recursive-type/src/data-structures/binary-tree/splay-tree.ts +0 -3
- package/backup/recursive-type/src/data-structures/binary-tree/tree-multiset.ts +0 -55
- package/backup/recursive-type/src/data-structures/binary-tree/two-three-tree.ts +0 -3
- package/backup/recursive-type/src/data-structures/diagrams/README.md +0 -5
- package/backup/recursive-type/src/data-structures/graph/abstract-graph.ts +0 -985
- package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/mst.jpg +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
- package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan.webp +0 -0
- package/backup/recursive-type/src/data-structures/graph/directed-graph.ts +0 -478
- package/backup/recursive-type/src/data-structures/graph/index.ts +0 -3
- package/backup/recursive-type/src/data-structures/graph/undirected-graph.ts +0 -293
- package/backup/recursive-type/src/data-structures/hash/coordinate-map.ts +0 -67
- package/backup/recursive-type/src/data-structures/hash/coordinate-set.ts +0 -56
- package/backup/recursive-type/src/data-structures/hash/hash-table.ts +0 -3
- package/backup/recursive-type/src/data-structures/hash/index.ts +0 -6
- package/backup/recursive-type/src/data-structures/hash/pair.ts +0 -3
- package/backup/recursive-type/src/data-structures/hash/tree-map.ts +0 -3
- package/backup/recursive-type/src/data-structures/hash/tree-set.ts +0 -3
- package/backup/recursive-type/src/data-structures/heap/heap.ts +0 -176
- package/backup/recursive-type/src/data-structures/heap/index.ts +0 -3
- package/backup/recursive-type/src/data-structures/heap/max-heap.ts +0 -31
- package/backup/recursive-type/src/data-structures/heap/min-heap.ts +0 -34
- package/backup/recursive-type/src/data-structures/index.ts +0 -15
- package/backup/recursive-type/src/data-structures/interfaces/abstract-graph.ts +0 -42
- package/backup/recursive-type/src/data-structures/interfaces/avl-tree.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/binary-tree.ts +0 -56
- package/backup/recursive-type/src/data-structures/interfaces/bst.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/directed-graph.ts +0 -15
- package/backup/recursive-type/src/data-structures/interfaces/doubly-linked-list.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/heap.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/index.ts +0 -13
- package/backup/recursive-type/src/data-structures/interfaces/navigator.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/priority-queue.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/segment-tree.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/singly-linked-list.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/tree-multiset.ts +0 -1
- package/backup/recursive-type/src/data-structures/interfaces/undirected-graph.ts +0 -3
- package/backup/recursive-type/src/data-structures/linked-list/doubly-linked-list.ts +0 -573
- package/backup/recursive-type/src/data-structures/linked-list/index.ts +0 -3
- package/backup/recursive-type/src/data-structures/linked-list/singly-linked-list.ts +0 -490
- package/backup/recursive-type/src/data-structures/linked-list/skip-linked-list.ts +0 -3
- package/backup/recursive-type/src/data-structures/matrix/index.ts +0 -4
- package/backup/recursive-type/src/data-structures/matrix/matrix.ts +0 -27
- package/backup/recursive-type/src/data-structures/matrix/matrix2d.ts +0 -208
- package/backup/recursive-type/src/data-structures/matrix/navigator.ts +0 -122
- package/backup/recursive-type/src/data-structures/matrix/vector2d.ts +0 -316
- package/backup/recursive-type/src/data-structures/priority-queue/index.ts +0 -3
- package/backup/recursive-type/src/data-structures/priority-queue/max-priority-queue.ts +0 -49
- package/backup/recursive-type/src/data-structures/priority-queue/min-priority-queue.ts +0 -50
- package/backup/recursive-type/src/data-structures/priority-queue/priority-queue.ts +0 -354
- package/backup/recursive-type/src/data-structures/queue/deque.ts +0 -251
- package/backup/recursive-type/src/data-structures/queue/index.ts +0 -2
- package/backup/recursive-type/src/data-structures/queue/queue.ts +0 -120
- package/backup/recursive-type/src/data-structures/stack/index.ts +0 -1
- package/backup/recursive-type/src/data-structures/stack/stack.ts +0 -98
- package/backup/recursive-type/src/data-structures/tree/index.ts +0 -1
- package/backup/recursive-type/src/data-structures/tree/tree.ts +0 -80
- package/backup/recursive-type/src/data-structures/trie/index.ts +0 -1
- package/backup/recursive-type/src/data-structures/trie/trie.ts +0 -227
- package/backup/recursive-type/src/data-structures/types/abstract-graph.ts +0 -5
- package/backup/recursive-type/src/data-structures/types/avl-tree.ts +0 -8
- package/backup/recursive-type/src/data-structures/types/binary-tree.ts +0 -10
- package/backup/recursive-type/src/data-structures/types/bst.ts +0 -6
- package/backup/recursive-type/src/data-structures/types/directed-graph.ts +0 -8
- package/backup/recursive-type/src/data-structures/types/doubly-linked-list.ts +0 -1
- package/backup/recursive-type/src/data-structures/types/heap.ts +0 -5
- package/backup/recursive-type/src/data-structures/types/index.ts +0 -12
- package/backup/recursive-type/src/data-structures/types/navigator.ts +0 -13
- package/backup/recursive-type/src/data-structures/types/priority-queue.ts +0 -9
- package/backup/recursive-type/src/data-structures/types/segment-tree.ts +0 -1
- package/backup/recursive-type/src/data-structures/types/singly-linked-list.ts +0 -1
- package/backup/recursive-type/src/data-structures/types/tree-multiset.ts +0 -1
- package/backup/recursive-type/src/index.ts +0 -1
- package/backup/recursive-type/src/utils/index.ts +0 -2
- package/backup/recursive-type/src/utils/types/index.ts +0 -1
- package/backup/recursive-type/src/utils/types/utils.ts +0 -6
- package/backup/recursive-type/src/utils/utils.ts +0 -78
- package/docs/.nojekyll +0 -1
- package/docs/assets/highlight.css +0 -92
- package/docs/assets/main.js +0 -58
- package/docs/assets/search.js +0 -1
- package/docs/assets/style.css +0 -1367
- package/docs/classes/AVLTree.html +0 -2451
- package/docs/classes/AVLTreeNode.html +0 -499
- package/docs/classes/AaTree.html +0 -172
- package/docs/classes/AbstractBinaryTree.html +0 -2118
- package/docs/classes/AbstractBinaryTreeNode.html +0 -524
- package/docs/classes/AbstractEdge.html +0 -295
- package/docs/classes/AbstractGraph.html +0 -1043
- package/docs/classes/AbstractVertex.html +0 -258
- package/docs/classes/ArrayDeque.html +0 -439
- package/docs/classes/BST.html +0 -2297
- package/docs/classes/BSTNode.html +0 -503
- package/docs/classes/BTree.html +0 -172
- package/docs/classes/BinaryIndexedTree.html +0 -341
- package/docs/classes/BinaryTree.html +0 -2133
- package/docs/classes/BinaryTreeNode.html +0 -501
- package/docs/classes/Character.html +0 -220
- package/docs/classes/CoordinateMap.html +0 -483
- package/docs/classes/CoordinateSet.html +0 -444
- package/docs/classes/Deque.html +0 -975
- package/docs/classes/DirectedEdge.html +0 -366
- package/docs/classes/DirectedGraph.html +0 -1443
- package/docs/classes/DirectedVertex.html +0 -254
- package/docs/classes/DoublyLinkedList.html +0 -968
- package/docs/classes/DoublyLinkedListNode.html +0 -297
- package/docs/classes/HashTable.html +0 -172
- package/docs/classes/Heap.html +0 -423
- package/docs/classes/HeapItem.html +0 -255
- package/docs/classes/Matrix2D.html +0 -502
- package/docs/classes/MatrixNTI2D.html +0 -240
- package/docs/classes/MaxHeap.html +0 -436
- package/docs/classes/MaxPriorityQueue.html +0 -836
- package/docs/classes/MinHeap.html +0 -437
- package/docs/classes/MinPriorityQueue.html +0 -838
- package/docs/classes/Navigator.html +0 -313
- package/docs/classes/ObjectDeque.html +0 -455
- package/docs/classes/Pair.html +0 -172
- package/docs/classes/PriorityQueue.html +0 -760
- package/docs/classes/Queue.html +0 -392
- package/docs/classes/RBTree.html +0 -2388
- package/docs/classes/RBTreeNode.html +0 -516
- package/docs/classes/SegmentTree.html +0 -434
- package/docs/classes/SegmentTreeNode.html +0 -357
- package/docs/classes/SinglyLinkedList.html +0 -788
- package/docs/classes/SinglyLinkedListNode.html +0 -270
- package/docs/classes/SkipLinkedList.html +0 -172
- package/docs/classes/SplayTree.html +0 -172
- package/docs/classes/Stack.html +0 -368
- package/docs/classes/TreeMap.html +0 -172
- package/docs/classes/TreeMultiSet.html +0 -2297
- package/docs/classes/TreeMultiSetNode.html +0 -499
- package/docs/classes/TreeNode.html +0 -343
- package/docs/classes/TreeSet.html +0 -172
- package/docs/classes/Trie.html +0 -372
- package/docs/classes/TrieNode.html +0 -280
- package/docs/classes/TwoThreeTree.html +0 -172
- package/docs/classes/UndirectedEdge.html +0 -337
- package/docs/classes/UndirectedGraph.html +0 -1210
- package/docs/classes/UndirectedVertex.html +0 -254
- package/docs/classes/Vector2D.html +0 -805
- package/docs/enums/CP.html +0 -181
- package/docs/enums/FamilyPosition.html +0 -181
- package/docs/enums/LoopType.html +0 -182
- package/docs/enums/RBColor.html +0 -174
- package/docs/enums/TopologicalProperty.html +0 -181
- package/docs/index.html +0 -645
- package/docs/interfaces/IAVLTree.html +0 -1378
- package/docs/interfaces/IAVLTreeNode.html +0 -405
- package/docs/interfaces/IAbstractBinaryTree.html +0 -1124
- package/docs/interfaces/IAbstractBinaryTreeNode.html +0 -384
- package/docs/interfaces/IAbstractGraph.html +0 -433
- package/docs/interfaces/IBST.html +0 -1271
- package/docs/interfaces/IBSTNode.html +0 -408
- package/docs/interfaces/IDirectedGraph.html +0 -572
- package/docs/interfaces/IUNDirectedGraph.html +0 -465
- package/docs/modules.html +0 -262
- package/docs/types/AVLTreeOptions.html +0 -150
- package/docs/types/AbstractBinaryTreeNodeProperties.html +0 -152
- package/docs/types/AbstractBinaryTreeNodeProperty.html +0 -152
- package/docs/types/AbstractBinaryTreeOptions.html +0 -156
- package/docs/types/AbstractRecursiveBinaryTreeNode.html +0 -152
- package/docs/types/BSTComparator.html +0 -162
- package/docs/types/BSTOptions.html +0 -152
- package/docs/types/BinaryTreeDeletedResult.html +0 -159
- package/docs/types/BinaryTreeNodeId.html +0 -147
- package/docs/types/BinaryTreeNodePropertyName.html +0 -147
- package/docs/types/BinaryTreeOptions.html +0 -150
- package/docs/types/DFSOrderPattern.html +0 -147
- package/docs/types/DijkstraResult.html +0 -167
- package/docs/types/Direction.html +0 -147
- package/docs/types/EdgeId.html +0 -147
- package/docs/types/HeapOptions.html +0 -168
- package/docs/types/IdObject.html +0 -157
- package/docs/types/KeyValObject.html +0 -152
- package/docs/types/NavigatorParams.html +0 -181
- package/docs/types/NodeOrPropertyName.html +0 -147
- package/docs/types/PriorityQueueComparator.html +0 -167
- package/docs/types/PriorityQueueDFSOrderPattern.html +0 -147
- package/docs/types/PriorityQueueOptions.html +0 -161
- package/docs/types/RBTreeOptions.html +0 -150
- package/docs/types/RecursiveAVLTreeNode.html +0 -152
- package/docs/types/RecursiveBSTNode.html +0 -152
- package/docs/types/RecursiveBinaryTreeNode.html +0 -152
- package/docs/types/RecursiveTreeMultiSetNode.html +0 -152
- package/docs/types/SegmentTreeNodeVal.html +0 -147
- package/docs/types/TopologicalStatus.html +0 -147
- package/docs/types/TreeMultiSetOptions.html +0 -152
- package/docs/types/Turning.html +0 -147
- package/docs/types/VertexId.html +0 -147
- package/notes/bst.test.ts +0 -181
- package/notes/note.md +0 -34
package/dist/utils/utils.js
CHANGED
|
@@ -61,7 +61,7 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
|
61
61
|
return to.concat(ar || Array.prototype.slice.call(from));
|
|
62
62
|
};
|
|
63
63
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
64
|
-
exports.trampolineAsync = exports.trampoline = exports.toThunk = exports.isThunk = exports.THUNK_SYMBOL = exports.arrayRemove = exports.uuidV4 = void 0;
|
|
64
|
+
exports.isNumber = exports.isObjectWithNumberId = exports.isObjectWithNonNumberId = exports.isObjectWithoutId = exports.isNonNumberNonObjectButDefined = exports.trampolineAsync = exports.trampoline = exports.toThunk = exports.isThunk = exports.THUNK_SYMBOL = exports.arrayRemove = exports.uuidV4 = void 0;
|
|
65
65
|
var uuidV4 = function () {
|
|
66
66
|
return 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'.replace(/[x]/g, function (c) {
|
|
67
67
|
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
|
|
@@ -211,3 +211,23 @@ exports.trampolineAsync = trampolineAsync;
|
|
|
211
211
|
// }
|
|
212
212
|
// }
|
|
213
213
|
// }
|
|
214
|
+
function isNonNumberNonObjectButDefined(val) {
|
|
215
|
+
return typeof val !== 'number' && typeof val !== 'object' && val !== undefined;
|
|
216
|
+
}
|
|
217
|
+
exports.isNonNumberNonObjectButDefined = isNonNumberNonObjectButDefined;
|
|
218
|
+
function isObjectWithoutId(val) {
|
|
219
|
+
return typeof val === 'object' && !('id' in val);
|
|
220
|
+
}
|
|
221
|
+
exports.isObjectWithoutId = isObjectWithoutId;
|
|
222
|
+
function isObjectWithNonNumberId(val) {
|
|
223
|
+
return typeof val === 'object' && 'id' in val && typeof val.id !== 'number';
|
|
224
|
+
}
|
|
225
|
+
exports.isObjectWithNonNumberId = isObjectWithNonNumberId;
|
|
226
|
+
function isObjectWithNumberId(val) {
|
|
227
|
+
return typeof val === 'object' && 'id' in val && typeof val.id === 'number';
|
|
228
|
+
}
|
|
229
|
+
exports.isObjectWithNumberId = isObjectWithNumberId;
|
|
230
|
+
function isNumber(val) {
|
|
231
|
+
return typeof val === 'number';
|
|
232
|
+
}
|
|
233
|
+
exports.isNumber = isNumber;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "data-structure-typed",
|
|
3
|
-
"version": "1.18.
|
|
3
|
+
"version": "1.18.8",
|
|
4
4
|
"description": "Explore our comprehensive Javascript Data Structure / TypeScript Data Structure Library, meticulously crafted to empower developers with a versatile set of essential data structures. Our library includes a wide range of data structures, such as Binary Tree, AVL Tree, Binary Search Tree (BST), Tree Multiset, Segment Tree, Binary Indexed Tree, Graph, Directed Graph, Undirected Graph, Singly Linked List, Hash, CoordinateSet, CoordinateMap, Heap, Doubly Linked List, Priority Queue, Max Priority Queue, Min Priority Queue, Queue, ObjectDeque, ArrayDeque, Stack, and Trie. Each data structure is thoughtfully designed and implemented using TypeScript to provide efficient, reliable, and easy-to-use solutions for your programming needs. Whether you're optimizing algorithms, managing data, or enhancing performance, our TypeScript Data Structure Library is your go-to resource. Elevate your coding experience with these fundamental building blocks for software development.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -62,6 +62,6 @@
|
|
|
62
62
|
"jest": "^29.6.2",
|
|
63
63
|
"ts-jest": "^29.1.1",
|
|
64
64
|
"typedoc": "^0.24.8",
|
|
65
|
-
"typescript": "^
|
|
65
|
+
"typescript": "^4.9.5"
|
|
66
66
|
}
|
|
67
67
|
}
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -1,288 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* data-structure-typed
|
|
3
|
-
*
|
|
4
|
-
* @author Tyler Zeng
|
|
5
|
-
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
-
* @license MIT License
|
|
7
|
-
*/
|
|
8
|
-
import {BST, BSTNode} from './bst';
|
|
9
|
-
import type {AVLTreeDeleted, BinaryTreeNodeId, RecursiveAVLTreeNode} from '../types';
|
|
10
|
-
import {IBinaryTreeNode} from '../interfaces';
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
export class AVLTreeNode<T, FAMILY extends AVLTreeNode<T, FAMILY > = RecursiveAVLTreeNode<T>> extends BSTNode<T, FAMILY> implements IBinaryTreeNode<T, FAMILY> {
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode<number>> extends BST<N> {
|
|
18
|
-
|
|
19
|
-
override _createNode(id: BinaryTreeNodeId, val: N['val'], count?: number): N {
|
|
20
|
-
const node = new AVLTreeNode<N['val'], N>(id, val, count);
|
|
21
|
-
return node as N;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* The function overrides the add method of a Binary Search Tree to insert a node with a given id and value, and then
|
|
26
|
-
* balances the tree.
|
|
27
|
-
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to add or
|
|
28
|
-
* update in the AVL tree.
|
|
29
|
-
* @param {N | null} val - The `val` parameter represents the value that you want to assign to the node with the given
|
|
30
|
-
* `id`. It can be of type `N` (the generic type) or `null`.
|
|
31
|
-
* @param {number} [count] - The `count` parameter is an optional parameter of type `number`. It represents the number
|
|
32
|
-
* of times the value `val` should be inserted into the AVL tree. If the `count` parameter is not provided, it defaults
|
|
33
|
-
* to `1`, indicating that the value should be inserted once.
|
|
34
|
-
* @returns The method is returning either an N object or null.
|
|
35
|
-
*/
|
|
36
|
-
override add(id: BinaryTreeNodeId, val: N['val'] | null, count?: number): N | null {
|
|
37
|
-
const inserted = super.add(id, val, count);
|
|
38
|
-
if (inserted) this.balancePath(inserted);
|
|
39
|
-
return inserted;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* The function overrides the remove method of the Binary Search Tree class, performs the removal operation, and
|
|
44
|
-
* then balances the tree if necessary.
|
|
45
|
-
* @param {BinaryTreeNodeId} id - The `id` parameter represents the identifier of the binary tree node that needs to be
|
|
46
|
-
* removed from the AVL tree.
|
|
47
|
-
* @param {boolean} [isUpdateAllLeftSum] - The `isUpdateAllLeftSum` parameter is an optional boolean parameter that
|
|
48
|
-
* determines whether the left sum of all nodes in the AVL tree should be updated after removing a node. If
|
|
49
|
-
* `isUpdateAllLeftSum` is set to `true`, the left sum of all nodes will be recalculated.
|
|
50
|
-
* @returns The method is returning an array of `AVLTreeDeleted<N>` objects.
|
|
51
|
-
*/
|
|
52
|
-
override remove(id: BinaryTreeNodeId, isUpdateAllLeftSum?: boolean): AVLTreeDeleted<N>[] {
|
|
53
|
-
const deletedResults = super.remove(id, isUpdateAllLeftSum);
|
|
54
|
-
for (const {needBalanced} of deletedResults) {
|
|
55
|
-
if (needBalanced) {
|
|
56
|
-
this.balancePath(needBalanced);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
return deletedResults;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* The balance factor of a given AVL tree node is calculated by subtracting the height of its left subtree from the
|
|
64
|
-
* height of its right subtree.
|
|
65
|
-
* @param node - The parameter "node" is of type N, which represents a node in an AVL tree.
|
|
66
|
-
* @returns The balance factor of the given AVL tree node.
|
|
67
|
-
*/
|
|
68
|
-
balanceFactor(node: N): number {
|
|
69
|
-
if (!node.right) // node has no right subtree
|
|
70
|
-
return -node.height;
|
|
71
|
-
else if (!node.left) // node has no left subtree
|
|
72
|
-
return +node.height;
|
|
73
|
-
else
|
|
74
|
-
return node.right.height - node.left.height;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* The function updates the height of a node in an AVL tree based on the heights of its left and right subtrees.
|
|
79
|
-
* @param node - The parameter `node` is an AVLTreeNode object, which represents a node in an AVL tree.
|
|
80
|
-
*/
|
|
81
|
-
updateHeight(node: N): void {
|
|
82
|
-
if (!node.left && !node.right) // node is a leaf
|
|
83
|
-
node.height = 0;
|
|
84
|
-
else if (!node.left) {
|
|
85
|
-
// node has no left subtree
|
|
86
|
-
const rightHeight = node.right ? node.right.height : 0;
|
|
87
|
-
node.height = 1 + rightHeight;
|
|
88
|
-
} else if (!node.right) // node has no right subtree
|
|
89
|
-
node.height = 1 + node.left.height;
|
|
90
|
-
else
|
|
91
|
-
node.height = 1 + Math.max(node.right.height, node.left.height);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* The `balancePath` function balances the AVL tree by performing appropriate rotations based on the balance factor of
|
|
96
|
-
* each node in the path from the given node to the root.
|
|
97
|
-
* @param node - The `node` parameter is an AVLTreeNode object, which represents a node in an AVL tree.
|
|
98
|
-
*/
|
|
99
|
-
balancePath(node: N): void {
|
|
100
|
-
const path = this.getPathToRoot(node);
|
|
101
|
-
for (let i = path.length - 1; i >= 0; i--) {
|
|
102
|
-
const A = path[i];
|
|
103
|
-
this.updateHeight(A);
|
|
104
|
-
switch (this.balanceFactor(A)) {
|
|
105
|
-
case -2:
|
|
106
|
-
if (A && A.left) {
|
|
107
|
-
if (this.balanceFactor(A.left) <= 0) {
|
|
108
|
-
this.balanceLL(A); // Perform LL rotation
|
|
109
|
-
} else {
|
|
110
|
-
this.balanceLR(A); // Perform LR rotation
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
break;
|
|
114
|
-
case +2:
|
|
115
|
-
if (A && A.right) {
|
|
116
|
-
if (this.balanceFactor(A.right) >= 0) {
|
|
117
|
-
this.balanceRR(A); // Perform RR rotation
|
|
118
|
-
} else {
|
|
119
|
-
this.balanceRL(A); // Perform RL rotation
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* The `balanceLL` function performs a left-left rotation on an AVL tree to balance it.
|
|
128
|
-
* @param A - The parameter A is an AVLTreeNode object.
|
|
129
|
-
*/
|
|
130
|
-
balanceLL(A: N): void {
|
|
131
|
-
const parentOfA = A.parent;
|
|
132
|
-
const B = A.left; // A is left-heavy and B is left-heavy
|
|
133
|
-
A.parent = B;
|
|
134
|
-
if (B && B.right) {
|
|
135
|
-
B.right.parent = A;
|
|
136
|
-
}
|
|
137
|
-
if (B) B.parent = parentOfA;
|
|
138
|
-
if (A === this.root) {
|
|
139
|
-
if (B) this._setRoot(B);
|
|
140
|
-
} else {
|
|
141
|
-
if (parentOfA?.left === A) {
|
|
142
|
-
parentOfA.left = B;
|
|
143
|
-
} else {
|
|
144
|
-
if (parentOfA) parentOfA.right = B;
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
if (B) {
|
|
149
|
-
A.left = B.right; // Make T2 the left subtree of A
|
|
150
|
-
B.right = A; // Make A the left child of B
|
|
151
|
-
}
|
|
152
|
-
this.updateHeight(A);
|
|
153
|
-
if (B) this.updateHeight(B);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* The `balanceLR` function performs a left-right rotation to balance an AVL tree.
|
|
158
|
-
* @param A - A is an AVLTreeNode object.
|
|
159
|
-
*/
|
|
160
|
-
balanceLR(A: N): void {
|
|
161
|
-
const parentOfA = A.parent;
|
|
162
|
-
const B = A.left; // A is left-heavy
|
|
163
|
-
let C = null;
|
|
164
|
-
if (B) {
|
|
165
|
-
C = B.right;// B is right-heavy
|
|
166
|
-
}
|
|
167
|
-
if (A) A.parent = C;
|
|
168
|
-
if (B) B.parent = C;
|
|
169
|
-
|
|
170
|
-
if (C) {
|
|
171
|
-
if (C.left) {
|
|
172
|
-
C.left.parent = B;
|
|
173
|
-
}
|
|
174
|
-
if (C.right) {
|
|
175
|
-
C.right.parent = A;
|
|
176
|
-
}
|
|
177
|
-
C.parent = parentOfA;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
if (A === this.root) {
|
|
181
|
-
if (C) this._setRoot(C);
|
|
182
|
-
} else {
|
|
183
|
-
if (parentOfA) {
|
|
184
|
-
if (parentOfA.left === A) {
|
|
185
|
-
parentOfA.left = C;
|
|
186
|
-
} else {
|
|
187
|
-
parentOfA.right = C;
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
if (C) {
|
|
193
|
-
A.left = C.right; // Make T3 the left subtree of A
|
|
194
|
-
if (B) B.right = C.left; // Make T2 the right subtree of B
|
|
195
|
-
C.left = B;
|
|
196
|
-
C.right = A;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
this.updateHeight(A); // Adjust heights
|
|
200
|
-
B && this.updateHeight(B);
|
|
201
|
-
C && this.updateHeight(C);
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* The `balanceRR` function performs a right-right rotation on an AVL tree to balance it.
|
|
206
|
-
* @param A - The parameter A is an AVLTreeNode object.
|
|
207
|
-
*/
|
|
208
|
-
balanceRR(A: N): void {
|
|
209
|
-
const parentOfA = A.parent;
|
|
210
|
-
const B = A.right; // A is right-heavy and B is right-heavy
|
|
211
|
-
A.parent = B;
|
|
212
|
-
if (B) {
|
|
213
|
-
if (B.left) {
|
|
214
|
-
B.left.parent = A;
|
|
215
|
-
}
|
|
216
|
-
B.parent = parentOfA;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
if (A === this.root) {
|
|
220
|
-
if (B) this._setRoot(B);
|
|
221
|
-
} else {
|
|
222
|
-
if (parentOfA) {
|
|
223
|
-
if (parentOfA.left === A) {
|
|
224
|
-
parentOfA.left = B;
|
|
225
|
-
} else {
|
|
226
|
-
parentOfA.right = B;
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
if (B) {
|
|
232
|
-
A.right = B.left; // Make T2 the right subtree of A
|
|
233
|
-
B.left = A;
|
|
234
|
-
}
|
|
235
|
-
this.updateHeight(A);
|
|
236
|
-
B && this.updateHeight(B);
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* The `balanceRL` function performs a right-left rotation to balance an AVL tree.
|
|
241
|
-
* @param A - A is an AVLTreeNode object.
|
|
242
|
-
*/
|
|
243
|
-
balanceRL(A: N): void {
|
|
244
|
-
const parentOfA = A.parent;
|
|
245
|
-
const B = A.right; // A is right-heavy
|
|
246
|
-
let C = null;
|
|
247
|
-
if (B) {
|
|
248
|
-
C = B.left; // B is left-heavy
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
A.parent = C;
|
|
252
|
-
if (B) B.parent = C;
|
|
253
|
-
|
|
254
|
-
if (C) {
|
|
255
|
-
if (C.left) {
|
|
256
|
-
C.left.parent = A;
|
|
257
|
-
}
|
|
258
|
-
if (C.right) {
|
|
259
|
-
C.right.parent = B;
|
|
260
|
-
}
|
|
261
|
-
C.parent = parentOfA;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
if (A === this.root) {
|
|
266
|
-
if (C) this._setRoot(C);
|
|
267
|
-
} else {
|
|
268
|
-
if (parentOfA) {
|
|
269
|
-
if (parentOfA.left === A) {
|
|
270
|
-
parentOfA.left = C;
|
|
271
|
-
} else {
|
|
272
|
-
parentOfA.right = C;
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
if (C) A.right = C.left; // Make T2 the right subtree of A
|
|
278
|
-
if (B && C) B.left = C.right; // Make T3 the left subtree of B
|
|
279
|
-
if (C) C.left = A;
|
|
280
|
-
if (C) C.right = B;
|
|
281
|
-
|
|
282
|
-
this.updateHeight(A); // Adjust heights
|
|
283
|
-
B && this.updateHeight(B);
|
|
284
|
-
C && this.updateHeight(C);
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* data-structure-typed
|
|
3
|
-
*
|
|
4
|
-
* @author Tyler Zeng
|
|
5
|
-
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
-
* @license MIT License
|
|
7
|
-
*/
|
|
8
|
-
export class BinaryIndexedTree {
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* The constructor initializes an array with a specified length and fills it with zeros.
|
|
12
|
-
* @param {number} n - The parameter `n` represents the size of the array that will be used to store the sum tree. The
|
|
13
|
-
* sum tree is a binary tree data structure used to efficiently calculate the sum of a range of elements in an array.
|
|
14
|
-
* The size of the sum tree array is `n + 1` because
|
|
15
|
-
*/
|
|
16
|
-
constructor(n: number) {
|
|
17
|
-
this._sumTree = new Array<number>(n + 1).fill(0);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
private _sumTree: number[];
|
|
21
|
-
|
|
22
|
-
get sumTree(): number[] {
|
|
23
|
-
return this._sumTree;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
static lowBit(x: number) {
|
|
27
|
-
return x & (-x);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* The update function updates the values in a binary indexed tree by adding a delta value to the specified index and
|
|
32
|
-
* its ancestors.
|
|
33
|
-
* @param {number} i - The parameter `i` represents the index of the element in the `_sumTree` array that needs to be
|
|
34
|
-
* updated.
|
|
35
|
-
* @param {number} delta - The "delta" parameter represents the change in value that needs to be added to the element
|
|
36
|
-
* at index "i" in the "_sumTree" array.
|
|
37
|
-
*/
|
|
38
|
-
update(i: number, delta: number) {
|
|
39
|
-
while (i < this._sumTree.length) {
|
|
40
|
-
this._sumTree[i] += delta;
|
|
41
|
-
i += BinaryIndexedTree.lowBit(i);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* The function calculates the prefix sum of an array using a binary indexed tree.
|
|
47
|
-
* @param {number} i - The parameter "i" in the function "getPrefixSum" represents the index of the element in the
|
|
48
|
-
* array for which we want to calculate the prefix sum.
|
|
49
|
-
* @returns The function `getPrefixSum` returns the prefix sum of the elements in the binary indexed tree up to index
|
|
50
|
-
* `i`.
|
|
51
|
-
*/
|
|
52
|
-
getPrefixSum(i: number) {
|
|
53
|
-
let sum = 0;
|
|
54
|
-
while (i > 0) {
|
|
55
|
-
sum += this._sumTree[i];
|
|
56
|
-
i -= BinaryIndexedTree.lowBit(i);
|
|
57
|
-
}
|
|
58
|
-
return sum;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* The function `getRangeSum` calculates the sum of a range of numbers in an array.
|
|
63
|
-
* @param {number} start - The start parameter is the starting index of the range for which we want to calculate the
|
|
64
|
-
* sum.
|
|
65
|
-
* @param {number} end - The "end" parameter represents the ending index of the range for which we want to calculate
|
|
66
|
-
* the sum.
|
|
67
|
-
* @returns the sum of the elements in the range specified by the start and end indices.
|
|
68
|
-
*/
|
|
69
|
-
getRangeSum(start: number, end: number): number {
|
|
70
|
-
if (!(0 <= start && start <= end && end <= this._sumTree.length))
|
|
71
|
-
throw 'Index out of bounds';
|
|
72
|
-
return this.getPrefixSum(end) - this.getPrefixSum(start);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
protected _setSumTree(value: number[]) {
|
|
76
|
-
this._sumTree = value;
|
|
77
|
-
}
|
|
78
|
-
}
|