data-structure-typed 2.1.1 → 2.2.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/CHANGELOG.md +1 -1
- package/CONTRIBUTING.md +4 -0
- package/README.md +19 -7
- package/dist/cjs/index.cjs +1175 -585
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/{index.cjs → cjs-legacy/index.cjs} +1145 -613
- package/dist/cjs-legacy/index.cjs.map +1 -0
- package/dist/esm/index.mjs +1175 -585
- package/dist/esm/index.mjs.map +1 -1
- package/dist/{index.js → esm-legacy/index.mjs} +1147 -615
- package/dist/esm-legacy/index.mjs.map +1 -0
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +58 -4
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +66 -4
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +59 -5
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/bst.d.ts +58 -4
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +60 -6
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +58 -4
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +66 -4
- package/dist/types/data-structures/heap/heap.d.ts +4 -4
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +1 -1
- package/dist/types/interfaces/binary-tree.d.ts +1 -1
- package/dist/umd/data-structure-typed.js +711 -228
- package/dist/umd/data-structure-typed.js.map +1 -1
- package/dist/umd/data-structure-typed.min.js +3 -3
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/jest.integration.config.js +7 -3
- package/package.json +29 -7
- package/src/data-structures/binary-tree/avl-tree-counter.ts +106 -15
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +116 -12
- package/src/data-structures/binary-tree/avl-tree.ts +107 -16
- package/src/data-structures/binary-tree/binary-tree.ts +4 -4
- package/src/data-structures/binary-tree/bst.ts +103 -12
- package/src/data-structures/binary-tree/red-black-tree.ts +110 -20
- package/src/data-structures/binary-tree/tree-counter.ts +105 -14
- package/src/data-structures/binary-tree/tree-multi-map.ts +123 -12
- package/src/data-structures/graph/abstract-graph.ts +5 -5
- package/src/data-structures/graph/directed-graph.ts +5 -5
- package/src/data-structures/graph/undirected-graph.ts +5 -5
- 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/test/integration/compile.test.mjs +159 -0
- package/test/integration/compile.test.ts +176 -0
- package/test/integration/heap.test.js +1 -1
- package/test/integration/index.html +1 -1
- package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +5 -4
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/bst.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +3 -3
- package/test/unit/data-structures/binary-tree/tree-counter.test.ts +5 -4
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +4 -4
- package/{tsconfig-base.json → tsconfig.base.json} +0 -1
- package/tsconfig.test.json +1 -0
- package/{tsconfig-types.json → tsconfig.types.json} +1 -3
- package/tsup.config.js +2 -3
- package/tsup.node.config.js +71 -0
- package/dist/index.cjs.map +0 -1
- package/dist/index.js.map +0 -1
- package/test/integration/compile.js +0 -144
- package/test/integration/compile.mjs +0 -135
- package/test/integration/compile.ts +0 -171
- package/tsup.node.config.ts +0 -37
|
@@ -5,16 +5,18 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { AVLTreeCounterOptions, BinaryTreeDeleteResult, BinaryTreeOptions, BSTNOptKeyOrNode, EntryCallback, IterationType } from '../../types';
|
|
8
|
+
import type { AVLTreeCounterOptions, BinaryTreeDeleteResult, BinaryTreeOptions, BSTNOptKeyOrNode, EntryCallback, FamilyPosition, IterationType, RBTNColor } from '../../types';
|
|
9
9
|
import { IBinaryTree } from '../../interfaces';
|
|
10
|
-
import { AVLTree
|
|
10
|
+
import { AVLTree } from './avl-tree';
|
|
11
11
|
/**
|
|
12
12
|
* AVL node with an extra 'count' field; keeps parent/child links.
|
|
13
13
|
* @remarks Time O(1), Space O(1)
|
|
14
14
|
* @template K
|
|
15
15
|
* @template V
|
|
16
16
|
*/
|
|
17
|
-
export declare class AVLTreeCounterNode<K = any, V = any>
|
|
17
|
+
export declare class AVLTreeCounterNode<K = any, V = any> {
|
|
18
|
+
key: K;
|
|
19
|
+
value?: V;
|
|
18
20
|
parent?: AVLTreeCounterNode<K, V>;
|
|
19
21
|
/**
|
|
20
22
|
* Create an AVL counter node.
|
|
@@ -53,6 +55,58 @@ export declare class AVLTreeCounterNode<K = any, V = any> extends AVLTreeNode<K,
|
|
|
53
55
|
* @returns void
|
|
54
56
|
*/
|
|
55
57
|
set right(v: AVLTreeCounterNode<K, V> | null | undefined);
|
|
58
|
+
_height: number;
|
|
59
|
+
/**
|
|
60
|
+
* Gets the height of the node (used in self-balancing trees).
|
|
61
|
+
* @remarks Time O(1), Space O(1)
|
|
62
|
+
*
|
|
63
|
+
* @returns The height.
|
|
64
|
+
*/
|
|
65
|
+
get height(): number;
|
|
66
|
+
/**
|
|
67
|
+
* Sets the height of the node.
|
|
68
|
+
* @remarks Time O(1), Space O(1)
|
|
69
|
+
*
|
|
70
|
+
* @param value - The new height.
|
|
71
|
+
*/
|
|
72
|
+
set height(value: number);
|
|
73
|
+
_color: RBTNColor;
|
|
74
|
+
/**
|
|
75
|
+
* Gets the color of the node (used in Red-Black trees).
|
|
76
|
+
* @remarks Time O(1), Space O(1)
|
|
77
|
+
*
|
|
78
|
+
* @returns The node's color.
|
|
79
|
+
*/
|
|
80
|
+
get color(): RBTNColor;
|
|
81
|
+
/**
|
|
82
|
+
* Sets the color of the node.
|
|
83
|
+
* @remarks Time O(1), Space O(1)
|
|
84
|
+
*
|
|
85
|
+
* @param value - The new color.
|
|
86
|
+
*/
|
|
87
|
+
set color(value: RBTNColor);
|
|
88
|
+
_count: number;
|
|
89
|
+
/**
|
|
90
|
+
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
91
|
+
* @remarks Time O(1), Space O(1)
|
|
92
|
+
*
|
|
93
|
+
* @returns The subtree node count.
|
|
94
|
+
*/
|
|
95
|
+
get count(): number;
|
|
96
|
+
/**
|
|
97
|
+
* Sets the count of nodes in the subtree.
|
|
98
|
+
* @remarks Time O(1), Space O(1)
|
|
99
|
+
*
|
|
100
|
+
* @param value - The new count.
|
|
101
|
+
*/
|
|
102
|
+
set count(value: number);
|
|
103
|
+
/**
|
|
104
|
+
* Gets the position of the node relative to its parent.
|
|
105
|
+
* @remarks Time O(1), Space O(1)
|
|
106
|
+
*
|
|
107
|
+
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
108
|
+
*/
|
|
109
|
+
get familyPosition(): FamilyPosition;
|
|
56
110
|
}
|
|
57
111
|
/**
|
|
58
112
|
* AVL tree that tracks an aggregate 'count' across nodes; supports balanced insert/delete and map-like mode.
|
|
@@ -78,7 +132,7 @@ export declare class AVLTreeCounter<K = any, V = any, R = any> extends AVLTree<K
|
|
|
78
132
|
* @returns Total count recomputed from nodes.
|
|
79
133
|
*/
|
|
80
134
|
getComputedCount(): number;
|
|
81
|
-
|
|
135
|
+
createNode(key: K, value?: V, count?: number): AVLTreeCounterNode<K, V>;
|
|
82
136
|
/**
|
|
83
137
|
* Type guard: check whether the input is an AVLTreeCounterNode.
|
|
84
138
|
* @remarks Time O(1), Space O(1)
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { AVLTreeMultiMapOptions, AVLTreeOptions, ElemOf, EntryCallback, IterationType } from '../../types';
|
|
8
|
+
import type { AVLTreeMultiMapOptions, AVLTreeOptions, ElemOf, EntryCallback, FamilyPosition, IterationType, RBTNColor } from '../../types';
|
|
9
9
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
/**
|
|
@@ -14,7 +14,9 @@ import { IBinaryTree } from '../../interfaces';
|
|
|
14
14
|
* @template K
|
|
15
15
|
* @template V
|
|
16
16
|
*/
|
|
17
|
-
export declare class AVLTreeMultiMapNode<K = any, V = any>
|
|
17
|
+
export declare class AVLTreeMultiMapNode<K = any, V = any> {
|
|
18
|
+
key: K;
|
|
19
|
+
value?: V[];
|
|
18
20
|
parent?: AVLTreeMultiMapNode<K, V>;
|
|
19
21
|
/**
|
|
20
22
|
* Create an AVLTreeMultiMap node with a value bucket.
|
|
@@ -23,7 +25,7 @@ export declare class AVLTreeMultiMapNode<K = any, V = any> extends AVLTreeNode<K
|
|
|
23
25
|
* @param value - Initial array of values.
|
|
24
26
|
* @returns New AVLTreeMultiMapNode instance.
|
|
25
27
|
*/
|
|
26
|
-
constructor(key: K, value
|
|
28
|
+
constructor(key: K, value?: V[]);
|
|
27
29
|
_left?: AVLTreeMultiMapNode<K, V> | null | undefined;
|
|
28
30
|
/**
|
|
29
31
|
* Get the left child pointer.
|
|
@@ -52,6 +54,58 @@ export declare class AVLTreeMultiMapNode<K = any, V = any> extends AVLTreeNode<K
|
|
|
52
54
|
* @returns void
|
|
53
55
|
*/
|
|
54
56
|
set right(v: AVLTreeMultiMapNode<K, V> | null | undefined);
|
|
57
|
+
_height: number;
|
|
58
|
+
/**
|
|
59
|
+
* Gets the height of the node (used in self-balancing trees).
|
|
60
|
+
* @remarks Time O(1), Space O(1)
|
|
61
|
+
*
|
|
62
|
+
* @returns The height.
|
|
63
|
+
*/
|
|
64
|
+
get height(): number;
|
|
65
|
+
/**
|
|
66
|
+
* Sets the height of the node.
|
|
67
|
+
* @remarks Time O(1), Space O(1)
|
|
68
|
+
*
|
|
69
|
+
* @param value - The new height.
|
|
70
|
+
*/
|
|
71
|
+
set height(value: number);
|
|
72
|
+
_color: RBTNColor;
|
|
73
|
+
/**
|
|
74
|
+
* Gets the color of the node (used in Red-Black trees).
|
|
75
|
+
* @remarks Time O(1), Space O(1)
|
|
76
|
+
*
|
|
77
|
+
* @returns The node's color.
|
|
78
|
+
*/
|
|
79
|
+
get color(): RBTNColor;
|
|
80
|
+
/**
|
|
81
|
+
* Sets the color of the node.
|
|
82
|
+
* @remarks Time O(1), Space O(1)
|
|
83
|
+
*
|
|
84
|
+
* @param value - The new color.
|
|
85
|
+
*/
|
|
86
|
+
set color(value: RBTNColor);
|
|
87
|
+
_count: number;
|
|
88
|
+
/**
|
|
89
|
+
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
90
|
+
* @remarks Time O(1), Space O(1)
|
|
91
|
+
*
|
|
92
|
+
* @returns The subtree node count.
|
|
93
|
+
*/
|
|
94
|
+
get count(): number;
|
|
95
|
+
/**
|
|
96
|
+
* Sets the count of nodes in the subtree.
|
|
97
|
+
* @remarks Time O(1), Space O(1)
|
|
98
|
+
*
|
|
99
|
+
* @param value - The new count.
|
|
100
|
+
*/
|
|
101
|
+
set count(value: number);
|
|
102
|
+
/**
|
|
103
|
+
* Gets the position of the node relative to its parent.
|
|
104
|
+
* @remarks Time O(1), Space O(1)
|
|
105
|
+
*
|
|
106
|
+
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
107
|
+
*/
|
|
108
|
+
get familyPosition(): FamilyPosition;
|
|
55
109
|
}
|
|
56
110
|
/**
|
|
57
111
|
* AVL-tree–based multimap (key → array of values). Preserves O(log N) updates and supports map-like mode.
|
|
@@ -69,7 +123,15 @@ export declare class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<
|
|
|
69
123
|
* @returns New AVLTreeMultiMap instance.
|
|
70
124
|
*/
|
|
71
125
|
constructor(keysNodesEntriesOrRaws?: Iterable<K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | R>, options?: AVLTreeMultiMapOptions<K, V[], R>);
|
|
72
|
-
|
|
126
|
+
createNode(key: K, value?: V[]): AVLTreeMultiMapNode<K, V>;
|
|
127
|
+
/**
|
|
128
|
+
* Checks if the given item is a `AVLTreeMultiMapNode` instance.
|
|
129
|
+
* @remarks Time O(1), Space O(1)
|
|
130
|
+
*
|
|
131
|
+
* @param keyNodeOrEntry - The item to check.
|
|
132
|
+
* @returns True if it's a AVLTreeMultiMapNode, false otherwise.
|
|
133
|
+
*/
|
|
134
|
+
isNode(keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined): keyNodeOrEntry is AVLTreeMultiMapNode<K, V>;
|
|
73
135
|
add(keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined): boolean;
|
|
74
136
|
add(key: K, value: V): boolean;
|
|
75
137
|
/**
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { BST
|
|
9
|
-
import type { AVLTreeOptions, BinaryTreeDeleteResult, BinaryTreeOptions, BSTNOptKeyOrNode, EntryCallback, IterationType } from '../../types';
|
|
8
|
+
import { BST } from './bst';
|
|
9
|
+
import type { AVLTreeOptions, BinaryTreeDeleteResult, BinaryTreeOptions, BSTNOptKeyOrNode, EntryCallback, FamilyPosition, IterationType, RBTNColor } from '../../types';
|
|
10
10
|
import { BSTOptions } from '../../types';
|
|
11
11
|
import { IBinaryTree } from '../../interfaces';
|
|
12
12
|
/**
|
|
@@ -16,7 +16,9 @@ import { IBinaryTree } from '../../interfaces';
|
|
|
16
16
|
* @template K - The type of the key.
|
|
17
17
|
* @template V - The type of the value.
|
|
18
18
|
*/
|
|
19
|
-
export declare class AVLTreeNode<K = any, V = any>
|
|
19
|
+
export declare class AVLTreeNode<K = any, V = any> {
|
|
20
|
+
key: K;
|
|
21
|
+
value?: V;
|
|
20
22
|
parent?: AVLTreeNode<K, V>;
|
|
21
23
|
/**
|
|
22
24
|
* Creates an instance of AVLTreeNode.
|
|
@@ -56,6 +58,58 @@ export declare class AVLTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
56
58
|
* @param v - The node to set as the right child.
|
|
57
59
|
*/
|
|
58
60
|
set right(v: AVLTreeNode<K, V> | null | undefined);
|
|
61
|
+
_height: number;
|
|
62
|
+
/**
|
|
63
|
+
* Gets the height of the node (used in self-balancing trees).
|
|
64
|
+
* @remarks Time O(1), Space O(1)
|
|
65
|
+
*
|
|
66
|
+
* @returns The height.
|
|
67
|
+
*/
|
|
68
|
+
get height(): number;
|
|
69
|
+
/**
|
|
70
|
+
* Sets the height of the node.
|
|
71
|
+
* @remarks Time O(1), Space O(1)
|
|
72
|
+
*
|
|
73
|
+
* @param value - The new height.
|
|
74
|
+
*/
|
|
75
|
+
set height(value: number);
|
|
76
|
+
_color: RBTNColor;
|
|
77
|
+
/**
|
|
78
|
+
* Gets the color of the node (used in Red-Black trees).
|
|
79
|
+
* @remarks Time O(1), Space O(1)
|
|
80
|
+
*
|
|
81
|
+
* @returns The node's color.
|
|
82
|
+
*/
|
|
83
|
+
get color(): RBTNColor;
|
|
84
|
+
/**
|
|
85
|
+
* Sets the color of the node.
|
|
86
|
+
* @remarks Time O(1), Space O(1)
|
|
87
|
+
*
|
|
88
|
+
* @param value - The new color.
|
|
89
|
+
*/
|
|
90
|
+
set color(value: RBTNColor);
|
|
91
|
+
_count: number;
|
|
92
|
+
/**
|
|
93
|
+
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
94
|
+
* @remarks Time O(1), Space O(1)
|
|
95
|
+
*
|
|
96
|
+
* @returns The subtree node count.
|
|
97
|
+
*/
|
|
98
|
+
get count(): number;
|
|
99
|
+
/**
|
|
100
|
+
* Sets the count of nodes in the subtree.
|
|
101
|
+
* @remarks Time O(1), Space O(1)
|
|
102
|
+
*
|
|
103
|
+
* @param value - The new count.
|
|
104
|
+
*/
|
|
105
|
+
set count(value: number);
|
|
106
|
+
/**
|
|
107
|
+
* Gets the position of the node relative to its parent.
|
|
108
|
+
* @remarks Time O(1), Space O(1)
|
|
109
|
+
*
|
|
110
|
+
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
111
|
+
*/
|
|
112
|
+
get familyPosition(): FamilyPosition;
|
|
59
113
|
}
|
|
60
114
|
/**
|
|
61
115
|
* Represents a self-balancing AVL (Adelson-Velsky and Landis) Tree.
|
|
@@ -153,7 +207,7 @@ export declare class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> imp
|
|
|
153
207
|
* @param [value] - The value for the new node.
|
|
154
208
|
* @returns The newly created AVLTreeNode.
|
|
155
209
|
*/
|
|
156
|
-
|
|
210
|
+
createNode(key: K, value?: V): AVLTreeNode<K, V>;
|
|
157
211
|
/**
|
|
158
212
|
* Checks if the given item is an `AVLTreeNode` instance.
|
|
159
213
|
* @remarks Time O(1), Space O(1)
|
|
@@ -219,7 +273,7 @@ export declare class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> imp
|
|
|
219
273
|
* @param [options] - Options for the new tree.
|
|
220
274
|
* @returns A new AVLTree.
|
|
221
275
|
*/
|
|
222
|
-
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK |
|
|
276
|
+
protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<BSTOptions<TK, TV, TR>>): AVLTree<TK, TV, TR>;
|
|
223
277
|
/**
|
|
224
278
|
* (Protected) Swaps properties of two nodes, including height.
|
|
225
279
|
* @remarks Time O(H) (due to `ensureNode`), but O(1) if nodes are passed directly.
|
|
@@ -260,7 +260,7 @@ export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntry
|
|
|
260
260
|
* @param [value] - The value for the new node (used if not in Map mode).
|
|
261
261
|
* @returns The newly created node.
|
|
262
262
|
*/
|
|
263
|
-
|
|
263
|
+
createNode(key: K, value?: V): BinaryTreeNode<K, V>;
|
|
264
264
|
/**
|
|
265
265
|
* Creates a new, empty tree of the same type and configuration.
|
|
266
266
|
* @remarks Time O(1) (excluding options cloning), Space O(1)
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BinaryTreeOptions, BSTNOptKeyOrNode, BSTOptions, BTNRep, Comparable, Comparator, CP, DFSOrderPattern, EntryCallback, IterationType, NodeCallback, NodePredicate, OptNode } from '../../types';
|
|
9
|
-
import { BinaryTree
|
|
8
|
+
import type { BinaryTreeOptions, BSTNOptKeyOrNode, BSTOptions, BTNRep, Comparable, Comparator, CP, DFSOrderPattern, EntryCallback, FamilyPosition, IterationType, NodeCallback, NodePredicate, OptNode, RBTNColor } from '../../types';
|
|
9
|
+
import { BinaryTree } from './binary-tree';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
import { Range } from '../../common';
|
|
12
12
|
/**
|
|
@@ -15,7 +15,9 @@ import { Range } from '../../common';
|
|
|
15
15
|
* @template K - The type of the key.
|
|
16
16
|
* @template V - The type of the value.
|
|
17
17
|
*/
|
|
18
|
-
export declare class BSTNode<K = any, V = any>
|
|
18
|
+
export declare class BSTNode<K = any, V = any> {
|
|
19
|
+
key: K;
|
|
20
|
+
value?: V;
|
|
19
21
|
parent?: BSTNode<K, V>;
|
|
20
22
|
/**
|
|
21
23
|
* Creates an instance of BSTNode.
|
|
@@ -55,6 +57,58 @@ export declare class BSTNode<K = any, V = any> extends BinaryTreeNode<K, V> {
|
|
|
55
57
|
* @param v - The node to set as the right child.
|
|
56
58
|
*/
|
|
57
59
|
set right(v: BSTNode<K, V> | null | undefined);
|
|
60
|
+
_height: number;
|
|
61
|
+
/**
|
|
62
|
+
* Gets the height of the node (used in self-balancing trees).
|
|
63
|
+
* @remarks Time O(1), Space O(1)
|
|
64
|
+
*
|
|
65
|
+
* @returns The height.
|
|
66
|
+
*/
|
|
67
|
+
get height(): number;
|
|
68
|
+
/**
|
|
69
|
+
* Sets the height of the node.
|
|
70
|
+
* @remarks Time O(1), Space O(1)
|
|
71
|
+
*
|
|
72
|
+
* @param value - The new height.
|
|
73
|
+
*/
|
|
74
|
+
set height(value: number);
|
|
75
|
+
_color: RBTNColor;
|
|
76
|
+
/**
|
|
77
|
+
* Gets the color of the node (used in Red-Black trees).
|
|
78
|
+
* @remarks Time O(1), Space O(1)
|
|
79
|
+
*
|
|
80
|
+
* @returns The node's color.
|
|
81
|
+
*/
|
|
82
|
+
get color(): RBTNColor;
|
|
83
|
+
/**
|
|
84
|
+
* Sets the color of the node.
|
|
85
|
+
* @remarks Time O(1), Space O(1)
|
|
86
|
+
*
|
|
87
|
+
* @param value - The new color.
|
|
88
|
+
*/
|
|
89
|
+
set color(value: RBTNColor);
|
|
90
|
+
_count: number;
|
|
91
|
+
/**
|
|
92
|
+
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
93
|
+
* @remarks Time O(1), Space O(1)
|
|
94
|
+
*
|
|
95
|
+
* @returns The subtree node count.
|
|
96
|
+
*/
|
|
97
|
+
get count(): number;
|
|
98
|
+
/**
|
|
99
|
+
* Sets the count of nodes in the subtree.
|
|
100
|
+
* @remarks Time O(1), Space O(1)
|
|
101
|
+
*
|
|
102
|
+
* @param value - The new count.
|
|
103
|
+
*/
|
|
104
|
+
set count(value: number);
|
|
105
|
+
/**
|
|
106
|
+
* Gets the position of the node relative to its parent.
|
|
107
|
+
* @remarks Time O(1), Space O(1)
|
|
108
|
+
*
|
|
109
|
+
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
110
|
+
*/
|
|
111
|
+
get familyPosition(): FamilyPosition;
|
|
58
112
|
}
|
|
59
113
|
/**
|
|
60
114
|
* Represents a Binary Search Tree (BST).
|
|
@@ -180,7 +234,7 @@ export declare class BST<K = any, V = any, R = any> extends BinaryTree<K, V, R>
|
|
|
180
234
|
* @param [value] - The value for the new node (used if not in Map mode).
|
|
181
235
|
* @returns The newly created BSTNode.
|
|
182
236
|
*/
|
|
183
|
-
|
|
237
|
+
createNode(key: K, value?: V): BSTNode<K, V>;
|
|
184
238
|
/**
|
|
185
239
|
* Ensures the input is a node. If it's a key or entry, it searches for the node.
|
|
186
240
|
* @remarks Time O(log N) (height of the tree), O(N) worst-case.
|
|
@@ -5,10 +5,12 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BinaryTreeDeleteResult, BinaryTreeOptions, CRUD, EntryCallback, RBTNColor, RedBlackTreeOptions } from '../../types';
|
|
9
|
-
import { BST
|
|
8
|
+
import type { BinaryTreeDeleteResult, BinaryTreeOptions, CRUD, EntryCallback, FamilyPosition, RBTNColor, RedBlackTreeOptions } from '../../types';
|
|
9
|
+
import { BST } from './bst';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
|
-
export declare class RedBlackTreeNode<K = any, V = any>
|
|
11
|
+
export declare class RedBlackTreeNode<K = any, V = any> {
|
|
12
|
+
key: K;
|
|
13
|
+
value?: V;
|
|
12
14
|
parent?: RedBlackTreeNode<K, V>;
|
|
13
15
|
/**
|
|
14
16
|
* Create a Red-Black Tree and optionally bulk-insert items.
|
|
@@ -47,14 +49,66 @@ export declare class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
47
49
|
* @returns void
|
|
48
50
|
*/
|
|
49
51
|
set right(v: RedBlackTreeNode<K, V> | null | undefined);
|
|
52
|
+
_height: number;
|
|
53
|
+
/**
|
|
54
|
+
* Gets the height of the node (used in self-balancing trees).
|
|
55
|
+
* @remarks Time O(1), Space O(1)
|
|
56
|
+
*
|
|
57
|
+
* @returns The height.
|
|
58
|
+
*/
|
|
59
|
+
get height(): number;
|
|
60
|
+
/**
|
|
61
|
+
* Sets the height of the node.
|
|
62
|
+
* @remarks Time O(1), Space O(1)
|
|
63
|
+
*
|
|
64
|
+
* @param value - The new height.
|
|
65
|
+
*/
|
|
66
|
+
set height(value: number);
|
|
67
|
+
_color: RBTNColor;
|
|
68
|
+
/**
|
|
69
|
+
* Gets the color of the node (used in Red-Black trees).
|
|
70
|
+
* @remarks Time O(1), Space O(1)
|
|
71
|
+
*
|
|
72
|
+
* @returns The node's color.
|
|
73
|
+
*/
|
|
74
|
+
get color(): RBTNColor;
|
|
75
|
+
/**
|
|
76
|
+
* Sets the color of the node.
|
|
77
|
+
* @remarks Time O(1), Space O(1)
|
|
78
|
+
*
|
|
79
|
+
* @param value - The new color.
|
|
80
|
+
*/
|
|
81
|
+
set color(value: RBTNColor);
|
|
82
|
+
_count: number;
|
|
83
|
+
/**
|
|
84
|
+
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
85
|
+
* @remarks Time O(1), Space O(1)
|
|
86
|
+
*
|
|
87
|
+
* @returns The subtree node count.
|
|
88
|
+
*/
|
|
89
|
+
get count(): number;
|
|
90
|
+
/**
|
|
91
|
+
* Sets the count of nodes in the subtree.
|
|
92
|
+
* @remarks Time O(1), Space O(1)
|
|
93
|
+
*
|
|
94
|
+
* @param value - The new count.
|
|
95
|
+
*/
|
|
96
|
+
set count(value: number);
|
|
97
|
+
/**
|
|
98
|
+
* Gets the position of the node relative to its parent.
|
|
99
|
+
* @remarks Time O(1), Space O(1)
|
|
100
|
+
*
|
|
101
|
+
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
102
|
+
*/
|
|
103
|
+
get familyPosition(): FamilyPosition;
|
|
50
104
|
}
|
|
51
105
|
/**
|
|
52
|
-
*
|
|
106
|
+
* Represents a Red-Black Tree (self-balancing BST) supporting map-like mode and stable O(log n) updates.
|
|
53
107
|
* @remarks Time O(1), Space O(1)
|
|
54
108
|
* @template K
|
|
55
109
|
* @template V
|
|
56
110
|
* @template R
|
|
57
|
-
* 1. Efficient self-balancing, but not completely balanced. Compared with AVLTree, the addition and deletion efficiency is high but the query efficiency is slightly lower.
|
|
111
|
+
* 1. Efficient self-balancing, but not completely balanced. Compared with AVLTree, the addition and deletion efficiency is high, but the query efficiency is slightly lower.
|
|
58
112
|
* 2. It is BST itself. Compared with Heap which is not completely ordered, RedBlackTree is completely ordered.
|
|
59
113
|
* @example
|
|
60
114
|
* // using Red-Black Tree as a price-based index for stock data
|
|
@@ -117,7 +171,7 @@ export declare class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R
|
|
|
117
171
|
* @param color - See parameter type for details.
|
|
118
172
|
* @returns A new RedBlackTreeNode instance.
|
|
119
173
|
*/
|
|
120
|
-
|
|
174
|
+
createNode(key: K, value?: V, color?: RBTNColor): RedBlackTreeNode<K, V>;
|
|
121
175
|
/**
|
|
122
176
|
* Type guard: check whether the input is a RedBlackTreeNode.
|
|
123
177
|
* @remarks Time O(1), Space O(1)
|
|
@@ -5,18 +5,20 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BinaryTreeDeleteResult, BinaryTreeOptions, BSTNOptKeyOrNode, EntryCallback, IterationType, RBTNColor, TreeCounterOptions } from '../../types';
|
|
8
|
+
import type { BinaryTreeDeleteResult, BinaryTreeOptions, BSTNOptKeyOrNode, EntryCallback, FamilyPosition, IterationType, RBTNColor, TreeCounterOptions } from '../../types';
|
|
9
9
|
import { BSTOptions } from '../../types';
|
|
10
10
|
import { BSTNode } from './bst';
|
|
11
11
|
import { IBinaryTree } from '../../interfaces';
|
|
12
|
-
import { RedBlackTree
|
|
12
|
+
import { RedBlackTree } from './red-black-tree';
|
|
13
13
|
/**
|
|
14
14
|
* RB-tree node with an extra 'count' field; keeps parent/child links.
|
|
15
15
|
* @remarks Time O(1), Space O(1)
|
|
16
16
|
* @template K
|
|
17
17
|
* @template V
|
|
18
18
|
*/
|
|
19
|
-
export declare class TreeCounterNode<K = any, V = any>
|
|
19
|
+
export declare class TreeCounterNode<K = any, V = any> {
|
|
20
|
+
key: K;
|
|
21
|
+
value?: V;
|
|
20
22
|
parent?: TreeCounterNode<K, V>;
|
|
21
23
|
/**
|
|
22
24
|
* Create a tree counter node.
|
|
@@ -56,6 +58,58 @@ export declare class TreeCounterNode<K = any, V = any> extends RedBlackTreeNode<
|
|
|
56
58
|
* @returns void
|
|
57
59
|
*/
|
|
58
60
|
set right(v: TreeCounterNode<K, V> | null | undefined);
|
|
61
|
+
_height: number;
|
|
62
|
+
/**
|
|
63
|
+
* Gets the height of the node (used in self-balancing trees).
|
|
64
|
+
* @remarks Time O(1), Space O(1)
|
|
65
|
+
*
|
|
66
|
+
* @returns The height.
|
|
67
|
+
*/
|
|
68
|
+
get height(): number;
|
|
69
|
+
/**
|
|
70
|
+
* Sets the height of the node.
|
|
71
|
+
* @remarks Time O(1), Space O(1)
|
|
72
|
+
*
|
|
73
|
+
* @param value - The new height.
|
|
74
|
+
*/
|
|
75
|
+
set height(value: number);
|
|
76
|
+
_color: RBTNColor;
|
|
77
|
+
/**
|
|
78
|
+
* Gets the color of the node (used in Red-Black trees).
|
|
79
|
+
* @remarks Time O(1), Space O(1)
|
|
80
|
+
*
|
|
81
|
+
* @returns The node's color.
|
|
82
|
+
*/
|
|
83
|
+
get color(): RBTNColor;
|
|
84
|
+
/**
|
|
85
|
+
* Sets the color of the node.
|
|
86
|
+
* @remarks Time O(1), Space O(1)
|
|
87
|
+
*
|
|
88
|
+
* @param value - The new color.
|
|
89
|
+
*/
|
|
90
|
+
set color(value: RBTNColor);
|
|
91
|
+
_count: number;
|
|
92
|
+
/**
|
|
93
|
+
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
94
|
+
* @remarks Time O(1), Space O(1)
|
|
95
|
+
*
|
|
96
|
+
* @returns The subtree node count.
|
|
97
|
+
*/
|
|
98
|
+
get count(): number;
|
|
99
|
+
/**
|
|
100
|
+
* Sets the count of nodes in the subtree.
|
|
101
|
+
* @remarks Time O(1), Space O(1)
|
|
102
|
+
*
|
|
103
|
+
* @param value - The new count.
|
|
104
|
+
*/
|
|
105
|
+
set count(value: number);
|
|
106
|
+
/**
|
|
107
|
+
* Gets the position of the node relative to its parent.
|
|
108
|
+
* @remarks Time O(1), Space O(1)
|
|
109
|
+
*
|
|
110
|
+
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
111
|
+
*/
|
|
112
|
+
get familyPosition(): FamilyPosition;
|
|
59
113
|
}
|
|
60
114
|
/**
|
|
61
115
|
* Red-Black Tree–based counter map (key → value with per-node count). Supports O(log N) updates and map-like mode.
|
|
@@ -86,7 +140,7 @@ export declare class TreeCounter<K = any, V = any, R = any> extends RedBlackTree
|
|
|
86
140
|
* @returns Total count recomputed from nodes.
|
|
87
141
|
*/
|
|
88
142
|
getComputedCount(): number;
|
|
89
|
-
|
|
143
|
+
createNode(key: K, value?: V, color?: RBTNColor, count?: number): TreeCounterNode<K, V>;
|
|
90
144
|
/**
|
|
91
145
|
* Type guard: check whether the input is a TreeCounterNode.
|
|
92
146
|
* @remarks Time O(1), Space O(1)
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { ElemOf, EntryCallback, RedBlackTreeOptions, TreeMultiMapOptions } from '../../types';
|
|
8
|
+
import type { ElemOf, EntryCallback, FamilyPosition, RBTNColor, RedBlackTreeOptions, TreeMultiMapOptions } from '../../types';
|
|
9
9
|
import { RedBlackTree, RedBlackTreeNode } from './red-black-tree';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
/**
|
|
@@ -14,7 +14,9 @@ import { IBinaryTree } from '../../interfaces';
|
|
|
14
14
|
* @template K
|
|
15
15
|
* @template V
|
|
16
16
|
*/
|
|
17
|
-
export declare class TreeMultiMapNode<K = any, V = any>
|
|
17
|
+
export declare class TreeMultiMapNode<K = any, V = any> {
|
|
18
|
+
key: K;
|
|
19
|
+
value?: V[];
|
|
18
20
|
parent?: TreeMultiMapNode<K, V>;
|
|
19
21
|
/**
|
|
20
22
|
* Create a TreeMultiMap node with an optional value bucket.
|
|
@@ -23,7 +25,7 @@ export declare class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode
|
|
|
23
25
|
* @param [value] - Initial array of values.
|
|
24
26
|
* @returns New TreeMultiMapNode instance.
|
|
25
27
|
*/
|
|
26
|
-
constructor(key: K, value?: V[]);
|
|
28
|
+
constructor(key: K, value?: V[], color?: RBTNColor);
|
|
27
29
|
_left?: TreeMultiMapNode<K, V> | null | undefined;
|
|
28
30
|
/**
|
|
29
31
|
* Get the left child pointer.
|
|
@@ -52,6 +54,58 @@ export declare class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode
|
|
|
52
54
|
* @returns void
|
|
53
55
|
*/
|
|
54
56
|
set right(v: TreeMultiMapNode<K, V> | null | undefined);
|
|
57
|
+
_height: number;
|
|
58
|
+
/**
|
|
59
|
+
* Gets the height of the node (used in self-balancing trees).
|
|
60
|
+
* @remarks Time O(1), Space O(1)
|
|
61
|
+
*
|
|
62
|
+
* @returns The height.
|
|
63
|
+
*/
|
|
64
|
+
get height(): number;
|
|
65
|
+
/**
|
|
66
|
+
* Sets the height of the node.
|
|
67
|
+
* @remarks Time O(1), Space O(1)
|
|
68
|
+
*
|
|
69
|
+
* @param value - The new height.
|
|
70
|
+
*/
|
|
71
|
+
set height(value: number);
|
|
72
|
+
_color: RBTNColor;
|
|
73
|
+
/**
|
|
74
|
+
* Gets the color of the node (used in Red-Black trees).
|
|
75
|
+
* @remarks Time O(1), Space O(1)
|
|
76
|
+
*
|
|
77
|
+
* @returns The node's color.
|
|
78
|
+
*/
|
|
79
|
+
get color(): RBTNColor;
|
|
80
|
+
/**
|
|
81
|
+
* Sets the color of the node.
|
|
82
|
+
* @remarks Time O(1), Space O(1)
|
|
83
|
+
*
|
|
84
|
+
* @param value - The new color.
|
|
85
|
+
*/
|
|
86
|
+
set color(value: RBTNColor);
|
|
87
|
+
_count: number;
|
|
88
|
+
/**
|
|
89
|
+
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
90
|
+
* @remarks Time O(1), Space O(1)
|
|
91
|
+
*
|
|
92
|
+
* @returns The subtree node count.
|
|
93
|
+
*/
|
|
94
|
+
get count(): number;
|
|
95
|
+
/**
|
|
96
|
+
* Sets the count of nodes in the subtree.
|
|
97
|
+
* @remarks Time O(1), Space O(1)
|
|
98
|
+
*
|
|
99
|
+
* @param value - The new count.
|
|
100
|
+
*/
|
|
101
|
+
set count(value: number);
|
|
102
|
+
/**
|
|
103
|
+
* Gets the position of the node relative to its parent.
|
|
104
|
+
* @remarks Time O(1), Space O(1)
|
|
105
|
+
*
|
|
106
|
+
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
107
|
+
*/
|
|
108
|
+
get familyPosition(): FamilyPosition;
|
|
55
109
|
}
|
|
56
110
|
/**
|
|
57
111
|
* Red-Black Tree–based multimap (key → array of values). Preserves O(log N) updates and supports map-like mode.
|
|
@@ -233,7 +287,15 @@ export declare class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTre
|
|
|
233
287
|
* @returns New TreeMultiMap instance.
|
|
234
288
|
*/
|
|
235
289
|
constructor(keysNodesEntriesOrRaws?: Iterable<K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | R>, options?: TreeMultiMapOptions<K, V[], R>);
|
|
236
|
-
|
|
290
|
+
createNode(key: K, value?: V[]): TreeMultiMapNode<K, V>;
|
|
291
|
+
/**
|
|
292
|
+
* Checks if the given item is a `TreeMultiMapNode` instance.
|
|
293
|
+
* @remarks Time O(1), Space O(1)
|
|
294
|
+
*
|
|
295
|
+
* @param keyNodeOrEntry - The item to check.
|
|
296
|
+
* @returns True if it's a TreeMultiMapNode, false otherwise.
|
|
297
|
+
*/
|
|
298
|
+
isNode(keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined): keyNodeOrEntry is TreeMultiMapNode<K, V>;
|
|
237
299
|
add(keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined): boolean;
|
|
238
300
|
add(key: K, value: V): boolean;
|
|
239
301
|
/**
|