data-structure-typed 0.9.16 → 1.3.0
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/LICENSE +21 -0
- package/README.md +665 -172
- package/dist/bundle.js +2 -0
- package/dist/bundle.js.LICENSE.txt +13 -0
- package/dist/data-structures/binary-tree/aa-tree.js +2 -5
- package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +364 -0
- package/dist/data-structures/binary-tree/abstract-binary-tree.js +1308 -0
- package/dist/data-structures/binary-tree/avl-tree.d.ts +85 -14
- package/dist/data-structures/binary-tree/avl-tree.js +142 -116
- package/dist/data-structures/binary-tree/b-tree.js +2 -5
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +39 -1
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +54 -13
- package/dist/data-structures/binary-tree/binary-tree.d.ts +29 -126
- package/dist/data-structures/binary-tree/binary-tree.js +31 -1093
- package/dist/data-structures/binary-tree/bst.d.ts +117 -23
- package/dist/data-structures/binary-tree/bst.js +233 -240
- package/dist/data-structures/binary-tree/index.d.ts +1 -0
- package/dist/data-structures/binary-tree/index.js +1 -0
- package/dist/data-structures/binary-tree/rb-tree.d.ts +18 -1
- package/dist/data-structures/binary-tree/rb-tree.js +40 -5
- package/dist/data-structures/binary-tree/segment-tree.d.ts +61 -11
- package/dist/data-structures/binary-tree/segment-tree.js +126 -93
- package/dist/data-structures/binary-tree/splay-tree.js +2 -5
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +213 -6
- package/dist/data-structures/binary-tree/tree-multiset.js +687 -34
- package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
- package/dist/data-structures/graph/abstract-graph.d.ts +270 -36
- package/dist/data-structures/graph/abstract-graph.js +610 -572
- package/dist/data-structures/graph/directed-graph.d.ts +173 -16
- package/dist/data-structures/graph/directed-graph.js +345 -313
- package/dist/data-structures/graph/index.d.ts +1 -0
- package/dist/data-structures/graph/index.js +1 -0
- package/dist/data-structures/graph/map-graph.d.ts +79 -0
- package/dist/data-structures/graph/map-graph.js +111 -0
- package/dist/data-structures/graph/undirected-graph.d.ts +111 -9
- package/dist/data-structures/graph/undirected-graph.js +203 -178
- package/dist/data-structures/hash/coordinate-map.d.ts +38 -1
- package/dist/data-structures/hash/coordinate-map.js +59 -36
- package/dist/data-structures/hash/coordinate-set.d.ts +32 -2
- package/dist/data-structures/hash/coordinate-set.js +49 -33
- package/dist/data-structures/hash/hash-table.d.ts +2 -1
- package/dist/data-structures/hash/hash-table.js +4 -0
- package/dist/data-structures/hash/pair.d.ts +2 -1
- package/dist/data-structures/hash/pair.js +4 -0
- package/dist/data-structures/hash/tree-map.d.ts +2 -1
- package/dist/data-structures/hash/tree-map.js +4 -0
- package/dist/data-structures/hash/tree-set.d.ts +2 -1
- package/dist/data-structures/hash/tree-set.js +4 -0
- package/dist/data-structures/heap/heap.d.ts +62 -45
- package/dist/data-structures/heap/heap.js +124 -86
- package/dist/data-structures/heap/max-heap.d.ts +13 -5
- package/dist/data-structures/heap/max-heap.js +18 -28
- package/dist/data-structures/heap/min-heap.d.ts +14 -5
- package/dist/data-structures/heap/min-heap.js +19 -28
- package/dist/data-structures/index.d.ts +1 -1
- package/dist/data-structures/index.js +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +193 -56
- package/dist/data-structures/linked-list/doubly-linked-list.js +484 -220
- package/dist/data-structures/linked-list/index.d.ts +1 -0
- package/dist/data-structures/linked-list/index.js +1 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +117 -315
- package/dist/data-structures/linked-list/singly-linked-list.js +374 -727
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -1
- package/dist/data-structures/linked-list/skip-linked-list.js +4 -0
- package/dist/data-structures/matrix/matrix.d.ts +12 -0
- package/dist/data-structures/matrix/matrix.js +21 -8
- package/dist/data-structures/matrix/matrix2d.d.ts +85 -2
- package/dist/data-structures/matrix/matrix2d.js +146 -80
- package/dist/data-structures/matrix/navigator.d.ts +36 -1
- package/dist/data-structures/matrix/navigator.js +46 -37
- package/dist/data-structures/matrix/vector2d.d.ts +142 -15
- package/dist/data-structures/matrix/vector2d.js +215 -109
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -2
- package/dist/data-structures/priority-queue/max-priority-queue.js +33 -26
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +12 -2
- package/dist/data-structures/priority-queue/min-priority-queue.js +34 -26
- package/dist/data-structures/priority-queue/priority-queue.d.ts +153 -3
- package/dist/data-structures/priority-queue/priority-queue.js +244 -143
- package/dist/data-structures/queue/deque.d.ts +141 -13
- package/dist/data-structures/queue/deque.js +200 -82
- package/dist/data-structures/queue/queue.d.ts +65 -38
- package/dist/data-structures/queue/queue.js +110 -66
- package/dist/data-structures/stack/stack.d.ts +27 -32
- package/dist/data-structures/stack/stack.js +47 -53
- package/dist/data-structures/tree/index.d.ts +1 -0
- package/dist/data-structures/tree/index.js +17 -0
- package/dist/data-structures/tree/tree.d.ts +14 -0
- package/dist/data-structures/tree/tree.js +60 -0
- package/dist/data-structures/trie/trie.d.ts +33 -10
- package/dist/data-structures/trie/trie.js +123 -208
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/interfaces/abstract-binary-tree.d.ts +90 -0
- package/dist/interfaces/abstract-graph.d.ts +17 -0
- package/dist/interfaces/avl-tree.d.ts +9 -0
- package/dist/interfaces/binary-tree.d.ts +6 -0
- package/dist/interfaces/bst.d.ts +17 -0
- package/dist/interfaces/directed-graph.d.ts +12 -0
- package/{src/data-structures/types/index.ts → dist/interfaces/index.d.ts} +10 -8
- package/dist/interfaces/index.js +31 -0
- package/{src/data-structures/hash/hash-table.ts → dist/interfaces/priority-queue.d.ts} +1 -1
- package/dist/interfaces/rb-tree.d.ts +8 -0
- package/dist/interfaces/segment-tree.d.ts +1 -0
- package/dist/interfaces/singly-linked-list.d.ts +1 -0
- package/dist/interfaces/singly-linked-list.js +2 -0
- package/dist/interfaces/tree-multiset.d.ts +7 -0
- package/dist/interfaces/tree-multiset.js +2 -0
- package/dist/interfaces/undirected-graph.d.ts +5 -0
- package/dist/interfaces/undirected-graph.js +2 -0
- package/dist/types/data-structures/abstract-binary-tree.d.ts +34 -0
- package/dist/types/data-structures/abstract-binary-tree.js +25 -0
- package/dist/types/data-structures/abstract-graph.d.ts +11 -0
- package/dist/types/data-structures/abstract-graph.js +2 -0
- package/dist/types/data-structures/avl-tree.d.ts +4 -0
- package/dist/types/data-structures/avl-tree.js +2 -0
- package/dist/types/data-structures/binary-tree.d.ts +4 -0
- package/dist/types/data-structures/binary-tree.js +2 -0
- package/dist/types/data-structures/bst.d.ts +13 -0
- package/dist/types/data-structures/bst.js +9 -0
- package/dist/types/data-structures/directed-graph.d.ts +6 -0
- package/dist/types/data-structures/directed-graph.js +9 -0
- package/dist/types/data-structures/doubly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/doubly-linked-list.js +2 -0
- package/dist/types/data-structures/heap.d.ts +3 -0
- package/dist/types/data-structures/heap.js +2 -0
- package/dist/{data-structures/types → types/data-structures}/index.d.ts +3 -1
- package/dist/{data-structures/types → types/data-structures}/index.js +3 -1
- package/dist/types/data-structures/map-graph.d.ts +1 -0
- package/dist/types/data-structures/map-graph.js +2 -0
- package/dist/{data-structures/types → types/data-structures}/navigator.d.ts +2 -2
- package/dist/types/data-structures/navigator.js +2 -0
- package/dist/{data-structures/types → types/data-structures}/priority-queue.d.ts +2 -2
- package/dist/types/data-structures/priority-queue.js +2 -0
- package/dist/types/data-structures/rb-tree.d.ts +8 -0
- package/dist/types/data-structures/rb-tree.js +8 -0
- package/dist/types/data-structures/segment-tree.js +2 -0
- package/dist/types/data-structures/singly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/singly-linked-list.js +2 -0
- package/dist/types/data-structures/tree-multiset.d.ts +4 -0
- package/dist/types/data-structures/tree-multiset.js +2 -0
- package/dist/types/helpers.d.ts +1 -0
- package/dist/types/helpers.js +2 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/index.js +19 -0
- package/dist/types/utils/index.d.ts +2 -0
- package/dist/types/utils/index.js +18 -0
- package/dist/types/utils/utils.d.ts +7 -0
- package/dist/types/utils/utils.js +2 -0
- package/dist/types/utils/validate-type.d.ts +19 -0
- package/dist/types/utils/validate-type.js +2 -0
- package/dist/utils/utils.d.ts +17 -103
- package/dist/utils/utils.js +40 -625
- package/package.json +96 -23
- package/.idea/data-structure-typed.iml +0 -12
- package/.idea/modules.xml +0 -8
- package/.idea/vcs.xml +0 -6
- package/dist/data-structures/trampoline.d.ts +0 -16
- package/dist/data-structures/trampoline.js +0 -130
- package/dist/data-structures/types/abstract-graph.d.ts +0 -29
- package/dist/data-structures/types/avl-tree.d.ts +0 -5
- package/dist/data-structures/types/binary-tree.d.ts +0 -16
- package/dist/data-structures/types/bst.d.ts +0 -7
- package/dist/data-structures/types/directed-graph.d.ts +0 -10
- package/dist/data-structures/types/doubly-linked-list.d.ts +0 -1
- package/dist/data-structures/types/heap.d.ts +0 -7
- package/dist/data-structures/types/singly-linked-list.d.ts +0 -5
- package/dist/data-structures/types/tree-multiset.d.ts +0 -5
- package/dist/data-structures/types/utils.d.ts +0 -52
- package/dist/data-structures/types/utils.js +0 -54
- package/src/data-structures/binary-tree/aa-tree.ts +0 -3
- package/src/data-structures/binary-tree/avl-tree.ts +0 -227
- package/src/data-structures/binary-tree/b-tree.ts +0 -3
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -33
- package/src/data-structures/binary-tree/binary-tree.ts +0 -1133
- package/src/data-structures/binary-tree/bst.ts +0 -395
- 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/index.ts +0 -11
- package/src/data-structures/binary-tree/rb-tree.ts +0 -3
- package/src/data-structures/binary-tree/segment-tree.ts +0 -172
- package/src/data-structures/binary-tree/splay-tree.ts +0 -3
- package/src/data-structures/binary-tree/tree-multiset.ts +0 -18
- package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
- package/src/data-structures/diagrams/README.md +0 -7
- package/src/data-structures/graph/abstract-graph.ts +0 -753
- package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.png +0 -0
- package/src/data-structures/graph/diagrams/adjacency-list.png +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.png +0 -0
- package/src/data-structures/graph/diagrams/adjacency-matrix.png +0 -0
- package/src/data-structures/graph/diagrams/dfs-can-do.png +0 -0
- package/src/data-structures/graph/diagrams/edge-list-pros-cons.png +0 -0
- package/src/data-structures/graph/diagrams/edge-list.png +0 -0
- package/src/data-structures/graph/diagrams/max-flow.png +0 -0
- package/src/data-structures/graph/diagrams/mst.png +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/directed-graph.ts +0 -306
- package/src/data-structures/graph/index.ts +0 -3
- package/src/data-structures/graph/undirected-graph.ts +0 -155
- package/src/data-structures/hash/coordinate-map.ts +0 -24
- package/src/data-structures/hash/coordinate-set.ts +0 -20
- package/src/data-structures/hash/index.ts +0 -6
- package/src/data-structures/heap/heap.ts +0 -127
- package/src/data-structures/heap/index.ts +0 -3
- package/src/data-structures/heap/max-heap.ts +0 -23
- package/src/data-structures/heap/min-heap.ts +0 -25
- package/src/data-structures/index.ts +0 -12
- package/src/data-structures/linked-list/doubly-linked-list.ts +0 -250
- package/src/data-structures/linked-list/index.ts +0 -2
- package/src/data-structures/linked-list/singly-linked-list.ts +0 -736
- package/src/data-structures/linked-list/skip-linked-list.ts +0 -1
- package/src/data-structures/matrix/index.ts +0 -4
- package/src/data-structures/matrix/matrix.ts +0 -13
- package/src/data-structures/matrix/matrix2d.ts +0 -125
- package/src/data-structures/matrix/navigator.ts +0 -87
- package/src/data-structures/matrix/vector2d.ts +0 -189
- package/src/data-structures/priority-queue/index.ts +0 -3
- package/src/data-structures/priority-queue/max-priority-queue.ts +0 -13
- package/src/data-structures/priority-queue/min-priority-queue.ts +0 -13
- package/src/data-structures/priority-queue/priority-queue.ts +0 -200
- package/src/data-structures/queue/deque.ts +0 -139
- package/src/data-structures/queue/index.ts +0 -2
- package/src/data-structures/queue/queue.ts +0 -122
- package/src/data-structures/stack/index.ts +0 -1
- package/src/data-structures/stack/stack.ts +0 -103
- package/src/data-structures/trampoline.ts +0 -51
- package/src/data-structures/trie/index.ts +0 -1
- package/src/data-structures/trie/trie.ts +0 -203
- package/src/data-structures/types/abstract-graph.ts +0 -51
- package/src/data-structures/types/avl-tree.ts +0 -6
- package/src/data-structures/types/binary-tree.ts +0 -15
- package/src/data-structures/types/bst.ts +0 -5
- package/src/data-structures/types/directed-graph.ts +0 -18
- package/src/data-structures/types/doubly-linked-list.ts +0 -1
- package/src/data-structures/types/heap.ts +0 -8
- package/src/data-structures/types/navigator.ts +0 -12
- package/src/data-structures/types/priority-queue.ts +0 -9
- package/src/data-structures/types/segment-tree.ts +0 -1
- package/src/data-structures/types/singly-linked-list.ts +0 -15
- package/src/data-structures/types/tree-multiset.ts +0 -3
- package/src/data-structures/types/utils.ts +0 -173
- package/src/index.ts +0 -1
- package/src/utils/index.ts +0 -1
- package/src/utils/utils.ts +0 -505
- package/tsconfig.json +0 -56
- /package/dist/{data-structures/types/abstract-graph.js → interfaces/abstract-binary-tree.js} +0 -0
- /package/dist/{data-structures/types/avl-tree.js → interfaces/abstract-graph.js} +0 -0
- /package/dist/{data-structures/types/binary-tree.js → interfaces/avl-tree.js} +0 -0
- /package/dist/{data-structures/types/bst.js → interfaces/binary-tree.js} +0 -0
- /package/dist/{data-structures/types/directed-graph.js → interfaces/bst.js} +0 -0
- /package/dist/{data-structures/types/doubly-linked-list.js → interfaces/directed-graph.js} +0 -0
- /package/{src/data-structures/hash/pair.ts → dist/interfaces/doubly-linked-list.d.ts} +0 -0
- /package/dist/{data-structures/types/heap.js → interfaces/doubly-linked-list.js} +0 -0
- /package/{src/data-structures/hash/tree-map.ts → dist/interfaces/heap.d.ts} +0 -0
- /package/dist/{data-structures/types/navigator.js → interfaces/heap.js} +0 -0
- /package/{src/data-structures/hash/tree-set.ts → dist/interfaces/navigator.d.ts} +0 -0
- /package/dist/{data-structures/types/priority-queue.js → interfaces/navigator.js} +0 -0
- /package/dist/{data-structures/types/segment-tree.js → interfaces/priority-queue.js} +0 -0
- /package/dist/{data-structures/types/singly-linked-list.js → interfaces/rb-tree.js} +0 -0
- /package/dist/{data-structures/types/tree-multiset.js → interfaces/segment-tree.js} +0 -0
- /package/dist/{data-structures/types → types/data-structures}/segment-tree.d.ts +0 -0
|
@@ -14,6 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./abstract-binary-tree"), exports);
|
|
17
18
|
__exportStar(require("./binary-tree"), exports);
|
|
18
19
|
__exportStar(require("./bst"), exports);
|
|
19
20
|
__exportStar(require("./binary-indexed-tree"), exports);
|
|
@@ -1,2 +1,19 @@
|
|
|
1
|
-
|
|
1
|
+
import { BinaryTreeNodeId, RBColor, RBTreeNodeNested, RBTreeOptions } from '../../types';
|
|
2
|
+
import { IRBTree, IRBTreeNode } from '../../interfaces/rb-tree';
|
|
3
|
+
import { BST, BSTNode } from './bst';
|
|
4
|
+
export declare class RBTreeNode<T = any, NEIGHBOR extends RBTreeNode<T, NEIGHBOR> = RBTreeNodeNested<T>> extends BSTNode<T, NEIGHBOR> implements IRBTreeNode<T, NEIGHBOR> {
|
|
5
|
+
constructor(id: BinaryTreeNodeId, val?: T, color?: RBColor);
|
|
6
|
+
private _color;
|
|
7
|
+
get color(): RBColor;
|
|
8
|
+
set color(value: RBColor);
|
|
9
|
+
}
|
|
10
|
+
export declare class RBTree<N extends RBTreeNode<N['val'], N> = RBTreeNode> extends BST<N> implements IRBTree<N> {
|
|
11
|
+
constructor(options?: RBTreeOptions);
|
|
12
|
+
createNode(id: BinaryTreeNodeId, val?: N['val']): N;
|
|
13
|
+
insert(id: number, val?: N | null): void;
|
|
14
|
+
private leftRotate;
|
|
15
|
+
private rightRotate;
|
|
16
|
+
private insertFixup;
|
|
17
|
+
private deleteFixup;
|
|
18
|
+
private transplant;
|
|
2
19
|
}
|
|
@@ -1,9 +1,44 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RBTree = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
exports.RBTree = exports.RBTreeNode = void 0;
|
|
4
|
+
const types_1 = require("../../types");
|
|
5
|
+
const bst_1 = require("./bst");
|
|
6
|
+
class RBTreeNode extends bst_1.BSTNode {
|
|
7
|
+
constructor(id, val, color = types_1.RBColor.RED) {
|
|
8
|
+
super(id, val);
|
|
9
|
+
this._color = color;
|
|
6
10
|
}
|
|
7
|
-
|
|
8
|
-
|
|
11
|
+
get color() {
|
|
12
|
+
return this._color;
|
|
13
|
+
}
|
|
14
|
+
set color(value) {
|
|
15
|
+
this._color = value;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.RBTreeNode = RBTreeNode;
|
|
19
|
+
class RBTree extends bst_1.BST {
|
|
20
|
+
constructor(options) {
|
|
21
|
+
super(options);
|
|
22
|
+
}
|
|
23
|
+
createNode(id, val) {
|
|
24
|
+
return new RBTreeNode(id, val, types_1.RBColor.RED);
|
|
25
|
+
}
|
|
26
|
+
// private override _root: BinaryTreeNode<N> | null = null;
|
|
27
|
+
//
|
|
28
|
+
// override get root(): BinaryTreeNode<N> | null {
|
|
29
|
+
// return this._root;
|
|
30
|
+
// }
|
|
31
|
+
insert(id, val) {
|
|
32
|
+
}
|
|
33
|
+
leftRotate(node) {
|
|
34
|
+
}
|
|
35
|
+
rightRotate(node) {
|
|
36
|
+
}
|
|
37
|
+
insertFixup(node) {
|
|
38
|
+
}
|
|
39
|
+
deleteFixup(node) {
|
|
40
|
+
}
|
|
41
|
+
transplant(u, v) {
|
|
42
|
+
}
|
|
43
|
+
}
|
|
9
44
|
exports.RBTree = RBTree;
|
|
@@ -1,33 +1,83 @@
|
|
|
1
|
-
|
|
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 type { SegmentTreeNodeVal } from '../../types';
|
|
2
9
|
export declare class SegmentTreeNode {
|
|
3
10
|
constructor(start: number, end: number, sum: number, val?: SegmentTreeNodeVal | null);
|
|
4
|
-
|
|
11
|
+
private _start;
|
|
5
12
|
get start(): number;
|
|
6
13
|
set start(v: number);
|
|
7
|
-
|
|
14
|
+
private _end;
|
|
8
15
|
get end(): number;
|
|
9
16
|
set end(v: number);
|
|
10
|
-
|
|
17
|
+
private _val;
|
|
11
18
|
get val(): SegmentTreeNodeVal | null;
|
|
12
19
|
set val(v: SegmentTreeNodeVal | null);
|
|
13
|
-
|
|
20
|
+
private _sum;
|
|
14
21
|
get sum(): number;
|
|
15
22
|
set sum(v: number);
|
|
16
|
-
|
|
23
|
+
private _left;
|
|
17
24
|
get left(): SegmentTreeNode | null;
|
|
18
25
|
set left(v: SegmentTreeNode | null);
|
|
19
|
-
|
|
26
|
+
private _right;
|
|
20
27
|
get right(): SegmentTreeNode | null;
|
|
21
28
|
set right(v: SegmentTreeNode | null);
|
|
22
29
|
}
|
|
23
30
|
export declare class SegmentTree {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
31
|
+
/**
|
|
32
|
+
* The constructor initializes the values, start, end, and root properties of an object.
|
|
33
|
+
* @param {number[]} values - An array of numbers that will be used to build a binary search tree.
|
|
34
|
+
* @param {number} [start] - The `start` parameter is the index of the first element in the `values` array that should
|
|
35
|
+
* be included in the range. If no value is provided for `start`, it defaults to 0, which means the range starts from
|
|
36
|
+
* the beginning of the array.
|
|
37
|
+
* @param {number} [end] - The "end" parameter is the index of the last element in the "values" array that should be
|
|
38
|
+
* included in the range. If not provided, it defaults to the index of the last element in the "values" array.
|
|
39
|
+
*/
|
|
27
40
|
constructor(values: number[], start?: number, end?: number);
|
|
28
|
-
|
|
41
|
+
private _values;
|
|
42
|
+
get values(): number[];
|
|
43
|
+
private _start;
|
|
44
|
+
get start(): number;
|
|
45
|
+
private _end;
|
|
46
|
+
get end(): number;
|
|
47
|
+
private _root;
|
|
29
48
|
get root(): SegmentTreeNode | null;
|
|
49
|
+
/**
|
|
50
|
+
* The function builds a segment tree by recursively dividing the given range into smaller segments and creating nodes
|
|
51
|
+
* for each segment.
|
|
52
|
+
* @param {number} start - The `start` parameter represents the starting index of the segment or range for which we are
|
|
53
|
+
* building the segment tree.
|
|
54
|
+
* @param {number} end - The `end` parameter represents the ending index of the segment or range for which we are
|
|
55
|
+
* building the segment tree.
|
|
56
|
+
* @returns a SegmentTreeNode object.
|
|
57
|
+
*/
|
|
30
58
|
build(start: number, end: number): SegmentTreeNode;
|
|
59
|
+
/**
|
|
60
|
+
* The function updates the value of a node in a segment tree and recalculates the sum of its children if they exist.
|
|
61
|
+
* @param {number} index - The index parameter represents the index of the node in the segment tree that needs to be
|
|
62
|
+
* updated.
|
|
63
|
+
* @param {number} sum - The `sum` parameter represents the new value that should be assigned to the `sum` property of
|
|
64
|
+
* the `SegmentTreeNode` at the specified `index`.
|
|
65
|
+
* @param {SegmentTreeNodeVal} [val] - The `val` parameter is an optional value that can be assigned to the `val`
|
|
66
|
+
* property of the `SegmentTreeNode` object. It is not currently used in the code, but you can uncomment the line `//
|
|
67
|
+
* cur.val = val;` and pass a value for `val` in the
|
|
68
|
+
* @returns The function does not return anything.
|
|
69
|
+
*/
|
|
31
70
|
updateNode(index: number, sum: number, val?: SegmentTreeNodeVal): void;
|
|
71
|
+
/**
|
|
72
|
+
* The function `querySumByRange` calculates the sum of values within a given range in a segment tree.
|
|
73
|
+
* @param {number} indexA - The starting index of the range for which you want to calculate the sum.
|
|
74
|
+
* @param {number} indexB - The parameter `indexB` represents the ending index of the range for which you want to
|
|
75
|
+
* calculate the sum.
|
|
76
|
+
* @returns The function `querySumByRange` returns a number.
|
|
77
|
+
*/
|
|
32
78
|
querySumByRange(indexA: number, indexB: number): number;
|
|
79
|
+
protected _setValues(value: number[]): void;
|
|
80
|
+
protected _setStart(value: number): void;
|
|
81
|
+
protected _setEnd(value: number): void;
|
|
82
|
+
protected _setRoot(v: SegmentTreeNode | null): void;
|
|
33
83
|
}
|
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* data-structure-typed
|
|
4
|
+
*
|
|
5
|
+
* @author Tyler Zeng
|
|
6
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
7
|
+
* @license MIT License
|
|
8
|
+
*/
|
|
2
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
10
|
exports.SegmentTree = exports.SegmentTreeNode = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
11
|
+
class SegmentTreeNode {
|
|
12
|
+
constructor(start, end, sum, val) {
|
|
6
13
|
this._start = 0;
|
|
7
14
|
this._end = 0;
|
|
8
15
|
this._val = null;
|
|
@@ -14,71 +21,55 @@ var SegmentTreeNode = /** @class */ (function () {
|
|
|
14
21
|
this._sum = sum;
|
|
15
22
|
this._val = val || null;
|
|
16
23
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
enumerable: false,
|
|
55
|
-
configurable: true
|
|
56
|
-
});
|
|
57
|
-
Object.defineProperty(SegmentTreeNode.prototype, "left", {
|
|
58
|
-
get: function () {
|
|
59
|
-
return this._left;
|
|
60
|
-
},
|
|
61
|
-
set: function (v) {
|
|
62
|
-
this._left = v;
|
|
63
|
-
},
|
|
64
|
-
enumerable: false,
|
|
65
|
-
configurable: true
|
|
66
|
-
});
|
|
67
|
-
Object.defineProperty(SegmentTreeNode.prototype, "right", {
|
|
68
|
-
get: function () {
|
|
69
|
-
return this._right;
|
|
70
|
-
},
|
|
71
|
-
set: function (v) {
|
|
72
|
-
this._right = v;
|
|
73
|
-
},
|
|
74
|
-
enumerable: false,
|
|
75
|
-
configurable: true
|
|
76
|
-
});
|
|
77
|
-
return SegmentTreeNode;
|
|
78
|
-
}());
|
|
24
|
+
get start() {
|
|
25
|
+
return this._start;
|
|
26
|
+
}
|
|
27
|
+
set start(v) {
|
|
28
|
+
this._start = v;
|
|
29
|
+
}
|
|
30
|
+
get end() {
|
|
31
|
+
return this._end;
|
|
32
|
+
}
|
|
33
|
+
set end(v) {
|
|
34
|
+
this._end = v;
|
|
35
|
+
}
|
|
36
|
+
get val() {
|
|
37
|
+
return this._val;
|
|
38
|
+
}
|
|
39
|
+
set val(v) {
|
|
40
|
+
this._val = v;
|
|
41
|
+
}
|
|
42
|
+
get sum() {
|
|
43
|
+
return this._sum;
|
|
44
|
+
}
|
|
45
|
+
set sum(v) {
|
|
46
|
+
this._sum = v;
|
|
47
|
+
}
|
|
48
|
+
get left() {
|
|
49
|
+
return this._left;
|
|
50
|
+
}
|
|
51
|
+
set left(v) {
|
|
52
|
+
this._left = v;
|
|
53
|
+
}
|
|
54
|
+
get right() {
|
|
55
|
+
return this._right;
|
|
56
|
+
}
|
|
57
|
+
set right(v) {
|
|
58
|
+
this._right = v;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
79
61
|
exports.SegmentTreeNode = SegmentTreeNode;
|
|
80
|
-
|
|
81
|
-
|
|
62
|
+
class SegmentTree {
|
|
63
|
+
/**
|
|
64
|
+
* The constructor initializes the values, start, end, and root properties of an object.
|
|
65
|
+
* @param {number[]} values - An array of numbers that will be used to build a binary search tree.
|
|
66
|
+
* @param {number} [start] - The `start` parameter is the index of the first element in the `values` array that should
|
|
67
|
+
* be included in the range. If no value is provided for `start`, it defaults to 0, which means the range starts from
|
|
68
|
+
* the beginning of the array.
|
|
69
|
+
* @param {number} [end] - The "end" parameter is the index of the last element in the "values" array that should be
|
|
70
|
+
* included in the range. If not provided, it defaults to the index of the last element in the "values" array.
|
|
71
|
+
*/
|
|
72
|
+
constructor(values, start, end) {
|
|
82
73
|
this._values = [];
|
|
83
74
|
this._start = 0;
|
|
84
75
|
start = start || 0;
|
|
@@ -88,37 +79,61 @@ var SegmentTree = /** @class */ (function () {
|
|
|
88
79
|
this._end = end;
|
|
89
80
|
this._root = this.build(start, end);
|
|
90
81
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
82
|
+
get values() {
|
|
83
|
+
return this._values;
|
|
84
|
+
}
|
|
85
|
+
get start() {
|
|
86
|
+
return this._start;
|
|
87
|
+
}
|
|
88
|
+
get end() {
|
|
89
|
+
return this._end;
|
|
90
|
+
}
|
|
91
|
+
get root() {
|
|
92
|
+
return this._root;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* The function builds a segment tree by recursively dividing the given range into smaller segments and creating nodes
|
|
96
|
+
* for each segment.
|
|
97
|
+
* @param {number} start - The `start` parameter represents the starting index of the segment or range for which we are
|
|
98
|
+
* building the segment tree.
|
|
99
|
+
* @param {number} end - The `end` parameter represents the ending index of the segment or range for which we are
|
|
100
|
+
* building the segment tree.
|
|
101
|
+
* @returns a SegmentTreeNode object.
|
|
102
|
+
*/
|
|
103
|
+
build(start, end) {
|
|
104
|
+
if (start === end)
|
|
100
105
|
return new SegmentTreeNode(start, end, this._values[start]);
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
var cur = new SegmentTreeNode(start, end, left.sum + right.sum);
|
|
106
|
+
const mid = start + Math.floor((end - start) / 2);
|
|
107
|
+
const left = this.build(start, mid);
|
|
108
|
+
const right = this.build(mid + 1, end);
|
|
109
|
+
const cur = new SegmentTreeNode(start, end, left.sum + right.sum);
|
|
106
110
|
cur.left = left;
|
|
107
111
|
cur.right = right;
|
|
108
112
|
return cur;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* The function updates the value of a node in a segment tree and recalculates the sum of its children if they exist.
|
|
116
|
+
* @param {number} index - The index parameter represents the index of the node in the segment tree that needs to be
|
|
117
|
+
* updated.
|
|
118
|
+
* @param {number} sum - The `sum` parameter represents the new value that should be assigned to the `sum` property of
|
|
119
|
+
* the `SegmentTreeNode` at the specified `index`.
|
|
120
|
+
* @param {SegmentTreeNodeVal} [val] - The `val` parameter is an optional value that can be assigned to the `val`
|
|
121
|
+
* property of the `SegmentTreeNode` object. It is not currently used in the code, but you can uncomment the line `//
|
|
122
|
+
* cur.val = val;` and pass a value for `val` in the
|
|
123
|
+
* @returns The function does not return anything.
|
|
124
|
+
*/
|
|
125
|
+
updateNode(index, sum, val) {
|
|
126
|
+
const root = this.root || null;
|
|
112
127
|
if (!root) {
|
|
113
128
|
return;
|
|
114
129
|
}
|
|
115
|
-
|
|
130
|
+
const dfs = (cur, index, sum, val) => {
|
|
116
131
|
if (cur.start === cur.end && cur.start === index) {
|
|
117
132
|
cur.sum = sum;
|
|
118
133
|
// cur.val = val;
|
|
119
134
|
return;
|
|
120
135
|
}
|
|
121
|
-
|
|
136
|
+
const mid = cur.start + Math.floor((cur.end - cur.start) / 2);
|
|
122
137
|
if (index <= mid) {
|
|
123
138
|
if (cur.left) {
|
|
124
139
|
dfs(cur.left, index, sum, val);
|
|
@@ -134,17 +149,24 @@ var SegmentTree = /** @class */ (function () {
|
|
|
134
149
|
}
|
|
135
150
|
};
|
|
136
151
|
dfs(root, index, sum);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* The function `querySumByRange` calculates the sum of values within a given range in a segment tree.
|
|
155
|
+
* @param {number} indexA - The starting index of the range for which you want to calculate the sum.
|
|
156
|
+
* @param {number} indexB - The parameter `indexB` represents the ending index of the range for which you want to
|
|
157
|
+
* calculate the sum.
|
|
158
|
+
* @returns The function `querySumByRange` returns a number.
|
|
159
|
+
*/
|
|
160
|
+
querySumByRange(indexA, indexB) {
|
|
161
|
+
const root = this.root || null;
|
|
140
162
|
if (!root) {
|
|
141
163
|
return 0;
|
|
142
164
|
}
|
|
143
|
-
|
|
165
|
+
const dfs = (cur, i, j) => {
|
|
144
166
|
if (cur.start === i && cur.end === j) {
|
|
145
167
|
return cur.sum;
|
|
146
168
|
}
|
|
147
|
-
|
|
169
|
+
const mid = cur.start + Math.floor((cur.end - cur.start) / 2);
|
|
148
170
|
if (j <= mid) {
|
|
149
171
|
// TODO after no-non-null-assertion not ensure the logic
|
|
150
172
|
if (cur.left) {
|
|
@@ -175,7 +197,18 @@ var SegmentTree = /** @class */ (function () {
|
|
|
175
197
|
}
|
|
176
198
|
};
|
|
177
199
|
return dfs(root, indexA, indexB);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
|
|
200
|
+
}
|
|
201
|
+
_setValues(value) {
|
|
202
|
+
this._values = value;
|
|
203
|
+
}
|
|
204
|
+
_setStart(value) {
|
|
205
|
+
this._start = value;
|
|
206
|
+
}
|
|
207
|
+
_setEnd(value) {
|
|
208
|
+
this._end = value;
|
|
209
|
+
}
|
|
210
|
+
_setRoot(v) {
|
|
211
|
+
this._root = v;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
181
214
|
exports.SegmentTree = SegmentTree;
|
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SplayTree = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
7
|
-
return SplayTree;
|
|
8
|
-
}());
|
|
4
|
+
class SplayTree {
|
|
5
|
+
}
|
|
9
6
|
exports.SplayTree = SplayTree;
|