min-priority-queue-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 (33) hide show
  1. package/dist/cjs/index.cjs +60 -56
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs-legacy/index.cjs +998 -0
  4. package/dist/cjs-legacy/index.cjs.map +1 -0
  5. package/dist/esm/index.mjs +60 -56
  6. package/dist/esm/index.mjs.map +1 -1
  7. package/dist/esm-legacy/index.mjs +990 -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/package.json +20 -2
  19. package/src/data-structures/base/iterable-entry-base.ts +4 -4
  20. package/src/data-structures/binary-tree/avl-tree-counter.ts +103 -12
  21. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +116 -12
  22. package/src/data-structures/binary-tree/avl-tree.ts +109 -16
  23. package/src/data-structures/binary-tree/binary-tree.ts +3 -2
  24. package/src/data-structures/binary-tree/bst.ts +104 -12
  25. package/src/data-structures/binary-tree/red-black-tree.ts +110 -19
  26. package/src/data-structures/binary-tree/tree-counter.ts +102 -11
  27. package/src/data-structures/binary-tree/tree-multi-map.ts +124 -12
  28. package/src/data-structures/graph/abstract-graph.ts +8 -8
  29. package/src/data-structures/graph/directed-graph.ts +5 -5
  30. package/src/data-structures/graph/undirected-graph.ts +5 -5
  31. package/src/data-structures/hash/hash-map.ts +4 -4
  32. package/src/types/data-structures/base/base.ts +1 -1
  33. package/tsup.node.config.js +40 -6
@@ -11,15 +11,18 @@ import type {
11
11
  BinaryTreeOptions,
12
12
  CRUD,
13
13
  EntryCallback,
14
+ FamilyPosition,
14
15
  OptNode,
15
16
  RBTNColor,
16
17
  RedBlackTreeOptions
17
18
  } from '../../types';
18
- import { BST, BSTNode } from './bst';
19
+ import { BST } from './bst';
19
20
  import { IBinaryTree } from '../../interfaces';
20
21
 
21
- export class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
22
- override parent?: RedBlackTreeNode<K, V> = undefined;
22
+ export class RedBlackTreeNode<K = any, V = any> {
23
+ key: K;
24
+ value?: V;
25
+ parent?: RedBlackTreeNode<K, V> = undefined;
23
26
 
24
27
  /**
25
28
  * Create a Red-Black Tree and optionally bulk-insert items.
@@ -31,11 +34,12 @@ export class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
31
34
  */
32
35
 
33
36
  constructor(key: K, value?: V, color: RBTNColor = 'BLACK') {
34
- super(key, value);
35
- this._color = color;
37
+ this.key = key;
38
+ this.value = value;
39
+ this.color = color;
36
40
  }
37
41
 
38
- override _left?: RedBlackTreeNode<K, V> | null | undefined = undefined;
42
+ _left?: RedBlackTreeNode<K, V> | null | undefined = undefined;
39
43
 
40
44
  /**
41
45
  * Get the left child pointer.
@@ -43,7 +47,7 @@ export class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
43
47
  * @returns Left child node, or null/undefined.
44
48
  */
45
49
 
46
- override get left(): RedBlackTreeNode<K, V> | null | undefined {
50
+ get left(): RedBlackTreeNode<K, V> | null | undefined {
47
51
  return this._left;
48
52
  }
49
53
 
@@ -54,14 +58,14 @@ export class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
54
58
  * @returns void
55
59
  */
56
60
 
57
- override set left(v: RedBlackTreeNode<K, V> | null | undefined) {
61
+ set left(v: RedBlackTreeNode<K, V> | null | undefined) {
58
62
  if (v) {
59
63
  v.parent = this;
60
64
  }
61
65
  this._left = v;
62
66
  }
63
67
 
64
- override _right?: RedBlackTreeNode<K, V> | null | undefined = undefined;
68
+ _right?: RedBlackTreeNode<K, V> | null | undefined = undefined;
65
69
 
66
70
  /**
67
71
  * Get the right child pointer.
@@ -69,7 +73,7 @@ export class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
69
73
  * @returns Right child node, or null/undefined.
70
74
  */
71
75
 
72
- override get right(): RedBlackTreeNode<K, V> | null | undefined {
76
+ get right(): RedBlackTreeNode<K, V> | null | undefined {
73
77
  return this._right;
74
78
  }
75
79
 
@@ -80,12 +84,98 @@ export class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
80
84
  * @returns void
81
85
  */
82
86
 
83
- override set right(v: RedBlackTreeNode<K, V> | null | undefined) {
87
+ set right(v: RedBlackTreeNode<K, V> | null | undefined) {
84
88
  if (v) {
85
89
  v.parent = this;
86
90
  }
87
91
  this._right = v;
88
92
  }
93
+
94
+ _height: number = 0;
95
+
96
+ /**
97
+ * Gets the height of the node (used in self-balancing trees).
98
+ * @remarks Time O(1), Space O(1)
99
+ *
100
+ * @returns The height.
101
+ */
102
+ get height(): number {
103
+ return this._height;
104
+ }
105
+
106
+ /**
107
+ * Sets the height of the node.
108
+ * @remarks Time O(1), Space O(1)
109
+ *
110
+ * @param value - The new height.
111
+ */
112
+ set height(value: number) {
113
+ this._height = value;
114
+ }
115
+
116
+ _color: RBTNColor = 'BLACK';
117
+
118
+ /**
119
+ * Gets the color of the node (used in Red-Black trees).
120
+ * @remarks Time O(1), Space O(1)
121
+ *
122
+ * @returns The node's color.
123
+ */
124
+ get color(): RBTNColor {
125
+ return this._color;
126
+ }
127
+
128
+ /**
129
+ * Sets the color of the node.
130
+ * @remarks Time O(1), Space O(1)
131
+ *
132
+ * @param value - The new color.
133
+ */
134
+ set color(value: RBTNColor) {
135
+ this._color = value;
136
+ }
137
+
138
+ _count: number = 1;
139
+
140
+ /**
141
+ * Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
142
+ * @remarks Time O(1), Space O(1)
143
+ *
144
+ * @returns The subtree node count.
145
+ */
146
+ get count(): number {
147
+ return this._count;
148
+ }
149
+
150
+ /**
151
+ * Sets the count of nodes in the subtree.
152
+ * @remarks Time O(1), Space O(1)
153
+ *
154
+ * @param value - The new count.
155
+ */
156
+ set count(value: number) {
157
+ this._count = value;
158
+ }
159
+
160
+ /**
161
+ * Gets the position of the node relative to its parent.
162
+ * @remarks Time O(1), Space O(1)
163
+ *
164
+ * @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
165
+ */
166
+ get familyPosition(): FamilyPosition {
167
+ if (!this.parent) {
168
+ return this.left || this.right ? 'ROOT' : 'ISOLATED';
169
+ }
170
+
171
+ if (this.parent.left === this) {
172
+ return this.left || this.right ? 'ROOT_LEFT' : 'LEFT';
173
+ } else if (this.parent.right === this) {
174
+ return this.left || this.right ? 'ROOT_RIGHT' : 'RIGHT';
175
+ }
176
+
177
+ return 'MAL_NODE';
178
+ }
89
179
  }
90
180
 
91
181
  /**
@@ -94,8 +184,9 @@ export class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
94
184
  * @template K
95
185
  * @template V
96
186
  * @template R
97
- * 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.
187
+ * 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.
98
188
  * 2. It is BST itself. Compared with Heap which is not completely ordered, RedBlackTree is completely ordered.
189
+ *
99
190
  * @example
100
191
  * // using Red-Black Tree as a price-based index for stock data
101
192
  * // Define the structure of individual stock records
@@ -330,7 +421,7 @@ export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implem
330
421
 
331
422
  let index = 0;
332
423
  for (const [key, value] of this) {
333
- out.add(callback.call(thisArg, key, value, index++, this));
424
+ out.add(callback.call(thisArg, value, key, index++, this));
334
425
  }
335
426
  return out;
336
427
  }
@@ -380,10 +471,10 @@ export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implem
380
471
  */
381
472
 
382
473
  protected _insert(node: RedBlackTreeNode<K, V>): CRUD {
383
- let current = this.root;
474
+ let current = this.root ?? this.NIL;
384
475
  let parent: RedBlackTreeNode<K, V> | undefined = undefined;
385
476
 
386
- while (this.isRealNode(current)) {
477
+ while (current !== this.NIL) {
387
478
  parent = current;
388
479
  const compared = this._compare(node.key, current.key);
389
480
  if (compared < 0) {
@@ -459,7 +550,7 @@ export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implem
459
550
  this._leftRotate(z);
460
551
  }
461
552
 
462
- if (z && this.isRealNode(z.parent) && this.isRealNode(z.parent.parent)) {
553
+ if (z && z.parent && z.parent.parent) {
463
554
  z.parent.color = 'BLACK';
464
555
  z.parent.parent.color = 'RED';
465
556
  this._rightRotate(z.parent.parent);
@@ -478,7 +569,7 @@ export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implem
478
569
  this._rightRotate(z);
479
570
  }
480
571
 
481
- if (z && this.isRealNode(z.parent) && this.isRealNode(z.parent.parent)) {
572
+ if (z && z.parent && z.parent.parent) {
482
573
  z.parent.color = 'BLACK';
483
574
  z.parent.parent.color = 'RED';
484
575
  this._leftRotate(z.parent.parent);
@@ -575,7 +666,7 @@ export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implem
575
666
  const y = x.right;
576
667
  x.right = y.left;
577
668
 
578
- if (this.isRealNode(y.left)) {
669
+ if (y.left && y.left !== this.NIL) {
579
670
  y.left.parent = x;
580
671
  }
581
672
 
@@ -608,7 +699,7 @@ export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implem
608
699
  const x = y.left;
609
700
  y.left = x.right;
610
701
 
611
- if (this.isRealNode(x.right)) {
702
+ if (x.right && x.right !== this.NIL) {
612
703
  x.right.parent = y;
613
704
  }
614
705
 
@@ -11,6 +11,7 @@ import type {
11
11
  BinaryTreeOptions,
12
12
  BSTNOptKeyOrNode,
13
13
  EntryCallback,
14
+ FamilyPosition,
14
15
  IterationType,
15
16
  OptNode,
16
17
  RBTNColor,
@@ -19,7 +20,7 @@ import type {
19
20
  import { BSTOptions } from '../../types';
20
21
  import { BSTNode } from './bst';
21
22
  import { IBinaryTree } from '../../interfaces';
22
- import { RedBlackTree, RedBlackTreeNode } from './red-black-tree';
23
+ import { RedBlackTree } from './red-black-tree';
23
24
 
24
25
  /**
25
26
  * RB-tree node with an extra 'count' field; keeps parent/child links.
@@ -27,8 +28,10 @@ import { RedBlackTree, RedBlackTreeNode } from './red-black-tree';
27
28
  * @template K
28
29
  * @template V
29
30
  */
30
- export class TreeCounterNode<K = any, V = any> extends RedBlackTreeNode<K, V> {
31
- override parent?: TreeCounterNode<K, V> = undefined;
31
+ export class TreeCounterNode<K = any, V = any> {
32
+ key: K;
33
+ value?: V;
34
+ parent?: TreeCounterNode<K, V> = undefined;
32
35
 
33
36
  /**
34
37
  * Create a tree counter node.
@@ -40,18 +43,20 @@ export class TreeCounterNode<K = any, V = any> extends RedBlackTreeNode<K, V> {
40
43
  * @returns New TreeCounterNode instance.
41
44
  */
42
45
  constructor(key: K, value?: V, count = 1, color: RBTNColor = 'BLACK') {
43
- super(key, value, color);
46
+ this.key = key;
47
+ this.value = value;
48
+ this.color = color;
44
49
  this.count = count;
45
50
  }
46
51
 
47
- override _left?: TreeCounterNode<K, V> | null | undefined = undefined;
52
+ _left?: TreeCounterNode<K, V> | null | undefined = undefined;
48
53
 
49
54
  /**
50
55
  * Get the left child pointer.
51
56
  * @remarks Time O(1), Space O(1)
52
57
  * @returns Left child node, or null/undefined.
53
58
  */
54
- override get left(): TreeCounterNode<K, V> | null | undefined {
59
+ get left(): TreeCounterNode<K, V> | null | undefined {
55
60
  return this._left;
56
61
  }
57
62
 
@@ -61,21 +66,21 @@ export class TreeCounterNode<K = any, V = any> extends RedBlackTreeNode<K, V> {
61
66
  * @param v - New left child node, or null/undefined.
62
67
  * @returns void
63
68
  */
64
- override set left(v: TreeCounterNode<K, V> | null | undefined) {
69
+ set left(v: TreeCounterNode<K, V> | null | undefined) {
65
70
  if (v) {
66
71
  v.parent = this;
67
72
  }
68
73
  this._left = v;
69
74
  }
70
75
 
71
- override _right?: TreeCounterNode<K, V> | null | undefined = undefined;
76
+ _right?: TreeCounterNode<K, V> | null | undefined = undefined;
72
77
 
73
78
  /**
74
79
  * Get the right child pointer.
75
80
  * @remarks Time O(1), Space O(1)
76
81
  * @returns Right child node, or null/undefined.
77
82
  */
78
- override get right(): TreeCounterNode<K, V> | null | undefined {
83
+ get right(): TreeCounterNode<K, V> | null | undefined {
79
84
  return this._right;
80
85
  }
81
86
 
@@ -85,12 +90,98 @@ export class TreeCounterNode<K = any, V = any> extends RedBlackTreeNode<K, V> {
85
90
  * @param v - New right child node, or null/undefined.
86
91
  * @returns void
87
92
  */
88
- override set right(v: TreeCounterNode<K, V> | null | undefined) {
93
+ set right(v: TreeCounterNode<K, V> | null | undefined) {
89
94
  if (v) {
90
95
  v.parent = this;
91
96
  }
92
97
  this._right = v;
93
98
  }
99
+
100
+ _height: number = 0;
101
+
102
+ /**
103
+ * Gets the height of the node (used in self-balancing trees).
104
+ * @remarks Time O(1), Space O(1)
105
+ *
106
+ * @returns The height.
107
+ */
108
+ get height(): number {
109
+ return this._height;
110
+ }
111
+
112
+ /**
113
+ * Sets the height of the node.
114
+ * @remarks Time O(1), Space O(1)
115
+ *
116
+ * @param value - The new height.
117
+ */
118
+ set height(value: number) {
119
+ this._height = value;
120
+ }
121
+
122
+ _color: RBTNColor = 'BLACK';
123
+
124
+ /**
125
+ * Gets the color of the node (used in Red-Black trees).
126
+ * @remarks Time O(1), Space O(1)
127
+ *
128
+ * @returns The node's color.
129
+ */
130
+ get color(): RBTNColor {
131
+ return this._color;
132
+ }
133
+
134
+ /**
135
+ * Sets the color of the node.
136
+ * @remarks Time O(1), Space O(1)
137
+ *
138
+ * @param value - The new color.
139
+ */
140
+ set color(value: RBTNColor) {
141
+ this._color = value;
142
+ }
143
+
144
+ _count: number = 1;
145
+
146
+ /**
147
+ * Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
148
+ * @remarks Time O(1), Space O(1)
149
+ *
150
+ * @returns The subtree node count.
151
+ */
152
+ get count(): number {
153
+ return this._count;
154
+ }
155
+
156
+ /**
157
+ * Sets the count of nodes in the subtree.
158
+ * @remarks Time O(1), Space O(1)
159
+ *
160
+ * @param value - The new count.
161
+ */
162
+ set count(value: number) {
163
+ this._count = value;
164
+ }
165
+
166
+ /**
167
+ * Gets the position of the node relative to its parent.
168
+ * @remarks Time O(1), Space O(1)
169
+ *
170
+ * @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
171
+ */
172
+ get familyPosition(): FamilyPosition {
173
+ if (!this.parent) {
174
+ return this.left || this.right ? 'ROOT' : 'ISOLATED';
175
+ }
176
+
177
+ if (this.parent.left === this) {
178
+ return this.left || this.right ? 'ROOT_LEFT' : 'LEFT';
179
+ } else if (this.parent.right === this) {
180
+ return this.left || this.right ? 'ROOT_RIGHT' : 'RIGHT';
181
+ }
182
+
183
+ return 'MAL_NODE';
184
+ }
94
185
  }
95
186
 
96
187
  /**
@@ -348,7 +439,7 @@ export class TreeCounter<K = any, V = any, R = any> extends RedBlackTree<K, V, R
348
439
 
349
440
  let index = 0;
350
441
  for (const [key, value] of this) {
351
- out.add(callback.call(thisArg, key, value, index++, this));
442
+ out.add(callback.call(thisArg, value, key, index++, this));
352
443
  }
353
444
  return out;
354
445
  }
@@ -6,7 +6,15 @@
6
6
  * @license MIT License
7
7
  */
8
8
 
9
- import type { BTNOptKeyOrNull, ElemOf, EntryCallback, RedBlackTreeOptions, TreeMultiMapOptions } from '../../types';
9
+ import type {
10
+ BTNOptKeyOrNull,
11
+ ElemOf,
12
+ EntryCallback,
13
+ FamilyPosition,
14
+ RBTNColor,
15
+ RedBlackTreeOptions,
16
+ TreeMultiMapOptions
17
+ } from '../../types';
10
18
  import { RedBlackTree, RedBlackTreeNode } from './red-black-tree';
11
19
  import { IBinaryTree } from '../../interfaces';
12
20
 
@@ -16,8 +24,10 @@ import { IBinaryTree } from '../../interfaces';
16
24
  * @template K
17
25
  * @template V
18
26
  */
19
- export class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode<K, V[]> {
20
- override parent?: TreeMultiMapNode<K, V> = undefined;
27
+ export class TreeMultiMapNode<K = any, V = any> {
28
+ key: K;
29
+ value?: V[];
30
+ parent?: TreeMultiMapNode<K, V> = undefined;
21
31
 
22
32
  /**
23
33
  * Create a TreeMultiMap node with an optional value bucket.
@@ -26,18 +36,20 @@ export class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode<K, V[]>
26
36
  * @param [value] - Initial array of values.
27
37
  * @returns New TreeMultiMapNode instance.
28
38
  */
29
- constructor(key: K, value?: V[]) {
30
- super(key, value);
39
+ constructor(key: K, value: V[] = [], color: RBTNColor = 'BLACK') {
40
+ this.key = key;
41
+ this.value = value;
42
+ this.color = color;
31
43
  }
32
44
 
33
- override _left?: TreeMultiMapNode<K, V> | null | undefined = undefined;
45
+ _left?: TreeMultiMapNode<K, V> | null | undefined = undefined;
34
46
 
35
47
  /**
36
48
  * Get the left child pointer.
37
49
  * @remarks Time O(1), Space O(1)
38
50
  * @returns Left child node, or null/undefined.
39
51
  */
40
- override get left(): TreeMultiMapNode<K, V> | null | undefined {
52
+ get left(): TreeMultiMapNode<K, V> | null | undefined {
41
53
  return this._left;
42
54
  }
43
55
 
@@ -47,21 +59,21 @@ export class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode<K, V[]>
47
59
  * @param v - New left child node, or null/undefined.
48
60
  * @returns void
49
61
  */
50
- override set left(v: TreeMultiMapNode<K, V> | null | undefined) {
62
+ set left(v: TreeMultiMapNode<K, V> | null | undefined) {
51
63
  if (v) {
52
64
  v.parent = this;
53
65
  }
54
66
  this._left = v;
55
67
  }
56
68
 
57
- override _right?: TreeMultiMapNode<K, V> | null | undefined = undefined;
69
+ _right?: TreeMultiMapNode<K, V> | null | undefined = undefined;
58
70
 
59
71
  /**
60
72
  * Get the right child pointer.
61
73
  * @remarks Time O(1), Space O(1)
62
74
  * @returns Right child node, or null/undefined.
63
75
  */
64
- override get right(): TreeMultiMapNode<K, V> | null | undefined {
76
+ get right(): TreeMultiMapNode<K, V> | null | undefined {
65
77
  return this._right;
66
78
  }
67
79
 
@@ -71,12 +83,98 @@ export class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode<K, V[]>
71
83
  * @param v - New right child node, or null/undefined.
72
84
  * @returns void
73
85
  */
74
- override set right(v: TreeMultiMapNode<K, V> | null | undefined) {
86
+ set right(v: TreeMultiMapNode<K, V> | null | undefined) {
75
87
  if (v) {
76
88
  v.parent = this;
77
89
  }
78
90
  this._right = v;
79
91
  }
92
+
93
+ _height: number = 0;
94
+
95
+ /**
96
+ * Gets the height of the node (used in self-balancing trees).
97
+ * @remarks Time O(1), Space O(1)
98
+ *
99
+ * @returns The height.
100
+ */
101
+ get height(): number {
102
+ return this._height;
103
+ }
104
+
105
+ /**
106
+ * Sets the height of the node.
107
+ * @remarks Time O(1), Space O(1)
108
+ *
109
+ * @param value - The new height.
110
+ */
111
+ set height(value: number) {
112
+ this._height = value;
113
+ }
114
+
115
+ _color: RBTNColor = 'BLACK';
116
+
117
+ /**
118
+ * Gets the color of the node (used in Red-Black trees).
119
+ * @remarks Time O(1), Space O(1)
120
+ *
121
+ * @returns The node's color.
122
+ */
123
+ get color(): RBTNColor {
124
+ return this._color;
125
+ }
126
+
127
+ /**
128
+ * Sets the color of the node.
129
+ * @remarks Time O(1), Space O(1)
130
+ *
131
+ * @param value - The new color.
132
+ */
133
+ set color(value: RBTNColor) {
134
+ this._color = value;
135
+ }
136
+
137
+ _count: number = 1;
138
+
139
+ /**
140
+ * Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
141
+ * @remarks Time O(1), Space O(1)
142
+ *
143
+ * @returns The subtree node count.
144
+ */
145
+ get count(): number {
146
+ return this._count;
147
+ }
148
+
149
+ /**
150
+ * Sets the count of nodes in the subtree.
151
+ * @remarks Time O(1), Space O(1)
152
+ *
153
+ * @param value - The new count.
154
+ */
155
+ set count(value: number) {
156
+ this._count = value;
157
+ }
158
+
159
+ /**
160
+ * Gets the position of the node relative to its parent.
161
+ * @remarks Time O(1), Space O(1)
162
+ *
163
+ * @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
164
+ */
165
+ get familyPosition(): FamilyPosition {
166
+ if (!this.parent) {
167
+ return this.left || this.right ? 'ROOT' : 'ISOLATED';
168
+ }
169
+
170
+ if (this.parent.left === this) {
171
+ return this.left || this.right ? 'ROOT_LEFT' : 'LEFT';
172
+ } else if (this.parent.right === this) {
173
+ return this.left || this.right ? 'ROOT_RIGHT' : 'RIGHT';
174
+ }
175
+
176
+ return 'MAL_NODE';
177
+ }
80
178
  }
81
179
 
82
180
  /**
@@ -85,6 +183,7 @@ export class TreeMultiMapNode<K = any, V = any> extends RedBlackTreeNode<K, V[]>
85
183
  * @template K
86
184
  * @template V
87
185
  * @template R
186
+ *
88
187
  * @example
89
188
  * // players ranked by score with their equipment
90
189
  * type Equipment = {
@@ -274,6 +373,19 @@ export class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTree<K, V[]
274
373
  return new TreeMultiMapNode<K, V>(key, this._isMapMode ? [] : value);
275
374
  }
276
375
 
376
+ /**
377
+ * Checks if the given item is a `TreeMultiMapNode` instance.
378
+ * @remarks Time O(1), Space O(1)
379
+ *
380
+ * @param keyNodeOrEntry - The item to check.
381
+ * @returns True if it's a TreeMultiMapNode, false otherwise.
382
+ */
383
+ override isNode(
384
+ keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined
385
+ ): keyNodeOrEntry is TreeMultiMapNode<K, V> {
386
+ return keyNodeOrEntry instanceof TreeMultiMapNode;
387
+ }
388
+
277
389
  override add(
278
390
  keyNodeOrEntry: K | TreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined
279
391
  ): boolean;
@@ -392,7 +504,7 @@ export class TreeMultiMap<K = any, V = any, R = any> extends RedBlackTree<K, V[]
392
504
  ): RedBlackTree<MK, MV, MR> {
393
505
  const out = this._createLike<MK, MV, MR>([], options);
394
506
  let i = 0;
395
- for (const [k, v] of this) out.add(callback.call(thisArg, k, v, i++, this));
507
+ for (const [k, v] of this) out.add(callback.call(thisArg, v, k, i++, this));
396
508
  return out;
397
509
  }
398
510
 
@@ -50,11 +50,11 @@ export abstract class AbstractEdge<E = any> {
50
50
  * @example examples will be generated by unit test
51
51
  */
52
52
  export abstract class AbstractGraph<
53
- V = any,
54
- E = any,
55
- VO extends AbstractVertex<V> = AbstractVertex<V>,
56
- EO extends AbstractEdge<E> = AbstractEdge<E>
57
- >
53
+ V = any,
54
+ E = any,
55
+ VO extends AbstractVertex<V> = AbstractVertex<V>,
56
+ EO extends AbstractEdge<E> = AbstractEdge<E>
57
+ >
58
58
  extends IterableEntryBase<VertexKey, V | undefined>
59
59
  implements IGraph<V, E, VO, EO>
60
60
  {
@@ -897,7 +897,7 @@ export abstract class AbstractGraph<
897
897
  const filtered: [VertexKey, V | undefined][] = [];
898
898
  let index = 0;
899
899
  for (const [key, value] of this) {
900
- if (predicate.call(thisArg, key, value, index, this)) {
900
+ if (predicate.call(thisArg, value, key, index, this)) {
901
901
  filtered.push([key, value]);
902
902
  }
903
903
  index++;
@@ -916,7 +916,7 @@ export abstract class AbstractGraph<
916
916
  const filtered: [VertexKey, V | undefined][] = [];
917
917
  let index = 0;
918
918
  for (const [key, value] of this) {
919
- if (predicate.call(thisArg, key, value, index, this)) {
919
+ if (predicate.call(thisArg, value, key, index, this)) {
920
920
  filtered.push([key, value]);
921
921
  }
922
922
  index++;
@@ -928,7 +928,7 @@ export abstract class AbstractGraph<
928
928
  const mapped: T[] = [];
929
929
  let index = 0;
930
930
  for (const [key, value] of this) {
931
- mapped.push(callback.call(thisArg, key, value, index, this));
931
+ mapped.push(callback.call(thisArg, value, key, index, this));
932
932
  index++;
933
933
  }
934
934
  return mapped;
@@ -38,11 +38,11 @@ export class DirectedEdge<E = any> extends AbstractEdge<E> {
38
38
  * @example examples will be generated by unit test
39
39
  */
40
40
  export class DirectedGraph<
41
- V = any,
42
- E = any,
43
- VO extends DirectedVertex<V> = DirectedVertex<V>,
44
- EO extends DirectedEdge<E> = DirectedEdge<E>
45
- >
41
+ V = any,
42
+ E = any,
43
+ VO extends DirectedVertex<V> = DirectedVertex<V>,
44
+ EO extends DirectedEdge<E> = DirectedEdge<E>
45
+ >
46
46
  extends AbstractGraph<V, E, VO, EO>
47
47
  implements IGraph<V, E, VO, EO>
48
48
  {