linked-list-typed 1.38.0 → 1.38.2
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/dist/data-structures/binary-tree/avl-tree.d.ts +100 -0
- package/dist/data-structures/binary-tree/avl-tree.js +341 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +144 -0
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +261 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +409 -0
- package/dist/data-structures/binary-tree/binary-tree.js +1065 -0
- package/dist/data-structures/binary-tree/bst.d.ts +167 -0
- package/dist/data-structures/binary-tree/bst.js +512 -0
- package/dist/data-structures/binary-tree/index.d.ts +7 -0
- package/dist/data-structures/binary-tree/index.js +23 -0
- package/dist/data-structures/binary-tree/rb-tree.d.ts +13 -0
- package/dist/data-structures/binary-tree/rb-tree.js +27 -0
- package/dist/data-structures/binary-tree/segment-tree.d.ts +83 -0
- package/dist/data-structures/binary-tree/segment-tree.js +228 -0
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +122 -0
- package/dist/data-structures/binary-tree/tree-multiset.js +351 -0
- package/dist/data-structures/graph/abstract-graph.d.ts +332 -0
- package/dist/data-structures/graph/abstract-graph.js +923 -0
- package/dist/data-structures/graph/directed-graph.d.ts +200 -0
- package/dist/data-structures/graph/directed-graph.js +422 -0
- package/dist/data-structures/graph/index.d.ts +4 -0
- package/dist/data-structures/graph/index.js +20 -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 +127 -0
- package/dist/data-structures/graph/undirected-graph.js +252 -0
- package/dist/data-structures/hash/coordinate-map.d.ts +45 -0
- package/dist/data-structures/hash/coordinate-map.js +65 -0
- package/dist/data-structures/hash/coordinate-set.d.ts +37 -0
- package/dist/data-structures/hash/coordinate-set.js +55 -0
- package/dist/data-structures/hash/hash-map.d.ts +56 -0
- package/dist/data-structures/hash/hash-map.js +171 -0
- package/dist/data-structures/hash/hash-table.d.ts +106 -0
- package/dist/data-structures/hash/hash-table.js +245 -0
- package/dist/data-structures/hash/index.d.ts +6 -0
- package/dist/data-structures/hash/index.js +22 -0
- package/dist/data-structures/hash/tree-map.d.ts +2 -0
- package/dist/data-structures/hash/tree-map.js +6 -0
- package/dist/data-structures/hash/tree-set.d.ts +2 -0
- package/dist/data-structures/hash/tree-set.js +6 -0
- package/dist/data-structures/heap/heap.d.ts +224 -0
- package/dist/data-structures/heap/heap.js +497 -0
- package/dist/data-structures/heap/index.d.ts +3 -0
- package/dist/data-structures/heap/index.js +19 -0
- package/dist/data-structures/heap/max-heap.d.ts +12 -0
- package/dist/data-structures/heap/max-heap.js +24 -0
- package/dist/data-structures/heap/min-heap.d.ts +12 -0
- package/dist/data-structures/heap/min-heap.js +24 -0
- package/dist/data-structures/index.d.ts +11 -0
- package/dist/data-structures/index.js +27 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +234 -0
- package/dist/data-structures/linked-list/doubly-linked-list.js +583 -0
- package/dist/data-structures/linked-list/index.d.ts +3 -0
- package/dist/data-structures/linked-list/index.js +19 -0
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +157 -0
- package/dist/data-structures/linked-list/singly-linked-list.js +448 -0
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +61 -0
- package/dist/data-structures/linked-list/skip-linked-list.js +142 -0
- package/dist/data-structures/matrix/index.d.ts +4 -0
- package/dist/data-structures/matrix/index.js +20 -0
- package/dist/data-structures/matrix/matrix.d.ts +21 -0
- package/dist/data-structures/matrix/matrix.js +28 -0
- package/dist/data-structures/matrix/matrix2d.d.ts +108 -0
- package/dist/data-structures/matrix/matrix2d.js +203 -0
- package/dist/data-structures/matrix/navigator.d.ts +52 -0
- package/dist/data-structures/matrix/navigator.js +106 -0
- package/dist/data-structures/matrix/vector2d.d.ts +201 -0
- package/dist/data-structures/matrix/vector2d.js +291 -0
- package/dist/data-structures/priority-queue/index.d.ts +3 -0
- package/dist/data-structures/priority-queue/index.js +19 -0
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -0
- package/dist/data-structures/priority-queue/max-priority-queue.js +24 -0
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +12 -0
- package/dist/data-structures/priority-queue/min-priority-queue.js +24 -0
- package/dist/data-structures/priority-queue/priority-queue.d.ts +12 -0
- package/dist/data-structures/priority-queue/priority-queue.js +17 -0
- package/dist/data-structures/queue/deque.d.ts +165 -0
- package/dist/data-structures/queue/deque.js +276 -0
- package/dist/data-structures/queue/index.d.ts +2 -0
- package/dist/data-structures/queue/index.js +18 -0
- package/dist/data-structures/queue/queue.d.ts +107 -0
- package/dist/data-structures/queue/queue.js +170 -0
- package/dist/data-structures/stack/index.d.ts +1 -0
- package/dist/data-structures/stack/index.js +17 -0
- package/dist/data-structures/stack/stack.d.ts +63 -0
- package/dist/data-structures/stack/stack.js +91 -0
- 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 +58 -0
- package/dist/data-structures/trie/index.d.ts +1 -0
- package/dist/data-structures/trie/index.js +17 -0
- package/dist/data-structures/trie/trie.d.ts +84 -0
- package/dist/data-structures/trie/trie.js +268 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.js +3 -3
- package/dist/interfaces/binary-tree.d.ts +7 -0
- package/dist/interfaces/binary-tree.js +2 -0
- package/dist/interfaces/doubly-linked-list.d.ts +1 -0
- package/dist/interfaces/doubly-linked-list.js +2 -0
- package/dist/interfaces/graph.d.ts +5 -0
- package/dist/interfaces/graph.js +2 -0
- package/dist/interfaces/heap.d.ts +1 -0
- package/dist/interfaces/heap.js +2 -0
- package/dist/interfaces/index.d.ts +8 -0
- package/dist/interfaces/index.js +24 -0
- package/dist/interfaces/navigator.d.ts +1 -0
- package/dist/interfaces/navigator.js +2 -0
- package/dist/interfaces/priority-queue.d.ts +1 -0
- package/dist/interfaces/priority-queue.js +2 -0
- package/dist/interfaces/segment-tree.d.ts +1 -0
- package/dist/interfaces/segment-tree.js +2 -0
- package/dist/interfaces/singly-linked-list.d.ts +1 -0
- package/dist/interfaces/singly-linked-list.js +2 -0
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +4 -0
- package/dist/types/data-structures/binary-tree/avl-tree.js +2 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +2 -0
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +31 -0
- package/dist/types/data-structures/binary-tree/binary-tree.js +24 -0
- package/dist/types/data-structures/binary-tree/bst.d.ts +7 -0
- package/dist/types/data-structures/binary-tree/bst.js +2 -0
- package/dist/types/data-structures/binary-tree/index.d.ts +6 -0
- package/dist/types/data-structures/binary-tree/index.js +22 -0
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +8 -0
- package/dist/types/data-structures/binary-tree/rb-tree.js +8 -0
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
- package/dist/types/data-structures/binary-tree/segment-tree.js +2 -0
- package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +4 -0
- package/dist/types/data-structures/binary-tree/tree-multiset.js +2 -0
- package/dist/types/data-structures/graph/abstract-graph.d.ts +10 -0
- package/dist/types/data-structures/graph/abstract-graph.js +2 -0
- package/dist/types/data-structures/graph/directed-graph.d.ts +6 -0
- package/dist/types/data-structures/graph/directed-graph.js +9 -0
- package/dist/types/data-structures/graph/index.d.ts +3 -0
- package/dist/types/data-structures/graph/index.js +19 -0
- package/dist/types/data-structures/graph/map-graph.d.ts +1 -0
- package/dist/types/data-structures/graph/map-graph.js +2 -0
- package/dist/types/data-structures/graph/undirected-graph.d.ts +1 -0
- package/dist/types/data-structures/graph/undirected-graph.js +2 -0
- package/dist/types/data-structures/hash/coordinate-map.d.ts +1 -0
- package/dist/types/data-structures/hash/coordinate-map.js +2 -0
- package/dist/types/data-structures/hash/coordinate-set.d.ts +1 -0
- package/dist/types/data-structures/hash/coordinate-set.js +2 -0
- package/dist/types/data-structures/hash/hash-map.d.ts +1 -0
- package/dist/types/data-structures/hash/hash-map.js +2 -0
- package/dist/types/data-structures/hash/hash-table.d.ts +1 -0
- package/dist/types/data-structures/hash/hash-table.js +2 -0
- package/dist/types/data-structures/hash/index.d.ts +1 -0
- package/dist/types/data-structures/hash/index.js +2 -0
- package/dist/types/data-structures/hash/tree-map.d.ts +1 -0
- package/dist/types/data-structures/hash/tree-map.js +2 -0
- package/dist/types/data-structures/hash/tree-set.d.ts +1 -0
- package/dist/types/data-structures/hash/tree-set.js +2 -0
- package/dist/types/data-structures/heap/heap.d.ts +1 -0
- package/dist/types/data-structures/heap/heap.js +2 -0
- package/dist/types/data-structures/heap/index.d.ts +1 -0
- package/dist/types/data-structures/heap/index.js +17 -0
- package/dist/types/data-structures/heap/max-heap.d.ts +1 -0
- package/dist/types/data-structures/heap/max-heap.js +2 -0
- package/dist/types/data-structures/heap/min-heap.d.ts +1 -0
- package/dist/types/data-structures/heap/min-heap.js +2 -0
- package/dist/types/data-structures/index.d.ts +11 -0
- package/dist/types/data-structures/index.js +27 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/linked-list/doubly-linked-list.js +2 -0
- package/dist/types/data-structures/linked-list/index.d.ts +2 -0
- package/dist/types/data-structures/linked-list/index.js +18 -0
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +1 -0
- package/dist/types/data-structures/linked-list/singly-linked-list.js +2 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +1 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.js +2 -0
- package/dist/types/data-structures/matrix/index.d.ts +1 -0
- package/dist/types/data-structures/matrix/index.js +17 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +1 -0
- package/dist/types/data-structures/matrix/matrix.js +2 -0
- package/dist/types/data-structures/matrix/matrix2d.d.ts +1 -0
- package/dist/types/data-structures/matrix/matrix2d.js +2 -0
- package/dist/types/data-structures/matrix/navigator.d.ts +14 -0
- package/dist/types/data-structures/matrix/navigator.js +2 -0
- package/dist/types/data-structures/matrix/vector2d.d.ts +1 -0
- package/dist/types/data-structures/matrix/vector2d.js +2 -0
- package/dist/types/data-structures/priority-queue/index.d.ts +3 -0
- package/dist/types/data-structures/priority-queue/index.js +19 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
- package/dist/types/data-structures/priority-queue/max-priority-queue.js +2 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
- package/dist/types/data-structures/priority-queue/min-priority-queue.js +2 -0
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -0
- package/dist/types/data-structures/priority-queue/priority-queue.js +2 -0
- package/dist/types/data-structures/queue/deque.d.ts +1 -0
- package/dist/types/data-structures/queue/deque.js +2 -0
- package/dist/types/data-structures/queue/index.d.ts +2 -0
- package/dist/types/data-structures/queue/index.js +18 -0
- package/dist/types/data-structures/queue/queue.d.ts +1 -0
- package/dist/types/data-structures/queue/queue.js +2 -0
- package/dist/types/data-structures/stack/index.d.ts +1 -0
- package/dist/types/data-structures/stack/index.js +17 -0
- package/dist/types/data-structures/stack/stack.d.ts +1 -0
- package/dist/types/data-structures/stack/stack.js +2 -0
- package/dist/types/data-structures/tree/index.d.ts +1 -0
- package/dist/types/data-structures/tree/index.js +17 -0
- package/dist/types/data-structures/tree/tree.d.ts +1 -0
- package/dist/types/data-structures/tree/tree.js +2 -0
- package/dist/types/data-structures/trie/index.d.ts +1 -0
- package/dist/types/data-structures/trie/index.js +17 -0
- package/dist/types/data-structures/trie/trie.d.ts +1 -0
- package/dist/types/data-structures/trie/trie.js +2 -0
- package/dist/types/helpers.d.ts +9 -0
- package/dist/types/helpers.js +9 -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/index.d.ts +1 -0
- package/dist/utils/index.js +17 -0
- package/dist/utils/utils.d.ts +20 -0
- package/dist/utils/utils.js +73 -0
- package/package.json +1 -4
- package/src/data-structures/binary-tree/avl-tree.ts +342 -0
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +298 -0
- package/src/data-structures/binary-tree/binary-tree.ts +1141 -0
- package/src/data-structures/binary-tree/bst.ts +529 -0
- package/src/data-structures/binary-tree/index.ts +7 -0
- package/src/data-structures/binary-tree/rb-tree.ts +366 -0
- package/src/data-structures/binary-tree/segment-tree.ts +257 -0
- package/src/data-structures/binary-tree/tree-multiset.ts +374 -0
- package/src/data-structures/graph/abstract-graph.ts +1043 -0
- package/src/data-structures/graph/directed-graph.ts +469 -0
- package/src/data-structures/graph/index.ts +4 -0
- package/src/data-structures/graph/map-graph.ts +134 -0
- package/src/data-structures/graph/undirected-graph.ts +273 -0
- package/src/data-structures/hash/coordinate-map.ts +67 -0
- package/src/data-structures/hash/coordinate-set.ts +56 -0
- package/src/data-structures/hash/hash-map.ts +209 -0
- package/src/data-structures/hash/hash-table.ts +280 -0
- package/src/data-structures/hash/index.ts +6 -0
- package/src/data-structures/hash/tree-map.ts +2 -0
- package/src/data-structures/hash/tree-set.ts +2 -0
- package/src/data-structures/heap/heap.ts +561 -0
- package/src/data-structures/heap/index.ts +3 -0
- package/src/data-structures/heap/max-heap.ts +24 -0
- package/src/data-structures/heap/min-heap.ts +24 -0
- package/src/data-structures/index.ts +11 -0
- package/src/data-structures/linked-list/doubly-linked-list.ts +636 -0
- package/src/data-structures/linked-list/index.ts +3 -0
- package/src/data-structures/linked-list/singly-linked-list.ts +501 -0
- package/src/data-structures/linked-list/skip-linked-list.ts +173 -0
- package/src/data-structures/matrix/index.ts +4 -0
- package/src/data-structures/matrix/matrix.ts +27 -0
- package/src/data-structures/matrix/matrix2d.ts +213 -0
- package/src/data-structures/matrix/navigator.ts +121 -0
- package/src/data-structures/matrix/vector2d.ts +317 -0
- package/src/data-structures/priority-queue/index.ts +3 -0
- package/src/data-structures/priority-queue/max-priority-queue.ts +23 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +23 -0
- package/src/data-structures/priority-queue/priority-queue.ts +16 -0
- package/src/data-structures/queue/deque.ts +298 -0
- package/src/data-structures/queue/index.ts +2 -0
- package/src/data-structures/queue/queue.ts +191 -0
- package/src/data-structures/stack/index.ts +1 -0
- package/src/data-structures/stack/stack.ts +98 -0
- package/src/data-structures/tree/index.ts +1 -0
- package/src/data-structures/tree/tree.ts +67 -0
- package/src/data-structures/trie/index.ts +1 -0
- package/src/data-structures/trie/trie.ts +286 -0
- package/src/index.ts +3 -3
- package/src/interfaces/binary-tree.ts +10 -0
- package/src/interfaces/doubly-linked-list.ts +1 -0
- package/src/interfaces/graph.ts +7 -0
- package/src/interfaces/heap.ts +1 -0
- package/src/interfaces/index.ts +8 -0
- package/src/interfaces/navigator.ts +1 -0
- package/src/interfaces/priority-queue.ts +1 -0
- package/src/interfaces/segment-tree.ts +1 -0
- package/src/interfaces/singly-linked-list.ts +1 -0
- package/src/types/data-structures/binary-tree/avl-tree.ts +5 -0
- package/src/types/data-structures/binary-tree/binary-indexed-tree.ts +1 -0
- package/src/types/data-structures/binary-tree/binary-tree.ts +35 -0
- package/src/types/data-structures/binary-tree/bst.ts +11 -0
- package/src/types/data-structures/binary-tree/index.ts +6 -0
- package/src/types/data-structures/binary-tree/rb-tree.ts +8 -0
- package/src/types/data-structures/binary-tree/segment-tree.ts +1 -0
- package/src/types/data-structures/binary-tree/tree-multiset.ts +6 -0
- package/src/types/data-structures/graph/abstract-graph.ts +11 -0
- package/src/types/data-structures/graph/directed-graph.ts +8 -0
- package/src/types/data-structures/graph/index.ts +3 -0
- package/src/types/data-structures/graph/map-graph.ts +1 -0
- package/src/types/data-structures/graph/undirected-graph.ts +1 -0
- package/src/types/data-structures/hash/coordinate-map.ts +1 -0
- package/src/types/data-structures/hash/coordinate-set.ts +1 -0
- package/src/types/data-structures/hash/hash-map.ts +1 -0
- package/src/types/data-structures/hash/hash-table.ts +1 -0
- package/src/types/data-structures/hash/index.ts +1 -0
- package/src/types/data-structures/hash/tree-map.ts +1 -0
- package/src/types/data-structures/hash/tree-set.ts +1 -0
- package/src/types/data-structures/heap/heap.ts +1 -0
- package/src/types/data-structures/heap/index.ts +1 -0
- package/src/types/data-structures/heap/max-heap.ts +1 -0
- package/src/types/data-structures/heap/min-heap.ts +1 -0
- package/src/types/data-structures/index.ts +11 -0
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +1 -0
- package/src/types/data-structures/linked-list/index.ts +2 -0
- package/src/types/data-structures/linked-list/singly-linked-list.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -0
- package/src/types/data-structures/matrix/index.ts +1 -0
- package/src/types/data-structures/matrix/matrix.ts +1 -0
- package/src/types/data-structures/matrix/matrix2d.ts +1 -0
- package/src/types/data-structures/matrix/navigator.ts +14 -0
- package/src/types/data-structures/matrix/vector2d.ts +1 -0
- package/src/types/data-structures/priority-queue/index.ts +3 -0
- package/src/types/data-structures/priority-queue/max-priority-queue.ts +1 -0
- package/src/types/data-structures/priority-queue/min-priority-queue.ts +1 -0
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -0
- package/src/types/data-structures/queue/deque.ts +1 -0
- package/src/types/data-structures/queue/index.ts +2 -0
- package/src/types/data-structures/queue/queue.ts +1 -0
- package/src/types/data-structures/stack/index.ts +1 -0
- package/src/types/data-structures/stack/stack.ts +1 -0
- package/src/types/data-structures/tree/index.ts +1 -0
- package/src/types/data-structures/tree/tree.ts +1 -0
- package/src/types/data-structures/trie/index.ts +1 -0
- package/src/types/data-structures/trie/trie.ts +1 -0
- package/src/types/helpers.ts +13 -0
- package/src/types/index.ts +3 -0
- package/src/types/utils/index.ts +2 -0
- package/src/types/utils/utils.ts +6 -0
- package/src/types/utils/validate-type.ts +35 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/utils.ts +86 -0
|
@@ -0,0 +1,228 @@
|
|
|
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
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.SegmentTree = exports.SegmentTreeNode = void 0;
|
|
11
|
+
class SegmentTreeNode {
|
|
12
|
+
constructor(start, end, sum, val) {
|
|
13
|
+
this._start = 0;
|
|
14
|
+
this._end = 0;
|
|
15
|
+
this._val = null;
|
|
16
|
+
this._sum = 0;
|
|
17
|
+
this._left = null;
|
|
18
|
+
this._right = null;
|
|
19
|
+
this._start = start;
|
|
20
|
+
this._end = end;
|
|
21
|
+
this._sum = sum;
|
|
22
|
+
this._val = val || null;
|
|
23
|
+
}
|
|
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
|
+
}
|
|
61
|
+
exports.SegmentTreeNode = SegmentTreeNode;
|
|
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) {
|
|
73
|
+
this._values = [];
|
|
74
|
+
this._start = 0;
|
|
75
|
+
start = start || 0;
|
|
76
|
+
end = end || values.length - 1;
|
|
77
|
+
this._values = values;
|
|
78
|
+
this._start = start;
|
|
79
|
+
this._end = end;
|
|
80
|
+
if (values.length > 0) {
|
|
81
|
+
this._root = this.build(start, end);
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
this._root = null;
|
|
85
|
+
this._values = [];
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
get values() {
|
|
89
|
+
return this._values;
|
|
90
|
+
}
|
|
91
|
+
get start() {
|
|
92
|
+
return this._start;
|
|
93
|
+
}
|
|
94
|
+
get end() {
|
|
95
|
+
return this._end;
|
|
96
|
+
}
|
|
97
|
+
get root() {
|
|
98
|
+
return this._root;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* The build function creates a segment tree by recursively dividing the given range into smaller segments and assigning
|
|
102
|
+
* the sum of values to each segment.
|
|
103
|
+
* @param {number} start - The `start` parameter represents the starting index of the segment or range for which we are
|
|
104
|
+
* building the segment tree.
|
|
105
|
+
* @param {number} end - The "end" parameter represents the ending index of the segment or range for which we want to
|
|
106
|
+
* build a segment tree.
|
|
107
|
+
* @returns a SegmentTreeNode object.
|
|
108
|
+
*/
|
|
109
|
+
build(start, end) {
|
|
110
|
+
if (start > end) {
|
|
111
|
+
return new SegmentTreeNode(start, end, 0);
|
|
112
|
+
}
|
|
113
|
+
if (start === end)
|
|
114
|
+
return new SegmentTreeNode(start, end, this._values[start]);
|
|
115
|
+
const mid = start + Math.floor((end - start) / 2);
|
|
116
|
+
const left = this.build(start, mid);
|
|
117
|
+
const right = this.build(mid + 1, end);
|
|
118
|
+
const cur = new SegmentTreeNode(start, end, left.sum + right.sum);
|
|
119
|
+
cur.left = left;
|
|
120
|
+
cur.right = right;
|
|
121
|
+
return cur;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* The function updates the value of a node in a segment tree and recalculates the sum of its children if they exist.
|
|
125
|
+
* @param {number} index - The index parameter represents the index of the node in the segment tree that needs to be
|
|
126
|
+
* updated.
|
|
127
|
+
* @param {number} sum - The `sum` parameter represents the new value that should be assigned to the `sum` property of
|
|
128
|
+
* the `SegmentTreeNode` at the specified `index`.
|
|
129
|
+
* @param {SegmentTreeNodeVal} [val] - The `val` parameter is an optional value that can be assigned to the `val`
|
|
130
|
+
* property of the `SegmentTreeNode` object. It is not currently used in the code, but you can uncomment the line `//
|
|
131
|
+
* cur.val = val;` and pass a value for `val` in the
|
|
132
|
+
* @returns The function does not return anything.
|
|
133
|
+
*/
|
|
134
|
+
updateNode(index, sum, val) {
|
|
135
|
+
const root = this.root || null;
|
|
136
|
+
if (!root) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
const dfs = (cur, index, sum, val) => {
|
|
140
|
+
if (cur.start === cur.end && cur.start === index) {
|
|
141
|
+
cur.sum = sum;
|
|
142
|
+
if (val !== undefined)
|
|
143
|
+
cur.val = val;
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
const mid = cur.start + Math.floor((cur.end - cur.start) / 2);
|
|
147
|
+
if (index <= mid) {
|
|
148
|
+
if (cur.left) {
|
|
149
|
+
dfs(cur.left, index, sum, val);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
if (cur.right) {
|
|
154
|
+
dfs(cur.right, index, sum, val);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
if (cur.left && cur.right) {
|
|
158
|
+
cur.sum = cur.left.sum + cur.right.sum;
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
dfs(root, index, sum, val);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* The function `querySumByRange` calculates the sum of values within a given range in a segment tree.
|
|
165
|
+
* @param {number} indexA - The starting index of the range for which you want to calculate the sum.
|
|
166
|
+
* @param {number} indexB - The parameter `indexB` represents the ending index of the range for which you want to
|
|
167
|
+
* calculate the sum.
|
|
168
|
+
* @returns The function `querySumByRange` returns a number.
|
|
169
|
+
*/
|
|
170
|
+
querySumByRange(indexA, indexB) {
|
|
171
|
+
const root = this.root || null;
|
|
172
|
+
if (!root) {
|
|
173
|
+
return 0;
|
|
174
|
+
}
|
|
175
|
+
if (indexA < 0 || indexB >= this.values.length || indexA > indexB) {
|
|
176
|
+
return NaN;
|
|
177
|
+
}
|
|
178
|
+
const dfs = (cur, i, j) => {
|
|
179
|
+
if (i <= cur.start && j >= cur.end) {
|
|
180
|
+
// The range [i, j] completely covers the current node's range [cur.start, cur.end]
|
|
181
|
+
return cur.sum;
|
|
182
|
+
}
|
|
183
|
+
const mid = cur.start + Math.floor((cur.end - cur.start) / 2);
|
|
184
|
+
if (j <= mid) {
|
|
185
|
+
if (cur.left) {
|
|
186
|
+
return dfs(cur.left, i, j);
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
return NaN;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
else if (i > mid) {
|
|
193
|
+
if (cur.right) {
|
|
194
|
+
return dfs(cur.right, i, j);
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
return NaN;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
// Query both left and right subtrees
|
|
202
|
+
let leftSum = 0;
|
|
203
|
+
let rightSum = 0;
|
|
204
|
+
if (cur.left) {
|
|
205
|
+
leftSum = dfs(cur.left, i, mid);
|
|
206
|
+
}
|
|
207
|
+
if (cur.right) {
|
|
208
|
+
rightSum = dfs(cur.right, mid + 1, j);
|
|
209
|
+
}
|
|
210
|
+
return leftSum + rightSum;
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
return dfs(root, indexA, indexB);
|
|
214
|
+
}
|
|
215
|
+
_setValues(value) {
|
|
216
|
+
this._values = value;
|
|
217
|
+
}
|
|
218
|
+
_setStart(value) {
|
|
219
|
+
this._start = value;
|
|
220
|
+
}
|
|
221
|
+
_setEnd(value) {
|
|
222
|
+
this._end = value;
|
|
223
|
+
}
|
|
224
|
+
_setRoot(v) {
|
|
225
|
+
this._root = v;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
exports.SegmentTree = SegmentTree;
|
|
@@ -0,0 +1,122 @@
|
|
|
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 { BinaryTreeNodeKey, TreeMultisetNodeNested, TreeMultisetOptions } from '../../types';
|
|
9
|
+
import { BinaryTreeDeletedResult, IterationType } from '../../types';
|
|
10
|
+
import { IBinaryTree } from '../../interfaces';
|
|
11
|
+
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
12
|
+
export declare class TreeMultisetNode<V = any, FAMILY extends TreeMultisetNode<V, FAMILY> = TreeMultisetNodeNested<V>> extends AVLTreeNode<V, FAMILY> {
|
|
13
|
+
count: number;
|
|
14
|
+
/**
|
|
15
|
+
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
16
|
+
* @param {BinaryTreeNodeKey} key - The `key` parameter is of type `BinaryTreeNodeKey` and represents the unique identifier
|
|
17
|
+
* of the binary tree node.
|
|
18
|
+
* @param {V} [val] - The `val` parameter is an optional parameter of type `V`. It represents the value of the binary
|
|
19
|
+
* tree node. If no value is provided, it will be `undefined`.
|
|
20
|
+
* @param {number} [count=1] - The `count` parameter is a number that represents the number of times a particular value
|
|
21
|
+
* occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count`
|
|
22
|
+
* parameter when creating a new instance of the `BinaryTreeNode` class.
|
|
23
|
+
*/
|
|
24
|
+
constructor(key: BinaryTreeNodeKey, val?: V, count?: number);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The only distinction between a TreeMultiset and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
28
|
+
*/
|
|
29
|
+
export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultisetNode> extends AVLTree<N> implements IBinaryTree<N> {
|
|
30
|
+
/**
|
|
31
|
+
* The constructor function for a TreeMultiset class in TypeScript, which extends another class and sets an option to
|
|
32
|
+
* merge duplicated values.
|
|
33
|
+
* @param {TreeMultisetOptions} [options] - An optional object that contains additional configuration options for the
|
|
34
|
+
* TreeMultiset.
|
|
35
|
+
*/
|
|
36
|
+
constructor(options?: TreeMultisetOptions);
|
|
37
|
+
private _count;
|
|
38
|
+
get count(): number;
|
|
39
|
+
/**
|
|
40
|
+
* The function creates a new BSTNode with the given key, value, and count.
|
|
41
|
+
* @param {BinaryTreeNodeKey} key - The key parameter is the unique identifier for the binary tree node. It is used to
|
|
42
|
+
* distinguish one node from another in the tree.
|
|
43
|
+
* @param {N} val - The `val` parameter represents the value that will be stored in the binary search tree node.
|
|
44
|
+
* @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
|
|
45
|
+
* occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
|
|
46
|
+
* @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
|
|
47
|
+
*/
|
|
48
|
+
createNode(key: BinaryTreeNodeKey, val?: N['val'], count?: number): N;
|
|
49
|
+
/**
|
|
50
|
+
* The `add` function adds a new node to a binary search tree, updating the count if the key already
|
|
51
|
+
* exists, and balancing the tree if necessary.
|
|
52
|
+
* @param {BinaryTreeNodeKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
|
53
|
+
* `BinaryTreeNodeKey` (which represents the key of the node to be added), a `N` (which represents a
|
|
54
|
+
* node to be added), or `null` (which represents a null node).
|
|
55
|
+
* @param [val] - The `val` parameter represents the value associated with the key that is being
|
|
56
|
+
* added to the binary tree.
|
|
57
|
+
* @param [count=1] - The `count` parameter represents the number of occurrences of the key/value
|
|
58
|
+
* pair that will be added to the binary tree. It has a default value of 1, which means that if no
|
|
59
|
+
* count is specified, the default count will be 1.
|
|
60
|
+
* @returns The function `add` returns a value of type `N | null | undefined`.
|
|
61
|
+
*/
|
|
62
|
+
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val'], count?: number): N | null | undefined;
|
|
63
|
+
/**
|
|
64
|
+
* The function adds a new node to a binary tree if there is an available slot in the parent node.
|
|
65
|
+
* @param {N | null} newNode - The `newNode` parameter represents the node that needs to be added to
|
|
66
|
+
* the tree. It can be either a node object (`N`) or `null`.
|
|
67
|
+
* @param {N} parent - The `parent` parameter represents the parent node to which the new node will
|
|
68
|
+
* be added as a child.
|
|
69
|
+
* @returns The method `_addTo` returns either the `parent.left`, `parent.right`, or `undefined`.
|
|
70
|
+
*/
|
|
71
|
+
_addTo(newNode: N | null, parent: N): N | null | undefined;
|
|
72
|
+
/**
|
|
73
|
+
* The `addMany` function adds multiple keys or nodes to a TreeMultiset and returns an array of the
|
|
74
|
+
* inserted nodes.
|
|
75
|
+
* @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of keys or nodes to be
|
|
76
|
+
* added to the multiset. Each element can be either a BinaryTreeNodeKey or a TreeMultisetNode.
|
|
77
|
+
* @param {N['val'][]} [data] - The `data` parameter is an optional array of values that correspond
|
|
78
|
+
* to the keys or nodes being added to the multiset. It is used to associate additional data with
|
|
79
|
+
* each key or node.
|
|
80
|
+
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
81
|
+
*/
|
|
82
|
+
addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N['val'][]): (N | null | undefined)[];
|
|
83
|
+
/**
|
|
84
|
+
* The `perfectlyBalance` function in TypeScript takes a sorted array of nodes and builds a balanced
|
|
85
|
+
* binary search tree using either a recursive or iterative approach.
|
|
86
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
87
|
+
* type of iteration to use when building a balanced binary search tree. It can have two possible
|
|
88
|
+
* values:
|
|
89
|
+
* @returns a boolean value.
|
|
90
|
+
*/
|
|
91
|
+
perfectlyBalance(iterationType?: IterationType): boolean;
|
|
92
|
+
/**
|
|
93
|
+
* The `delete` function in a binary search tree deletes a node from the tree and returns the deleted
|
|
94
|
+
* node along with the parent node that needs to be balanced.
|
|
95
|
+
* @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node object
|
|
96
|
+
* (`N`) or a key value (`BinaryTreeNodeKey`). It represents the node or key that needs to be deleted
|
|
97
|
+
* from the binary tree.
|
|
98
|
+
* @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
|
|
99
|
+
* being deleted. If set to true, the count of the node will not be considered and the node will be
|
|
100
|
+
* deleted regardless of its count. If set to false (default), the count of the node will be
|
|
101
|
+
* decremented by 1 and
|
|
102
|
+
* @returns The method `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
103
|
+
*/
|
|
104
|
+
delete(nodeOrKey: N | BinaryTreeNodeKey, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[];
|
|
105
|
+
/**
|
|
106
|
+
* The clear() function clears the contents of a data structure and sets the count to zero.
|
|
107
|
+
*/
|
|
108
|
+
clear(): void;
|
|
109
|
+
/**
|
|
110
|
+
* The function swaps the values of two nodes in a binary tree.
|
|
111
|
+
* @param {N} srcNode - The source node that needs to be swapped with the destination node.
|
|
112
|
+
* @param {N} destNode - The `destNode` parameter represents the destination node where the values
|
|
113
|
+
* from `srcNode` will be swapped into.
|
|
114
|
+
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
115
|
+
*/
|
|
116
|
+
protected _swap(srcNode: N, destNode: N): N;
|
|
117
|
+
/**
|
|
118
|
+
* The function sets the value of the "_count" property.
|
|
119
|
+
* @param {number} v - number
|
|
120
|
+
*/
|
|
121
|
+
protected _setCount(v: number): void;
|
|
122
|
+
}
|