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,14 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
LoopType,
|
|
8
|
-
} from './binary-tree';
|
|
9
|
-
|
|
10
|
-
export type BSTComparator = (a: BinaryTreeNodeId, b: BinaryTreeNodeId) => number;
|
|
11
|
-
export type BSTDeletedResult<T> = { deleted: BSTNode<T> | null, needBalanced: BSTNode<T> | null };
|
|
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, FamilyPosition, LoopType,} from './binary-tree';
|
|
12
7
|
|
|
13
8
|
export enum CP {lt = -1, eq = 0, gt = 1}
|
|
14
9
|
|
|
@@ -19,15 +14,6 @@ export class BSTNode<T> extends BinaryTreeNode<T> {
|
|
|
19
14
|
}
|
|
20
15
|
|
|
21
16
|
export class BST<T> extends BinaryTree<T> {
|
|
22
|
-
protected _comparator: BSTComparator = (a, b) => a - b;
|
|
23
|
-
|
|
24
|
-
protected _compare(a: BinaryTreeNodeId, b: BinaryTreeNodeId): CP {
|
|
25
|
-
const compared = this._comparator(a, b);
|
|
26
|
-
if (compared > 0) return CP.gt;
|
|
27
|
-
else if (compared < 0) return CP.lt;
|
|
28
|
-
else return CP.eq;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
17
|
constructor(options?: {
|
|
32
18
|
comparator?: BSTComparator,
|
|
33
19
|
loopType?: LoopType
|
|
@@ -45,6 +31,18 @@ export class BST<T> extends BinaryTree<T> {
|
|
|
45
31
|
return val !== null ? new BSTNode<T>(id, val, count) : null;
|
|
46
32
|
}
|
|
47
33
|
|
|
34
|
+
/**
|
|
35
|
+
* The `put` function inserts a new node into a binary search tree, updating the count and value of an existing node if
|
|
36
|
+
* the ID matches, and returns the inserted node.
|
|
37
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter represents the identifier of the binary tree node. It is used to
|
|
38
|
+
* determine the position of the node in the binary search tree.
|
|
39
|
+
* @param {T | null} val - The `val` parameter represents the value to be stored in the binary search tree node. It can
|
|
40
|
+
* be of type `T` (the generic type) or `null`.
|
|
41
|
+
* @param {number} [count=1] - The `count` parameter represents the number of times the value should be inserted into
|
|
42
|
+
* the binary search tree. By default, it is set to 1, meaning that if no count is specified, the value will be
|
|
43
|
+
* inserted once.
|
|
44
|
+
* @returns The method `put` returns a `BSTNode<T>` object or `null`.
|
|
45
|
+
*/
|
|
48
46
|
override put(id: BinaryTreeNodeId, val: T | null, count: number = 1): BSTNode<T> | null {
|
|
49
47
|
let inserted: BSTNode<T> | null = null;
|
|
50
48
|
const newNode = this.createNode(id, val, count);
|
|
@@ -123,7 +121,6 @@ export class BST<T> extends BinaryTree<T> {
|
|
|
123
121
|
|
|
124
122
|
override remove(id: BinaryTreeNodeId, ignoreCount?: boolean): BSTDeletedResult<T>[] {
|
|
125
123
|
const bstDeletedResult: BSTDeletedResult<T>[] = [];
|
|
126
|
-
|
|
127
124
|
if (!this.root) return bstDeletedResult;
|
|
128
125
|
|
|
129
126
|
const curr: BSTNode<T> | null = this.get(id);
|
|
@@ -373,8 +370,8 @@ export class BST<T> extends BinaryTree<T> {
|
|
|
373
370
|
_height(this.root);
|
|
374
371
|
} else {
|
|
375
372
|
const stack: BSTNode<T>[] = [];
|
|
376
|
-
let node: BSTNode<T> | null | undefined = this.root, last: BSTNode<T> | null = null
|
|
377
|
-
|
|
373
|
+
let node: BSTNode<T> | null | undefined = this.root, last: BSTNode<T> | null = null;
|
|
374
|
+
const depths: Map<BSTNode<T>, number> = new Map();
|
|
378
375
|
|
|
379
376
|
while (stack.length > 0 || node) {
|
|
380
377
|
if (node) {
|
|
@@ -385,8 +382,8 @@ export class BST<T> extends BinaryTree<T> {
|
|
|
385
382
|
if (!node.right || last === node.right) {
|
|
386
383
|
node = stack.pop();
|
|
387
384
|
if (node) {
|
|
388
|
-
|
|
389
|
-
|
|
385
|
+
const left = node.left ? depths.get(node.left) ?? -1 : -1;
|
|
386
|
+
const right = node.right ? depths.get(node.right) ?? -1 : -1;
|
|
390
387
|
if (Math.abs(left - right) > 1) return false;
|
|
391
388
|
depths.set(node, 1 + Math.max(left, right));
|
|
392
389
|
last = node;
|
|
@@ -400,5 +397,14 @@ export class BST<T> extends BinaryTree<T> {
|
|
|
400
397
|
return balanced;
|
|
401
398
|
}
|
|
402
399
|
|
|
400
|
+
protected _comparator: BSTComparator = (a, b) => a - b;
|
|
401
|
+
|
|
402
|
+
protected _compare(a: BinaryTreeNodeId, b: BinaryTreeNodeId): CP {
|
|
403
|
+
const compared = this._comparator(a, b);
|
|
404
|
+
if (compared > 0) return CP.gt;
|
|
405
|
+
else if (compared < 0) return CP.lt;
|
|
406
|
+
else return CP.eq;
|
|
407
|
+
}
|
|
408
|
+
|
|
403
409
|
// --- end additional functions
|
|
404
410
|
}
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -1,7 +1,20 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
3
|
+
* @license MIT
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type {SegmentTreeNodeVal} from '../types';
|
|
2
7
|
|
|
3
8
|
export class SegmentTreeNode {
|
|
9
|
+
constructor(start: number, end: number, sum: number, val?: SegmentTreeNodeVal | null) {
|
|
10
|
+
this._start = start;
|
|
11
|
+
this._end = end;
|
|
12
|
+
this._sum = sum;
|
|
13
|
+
this._val = val || null;
|
|
14
|
+
}
|
|
15
|
+
|
|
4
16
|
protected _start = 0;
|
|
17
|
+
|
|
5
18
|
get start(): number {
|
|
6
19
|
return this._start;
|
|
7
20
|
}
|
|
@@ -11,6 +24,7 @@ export class SegmentTreeNode {
|
|
|
11
24
|
}
|
|
12
25
|
|
|
13
26
|
protected _end = 0;
|
|
27
|
+
|
|
14
28
|
get end(): number {
|
|
15
29
|
return this._end;
|
|
16
30
|
}
|
|
@@ -20,6 +34,7 @@ export class SegmentTreeNode {
|
|
|
20
34
|
}
|
|
21
35
|
|
|
22
36
|
protected _val: SegmentTreeNodeVal | null = null;
|
|
37
|
+
|
|
23
38
|
get val(): SegmentTreeNodeVal | null {
|
|
24
39
|
return this._val;
|
|
25
40
|
}
|
|
@@ -29,6 +44,7 @@ export class SegmentTreeNode {
|
|
|
29
44
|
}
|
|
30
45
|
|
|
31
46
|
protected _sum = 0;
|
|
47
|
+
|
|
32
48
|
get sum(): number {
|
|
33
49
|
return this._sum;
|
|
34
50
|
}
|
|
@@ -38,6 +54,7 @@ export class SegmentTreeNode {
|
|
|
38
54
|
}
|
|
39
55
|
|
|
40
56
|
protected _left: SegmentTreeNode | null = null;
|
|
57
|
+
|
|
41
58
|
get left(): SegmentTreeNode | null {
|
|
42
59
|
return this._left;
|
|
43
60
|
}
|
|
@@ -47,6 +64,7 @@ export class SegmentTreeNode {
|
|
|
47
64
|
}
|
|
48
65
|
|
|
49
66
|
protected _right: SegmentTreeNode | null = null;
|
|
67
|
+
|
|
50
68
|
get right(): SegmentTreeNode | null {
|
|
51
69
|
return this._right;
|
|
52
70
|
}
|
|
@@ -54,23 +72,12 @@ export class SegmentTreeNode {
|
|
|
54
72
|
set right(v: SegmentTreeNode | null) {
|
|
55
73
|
this._right = v;
|
|
56
74
|
}
|
|
57
|
-
|
|
58
|
-
constructor(start: number, end: number, sum: number, val?: SegmentTreeNodeVal | null) {
|
|
59
|
-
this._start = start;
|
|
60
|
-
this._end = end;
|
|
61
|
-
this._sum = sum;
|
|
62
|
-
this._val = val || null;
|
|
63
|
-
}
|
|
64
75
|
}
|
|
65
76
|
|
|
66
77
|
export class SegmentTree {
|
|
67
78
|
protected _values: number[] = [];
|
|
68
79
|
protected _start = 0;
|
|
69
80
|
protected _end: number;
|
|
70
|
-
protected _root: SegmentTreeNode | null;
|
|
71
|
-
get root(): SegmentTreeNode | null {
|
|
72
|
-
return this._root;
|
|
73
|
-
}
|
|
74
81
|
|
|
75
82
|
constructor(values: number[], start?: number, end?: number) {
|
|
76
83
|
start = start || 0;
|
|
@@ -81,6 +88,12 @@ export class SegmentTree {
|
|
|
81
88
|
this._root = this.build(start, end);
|
|
82
89
|
}
|
|
83
90
|
|
|
91
|
+
protected _root: SegmentTreeNode | null;
|
|
92
|
+
|
|
93
|
+
get root(): SegmentTreeNode | null {
|
|
94
|
+
return this._root;
|
|
95
|
+
}
|
|
96
|
+
|
|
84
97
|
build(start: number, end: number): SegmentTreeNode {
|
|
85
98
|
if (start === end) {
|
|
86
99
|
return new SegmentTreeNode(start, end, this._values[start]);
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
|
|
3
|
+
* @license MIT
|
|
4
|
+
*/
|
|
1
5
|
import {BST, BSTNode} from './bst';
|
|
2
|
-
import {BinaryTreeNodeId} from '
|
|
3
|
-
|
|
4
|
-
export type TreeMultiSetDeletedResult<T> = { deleted: BSTNode<T> | null, needBalanced: BSTNode<T> | null };
|
|
5
|
-
|
|
6
|
+
import type {BinaryTreeNodeId, TreeMultiSetDeletedResult} from '../types';
|
|
6
7
|
|
|
7
8
|
export class TreeMultiSet<T> extends BST<T> {
|
|
8
9
|
override createNode(id: BinaryTreeNodeId, val: T, count?: number): BSTNode<T> {
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// 操作 常见名称 Ada Java JavaScript C++ Python Perl PHP Ruby // 尾部插入 inject, snoc Append offerLast push push_back append push
|
|
2
|
+
array_push push // 头部插入 push, cons Prepend offerFirst unshift push_front appendleft unshift array_unshift unshift //
|
|
3
|
+
尾部删除 eject Delete_Last pollLast pop pop_back pop pop array_pop pop // 头部删除 pop Delete_First pollFirst shift pop_front
|
|
4
|
+
popleft shift array_shift shift // 查看尾部 Last_Element peekLast [length - 1] back [-1] $array[-1] end
|
|
5
|
+
last // 查看头部 First_Element peekFirst [0] front [0] $array[0] reset first
|