data-structure-typed 0.8.18 → 1.12.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.dependency-cruiser.js +449 -0
- package/.idea/data-structure-typed.iml +2 -0
- package/.idea/modules.xml +1 -1
- package/README.md +298 -2
- package/dist/data-structures/binary-tree/aa-tree.js +5 -2
- package/dist/data-structures/binary-tree/avl-tree.d.ts +58 -5
- package/dist/data-structures/binary-tree/avl-tree.js +150 -46
- package/dist/data-structures/binary-tree/b-tree.js +5 -2
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +28 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +41 -13
- package/dist/data-structures/binary-tree/binary-tree.d.ts +230 -36
- package/dist/data-structures/binary-tree/binary-tree.js +747 -369
- package/dist/data-structures/binary-tree/bst.d.ts +20 -8
- package/dist/data-structures/binary-tree/bst.js +164 -107
- package/dist/data-structures/binary-tree/rb-tree.js +5 -2
- package/dist/data-structures/binary-tree/segment-tree.d.ts +7 -3
- package/dist/data-structures/binary-tree/segment-tree.js +95 -61
- package/dist/data-structures/binary-tree/splay-tree.js +5 -2
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +5 -5
- package/dist/data-structures/binary-tree/tree-multiset.js +35 -11
- package/dist/data-structures/binary-tree/two-three-tree.js +5 -2
- package/dist/data-structures/graph/abstract-graph.d.ts +168 -46
- package/dist/data-structures/graph/abstract-graph.js +712 -323
- package/dist/data-structures/graph/directed-graph.d.ts +114 -12
- package/dist/data-structures/graph/directed-graph.js +372 -128
- package/dist/data-structures/graph/undirected-graph.d.ts +67 -3
- package/dist/data-structures/graph/undirected-graph.js +233 -81
- package/dist/data-structures/hash/coordinate-map.d.ts +33 -1
- package/dist/data-structures/hash/coordinate-map.js +70 -20
- package/dist/data-structures/hash/coordinate-set.d.ts +25 -0
- package/dist/data-structures/hash/coordinate-set.js +58 -15
- package/dist/data-structures/hash/index.d.ts +5 -0
- package/dist/data-structures/hash/index.js +5 -0
- package/dist/data-structures/heap/heap.d.ts +26 -37
- package/dist/data-structures/heap/heap.js +56 -60
- package/dist/data-structures/heap/max-heap.d.ts +8 -2
- package/dist/data-structures/heap/max-heap.js +32 -9
- package/dist/data-structures/heap/min-heap.d.ts +9 -2
- package/dist/data-structures/heap/min-heap.js +33 -9
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +14 -7
- package/dist/data-structures/linked-list/doubly-linked-list.js +101 -61
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +18 -19
- package/dist/data-structures/linked-list/singly-linked-list.js +312 -174
- package/dist/data-structures/matrix/matrix.d.ts +9 -0
- package/dist/data-structures/matrix/matrix.js +19 -7
- package/dist/data-structures/matrix/matrix2d.d.ts +84 -4
- package/dist/data-structures/matrix/matrix2d.js +158 -61
- package/dist/data-structures/matrix/navigator.d.ts +34 -16
- package/dist/data-structures/matrix/navigator.js +65 -18
- package/dist/data-structures/matrix/vector2d.d.ts +153 -29
- package/dist/data-structures/matrix/vector2d.js +249 -102
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +11 -2
- package/dist/data-structures/priority-queue/max-priority-queue.js +33 -8
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -2
- package/dist/data-structures/priority-queue/min-priority-queue.js +33 -8
- package/dist/data-structures/priority-queue/priority-queue.d.ts +145 -21
- package/dist/data-structures/priority-queue/priority-queue.js +285 -116
- package/dist/data-structures/queue/deque.d.ts +69 -0
- package/dist/data-structures/queue/deque.js +151 -56
- package/dist/data-structures/queue/queue.d.ts +34 -37
- package/dist/data-structures/queue/queue.js +59 -61
- package/dist/data-structures/stack/stack.d.ts +29 -35
- package/dist/data-structures/stack/stack.js +51 -56
- package/dist/data-structures/trie/trie.d.ts +36 -6
- package/dist/data-structures/trie/trie.js +256 -83
- package/dist/data-structures/types/abstract-graph.d.ts +29 -0
- package/dist/data-structures/types/abstract-graph.js +2 -0
- package/dist/data-structures/types/avl-tree.d.ts +5 -0
- package/dist/data-structures/types/avl-tree.js +2 -0
- package/dist/data-structures/types/binary-tree.d.ts +16 -0
- package/dist/data-structures/types/binary-tree.js +2 -0
- package/dist/data-structures/types/bst.d.ts +7 -0
- package/dist/data-structures/types/bst.js +2 -0
- package/dist/data-structures/types/directed-graph.d.ts +10 -0
- package/dist/data-structures/types/directed-graph.js +2 -0
- package/dist/data-structures/types/doubly-linked-list.d.ts +1 -0
- package/dist/data-structures/types/doubly-linked-list.js +2 -0
- package/dist/data-structures/types/heap.d.ts +7 -0
- package/dist/data-structures/types/heap.js +2 -0
- package/dist/data-structures/types/index.d.ts +13 -0
- package/dist/data-structures/types/index.js +29 -0
- package/dist/data-structures/types/navigator.d.ts +14 -0
- package/dist/data-structures/types/navigator.js +2 -0
- package/dist/data-structures/types/priority-queue.d.ts +7 -0
- package/dist/data-structures/types/priority-queue.js +2 -0
- package/dist/data-structures/types/segment-tree.d.ts +1 -0
- package/dist/data-structures/types/segment-tree.js +2 -0
- package/dist/data-structures/types/singly-linked-list.js +2 -0
- package/dist/data-structures/types/tree-multiset.d.ts +5 -0
- package/dist/data-structures/types/tree-multiset.js +2 -0
- package/dist/utils/trampoline.d.ts +14 -0
- package/dist/utils/trampoline.js +130 -0
- package/dist/utils/types/index.js +17 -0
- package/dist/{types → utils}/types/utils.d.ts +15 -1
- package/dist/{types → utils/types}/utils.js +21 -19
- package/dist/{utils.d.ts → utils/utils.d.ts} +5 -22
- package/dist/utils/utils.js +651 -0
- package/docs/.nojekyll +1 -0
- package/docs/assets/highlight.css +85 -0
- package/docs/assets/main.js +58 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1367 -0
- package/docs/classes/AVLTree.html +2046 -0
- package/docs/classes/AVLTreeNode.html +423 -0
- package/docs/classes/AaTree.html +117 -0
- package/docs/classes/AbstractEdge.html +198 -0
- package/docs/classes/AbstractGraph.html +891 -0
- package/docs/classes/AbstractVertex.html +164 -0
- package/docs/classes/ArrayDeque.html +384 -0
- package/docs/classes/BST.html +1893 -0
- package/docs/classes/BSTNode.html +425 -0
- package/docs/classes/BTree.html +117 -0
- package/docs/classes/BinaryIndexedTree.html +244 -0
- package/docs/classes/BinaryTree.html +1754 -0
- package/docs/classes/BinaryTreeNode.html +396 -0
- package/docs/classes/Character.html +165 -0
- package/docs/classes/CoordinateMap.html +394 -0
- package/docs/classes/CoordinateSet.html +355 -0
- package/docs/classes/Deque.html +617 -0
- package/docs/classes/DirectedEdge.html +247 -0
- package/docs/classes/DirectedGraph.html +1207 -0
- package/docs/classes/DirectedVertex.html +154 -0
- package/docs/classes/DoublyLinkedList.html +619 -0
- package/docs/classes/DoublyLinkedListNode.html +160 -0
- package/docs/classes/Heap.html +315 -0
- package/docs/classes/Matrix2D.html +447 -0
- package/docs/classes/MatrixNTI2D.html +181 -0
- package/docs/classes/MaxHeap.html +325 -0
- package/docs/classes/MaxPriorityQueue.html +668 -0
- package/docs/classes/MinHeap.html +326 -0
- package/docs/classes/MinPriorityQueue.html +668 -0
- package/docs/classes/Navigator.html +285 -0
- package/docs/classes/ObjectDeque.html +289 -0
- package/docs/classes/PriorityQueue.html +643 -0
- package/docs/classes/Queue.html +337 -0
- package/docs/classes/RBTree.html +117 -0
- package/docs/classes/SegmentTree.html +234 -0
- package/docs/classes/SegmentTreeNode.html +302 -0
- package/docs/classes/SinglyLinkedList.html +1035 -0
- package/docs/classes/SinglyLinkedListNode.html +304 -0
- package/docs/classes/SplayTree.html +117 -0
- package/docs/classes/Stack.html +313 -0
- package/docs/classes/TreeMultiSet.html +1897 -0
- package/docs/classes/Trie.html +317 -0
- package/docs/classes/TrieNode.html +221 -0
- package/docs/classes/TwoThreeTree.html +117 -0
- package/docs/classes/UndirectedEdge.html +220 -0
- package/docs/classes/UndirectedGraph.html +1006 -0
- package/docs/classes/UndirectedVertex.html +154 -0
- package/docs/classes/Vector2D.html +746 -0
- package/docs/enums/CP.html +126 -0
- package/docs/enums/FamilyPosition.html +126 -0
- package/docs/enums/LoopType.html +119 -0
- package/docs/index.html +288 -0
- package/docs/modules.html +146 -0
- package/jest.config.js +5 -0
- package/package.json +33 -47
- package/rename_clear_files.sh +29 -0
- package/src/assets/complexities-diff.jpg +0 -0
- package/src/assets/data-structure-complexities.jpg +0 -0
- package/src/data-structures/binary-tree/avl-tree.ts +58 -6
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +31 -4
- package/src/data-structures/binary-tree/binary-tree.ts +460 -145
- package/src/data-structures/binary-tree/bst.ts +31 -25
- package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
- package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
- package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
- package/src/data-structures/binary-tree/segment-tree.ts +25 -12
- package/src/data-structures/binary-tree/tree-multiset.ts +5 -4
- package/src/data-structures/diagrams/README.md +5 -0
- package/src/data-structures/graph/abstract-graph.ts +224 -108
- package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
- package/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
- package/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
- package/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
- package/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
- package/src/data-structures/graph/diagrams/mst.jpg +0 -0
- package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
- package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
- package/src/data-structures/graph/diagrams/tarjan.webp +0 -0
- package/src/data-structures/graph/directed-graph.ts +132 -26
- package/src/data-structures/graph/undirected-graph.ts +78 -11
- package/src/data-structures/hash/coordinate-map.ts +33 -1
- package/src/data-structures/hash/coordinate-set.ts +25 -0
- package/src/data-structures/hash/index.ts +5 -0
- package/src/data-structures/heap/heap.ts +27 -41
- package/src/data-structures/heap/max-heap.ts +8 -2
- package/src/data-structures/heap/min-heap.ts +9 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +50 -17
- package/src/data-structures/linked-list/singly-linked-list.ts +56 -39
- package/src/data-structures/matrix/matrix.ts +11 -0
- package/src/data-structures/matrix/matrix2d.ts +90 -10
- package/src/data-structures/matrix/navigator.ts +34 -14
- package/src/data-structures/matrix/vector2d.ts +187 -63
- package/src/data-structures/priority-queue/max-priority-queue.ts +12 -3
- package/src/data-structures/priority-queue/min-priority-queue.ts +12 -3
- package/src/data-structures/priority-queue/priority-queue.ts +200 -78
- package/src/data-structures/queue/deque.ts +71 -2
- package/src/data-structures/queue/queue.ts +37 -40
- package/src/data-structures/stack/stack.ts +32 -38
- package/src/data-structures/trie/trie.ts +83 -14
- package/src/data-structures/types/abstract-graph.ts +51 -0
- package/src/data-structures/types/avl-tree.ts +6 -0
- package/src/data-structures/types/binary-tree.ts +15 -0
- package/src/data-structures/types/bst.ts +5 -0
- package/src/data-structures/types/directed-graph.ts +18 -0
- package/src/data-structures/types/doubly-linked-list.ts +1 -0
- package/src/data-structures/types/heap.ts +8 -0
- package/src/data-structures/types/index.ts +13 -0
- package/src/data-structures/types/navigator.ts +13 -0
- package/src/data-structures/types/priority-queue.ts +9 -0
- package/src/data-structures/types/segment-tree.ts +1 -0
- package/src/data-structures/types/singly-linked-list.ts +1 -0
- package/src/data-structures/types/tree-multiset.ts +3 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/trampoline.ts +51 -0
- package/src/utils/types/index.ts +1 -0
- package/src/{types → utils/types}/utils.ts +27 -5
- package/src/{utils.ts → utils/utils.ts} +41 -131
- package/tests/unit/data-structures/binary-tree/bst.test.ts +185 -0
- package/tests/unit/data-structures/graph/directed-graph.test.ts +71 -0
- package/{dist/types/data-structures/graph/index.d.ts → tests/unit/data-structures/graph/index.ts} +1 -1
- package/tests/unit/data-structures/graph/undirected-graph.ts +0 -0
- package/tsconfig.json +9 -6
- package/dist/data-structures/trampoline.d.ts +0 -25
- package/dist/data-structures/trampoline.js +0 -52
- package/dist/types/data-structures/binary-tree/aa-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -21
- package/dist/types/data-structures/binary-tree/b-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -8
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -140
- package/dist/types/data-structures/binary-tree/bst.d.ts +0 -32
- package/dist/types/data-structures/binary-tree/index.d.ts +0 -4
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +0 -33
- package/dist/types/data-structures/binary-tree/splay-tree.d.ts +0 -2
- package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +0 -11
- package/dist/types/data-structures/binary-tree/two-three-tree.d.ts +0 -2
- package/dist/types/data-structures/graph/abstract-graph.d.ts +0 -126
- package/dist/types/data-structures/graph/directed-graph.d.ts +0 -51
- package/dist/types/data-structures/graph/undirected-graph.d.ts +0 -24
- package/dist/types/data-structures/hash/coordinate-map.d.ts +0 -8
- package/dist/types/data-structures/hash/coordinate-set.d.ts +0 -7
- package/dist/types/data-structures/hash/index.d.ts +0 -1
- package/dist/types/data-structures/hash/pair.d.ts +0 -1
- package/dist/types/data-structures/hash/tree-map.d.ts +0 -1
- package/dist/types/data-structures/hash/tree-set.d.ts +0 -1
- package/dist/types/data-structures/heap/heap.d.ts +0 -72
- package/dist/types/data-structures/heap/index.d.ts +0 -3
- package/dist/types/data-structures/heap/max-heap.d.ts +0 -14
- package/dist/types/data-structures/heap/min-heap.d.ts +0 -14
- package/dist/types/data-structures/index.d.ts +0 -9
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +0 -59
- package/dist/types/data-structures/linked-list/index.d.ts +0 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +0 -358
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +0 -1
- package/dist/types/data-structures/matrix/index.d.ts +0 -3
- package/dist/types/data-structures/matrix/matrix.d.ts +0 -9
- package/dist/types/data-structures/matrix/matrix2d.d.ts +0 -25
- package/dist/types/data-structures/matrix/navigator.d.ts +0 -31
- package/dist/types/data-structures/matrix/vector2d.d.ts +0 -74
- package/dist/types/data-structures/priority-queue/index.d.ts +0 -3
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +0 -4
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +0 -4
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +0 -36
- package/dist/types/data-structures/queue/deque.d.ts +0 -37
- package/dist/types/data-structures/queue/index.d.ts +0 -1
- package/dist/types/data-structures/queue/queue.d.ts +0 -76
- package/dist/types/data-structures/stack/index.d.ts +0 -1
- package/dist/types/data-structures/stack/stack.d.ts +0 -69
- package/dist/types/data-structures/trampoline.d.ts +0 -25
- package/dist/types/data-structures/trie/index.d.ts +0 -1
- package/dist/types/data-structures/trie/trie.d.ts +0 -28
- package/dist/types/utils.d.ts +0 -46
- package/dist/utils.js +0 -569
- package/src/data-structures/trampoline.ts +0 -91
- package/src/types/index.ts +0 -1
- /package/dist/{types/data-structures/hash/hash-table.d.ts → data-structures/types/singly-linked-list.d.ts} +0 -0
- /package/dist/{types → utils}/index.d.ts +0 -0
- /package/dist/{types → utils}/index.js +0 -0
- /package/dist/{types → utils}/types/index.d.ts +0 -0
- /package/{src/types/patches/index.d.ts → tests/unit/data-structures/graph/abstract-graph.ts} +0 -0
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
3
|
+
* @license MIT
|
|
4
|
+
*/
|
|
5
|
+
import type { BinaryTreeNodeId, BinaryTreeNodePropertyName, BSTComparator, BSTDeletedResult } from '../types';
|
|
6
|
+
import { BinaryTree, BinaryTreeNode, LoopType } from './binary-tree';
|
|
7
7
|
export declare enum CP {
|
|
8
8
|
lt = -1,
|
|
9
9
|
eq = 0,
|
|
@@ -13,13 +13,23 @@ export declare class BSTNode<T> extends BinaryTreeNode<T> {
|
|
|
13
13
|
clone(): BSTNode<T>;
|
|
14
14
|
}
|
|
15
15
|
export declare class BST<T> extends BinaryTree<T> {
|
|
16
|
-
protected _comparator: BSTComparator;
|
|
17
|
-
protected _compare(a: BinaryTreeNodeId, b: BinaryTreeNodeId): CP;
|
|
18
16
|
constructor(options?: {
|
|
19
17
|
comparator?: BSTComparator;
|
|
20
18
|
loopType?: LoopType;
|
|
21
19
|
});
|
|
22
20
|
createNode(id: BinaryTreeNodeId, val: T | null, count?: number): BSTNode<T> | null;
|
|
21
|
+
/**
|
|
22
|
+
* The `put` function inserts a new node into a binary search tree, updating the count and value of an existing node if
|
|
23
|
+
* the ID matches, and returns the inserted node.
|
|
24
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter represents the identifier of the binary tree node. It is used to
|
|
25
|
+
* determine the position of the node in the binary search tree.
|
|
26
|
+
* @param {T | null} val - The `val` parameter represents the value to be stored in the binary search tree node. It can
|
|
27
|
+
* be of type `T` (the generic type) or `null`.
|
|
28
|
+
* @param {number} [count=1] - The `count` parameter represents the number of times the value should be inserted into
|
|
29
|
+
* the binary search tree. By default, it is set to 1, meaning that if no count is specified, the value will be
|
|
30
|
+
* inserted once.
|
|
31
|
+
* @returns The method `put` returns a `BSTNode<T>` object or `null`.
|
|
32
|
+
*/
|
|
23
33
|
put(id: BinaryTreeNodeId, val: T | null, count?: number): BSTNode<T> | null;
|
|
24
34
|
get(nodeProperty: BinaryTreeNodeId | T, propertyName?: BinaryTreeNodePropertyName): BSTNode<T> | null;
|
|
25
35
|
lastKey(): number;
|
|
@@ -29,4 +39,6 @@ export declare class BST<T> extends BinaryTree<T> {
|
|
|
29
39
|
allGreaterNodesAdd(node: BSTNode<T>, delta: number, propertyName?: BinaryTreeNodePropertyName): boolean;
|
|
30
40
|
balance(): boolean;
|
|
31
41
|
isAVLBalanced(): boolean;
|
|
42
|
+
protected _comparator: BSTComparator;
|
|
43
|
+
protected _compare(a: BinaryTreeNodeId, b: BinaryTreeNodeId): CP;
|
|
32
44
|
}
|
|
@@ -1,46 +1,88 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
18
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
19
|
+
if (!m) return o;
|
|
20
|
+
var i = m.call(o), r, ar = [], e;
|
|
21
|
+
try {
|
|
22
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
23
|
+
}
|
|
24
|
+
catch (error) { e = { error: error }; }
|
|
25
|
+
finally {
|
|
26
|
+
try {
|
|
27
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
28
|
+
}
|
|
29
|
+
finally { if (e) throw e.error; }
|
|
30
|
+
}
|
|
31
|
+
return ar;
|
|
32
|
+
};
|
|
2
33
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
34
|
exports.BST = exports.BSTNode = exports.CP = void 0;
|
|
4
|
-
|
|
35
|
+
var binary_tree_1 = require("./binary-tree");
|
|
5
36
|
var CP;
|
|
6
37
|
(function (CP) {
|
|
7
38
|
CP[CP["lt"] = -1] = "lt";
|
|
8
39
|
CP[CP["eq"] = 0] = "eq";
|
|
9
40
|
CP[CP["gt"] = 1] = "gt";
|
|
10
41
|
})(CP = exports.CP || (exports.CP = {}));
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
42
|
+
var BSTNode = /** @class */ (function (_super) {
|
|
43
|
+
__extends(BSTNode, _super);
|
|
44
|
+
function BSTNode() {
|
|
45
|
+
return _super !== null && _super.apply(this, arguments) || this;
|
|
14
46
|
}
|
|
15
|
-
|
|
47
|
+
BSTNode.prototype.clone = function () {
|
|
48
|
+
return new BSTNode(this.id, this.val, this.count);
|
|
49
|
+
};
|
|
50
|
+
return BSTNode;
|
|
51
|
+
}(binary_tree_1.BinaryTreeNode));
|
|
16
52
|
exports.BSTNode = BSTNode;
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
else if (compared < 0)
|
|
23
|
-
return CP.lt;
|
|
24
|
-
else
|
|
25
|
-
return CP.eq;
|
|
26
|
-
}
|
|
27
|
-
constructor(options) {
|
|
28
|
-
super(options);
|
|
29
|
-
this._comparator = (a, b) => a - b;
|
|
53
|
+
var BST = /** @class */ (function (_super) {
|
|
54
|
+
__extends(BST, _super);
|
|
55
|
+
function BST(options) {
|
|
56
|
+
var _this = _super.call(this, options) || this;
|
|
57
|
+
_this._comparator = function (a, b) { return a - b; };
|
|
30
58
|
if (options !== undefined) {
|
|
31
|
-
|
|
59
|
+
var comparator = options.comparator;
|
|
32
60
|
if (comparator !== undefined) {
|
|
33
|
-
|
|
61
|
+
_this._comparator = comparator;
|
|
34
62
|
}
|
|
35
63
|
}
|
|
64
|
+
return _this;
|
|
36
65
|
}
|
|
37
|
-
createNode(id, val, count) {
|
|
66
|
+
BST.prototype.createNode = function (id, val, count) {
|
|
38
67
|
return val !== null ? new BSTNode(id, val, count) : null;
|
|
39
|
-
}
|
|
40
|
-
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* The `put` function inserts a new node into a binary search tree, updating the count and value of an existing node if
|
|
71
|
+
* the ID matches, and returns the inserted node.
|
|
72
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter represents the identifier of the binary tree node. It is used to
|
|
73
|
+
* determine the position of the node in the binary search tree.
|
|
74
|
+
* @param {T | null} val - The `val` parameter represents the value to be stored in the binary search tree node. It can
|
|
75
|
+
* be of type `T` (the generic type) or `null`.
|
|
76
|
+
* @param {number} [count=1] - The `count` parameter represents the number of times the value should be inserted into
|
|
77
|
+
* the binary search tree. By default, it is set to 1, meaning that if no count is specified, the value will be
|
|
78
|
+
* inserted once.
|
|
79
|
+
* @returns The method `put` returns a `BSTNode<T>` object or `null`.
|
|
80
|
+
*/
|
|
81
|
+
BST.prototype.put = function (id, val, count) {
|
|
41
82
|
var _a;
|
|
42
|
-
|
|
43
|
-
|
|
83
|
+
if (count === void 0) { count = 1; }
|
|
84
|
+
var inserted = null;
|
|
85
|
+
var newNode = this.createNode(id, val, count);
|
|
44
86
|
if (this.root === null) {
|
|
45
87
|
this.root = newNode;
|
|
46
88
|
this.size++;
|
|
@@ -48,8 +90,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
48
90
|
inserted = (this.root);
|
|
49
91
|
}
|
|
50
92
|
else {
|
|
51
|
-
|
|
52
|
-
|
|
93
|
+
var cur = this.root;
|
|
94
|
+
var traversing = true;
|
|
53
95
|
while (traversing) {
|
|
54
96
|
if (cur !== null && newNode !== null) {
|
|
55
97
|
if (this._compare(cur.id, id) === CP.eq) {
|
|
@@ -109,13 +151,13 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
109
151
|
}
|
|
110
152
|
}
|
|
111
153
|
return inserted;
|
|
112
|
-
}
|
|
113
|
-
get(nodeProperty, propertyName) {
|
|
154
|
+
};
|
|
155
|
+
BST.prototype.get = function (nodeProperty, propertyName) {
|
|
114
156
|
var _a;
|
|
115
157
|
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
116
158
|
return (_a = this.getNodes(nodeProperty, propertyName, true)[0]) !== null && _a !== void 0 ? _a : null;
|
|
117
|
-
}
|
|
118
|
-
lastKey() {
|
|
159
|
+
};
|
|
160
|
+
BST.prototype.lastKey = function () {
|
|
119
161
|
var _a, _b, _c, _d, _e, _f;
|
|
120
162
|
if (this._compare(0, 1) === CP.lt)
|
|
121
163
|
return (_b = (_a = this.getRightMost()) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : 0;
|
|
@@ -123,16 +165,16 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
123
165
|
return (_d = (_c = this.getLeftMost()) === null || _c === void 0 ? void 0 : _c.id) !== null && _d !== void 0 ? _d : 0;
|
|
124
166
|
else
|
|
125
167
|
return (_f = (_e = this.getRightMost()) === null || _e === void 0 ? void 0 : _e.id) !== null && _f !== void 0 ? _f : 0;
|
|
126
|
-
}
|
|
127
|
-
remove(id, ignoreCount) {
|
|
128
|
-
|
|
168
|
+
};
|
|
169
|
+
BST.prototype.remove = function (id, ignoreCount) {
|
|
170
|
+
var bstDeletedResult = [];
|
|
129
171
|
if (!this.root)
|
|
130
172
|
return bstDeletedResult;
|
|
131
|
-
|
|
173
|
+
var curr = this.get(id);
|
|
132
174
|
if (!curr)
|
|
133
175
|
return bstDeletedResult;
|
|
134
|
-
|
|
135
|
-
|
|
176
|
+
var parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent : null;
|
|
177
|
+
var needBalanced = null, orgCurrent = curr;
|
|
136
178
|
if (curr.count > 1 && !ignoreCount) {
|
|
137
179
|
curr.count--;
|
|
138
180
|
this.count--;
|
|
@@ -156,9 +198,9 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
156
198
|
}
|
|
157
199
|
}
|
|
158
200
|
else {
|
|
159
|
-
|
|
201
|
+
var leftSubTreeMax = curr.left ? this.getRightMost(curr.left) : null;
|
|
160
202
|
if (leftSubTreeMax) {
|
|
161
|
-
|
|
203
|
+
var parentOfLeftSubTreeMax = leftSubTreeMax.parent;
|
|
162
204
|
orgCurrent = curr.swapLocation(leftSubTreeMax);
|
|
163
205
|
if (parentOfLeftSubTreeMax) {
|
|
164
206
|
if (parentOfLeftSubTreeMax.right === leftSubTreeMax)
|
|
@@ -172,37 +214,38 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
172
214
|
this.size--;
|
|
173
215
|
this.count -= curr.count;
|
|
174
216
|
}
|
|
175
|
-
bstDeletedResult.push({ deleted: orgCurrent, needBalanced });
|
|
217
|
+
bstDeletedResult.push({ deleted: orgCurrent, needBalanced: needBalanced });
|
|
176
218
|
return bstDeletedResult;
|
|
177
|
-
}
|
|
178
|
-
getNodes(nodeProperty, propertyName, onlyOne) {
|
|
219
|
+
};
|
|
220
|
+
BST.prototype.getNodes = function (nodeProperty, propertyName, onlyOne) {
|
|
221
|
+
var _this = this;
|
|
179
222
|
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
180
223
|
if (!this.root)
|
|
181
224
|
return [];
|
|
182
|
-
|
|
225
|
+
var result = [];
|
|
183
226
|
if (this._loopType === binary_tree_1.LoopType.recursive) {
|
|
184
|
-
|
|
185
|
-
if (
|
|
227
|
+
var _traverse_1 = function (cur) {
|
|
228
|
+
if (_this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
|
|
186
229
|
return;
|
|
187
230
|
if (!cur.left && !cur.right)
|
|
188
231
|
return;
|
|
189
232
|
if (propertyName === 'id') {
|
|
190
|
-
if (
|
|
191
|
-
cur.left &&
|
|
192
|
-
if (
|
|
193
|
-
cur.right &&
|
|
233
|
+
if (_this._compare(cur.id, nodeProperty) === CP.gt)
|
|
234
|
+
cur.left && _traverse_1(cur.left);
|
|
235
|
+
if (_this._compare(cur.id, nodeProperty) === CP.lt)
|
|
236
|
+
cur.right && _traverse_1(cur.right);
|
|
194
237
|
}
|
|
195
238
|
else {
|
|
196
|
-
cur.left &&
|
|
197
|
-
cur.right &&
|
|
239
|
+
cur.left && _traverse_1(cur.left);
|
|
240
|
+
cur.right && _traverse_1(cur.right);
|
|
198
241
|
}
|
|
199
242
|
};
|
|
200
|
-
|
|
243
|
+
_traverse_1(this.root);
|
|
201
244
|
}
|
|
202
245
|
else {
|
|
203
|
-
|
|
246
|
+
var queue = [this.root];
|
|
204
247
|
while (queue.length > 0) {
|
|
205
|
-
|
|
248
|
+
var cur = queue.shift();
|
|
206
249
|
if (cur) {
|
|
207
250
|
if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
|
|
208
251
|
return result;
|
|
@@ -220,14 +263,15 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
220
263
|
}
|
|
221
264
|
}
|
|
222
265
|
return result;
|
|
223
|
-
}
|
|
266
|
+
};
|
|
224
267
|
// --- start additional functions
|
|
225
|
-
lesserSum(id, propertyName) {
|
|
268
|
+
BST.prototype.lesserSum = function (id, propertyName) {
|
|
269
|
+
var _this = this;
|
|
226
270
|
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
227
271
|
if (!this.root)
|
|
228
272
|
return 0;
|
|
229
|
-
|
|
230
|
-
|
|
273
|
+
var getSumByPropertyName = function (cur) {
|
|
274
|
+
var needSum;
|
|
231
275
|
switch (propertyName) {
|
|
232
276
|
case 'id':
|
|
233
277
|
needSum = cur.id;
|
|
@@ -241,39 +285,39 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
241
285
|
}
|
|
242
286
|
return needSum;
|
|
243
287
|
};
|
|
244
|
-
|
|
288
|
+
var sum = 0;
|
|
245
289
|
if (this._loopType === binary_tree_1.LoopType.recursive) {
|
|
246
|
-
|
|
247
|
-
|
|
290
|
+
var _traverse_2 = function (cur) {
|
|
291
|
+
var compared = _this._compare(cur.id, id);
|
|
248
292
|
if (compared === CP.eq) {
|
|
249
293
|
if (cur.right)
|
|
250
|
-
sum +=
|
|
294
|
+
sum += _this.subTreeSum(cur.right, propertyName);
|
|
251
295
|
return;
|
|
252
296
|
}
|
|
253
297
|
else if (compared === CP.lt) {
|
|
254
298
|
if (cur.left)
|
|
255
|
-
sum +=
|
|
299
|
+
sum += _this.subTreeSum(cur.left, propertyName);
|
|
256
300
|
sum += getSumByPropertyName(cur);
|
|
257
301
|
if (cur.right)
|
|
258
|
-
|
|
302
|
+
_traverse_2(cur.right);
|
|
259
303
|
else
|
|
260
304
|
return;
|
|
261
305
|
}
|
|
262
306
|
else {
|
|
263
307
|
if (cur.left)
|
|
264
|
-
|
|
308
|
+
_traverse_2(cur.left);
|
|
265
309
|
else
|
|
266
310
|
return;
|
|
267
311
|
}
|
|
268
312
|
};
|
|
269
|
-
|
|
313
|
+
_traverse_2(this.root);
|
|
270
314
|
}
|
|
271
315
|
else {
|
|
272
|
-
|
|
316
|
+
var queue = [this.root];
|
|
273
317
|
while (queue.length > 0) {
|
|
274
|
-
|
|
318
|
+
var cur = queue.shift();
|
|
275
319
|
if (cur) {
|
|
276
|
-
|
|
320
|
+
var compared = this._compare(cur.id, id);
|
|
277
321
|
if (compared === CP.eq) {
|
|
278
322
|
if (cur.right)
|
|
279
323
|
sum += this.subTreeSum(cur.right, propertyName);
|
|
@@ -298,12 +342,13 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
298
342
|
}
|
|
299
343
|
}
|
|
300
344
|
return sum;
|
|
301
|
-
}
|
|
302
|
-
allGreaterNodesAdd(node, delta, propertyName) {
|
|
345
|
+
};
|
|
346
|
+
BST.prototype.allGreaterNodesAdd = function (node, delta, propertyName) {
|
|
347
|
+
var _this = this;
|
|
303
348
|
propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
|
|
304
349
|
if (!this.root)
|
|
305
350
|
return false;
|
|
306
|
-
|
|
351
|
+
var _sumByPropertyName = function (cur) {
|
|
307
352
|
switch (propertyName) {
|
|
308
353
|
case 'id':
|
|
309
354
|
cur.id += delta;
|
|
@@ -317,25 +362,25 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
317
362
|
}
|
|
318
363
|
};
|
|
319
364
|
if (this._loopType === binary_tree_1.LoopType.recursive) {
|
|
320
|
-
|
|
321
|
-
|
|
365
|
+
var _traverse_3 = function (cur) {
|
|
366
|
+
var compared = _this._compare(cur.id, node.id);
|
|
322
367
|
_sumByPropertyName(cur);
|
|
323
368
|
if (!cur.left && !cur.right)
|
|
324
369
|
return;
|
|
325
370
|
if (cur.left && compared === CP.gt)
|
|
326
|
-
|
|
371
|
+
_traverse_3(cur.left);
|
|
327
372
|
else if (cur.right && compared === CP.gt)
|
|
328
|
-
|
|
373
|
+
_traverse_3(cur.right);
|
|
329
374
|
};
|
|
330
|
-
|
|
375
|
+
_traverse_3(this.root);
|
|
331
376
|
return true;
|
|
332
377
|
}
|
|
333
378
|
else {
|
|
334
|
-
|
|
379
|
+
var queue = [this.root];
|
|
335
380
|
while (queue.length > 0) {
|
|
336
|
-
|
|
381
|
+
var cur = queue.shift();
|
|
337
382
|
if (cur) {
|
|
338
|
-
|
|
383
|
+
var compared = this._compare(cur.id, node.id);
|
|
339
384
|
_sumByPropertyName(cur);
|
|
340
385
|
if (cur.left && compared === CP.gt)
|
|
341
386
|
queue.push(cur.left);
|
|
@@ -345,34 +390,35 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
345
390
|
}
|
|
346
391
|
return true;
|
|
347
392
|
}
|
|
348
|
-
}
|
|
349
|
-
balance() {
|
|
350
|
-
|
|
393
|
+
};
|
|
394
|
+
BST.prototype.balance = function () {
|
|
395
|
+
var _this = this;
|
|
396
|
+
var sorted = this.DFS('in', 'node'), n = sorted.length;
|
|
351
397
|
this.clear();
|
|
352
398
|
if (sorted.length < 1)
|
|
353
399
|
return false;
|
|
354
400
|
if (this._loopType === binary_tree_1.LoopType.recursive) {
|
|
355
|
-
|
|
401
|
+
var buildBalanceBST_1 = function (l, r) {
|
|
356
402
|
if (l > r)
|
|
357
403
|
return;
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
404
|
+
var m = l + Math.floor((r - l) / 2);
|
|
405
|
+
var midNode = sorted[m];
|
|
406
|
+
_this.put(midNode.id, midNode.val, midNode.count);
|
|
407
|
+
buildBalanceBST_1(l, m - 1);
|
|
408
|
+
buildBalanceBST_1(m + 1, r);
|
|
363
409
|
};
|
|
364
|
-
|
|
410
|
+
buildBalanceBST_1(0, n - 1);
|
|
365
411
|
return true;
|
|
366
412
|
}
|
|
367
413
|
else {
|
|
368
|
-
|
|
414
|
+
var stack = [[0, n - 1]];
|
|
369
415
|
while (stack.length > 0) {
|
|
370
|
-
|
|
416
|
+
var popped = stack.pop();
|
|
371
417
|
if (popped) {
|
|
372
|
-
|
|
418
|
+
var _a = __read(popped, 2), l = _a[0], r = _a[1];
|
|
373
419
|
if (l <= r) {
|
|
374
|
-
|
|
375
|
-
|
|
420
|
+
var m = l + Math.floor((r - l) / 2);
|
|
421
|
+
var midNode = sorted[m];
|
|
376
422
|
this.put(midNode.id, midNode.val, midNode.count);
|
|
377
423
|
stack.push([m + 1, r]);
|
|
378
424
|
stack.push([l, m - 1]);
|
|
@@ -381,26 +427,27 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
381
427
|
}
|
|
382
428
|
return true;
|
|
383
429
|
}
|
|
384
|
-
}
|
|
385
|
-
isAVLBalanced() {
|
|
430
|
+
};
|
|
431
|
+
BST.prototype.isAVLBalanced = function () {
|
|
386
432
|
var _a, _b;
|
|
387
433
|
if (!this.root)
|
|
388
434
|
return true;
|
|
389
|
-
|
|
435
|
+
var balanced = true;
|
|
390
436
|
if (this._loopType === binary_tree_1.LoopType.recursive) {
|
|
391
|
-
|
|
437
|
+
var _height_1 = function (cur) {
|
|
392
438
|
if (!cur)
|
|
393
439
|
return 0;
|
|
394
|
-
|
|
440
|
+
var leftHeight = _height_1(cur.left), rightHeight = _height_1(cur.right);
|
|
395
441
|
if (Math.abs(leftHeight - rightHeight) > 1)
|
|
396
442
|
balanced = false;
|
|
397
443
|
return Math.max(leftHeight, rightHeight) + 1;
|
|
398
444
|
};
|
|
399
|
-
|
|
445
|
+
_height_1(this.root);
|
|
400
446
|
}
|
|
401
447
|
else {
|
|
402
|
-
|
|
403
|
-
|
|
448
|
+
var stack = [];
|
|
449
|
+
var node = this.root, last = null;
|
|
450
|
+
var depths = new Map();
|
|
404
451
|
while (stack.length > 0 || node) {
|
|
405
452
|
if (node) {
|
|
406
453
|
stack.push(node);
|
|
@@ -411,8 +458,8 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
411
458
|
if (!node.right || last === node.right) {
|
|
412
459
|
node = stack.pop();
|
|
413
460
|
if (node) {
|
|
414
|
-
|
|
415
|
-
|
|
461
|
+
var left = node.left ? (_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1 : -1;
|
|
462
|
+
var right = node.right ? (_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1 : -1;
|
|
416
463
|
if (Math.abs(left - right) > 1)
|
|
417
464
|
return false;
|
|
418
465
|
depths.set(node, 1 + Math.max(left, right));
|
|
@@ -426,6 +473,16 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
426
473
|
}
|
|
427
474
|
}
|
|
428
475
|
return balanced;
|
|
429
|
-
}
|
|
430
|
-
|
|
476
|
+
};
|
|
477
|
+
BST.prototype._compare = function (a, b) {
|
|
478
|
+
var compared = this._comparator(a, b);
|
|
479
|
+
if (compared > 0)
|
|
480
|
+
return CP.gt;
|
|
481
|
+
else if (compared < 0)
|
|
482
|
+
return CP.lt;
|
|
483
|
+
else
|
|
484
|
+
return CP.eq;
|
|
485
|
+
};
|
|
486
|
+
return BST;
|
|
487
|
+
}(binary_tree_1.BinaryTree));
|
|
431
488
|
exports.BST = BST;
|
|
@@ -1,5 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
3
|
+
* @license MIT
|
|
4
|
+
*/
|
|
5
|
+
import type { SegmentTreeNodeVal } from '../types';
|
|
2
6
|
export declare class SegmentTreeNode {
|
|
7
|
+
constructor(start: number, end: number, sum: number, val?: SegmentTreeNodeVal | null);
|
|
3
8
|
protected _start: number;
|
|
4
9
|
get start(): number;
|
|
5
10
|
set start(v: number);
|
|
@@ -18,15 +23,14 @@ export declare class SegmentTreeNode {
|
|
|
18
23
|
protected _right: SegmentTreeNode | null;
|
|
19
24
|
get right(): SegmentTreeNode | null;
|
|
20
25
|
set right(v: SegmentTreeNode | null);
|
|
21
|
-
constructor(start: number, end: number, sum: number, val?: SegmentTreeNodeVal | null);
|
|
22
26
|
}
|
|
23
27
|
export declare class SegmentTree {
|
|
24
28
|
protected _values: number[];
|
|
25
29
|
protected _start: number;
|
|
26
30
|
protected _end: number;
|
|
31
|
+
constructor(values: number[], start?: number, end?: number);
|
|
27
32
|
protected _root: SegmentTreeNode | null;
|
|
28
33
|
get root(): SegmentTreeNode | null;
|
|
29
|
-
constructor(values: number[], start?: number, end?: number);
|
|
30
34
|
build(start: number, end: number): SegmentTreeNode;
|
|
31
35
|
updateNode(index: number, sum: number, val?: SegmentTreeNodeVal): void;
|
|
32
36
|
querySumByRange(indexA: number, indexB: number): number;
|