directed-graph-typed 2.1.1 → 2.1.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/cjs/index.cjs +2798 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/esm/index.mjs +2789 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/types/data-structures/base/index.d.ts +2 -1
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +182 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +135 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +291 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +174 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +754 -29
- package/dist/types/data-structures/binary-tree/bst.d.ts +413 -12
- package/dist/types/data-structures/binary-tree/index.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +208 -3
- package/dist/types/data-structures/binary-tree/segment-tree.d.ts +160 -1
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +190 -2
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +270 -2
- package/dist/types/data-structures/graph/abstract-graph.d.ts +340 -14
- package/dist/types/data-structures/graph/directed-graph.d.ts +207 -1
- package/dist/types/data-structures/graph/index.d.ts +2 -1
- package/dist/types/data-structures/graph/map-graph.d.ts +78 -1
- package/dist/types/data-structures/graph/undirected-graph.d.ts +188 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +345 -19
- package/dist/types/data-structures/hash/index.d.ts +0 -1
- package/dist/types/data-structures/heap/heap.d.ts +503 -5
- package/dist/types/data-structures/heap/index.d.ts +2 -0
- package/dist/types/data-structures/heap/max-heap.d.ts +32 -1
- package/dist/types/data-structures/heap/min-heap.d.ts +33 -1
- package/dist/types/data-structures/index.d.ts +7 -7
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +769 -2
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +451 -2
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +27 -4
- package/dist/types/data-structures/matrix/index.d.ts +1 -1
- package/dist/types/data-structures/matrix/matrix.d.ts +168 -7
- package/dist/types/data-structures/matrix/navigator.d.ts +54 -13
- package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +27 -1
- package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +26 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +15 -2
- package/dist/types/data-structures/queue/deque.d.ts +431 -4
- package/dist/types/data-structures/queue/queue.d.ts +308 -4
- package/dist/types/data-structures/stack/stack.d.ts +306 -2
- package/dist/types/data-structures/tree/tree.d.ts +62 -1
- package/dist/types/data-structures/trie/trie.d.ts +350 -4
- package/dist/types/index.d.ts +13 -2
- package/dist/{interfaces → types/interfaces}/binary-tree.d.ts +1 -1
- package/dist/types/types/data-structures/base/index.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/avl-tree.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/binary-tree.d.ts +29 -0
- package/dist/types/types/data-structures/binary-tree/bst.d.ts +12 -0
- package/dist/{data-structures → types/types/data-structures}/binary-tree/index.d.ts +2 -3
- package/dist/types/types/data-structures/binary-tree/red-black-tree.d.ts +3 -0
- package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
- package/dist/types/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
- package/dist/types/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -0
- package/dist/types/types/data-structures/graph/abstract-graph.d.ts +14 -0
- package/dist/types/types/data-structures/graph/directed-graph.d.ts +1 -0
- package/dist/{data-structures → types/types/data-structures}/graph/index.d.ts +1 -2
- package/dist/types/types/data-structures/graph/map-graph.d.ts +1 -0
- package/dist/types/types/data-structures/graph/undirected-graph.d.ts +1 -0
- package/dist/types/types/data-structures/hash/hash-map.d.ts +19 -0
- package/dist/types/types/data-structures/hash/index.d.ts +2 -0
- package/dist/types/types/data-structures/heap/heap.d.ts +5 -0
- package/dist/types/types/data-structures/heap/index.d.ts +1 -0
- package/dist/types/types/data-structures/heap/max-heap.d.ts +1 -0
- package/dist/types/types/data-structures/heap/min-heap.d.ts +1 -0
- package/dist/types/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -0
- package/dist/types/types/data-structures/linked-list/singly-linked-list.d.ts +2 -0
- package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +4 -0
- package/dist/types/types/data-structures/matrix/matrix.d.ts +7 -0
- package/dist/types/types/data-structures/matrix/navigator.d.ts +14 -0
- package/dist/types/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
- package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +2 -0
- package/dist/types/types/data-structures/queue/deque.d.ts +4 -0
- package/dist/types/types/data-structures/queue/queue.d.ts +4 -0
- package/dist/types/types/data-structures/stack/stack.d.ts +2 -0
- package/dist/types/types/data-structures/tree/tree.d.ts +1 -0
- package/dist/types/types/data-structures/trie/trie.d.ts +4 -0
- package/dist/types/types/index.d.ts +3 -0
- package/dist/types/types/utils/index.d.ts +2 -0
- package/dist/types/types/utils/utils.d.ts +22 -0
- package/dist/types/utils/index.d.ts +1 -1
- package/dist/types/utils/utils.d.ts +209 -22
- package/dist/umd/directed-graph-typed.js +2791 -0
- package/dist/umd/directed-graph-typed.js.map +1 -0
- package/dist/umd/directed-graph-typed.min.js +9 -0
- package/dist/umd/directed-graph-typed.min.js.map +1 -0
- package/package.json +25 -5
- package/src/data-structures/binary-tree/avl-tree-counter.ts +4 -4
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
- package/src/data-structures/binary-tree/avl-tree.ts +2 -2
- package/src/data-structures/binary-tree/binary-tree.ts +4 -4
- package/src/data-structures/binary-tree/bst.ts +1 -1
- package/src/data-structures/binary-tree/red-black-tree.ts +2 -2
- package/src/data-structures/binary-tree/tree-counter.ts +4 -4
- package/src/data-structures/binary-tree/tree-multi-map.ts +1 -1
- package/src/data-structures/heap/heap.ts +5 -5
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -2
- package/src/interfaces/binary-tree.ts +1 -1
- package/tsconfig.base.json +23 -0
- package/tsconfig.json +8 -34
- package/tsconfig.test.json +8 -0
- package/tsconfig.types.json +15 -0
- package/tsup.config.js +28 -0
- package/tsup.node.config.js +37 -0
- package/dist/common/index.js +0 -28
- package/dist/constants/index.js +0 -8
- package/dist/data-structures/base/index.d.ts +0 -2
- package/dist/data-structures/base/index.js +0 -18
- package/dist/data-structures/base/iterable-element-base.js +0 -243
- package/dist/data-structures/base/iterable-entry-base.js +0 -183
- package/dist/data-structures/base/linear-base.js +0 -415
- package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +0 -182
- package/dist/data-structures/binary-tree/avl-tree-counter.js +0 -374
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -135
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +0 -250
- package/dist/data-structures/binary-tree/avl-tree.d.ts +0 -291
- package/dist/data-structures/binary-tree/avl-tree.js +0 -611
- package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -174
- package/dist/data-structures/binary-tree/binary-indexed-tree.js +0 -294
- package/dist/data-structures/binary-tree/binary-tree.d.ts +0 -754
- package/dist/data-structures/binary-tree/binary-tree.js +0 -1925
- package/dist/data-structures/binary-tree/bst.d.ts +0 -413
- package/dist/data-structures/binary-tree/bst.js +0 -903
- package/dist/data-structures/binary-tree/index.js +0 -26
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +0 -208
- package/dist/data-structures/binary-tree/red-black-tree.js +0 -546
- package/dist/data-structures/binary-tree/segment-tree.d.ts +0 -160
- package/dist/data-structures/binary-tree/segment-tree.js +0 -297
- package/dist/data-structures/binary-tree/tree-counter.d.ts +0 -190
- package/dist/data-structures/binary-tree/tree-counter.js +0 -413
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +0 -270
- package/dist/data-structures/binary-tree/tree-multi-map.js +0 -384
- package/dist/data-structures/graph/abstract-graph.d.ts +0 -340
- package/dist/data-structures/graph/abstract-graph.js +0 -896
- package/dist/data-structures/graph/directed-graph.d.ts +0 -207
- package/dist/data-structures/graph/directed-graph.js +0 -525
- package/dist/data-structures/graph/index.js +0 -20
- package/dist/data-structures/graph/map-graph.d.ts +0 -78
- package/dist/data-structures/graph/map-graph.js +0 -107
- package/dist/data-structures/graph/undirected-graph.d.ts +0 -188
- package/dist/data-structures/graph/undirected-graph.js +0 -424
- package/dist/data-structures/hash/hash-map.d.ts +0 -345
- package/dist/data-structures/hash/hash-map.js +0 -692
- package/dist/data-structures/hash/index.d.ts +0 -1
- package/dist/data-structures/hash/index.js +0 -17
- package/dist/data-structures/heap/heap.d.ts +0 -503
- package/dist/data-structures/heap/heap.js +0 -901
- package/dist/data-structures/heap/index.d.ts +0 -3
- package/dist/data-structures/heap/index.js +0 -19
- package/dist/data-structures/heap/max-heap.d.ts +0 -32
- package/dist/data-structures/heap/max-heap.js +0 -40
- package/dist/data-structures/heap/min-heap.d.ts +0 -33
- package/dist/data-structures/heap/min-heap.js +0 -31
- package/dist/data-structures/index.js +0 -28
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +0 -769
- package/dist/data-structures/linked-list/doubly-linked-list.js +0 -1111
- package/dist/data-structures/linked-list/index.js +0 -19
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +0 -451
- package/dist/data-structures/linked-list/singly-linked-list.js +0 -850
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -27
- package/dist/data-structures/linked-list/skip-linked-list.js +0 -144
- package/dist/data-structures/matrix/index.js +0 -18
- package/dist/data-structures/matrix/matrix.d.ts +0 -168
- package/dist/data-structures/matrix/matrix.js +0 -448
- package/dist/data-structures/matrix/navigator.d.ts +0 -55
- package/dist/data-structures/matrix/navigator.js +0 -111
- package/dist/data-structures/priority-queue/index.js +0 -19
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +0 -27
- package/dist/data-structures/priority-queue/max-priority-queue.js +0 -34
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +0 -26
- package/dist/data-structures/priority-queue/min-priority-queue.js +0 -24
- package/dist/data-structures/priority-queue/priority-queue.d.ts +0 -15
- package/dist/data-structures/priority-queue/priority-queue.js +0 -20
- package/dist/data-structures/queue/deque.d.ts +0 -431
- package/dist/data-structures/queue/deque.js +0 -879
- package/dist/data-structures/queue/index.js +0 -18
- package/dist/data-structures/queue/queue.d.ts +0 -308
- package/dist/data-structures/queue/queue.js +0 -473
- package/dist/data-structures/stack/index.js +0 -17
- package/dist/data-structures/stack/stack.d.ts +0 -306
- package/dist/data-structures/stack/stack.js +0 -401
- package/dist/data-structures/tree/index.js +0 -17
- package/dist/data-structures/tree/tree.d.ts +0 -62
- package/dist/data-structures/tree/tree.js +0 -107
- package/dist/data-structures/trie/index.js +0 -17
- package/dist/data-structures/trie/trie.d.ts +0 -350
- package/dist/data-structures/trie/trie.js +0 -610
- package/dist/index.d.ts +0 -14
- package/dist/index.js +0 -30
- package/dist/interfaces/binary-tree.js +0 -2
- package/dist/interfaces/doubly-linked-list.js +0 -2
- package/dist/interfaces/graph.js +0 -2
- package/dist/interfaces/heap.js +0 -2
- package/dist/interfaces/index.js +0 -24
- package/dist/interfaces/navigator.js +0 -2
- package/dist/interfaces/priority-queue.js +0 -2
- package/dist/interfaces/segment-tree.js +0 -2
- package/dist/interfaces/singly-linked-list.js +0 -2
- package/dist/types/common.js +0 -2
- package/dist/types/data-structures/base/base.js +0 -2
- package/dist/types/data-structures/base/index.js +0 -17
- package/dist/types/data-structures/binary-tree/avl-tree-counter.js +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.js +0 -2
- package/dist/types/data-structures/binary-tree/avl-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/binary-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/bst.js +0 -2
- package/dist/types/data-structures/binary-tree/index.js +0 -25
- package/dist/types/data-structures/binary-tree/red-black-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/segment-tree.js +0 -2
- package/dist/types/data-structures/binary-tree/tree-counter.js +0 -2
- package/dist/types/data-structures/binary-tree/tree-multi-map.js +0 -2
- package/dist/types/data-structures/graph/abstract-graph.js +0 -2
- package/dist/types/data-structures/graph/directed-graph.js +0 -2
- package/dist/types/data-structures/graph/index.js +0 -19
- package/dist/types/data-structures/graph/map-graph.js +0 -2
- package/dist/types/data-structures/graph/undirected-graph.js +0 -2
- package/dist/types/data-structures/hash/hash-map.js +0 -2
- package/dist/types/data-structures/hash/index.js +0 -17
- package/dist/types/data-structures/heap/heap.js +0 -2
- package/dist/types/data-structures/heap/index.js +0 -17
- package/dist/types/data-structures/heap/max-heap.js +0 -2
- package/dist/types/data-structures/heap/min-heap.js +0 -2
- package/dist/types/data-structures/index.js +0 -28
- package/dist/types/data-structures/linked-list/doubly-linked-list.js +0 -2
- package/dist/types/data-structures/linked-list/index.js +0 -19
- package/dist/types/data-structures/linked-list/singly-linked-list.js +0 -2
- package/dist/types/data-structures/linked-list/skip-linked-list.js +0 -2
- package/dist/types/data-structures/matrix/index.js +0 -18
- package/dist/types/data-structures/matrix/matrix.js +0 -2
- package/dist/types/data-structures/matrix/navigator.js +0 -2
- package/dist/types/data-structures/priority-queue/index.js +0 -19
- package/dist/types/data-structures/priority-queue/max-priority-queue.js +0 -2
- package/dist/types/data-structures/priority-queue/min-priority-queue.js +0 -2
- package/dist/types/data-structures/priority-queue/priority-queue.js +0 -2
- package/dist/types/data-structures/queue/deque.js +0 -2
- package/dist/types/data-structures/queue/index.js +0 -18
- package/dist/types/data-structures/queue/queue.js +0 -2
- package/dist/types/data-structures/stack/index.js +0 -17
- package/dist/types/data-structures/stack/stack.js +0 -2
- package/dist/types/data-structures/tree/index.js +0 -17
- package/dist/types/data-structures/tree/tree.js +0 -2
- package/dist/types/data-structures/trie/index.js +0 -17
- package/dist/types/data-structures/trie/trie.js +0 -2
- package/dist/types/index.js +0 -19
- package/dist/types/utils/index.js +0 -18
- package/dist/types/utils/utils.js +0 -2
- package/dist/types/utils/validate-type.js +0 -2
- package/dist/utils/index.d.ts +0 -2
- package/dist/utils/index.js +0 -18
- package/dist/utils/number.js +0 -24
- package/dist/utils/utils.d.ts +0 -209
- package/dist/utils/utils.js +0 -353
- package/dist/{common → types/common}/index.d.ts +0 -0
- package/dist/{constants → types/constants}/index.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/iterable-element-base.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/iterable-entry-base.d.ts +0 -0
- package/dist/{data-structures → types/data-structures}/base/linear-base.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/doubly-linked-list.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/graph.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/heap.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/index.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/navigator.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/priority-queue.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/segment-tree.d.ts +0 -0
- package/dist/{interfaces → types/interfaces}/singly-linked-list.d.ts +0 -0
- package/dist/types/{common.d.ts → types/common.d.ts} +0 -0
- package/dist/types/{data-structures → types/data-structures}/base/base.d.ts +0 -0
- package/dist/{data-structures → types/types/data-structures}/index.d.ts +7 -7
- package/dist/{data-structures → types/types/data-structures}/linked-list/index.d.ts +0 -0
- package/dist/{data-structures → types/types/data-structures}/matrix/index.d.ts +1 -1
- /package/dist/{data-structures → types/types/data-structures}/priority-queue/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/queue/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/stack/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/tree/index.d.ts +0 -0
- /package/dist/{data-structures → types/types/data-structures}/trie/index.d.ts +0 -0
- /package/dist/types/{utils → types/utils}/validate-type.d.ts +0 -0
- /package/dist/{utils → types/utils}/number.d.ts +0 -0
|
@@ -1,2 +1,182 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
import type { AVLTreeCounterOptions, BinaryTreeDeleteResult, BinaryTreeOptions, BSTNOptKeyOrNode, EntryCallback, IterationType } from '../../types';
|
|
9
|
+
import { IBinaryTree } from '../../interfaces';
|
|
10
|
+
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
11
|
+
/**
|
|
12
|
+
* AVL node with an extra 'count' field; keeps parent/child links.
|
|
13
|
+
* @remarks Time O(1), Space O(1)
|
|
14
|
+
* @template K
|
|
15
|
+
* @template V
|
|
16
|
+
*/
|
|
17
|
+
export declare class AVLTreeCounterNode<K = any, V = any> extends AVLTreeNode<K, V> {
|
|
18
|
+
parent?: AVLTreeCounterNode<K, V>;
|
|
19
|
+
/**
|
|
20
|
+
* Create an AVL counter node.
|
|
21
|
+
* @remarks Time O(1), Space O(1)
|
|
22
|
+
* @param key - Key of the node.
|
|
23
|
+
* @param [value] - Associated value (ignored in map mode).
|
|
24
|
+
* @param [count] - Initial count for this node (default 1).
|
|
25
|
+
* @returns New AVLTreeCounterNode instance.
|
|
26
|
+
*/
|
|
27
|
+
constructor(key: K, value?: V, count?: number);
|
|
28
|
+
_left?: AVLTreeCounterNode<K, V> | null | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* Get the left child pointer.
|
|
31
|
+
* @remarks Time O(1), Space O(1)
|
|
32
|
+
* @returns Left child node, or null/undefined.
|
|
33
|
+
*/
|
|
34
|
+
get left(): AVLTreeCounterNode<K, V> | null | undefined;
|
|
35
|
+
/**
|
|
36
|
+
* Set the left child and update its parent pointer.
|
|
37
|
+
* @remarks Time O(1), Space O(1)
|
|
38
|
+
* @param v - New left child node, or null/undefined.
|
|
39
|
+
* @returns void
|
|
40
|
+
*/
|
|
41
|
+
set left(v: AVLTreeCounterNode<K, V> | null | undefined);
|
|
42
|
+
_right?: AVLTreeCounterNode<K, V> | null | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* Get the right child pointer.
|
|
45
|
+
* @remarks Time O(1), Space O(1)
|
|
46
|
+
* @returns Right child node, or null/undefined.
|
|
47
|
+
*/
|
|
48
|
+
get right(): AVLTreeCounterNode<K, V> | null | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* Set the right child and update its parent pointer.
|
|
51
|
+
* @remarks Time O(1), Space O(1)
|
|
52
|
+
* @param v - New right child node, or null/undefined.
|
|
53
|
+
* @returns void
|
|
54
|
+
*/
|
|
55
|
+
set right(v: AVLTreeCounterNode<K, V> | null | undefined);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* AVL tree that tracks an aggregate 'count' across nodes; supports balanced insert/delete and map-like mode.
|
|
59
|
+
* @remarks Time O(1), Space O(1)
|
|
60
|
+
* @template K
|
|
61
|
+
* @template V
|
|
62
|
+
* @template R
|
|
63
|
+
*/
|
|
64
|
+
export declare class AVLTreeCounter<K = any, V = any, R = any> extends AVLTree<K, V, R> implements IBinaryTree<K, V, R> {
|
|
65
|
+
/**
|
|
66
|
+
* Create a AVLTreeCounter instance
|
|
67
|
+
* @remarks Time O(n), Space O(n)
|
|
68
|
+
* @param keysNodesEntriesOrRaws
|
|
69
|
+
* @param options
|
|
70
|
+
* @returns New AVLTreeCounterNode instance.
|
|
71
|
+
*/
|
|
72
|
+
constructor(keysNodesEntriesOrRaws?: Iterable<K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, options?: AVLTreeCounterOptions<K, V, R>);
|
|
73
|
+
protected _count: number;
|
|
74
|
+
get count(): number;
|
|
75
|
+
/**
|
|
76
|
+
* Compute the total count by traversing the tree (sums node.count).
|
|
77
|
+
* @remarks Time O(N), Space O(H)
|
|
78
|
+
* @returns Total count recomputed from nodes.
|
|
79
|
+
*/
|
|
80
|
+
getComputedCount(): number;
|
|
81
|
+
createNode(key: K, value?: V, count?: number): AVLTreeCounterNode<K, V>;
|
|
82
|
+
/**
|
|
83
|
+
* Type guard: check whether the input is an AVLTreeCounterNode.
|
|
84
|
+
* @remarks Time O(1), Space O(1)
|
|
85
|
+
* @returns True if the value is an AVLTreeCounterNode.
|
|
86
|
+
*/
|
|
87
|
+
isNode(keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): keyNodeOrEntry is AVLTreeCounterNode<K, V>;
|
|
88
|
+
/**
|
|
89
|
+
* Insert or increment a node and update aggregate count.
|
|
90
|
+
* @remarks Time O(log N), Space O(1)
|
|
91
|
+
* @param keyNodeOrEntry - Key, node, or [key, value] entry to insert.
|
|
92
|
+
* @param [value] - Value when a bare key is provided (ignored in map mode).
|
|
93
|
+
* @param [count] - How much to increase the node's count (default 1).
|
|
94
|
+
* @returns True if inserted/updated; false if ignored.
|
|
95
|
+
*/
|
|
96
|
+
add(keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V, count?: number): boolean;
|
|
97
|
+
/**
|
|
98
|
+
* Delete a node (or decrement its count) and rebalance if needed.
|
|
99
|
+
* @remarks Time O(log N), Space O(1)
|
|
100
|
+
* @param keyNodeOrEntry - Key, node, or [key, value] entry identifying the node.
|
|
101
|
+
* @param [ignoreCount] - If true, remove the node regardless of its count.
|
|
102
|
+
* @returns Array of deletion results including deleted node and a rebalance hint when present.
|
|
103
|
+
*/
|
|
104
|
+
delete(keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, ignoreCount?: boolean): BinaryTreeDeleteResult<AVLTreeCounterNode<K, V>>[];
|
|
105
|
+
/**
|
|
106
|
+
* Remove all nodes and reset aggregate counters.
|
|
107
|
+
* @remarks Time O(N), Space O(1)
|
|
108
|
+
* @returns void
|
|
109
|
+
*/
|
|
110
|
+
clear(): void;
|
|
111
|
+
/**
|
|
112
|
+
* Rebuild the tree into a perfectly balanced form using in-order nodes.
|
|
113
|
+
* @remarks Time O(N), Space O(N)
|
|
114
|
+
* @param [iterationType] - Traversal style to use when constructing the balanced tree.
|
|
115
|
+
* @returns True if rebalancing succeeded (tree not empty).
|
|
116
|
+
*/
|
|
117
|
+
perfectlyBalance(iterationType?: IterationType): boolean;
|
|
118
|
+
/**
|
|
119
|
+
* Deep copy this tree, preserving map mode and aggregate counts.
|
|
120
|
+
* @remarks Time O(N), Space O(N)
|
|
121
|
+
* @returns A deep copy of this tree.
|
|
122
|
+
*/
|
|
123
|
+
clone(): this;
|
|
124
|
+
/**
|
|
125
|
+
* Create a new AVLTreeCounter by mapping each [key, value] entry.
|
|
126
|
+
* @remarks Time O(N log N), Space O(N)
|
|
127
|
+
* @template MK
|
|
128
|
+
* @template MV
|
|
129
|
+
* @template MR
|
|
130
|
+
* @param callback - Function mapping (key, value, index, tree) → [newKey, newValue].
|
|
131
|
+
* @param [options] - Options for the output tree.
|
|
132
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
133
|
+
* @returns A new AVLTreeCounter with mapped entries.
|
|
134
|
+
*/
|
|
135
|
+
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<BinaryTreeOptions<MK, MV, MR>>, thisArg?: unknown): AVLTreeCounter<MK, MV, MR>;
|
|
136
|
+
/**
|
|
137
|
+
* (Protected) Create an empty instance of the same concrete class.
|
|
138
|
+
* @remarks Time O(1), Space O(1)
|
|
139
|
+
* @template TK
|
|
140
|
+
* @template TV
|
|
141
|
+
* @template TR
|
|
142
|
+
* @param [options] - Optional constructor options for the like-kind instance.
|
|
143
|
+
* @returns An empty like-kind instance.
|
|
144
|
+
*/
|
|
145
|
+
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<AVLTreeCounterOptions<TK, TV, TR>>): this;
|
|
146
|
+
/**
|
|
147
|
+
* (Protected) Create a like-kind instance and seed it from an iterable.
|
|
148
|
+
* @remarks Time O(N log N), Space O(N)
|
|
149
|
+
* @template TK
|
|
150
|
+
* @template TV
|
|
151
|
+
* @template TR
|
|
152
|
+
* @param iter - Iterable used to seed the new tree.
|
|
153
|
+
* @param [options] - Options merged with the current snapshot.
|
|
154
|
+
* @returns A like-kind AVLTreeCounter built from the iterable.
|
|
155
|
+
*/
|
|
156
|
+
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | AVLTreeCounterNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<AVLTreeCounterOptions<TK, TV, TR>>): AVLTreeCounter<TK, TV, TR>;
|
|
157
|
+
/**
|
|
158
|
+
* (Protected) Normalize input into a node plus its effective value and count.
|
|
159
|
+
* @remarks Time O(1), Space O(1)
|
|
160
|
+
* @param keyNodeOrEntry - Key, node, or [key, value] entry.
|
|
161
|
+
* @param [value] - Value used when a bare key is provided.
|
|
162
|
+
* @param [count] - Count increment to apply (default 1).
|
|
163
|
+
* @returns Tuple [node, value] where node may be undefined.
|
|
164
|
+
*/
|
|
165
|
+
protected _keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V, count?: number): [AVLTreeCounterNode<K, V> | undefined, V | undefined];
|
|
166
|
+
/**
|
|
167
|
+
* (Protected) Swap keys/values/counters between the source and destination nodes.
|
|
168
|
+
* @remarks Time O(1), Space O(1)
|
|
169
|
+
* @param srcNode - Source node (or key) whose properties will be moved.
|
|
170
|
+
* @param destNode - Destination node (or key) to receive properties.
|
|
171
|
+
* @returns Destination node after swap, or undefined.
|
|
172
|
+
*/
|
|
173
|
+
protected _swapProperties(srcNode: BSTNOptKeyOrNode<K, AVLTreeCounterNode<K, V>>, destNode: BSTNOptKeyOrNode<K, AVLTreeCounterNode<K, V>>): AVLTreeCounterNode<K, V> | undefined;
|
|
174
|
+
/**
|
|
175
|
+
* (Protected) Replace one node by another and adjust counters accordingly.
|
|
176
|
+
* @remarks Time O(1), Space O(1)
|
|
177
|
+
* @param oldNode - Node being replaced.
|
|
178
|
+
* @param newNode - Replacement node.
|
|
179
|
+
* @returns The new node after replacement.
|
|
180
|
+
*/
|
|
181
|
+
protected _replaceNode(oldNode: AVLTreeCounterNode<K, V>, newNode: AVLTreeCounterNode<K, V>): AVLTreeCounterNode<K, V>;
|
|
182
|
+
}
|
|
@@ -1,2 +1,135 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
import type { AVLTreeMultiMapOptions, AVLTreeOptions, ElemOf, EntryCallback, IterationType } from '../../types';
|
|
9
|
+
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
10
|
+
import { IBinaryTree } from '../../interfaces';
|
|
11
|
+
/**
|
|
12
|
+
* Node used by AVLTreeMultiMap; stores the key with a bucket of values (array).
|
|
13
|
+
* @remarks Time O(1), Space O(1)
|
|
14
|
+
* @template K
|
|
15
|
+
* @template V
|
|
16
|
+
*/
|
|
17
|
+
export declare class AVLTreeMultiMapNode<K = any, V = any> extends AVLTreeNode<K, V[]> {
|
|
18
|
+
parent?: AVLTreeMultiMapNode<K, V>;
|
|
19
|
+
/**
|
|
20
|
+
* Create an AVLTreeMultiMap node with a value bucket.
|
|
21
|
+
* @remarks Time O(1), Space O(1)
|
|
22
|
+
* @param key - Key of the node.
|
|
23
|
+
* @param value - Initial array of values.
|
|
24
|
+
* @returns New AVLTreeMultiMapNode instance.
|
|
25
|
+
*/
|
|
26
|
+
constructor(key: K, value: V[]);
|
|
27
|
+
_left?: AVLTreeMultiMapNode<K, V> | null | undefined;
|
|
28
|
+
/**
|
|
29
|
+
* Get the left child pointer.
|
|
30
|
+
* @remarks Time O(1), Space O(1)
|
|
31
|
+
* @returns Left child node, or null/undefined.
|
|
32
|
+
*/
|
|
33
|
+
get left(): AVLTreeMultiMapNode<K, V> | null | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* Set the left child and update its parent pointer.
|
|
36
|
+
* @remarks Time O(1), Space O(1)
|
|
37
|
+
* @param v - New left child node, or null/undefined.
|
|
38
|
+
* @returns void
|
|
39
|
+
*/
|
|
40
|
+
set left(v: AVLTreeMultiMapNode<K, V> | null | undefined);
|
|
41
|
+
_right?: AVLTreeMultiMapNode<K, V> | null | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* Get the right child pointer.
|
|
44
|
+
* @remarks Time O(1), Space O(1)
|
|
45
|
+
* @returns Right child node, or null/undefined.
|
|
46
|
+
*/
|
|
47
|
+
get right(): AVLTreeMultiMapNode<K, V> | null | undefined;
|
|
48
|
+
/**
|
|
49
|
+
* Set the right child and update its parent pointer.
|
|
50
|
+
* @remarks Time O(1), Space O(1)
|
|
51
|
+
* @param v - New right child node, or null/undefined.
|
|
52
|
+
* @returns void
|
|
53
|
+
*/
|
|
54
|
+
set right(v: AVLTreeMultiMapNode<K, V> | null | undefined);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* AVL-tree–based multimap (key → array of values). Preserves O(log N) updates and supports map-like mode.
|
|
58
|
+
* @remarks Time O(1), Space O(1)
|
|
59
|
+
* @template K
|
|
60
|
+
* @template V
|
|
61
|
+
* @template R
|
|
62
|
+
*/
|
|
63
|
+
export declare class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<K, V[], R> implements IBinaryTree<K, V[], R> {
|
|
64
|
+
/**
|
|
65
|
+
* Create an AVLTreeMultiMap and optionally bulk-insert items.
|
|
66
|
+
* @remarks Time O(N log N), Space O(N)
|
|
67
|
+
* @param [keysNodesEntriesOrRaws] - Iterable of keys/nodes/entries/raw items to insert.
|
|
68
|
+
* @param [options] - Options for AVLTreeMultiMap (comparator, reverse, map mode).
|
|
69
|
+
* @returns New AVLTreeMultiMap instance.
|
|
70
|
+
*/
|
|
71
|
+
constructor(keysNodesEntriesOrRaws?: Iterable<K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | R>, options?: AVLTreeMultiMapOptions<K, V[], R>);
|
|
72
|
+
createNode(key: K, value?: V[]): AVLTreeMultiMapNode<K, V>;
|
|
73
|
+
add(keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined): boolean;
|
|
74
|
+
add(key: K, value: V): boolean;
|
|
75
|
+
/**
|
|
76
|
+
* Delete a single value from the bucket at a given key. Removes the key if the bucket becomes empty.
|
|
77
|
+
* @remarks Time O(log N), Space O(1)
|
|
78
|
+
* @param keyNodeOrEntry - Key, node, or [key, values] entry to locate the bucket.
|
|
79
|
+
* @param value - Value to remove from the bucket.
|
|
80
|
+
* @returns True if the value was removed; false if not found.
|
|
81
|
+
*/
|
|
82
|
+
deleteValue(keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined, value: V): boolean;
|
|
83
|
+
/**
|
|
84
|
+
* Rebuild the tree into a perfectly balanced form using in-order nodes.
|
|
85
|
+
* @remarks Time O(N), Space O(N)
|
|
86
|
+
* @param [iterationType] - Traversal style to use when constructing the balanced tree.
|
|
87
|
+
* @returns True if rebalancing succeeded (tree not empty).
|
|
88
|
+
*/
|
|
89
|
+
perfectlyBalance(iterationType?: IterationType): boolean;
|
|
90
|
+
/**
|
|
91
|
+
* Create a new tree by mapping each [key, values] bucket.
|
|
92
|
+
* @remarks Time O(N log N), Space O(N)
|
|
93
|
+
* @template MK
|
|
94
|
+
* @template MVArr
|
|
95
|
+
* @template MR
|
|
96
|
+
* @param callback - Function mapping (key, values, index, tree) → [newKey, newValue].
|
|
97
|
+
* @param [options] - Options for the output tree.
|
|
98
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
99
|
+
* @returns A new AVLTreeMultiMap when mapping to array values; see overloads.
|
|
100
|
+
*/
|
|
101
|
+
map<MK = K, MVArr extends unknown[] = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MVArr]>, options?: Partial<AVLTreeOptions<MK, MVArr, MR>>, thisArg?: unknown): AVLTreeMultiMap<MK, ElemOf<MVArr>, MR>;
|
|
102
|
+
/**
|
|
103
|
+
* Create a new tree by mapping each [key, values] bucket.
|
|
104
|
+
* @remarks Time O(N log N), Space O(N)
|
|
105
|
+
* @template MK
|
|
106
|
+
* @template MV
|
|
107
|
+
* @template MR
|
|
108
|
+
* @param callback - Function mapping (key, values, index, tree) → [newKey, newValue].
|
|
109
|
+
* @param [options] - Options for the output tree.
|
|
110
|
+
* @param [thisArg] - Value for `this` inside the callback.
|
|
111
|
+
* @returns A new AVLTree when mapping to non-array values; see overloads.
|
|
112
|
+
*/
|
|
113
|
+
map<MK = K, MV = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MV]>, options?: Partial<AVLTreeOptions<MK, MV, MR>>, thisArg?: unknown): AVLTree<MK, MV, MR>;
|
|
114
|
+
/**
|
|
115
|
+
* (Protected) Create an empty instance of the same concrete class.
|
|
116
|
+
* @remarks Time O(1), Space O(1)
|
|
117
|
+
* @template TK
|
|
118
|
+
* @template TV
|
|
119
|
+
* @template TR
|
|
120
|
+
* @param [options] - Optional constructor options for the like-kind instance.
|
|
121
|
+
* @returns An empty like-kind instance.
|
|
122
|
+
*/
|
|
123
|
+
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<AVLTreeOptions<TK, TV, TR>>): this;
|
|
124
|
+
/**
|
|
125
|
+
* (Protected) Create a like-kind instance and seed it from an iterable.
|
|
126
|
+
* @remarks Time O(N log N), Space O(N)
|
|
127
|
+
* @template TK
|
|
128
|
+
* @template TV
|
|
129
|
+
* @template TR
|
|
130
|
+
* @param iter - Iterable used to seed the new tree.
|
|
131
|
+
* @param [options] - Options merged with the current snapshot.
|
|
132
|
+
* @returns A like-kind AVLTree built from the iterable.
|
|
133
|
+
*/
|
|
134
|
+
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<AVLTreeOptions<TK, TV, TR>>): AVLTree<TK, TV, TR>;
|
|
135
|
+
}
|
|
@@ -1,2 +1,291 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Pablo Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
import { BST, BSTNode } from './bst';
|
|
9
|
+
import type { AVLTreeOptions, BinaryTreeDeleteResult, BinaryTreeOptions, BSTNOptKeyOrNode, EntryCallback, IterationType } from '../../types';
|
|
10
|
+
import { BSTOptions } from '../../types';
|
|
11
|
+
import { IBinaryTree } from '../../interfaces';
|
|
12
|
+
/**
|
|
13
|
+
* Represents a Node in an AVL (Adelson-Velsky and Landis) Tree.
|
|
14
|
+
* It extends a BSTNode and ensures the 'height' property is maintained.
|
|
15
|
+
*
|
|
16
|
+
* @template K - The type of the key.
|
|
17
|
+
* @template V - The type of the value.
|
|
18
|
+
*/
|
|
19
|
+
export declare class AVLTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
20
|
+
parent?: AVLTreeNode<K, V>;
|
|
21
|
+
/**
|
|
22
|
+
* Creates an instance of AVLTreeNode.
|
|
23
|
+
* @remarks Time O(1), Space O(1)
|
|
24
|
+
*
|
|
25
|
+
* @param key - The key of the node.
|
|
26
|
+
* @param [value] - The value associated with the key.
|
|
27
|
+
*/
|
|
28
|
+
constructor(key: K, value?: V);
|
|
29
|
+
_left?: AVLTreeNode<K, V> | null | undefined;
|
|
30
|
+
/**
|
|
31
|
+
* Gets the left child of the node.
|
|
32
|
+
* @remarks Time O(1), Space O(1)
|
|
33
|
+
*
|
|
34
|
+
* @returns The left child.
|
|
35
|
+
*/
|
|
36
|
+
get left(): AVLTreeNode<K, V> | null | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* Sets the left child of the node and updates its parent reference.
|
|
39
|
+
* @remarks Time O(1), Space O(1)
|
|
40
|
+
*
|
|
41
|
+
* @param v - The node to set as the left child.
|
|
42
|
+
*/
|
|
43
|
+
set left(v: AVLTreeNode<K, V> | null | undefined);
|
|
44
|
+
_right?: AVLTreeNode<K, V> | null | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* Gets the right child of the node.
|
|
47
|
+
* @remarks Time O(1), Space O(1)
|
|
48
|
+
*
|
|
49
|
+
* @returns The right child.
|
|
50
|
+
*/
|
|
51
|
+
get right(): AVLTreeNode<K, V> | null | undefined;
|
|
52
|
+
/**
|
|
53
|
+
* Sets the right child of the node and updates its parent reference.
|
|
54
|
+
* @remarks Time O(1), Space O(1)
|
|
55
|
+
*
|
|
56
|
+
* @param v - The node to set as the right child.
|
|
57
|
+
*/
|
|
58
|
+
set right(v: AVLTreeNode<K, V> | null | undefined);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Represents a self-balancing AVL (Adelson-Velsky and Landis) Tree.
|
|
62
|
+
* This tree extends BST and performs rotations on add/delete to maintain balance.
|
|
63
|
+
*
|
|
64
|
+
* @template K - The type of the key.
|
|
65
|
+
* @template V - The type of the value.
|
|
66
|
+
* @template R - The type of the raw data object (if using `toEntryFn`).
|
|
67
|
+
*
|
|
68
|
+
* 1. Height-Balanced: Each node's left and right subtrees differ in height by no more than one.
|
|
69
|
+
* 2. Automatic Rebalancing: AVL trees rebalance themselves automatically during insertions and deletions.
|
|
70
|
+
* 3. Rotations for Balancing: Utilizes rotations (single or double) to maintain balance after updates.
|
|
71
|
+
* 4. Order Preservation: Maintains the binary search tree property where left child values are less than the parent, and right child values are greater.
|
|
72
|
+
* 5. Efficient Lookups: Offers O(log n) search time, where 'n' is the number of nodes, due to its balanced nature.
|
|
73
|
+
* 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
|
|
74
|
+
* 7. Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes.@example
|
|
75
|
+
* // Find elements in a range
|
|
76
|
+
* // In interval queries, AVL trees, with their strictly balanced structure and lower height, offer better query efficiency, making them ideal for frequent and high-performance interval queries. In contrast, Red-Black trees, with lower update costs, are more suitable for scenarios involving frequent insertions and deletions where the requirements for interval queries are less demanding.
|
|
77
|
+
* type Datum = { timestamp: Date; temperature: number };
|
|
78
|
+
* // Fixed dataset of CPU temperature readings
|
|
79
|
+
* const cpuData: Datum[] = [
|
|
80
|
+
* { timestamp: new Date('2024-12-02T00:00:00'), temperature: 55.1 },
|
|
81
|
+
* { timestamp: new Date('2024-12-02T00:01:00'), temperature: 56.3 },
|
|
82
|
+
* { timestamp: new Date('2024-12-02T00:02:00'), temperature: 54.8 },
|
|
83
|
+
* { timestamp: new Date('2024-12-02T00:03:00'), temperature: 57.2 },
|
|
84
|
+
* { timestamp: new Date('2024-12-02T00:04:00'), temperature: 58.0 },
|
|
85
|
+
* { timestamp: new Date('2024-12-02T00:05:00'), temperature: 59.4 },
|
|
86
|
+
* { timestamp: new Date('2024-12-02T00:06:00'), temperature: 60.1 },
|
|
87
|
+
* { timestamp: new Date('2024-12-02T00:07:00'), temperature: 61.3 },
|
|
88
|
+
* { timestamp: new Date('2024-12-02T00:08:00'), temperature: 62.0 },
|
|
89
|
+
* { timestamp: new Date('2024-12-02T00:09:00'), temperature: 63.5 },
|
|
90
|
+
* { timestamp: new Date('2024-12-02T00:10:00'), temperature: 64.0 },
|
|
91
|
+
* { timestamp: new Date('2024-12-02T00:11:00'), temperature: 62.8 },
|
|
92
|
+
* { timestamp: new Date('2024-12-02T00:12:00'), temperature: 61.5 },
|
|
93
|
+
* { timestamp: new Date('2024-12-02T00:13:00'), temperature: 60.2 },
|
|
94
|
+
* { timestamp: new Date('2024-12-02T00:14:00'), temperature: 59.8 },
|
|
95
|
+
* { timestamp: new Date('2024-12-02T00:15:00'), temperature: 58.6 },
|
|
96
|
+
* { timestamp: new Date('2024-12-02T00:16:00'), temperature: 57.4 },
|
|
97
|
+
* { timestamp: new Date('2024-12-02T00:17:00'), temperature: 56.2 },
|
|
98
|
+
* { timestamp: new Date('2024-12-02T00:18:00'), temperature: 55.7 },
|
|
99
|
+
* { timestamp: new Date('2024-12-02T00:19:00'), temperature: 54.5 },
|
|
100
|
+
* { timestamp: new Date('2024-12-02T00:20:00'), temperature: 53.2 },
|
|
101
|
+
* { timestamp: new Date('2024-12-02T00:21:00'), temperature: 52.8 },
|
|
102
|
+
* { timestamp: new Date('2024-12-02T00:22:00'), temperature: 51.9 },
|
|
103
|
+
* { timestamp: new Date('2024-12-02T00:23:00'), temperature: 50.5 },
|
|
104
|
+
* { timestamp: new Date('2024-12-02T00:24:00'), temperature: 49.8 },
|
|
105
|
+
* { timestamp: new Date('2024-12-02T00:25:00'), temperature: 48.7 },
|
|
106
|
+
* { timestamp: new Date('2024-12-02T00:26:00'), temperature: 47.5 },
|
|
107
|
+
* { timestamp: new Date('2024-12-02T00:27:00'), temperature: 46.3 },
|
|
108
|
+
* { timestamp: new Date('2024-12-02T00:28:00'), temperature: 45.9 },
|
|
109
|
+
* { timestamp: new Date('2024-12-02T00:29:00'), temperature: 45.0 }
|
|
110
|
+
* ];
|
|
111
|
+
*
|
|
112
|
+
* // Create an AVL tree to store CPU temperature data
|
|
113
|
+
* const cpuTemperatureTree = new AVLTree<Date, number, Datum>(cpuData, {
|
|
114
|
+
* toEntryFn: ({ timestamp, temperature }) => [timestamp, temperature]
|
|
115
|
+
* });
|
|
116
|
+
*
|
|
117
|
+
* // Query a specific time range (e.g., from 00:05 to 00:15)
|
|
118
|
+
* const rangeStart = new Date('2024-12-02T00:05:00');
|
|
119
|
+
* const rangeEnd = new Date('2024-12-02T00:15:00');
|
|
120
|
+
* const rangeResults = cpuTemperatureTree.rangeSearch([rangeStart, rangeEnd], node => ({
|
|
121
|
+
* minute: node ? node.key.getMinutes() : 0,
|
|
122
|
+
* temperature: cpuTemperatureTree.get(node ? node.key : undefined)
|
|
123
|
+
* }));
|
|
124
|
+
*
|
|
125
|
+
* console.log(rangeResults); // [
|
|
126
|
+
* // { minute: 5, temperature: 59.4 },
|
|
127
|
+
* // { minute: 6, temperature: 60.1 },
|
|
128
|
+
* // { minute: 7, temperature: 61.3 },
|
|
129
|
+
* // { minute: 8, temperature: 62 },
|
|
130
|
+
* // { minute: 9, temperature: 63.5 },
|
|
131
|
+
* // { minute: 10, temperature: 64 },
|
|
132
|
+
* // { minute: 11, temperature: 62.8 },
|
|
133
|
+
* // { minute: 12, temperature: 61.5 },
|
|
134
|
+
* // { minute: 13, temperature: 60.2 },
|
|
135
|
+
* // { minute: 14, temperature: 59.8 },
|
|
136
|
+
* // { minute: 15, temperature: 58.6 }
|
|
137
|
+
* // ]
|
|
138
|
+
*/
|
|
139
|
+
export declare class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements IBinaryTree<K, V, R> {
|
|
140
|
+
/**
|
|
141
|
+
* Creates an instance of AVLTree.
|
|
142
|
+
* @remarks Time O(N log N) (from `addMany` with balanced add). Space O(N).
|
|
143
|
+
*
|
|
144
|
+
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to add.
|
|
145
|
+
* @param [options] - Configuration options for the AVL tree.
|
|
146
|
+
*/
|
|
147
|
+
constructor(keysNodesEntriesOrRaws?: Iterable<K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, options?: AVLTreeOptions<K, V, R>);
|
|
148
|
+
/**
|
|
149
|
+
* (Protected) Creates a new AVL tree node.
|
|
150
|
+
* @remarks Time O(1), Space O(1)
|
|
151
|
+
*
|
|
152
|
+
* @param key - The key for the new node.
|
|
153
|
+
* @param [value] - The value for the new node.
|
|
154
|
+
* @returns The newly created AVLTreeNode.
|
|
155
|
+
*/
|
|
156
|
+
createNode(key: K, value?: V): AVLTreeNode<K, V>;
|
|
157
|
+
/**
|
|
158
|
+
* Checks if the given item is an `AVLTreeNode` instance.
|
|
159
|
+
* @remarks Time O(1), Space O(1)
|
|
160
|
+
*
|
|
161
|
+
* @param keyNodeOrEntry - The item to check.
|
|
162
|
+
* @returns True if it's an AVLTreeNode, false otherwise.
|
|
163
|
+
*/
|
|
164
|
+
isNode(keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): keyNodeOrEntry is AVLTreeNode<K, V>;
|
|
165
|
+
/**
|
|
166
|
+
* Adds a new node to the AVL tree and balances the tree path.
|
|
167
|
+
* @remarks Time O(log N) (O(H) for BST add + O(H) for `_balancePath`). Space O(H) for path/recursion.
|
|
168
|
+
*
|
|
169
|
+
* @param keyNodeOrEntry - The key, node, or entry to add.
|
|
170
|
+
* @param [value] - The value, if providing just a key.
|
|
171
|
+
* @returns True if the addition was successful, false otherwise.
|
|
172
|
+
*/
|
|
173
|
+
add(keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): boolean;
|
|
174
|
+
/**
|
|
175
|
+
* Deletes a node from the AVL tree and re-balances the tree.
|
|
176
|
+
* @remarks Time O(log N) (O(H) for BST delete + O(H) for `_balancePath`). Space O(H) for path/recursion.
|
|
177
|
+
*
|
|
178
|
+
* @param keyNodeOrEntry - The node to delete.
|
|
179
|
+
* @returns An array containing deletion results.
|
|
180
|
+
*/
|
|
181
|
+
delete(keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): BinaryTreeDeleteResult<AVLTreeNode<K, V>>[];
|
|
182
|
+
/**
|
|
183
|
+
* Rebuilds the tree to be perfectly balanced.
|
|
184
|
+
* @remarks AVL trees are already height-balanced, but this makes them *perfectly* balanced (minimal height and all leaves at N or N-1).
|
|
185
|
+
* Time O(N) (O(N) for DFS, O(N) for sorted build). Space O(N) for node array and recursion stack.
|
|
186
|
+
*
|
|
187
|
+
* @param [iterationType=this.iterationType] - The traversal method for the initial node export.
|
|
188
|
+
* @returns True if successful, false if the tree was empty.
|
|
189
|
+
*/
|
|
190
|
+
perfectlyBalance(iterationType?: IterationType): boolean;
|
|
191
|
+
/**
|
|
192
|
+
* Creates a new AVLTree by mapping each [key, value] pair.
|
|
193
|
+
* @remarks Time O(N log N) (O(N) iteration + O(log M) `add` for each item into the new tree). Space O(N) for the new tree.
|
|
194
|
+
*
|
|
195
|
+
* @template MK - New key type.
|
|
196
|
+
* @template MV - New value type.
|
|
197
|
+
* @template MR - New raw type.
|
|
198
|
+
* @param callback - A function to map each [key, value] pair.
|
|
199
|
+
* @param [options] - Options for the new AVLTree.
|
|
200
|
+
* @param [thisArg] - `this` context for the callback.
|
|
201
|
+
* @returns A new, mapped AVLTree.
|
|
202
|
+
*/
|
|
203
|
+
map<MK = K, MV = V, MR = any>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<BinaryTreeOptions<MK, MV, MR>>, thisArg?: unknown): AVLTree<MK, MV, MR>;
|
|
204
|
+
/**
|
|
205
|
+
* (Protected) Creates a new, empty instance of the same AVLTree constructor.
|
|
206
|
+
* @remarks Time O(1)
|
|
207
|
+
*
|
|
208
|
+
* @template TK, TV, TR - Generic types for the new instance.
|
|
209
|
+
* @param [options] - Options for the new tree.
|
|
210
|
+
* @returns A new, empty tree.
|
|
211
|
+
*/
|
|
212
|
+
protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<BSTOptions<TK, TV, TR>>): this;
|
|
213
|
+
/**
|
|
214
|
+
* (Protected) Creates a new instance of the same AVLTree constructor, potentially with different generic types.
|
|
215
|
+
* @remarks Time O(N log N) (from constructor) due to processing the iterable.
|
|
216
|
+
*
|
|
217
|
+
* @template TK, TV, TR - Generic types for the new instance.
|
|
218
|
+
* @param [iter=[]] - An iterable to populate the new tree.
|
|
219
|
+
* @param [options] - Options for the new tree.
|
|
220
|
+
* @returns A new AVLTree.
|
|
221
|
+
*/
|
|
222
|
+
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | BSTNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<BSTOptions<TK, TV, TR>>): AVLTree<TK, TV, TR>;
|
|
223
|
+
/**
|
|
224
|
+
* (Protected) Swaps properties of two nodes, including height.
|
|
225
|
+
* @remarks Time O(H) (due to `ensureNode`), but O(1) if nodes are passed directly.
|
|
226
|
+
*
|
|
227
|
+
* @param srcNode - The source node.
|
|
228
|
+
* @param destNode - The destination node.
|
|
229
|
+
* @returns The `destNode` (now holding `srcNode`'s properties).
|
|
230
|
+
*/
|
|
231
|
+
protected _swapProperties(srcNode: BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>, destNode: BSTNOptKeyOrNode<K, AVLTreeNode<K, V>>): AVLTreeNode<K, V> | undefined;
|
|
232
|
+
/**
|
|
233
|
+
* (Protected) Calculates the balance factor (height(right) - height(left)).
|
|
234
|
+
* @remarks Time O(1) (assumes heights are stored).
|
|
235
|
+
*
|
|
236
|
+
* @param node - The node to check.
|
|
237
|
+
* @returns The balance factor (positive if right-heavy, negative if left-heavy).
|
|
238
|
+
*/
|
|
239
|
+
protected _balanceFactor(node: AVLTreeNode<K, V>): number;
|
|
240
|
+
/**
|
|
241
|
+
* (Protected) Recalculates and updates the height of a node based on its children's heights.
|
|
242
|
+
* @remarks Time O(1) (assumes children's heights are correct).
|
|
243
|
+
*
|
|
244
|
+
* @param node - The node to update.
|
|
245
|
+
*/
|
|
246
|
+
protected _updateHeight(node: AVLTreeNode<K, V>): void;
|
|
247
|
+
/**
|
|
248
|
+
* (Protected) Performs a Left-Left (LL) rotation (a single right rotation).
|
|
249
|
+
* @remarks Time O(1), Space O(1)
|
|
250
|
+
*
|
|
251
|
+
* @param A - The unbalanced node (root of the unbalanced subtree).
|
|
252
|
+
*/
|
|
253
|
+
protected _balanceLL(A: AVLTreeNode<K, V>): void;
|
|
254
|
+
/**
|
|
255
|
+
* (Protected) Performs a Left-Right (LR) double rotation.
|
|
256
|
+
* @remarks Time O(1), Space O(1)
|
|
257
|
+
*
|
|
258
|
+
* @param A - The unbalanced node (root of the unbalanced subtree).
|
|
259
|
+
*/
|
|
260
|
+
protected _balanceLR(A: AVLTreeNode<K, V>): void;
|
|
261
|
+
/**
|
|
262
|
+
* (Protected) Performs a Right-Right (RR) rotation (a single left rotation).
|
|
263
|
+
* @remarks Time O(1), Space O(1)
|
|
264
|
+
*
|
|
265
|
+
* @param A - The unbalanced node (root of the unbalanced subtree).
|
|
266
|
+
*/
|
|
267
|
+
protected _balanceRR(A: AVLTreeNode<K, V>): void;
|
|
268
|
+
/**
|
|
269
|
+
* (Protected) Performs a Right-Left (RL) double rotation.
|
|
270
|
+
* @remarks Time O(1), Space O(1)
|
|
271
|
+
*
|
|
272
|
+
* @param A - The unbalanced node (root of the unbalanced subtree).
|
|
273
|
+
*/
|
|
274
|
+
protected _balanceRL(A: AVLTreeNode<K, V>): void;
|
|
275
|
+
/**
|
|
276
|
+
* (Protected) Traverses up the tree from the specified node, updating heights and performing rotations as needed.
|
|
277
|
+
* @remarks Time O(log N) (O(H)), as it traverses the path to root. Space O(H) for the path array.
|
|
278
|
+
*
|
|
279
|
+
* @param node - The node to start balancing from (e.g., the newly inserted node or parent of the deleted node).
|
|
280
|
+
*/
|
|
281
|
+
protected _balancePath(node: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): void;
|
|
282
|
+
/**
|
|
283
|
+
* (Protected) Replaces a node, ensuring height is copied.
|
|
284
|
+
* @remarks Time O(1)
|
|
285
|
+
*
|
|
286
|
+
* @param oldNode - The node to be replaced.
|
|
287
|
+
* @param newNode - The node to insert.
|
|
288
|
+
* @returns The `newNode`.
|
|
289
|
+
*/
|
|
290
|
+
protected _replaceNode(oldNode: AVLTreeNode<K, V>, newNode: AVLTreeNode<K, V>): AVLTreeNode<K, V>;
|
|
291
|
+
}
|