red-black-tree-typed 2.1.2 → 2.2.1

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.
Files changed (37) hide show
  1. package/dist/cjs/index.cjs +329 -177
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +4019 -0
  4. package/dist/cjs-legacy/index.cjs.map +1 -0
  5. package/dist/esm/index.mjs +329 -177
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +4010 -0
  8. package/dist/esm-legacy/index.mjs.map +1 -0
  9. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +57 -3
  10. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +65 -3
  11. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +61 -5
  12. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -0
  13. package/dist/types/data-structures/binary-tree/bst.d.ts +58 -3
  14. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +59 -4
  15. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +57 -3
  16. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +66 -3
  17. package/dist/types/types/data-structures/base/base.d.ts +1 -1
  18. package/dist/umd/red-black-tree-typed.js +176 -22
  19. package/dist/umd/red-black-tree-typed.js.map +1 -1
  20. package/dist/umd/red-black-tree-typed.min.js +3 -3
  21. package/dist/umd/red-black-tree-typed.min.js.map +1 -1
  22. package/package.json +20 -2
  23. package/src/data-structures/base/iterable-entry-base.ts +4 -4
  24. package/src/data-structures/binary-tree/avl-tree-counter.ts +103 -12
  25. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +116 -12
  26. package/src/data-structures/binary-tree/avl-tree.ts +109 -16
  27. package/src/data-structures/binary-tree/binary-tree.ts +3 -2
  28. package/src/data-structures/binary-tree/bst.ts +104 -12
  29. package/src/data-structures/binary-tree/red-black-tree.ts +110 -19
  30. package/src/data-structures/binary-tree/tree-counter.ts +102 -11
  31. package/src/data-structures/binary-tree/tree-multi-map.ts +124 -12
  32. package/src/data-structures/graph/abstract-graph.ts +8 -8
  33. package/src/data-structures/graph/directed-graph.ts +5 -5
  34. package/src/data-structures/graph/undirected-graph.ts +5 -5
  35. package/src/data-structures/hash/hash-map.ts +4 -4
  36. package/src/types/data-structures/base/base.ts +1 -1
  37. package/tsup.node.config.js +40 -6
@@ -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, AVLTreeNode } from './avl-tree';
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> extends AVLTreeNode<K, V> {
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.
@@ -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> extends AVLTreeNode<K, V[]> {
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: V[]);
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.
@@ -70,6 +124,14 @@ export declare class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<
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, BSTNode } from './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> extends BSTNode<K, V> {
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.
@@ -71,7 +125,9 @@ export declare class AVLTreeNode<K = any, V = any> extends BSTNode<K, V> {
71
125
  * 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
126
  * 5. Efficient Lookups: Offers O(log n) search time, where 'n' is the number of nodes, due to its balanced nature.
73
127
  * 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
128
+ * 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.
129
+ *
130
+ * @example
75
131
  * // Find elements in a range
76
132
  * // 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
133
  * type Datum = { timestamp: Date; temperature: number };
@@ -219,7 +275,7 @@ export declare class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> imp
219
275
  * @param [options] - Options for the new tree.
220
276
  * @returns A new AVLTree.
221
277
  */
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>;
278
+ 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
279
  /**
224
280
  * (Protected) Swaps properties of two nodes, including height.
225
281
  * @remarks Time O(H) (due to `ensureNode`), but O(1) if nodes are passed directly.
@@ -123,6 +123,7 @@ export declare class BinaryTreeNode<K = any, V = any> {
123
123
  * 3. Depth and Height: Depth is the number of edges from the root to a node; height is the maximum depth in the tree.
124
124
  * 4. Subtrees: Each child of a node forms the root of a subtree.
125
125
  * 5. Leaf Nodes: Nodes without children are leaves.
126
+ *
126
127
  * @example
127
128
  * // determine loan approval using a decision tree
128
129
  * // Decision tree structure
@@ -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, BinaryTreeNode } from './binary-tree';
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> extends BinaryTreeNode<K, V> {
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).
@@ -70,6 +124,7 @@ export declare class BSTNode<K = any, V = any> extends BinaryTreeNode<K, V> {
70
124
  * 5. Logarithmic Operations: Ideal operations like insertion, deletion, and searching are O(log n) time-efficient.
71
125
  * 6. Balance Variability: Can become unbalanced; special types maintain balance.
72
126
  * 7. No Auto-Balancing: Standard BSTs don't automatically balance themselves.
127
+ *
73
128
  * @example
74
129
  * // Merge 3 sorted datasets
75
130
  * const dataset1 = new BST<number, string>([
@@ -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, BSTNode } from './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> extends BSTNode<K, V> {
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,6 +49,58 @@ 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.
@@ -54,8 +108,9 @@ export declare class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
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.
113
+ *
59
114
  * @example
60
115
  * // using Red-Black Tree as a price-based index for stock data
61
116
  * // Define the structure of individual stock records
@@ -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, RedBlackTreeNode } from './red-black-tree';
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> extends RedBlackTreeNode<K, V> {
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.
@@ -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> extends RedBlackTreeNode<K, V[]> {
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.
@@ -59,6 +113,7 @@ export declare class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode
59
113
  * @template K
60
114
  * @template V
61
115
  * @template R
116
+ *
62
117
  * @example
63
118
  * // players ranked by score with their equipment
64
119
  * type Equipment = {
@@ -234,6 +289,14 @@ export declare class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTre
234
289
  */
235
290
  constructor(keysNodesEntriesOrRaws?: Iterable<K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | R>, options?: TreeMultiMapOptions<K, V[], R>);
236
291
  createNode(key: K, value?: V[]): TreeMultiMapNode<K, V>;
292
+ /**
293
+ * Checks if the given item is a `TreeMultiMapNode` instance.
294
+ * @remarks Time O(1), Space O(1)
295
+ *
296
+ * @param keyNodeOrEntry - The item to check.
297
+ * @returns True if it's a TreeMultiMapNode, false otherwise.
298
+ */
299
+ isNode(keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined): keyNodeOrEntry is TreeMultiMapNode<K, V>;
237
300
  add(keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined): boolean;
238
301
  add(key: K, value: V): boolean;
239
302
  /**
@@ -1,6 +1,6 @@
1
1
  import { IterableElementBase, IterableEntryBase } from '../../../data-structures';
2
2
  import { LinearBase } from '../../../data-structures/base/linear-base';
3
- export type EntryCallback<K, V, R> = (key: K, value: V, index: number, original: IterableEntryBase<K, V>) => R;
3
+ export type EntryCallback<K, V, R> = (value: V, key: K, index: number, original: IterableEntryBase<K, V>) => R;
4
4
  export type ElementCallback<E, R, RT> = (element: E, index: number, original: IterableElementBase<E, R>) => RT;
5
5
  export type ReduceEntryCallback<K, V, R> = (accumulator: R, value: V, key: K, index: number, original: IterableEntryBase<K, V>) => R;
6
6
  export type ReduceElementCallback<E, R, U = E> = (accumulator: U, value: E, index: number, self: IterableElementBase<E, R>) => U;