data-structure-typed 1.50.3 → 1.50.4

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 (63) hide show
  1. package/.github/ISSUE_TEMPLATE/bug_report.md +0 -13
  2. package/CHANGELOG.md +1 -1
  3. package/README.md +6 -6
  4. package/README_zh-CN.md +6 -6
  5. package/dist/cjs/data-structures/binary-tree/{tree-multimap.d.ts → avl-tree-multi-map.d.ts} +11 -11
  6. package/dist/cjs/data-structures/binary-tree/{tree-multimap.js → avl-tree-multi-map.js} +15 -15
  7. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -0
  8. package/dist/cjs/data-structures/binary-tree/bst.js +5 -7
  9. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  10. package/dist/cjs/data-structures/binary-tree/index.d.ts +2 -1
  11. package/dist/cjs/data-structures/binary-tree/index.js +2 -1
  12. package/dist/cjs/data-structures/binary-tree/index.js.map +1 -1
  13. package/dist/cjs/data-structures/binary-tree/rb-tree.js +17 -9
  14. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  15. package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +200 -0
  16. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +400 -0
  17. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -0
  18. package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -0
  19. package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.js +3 -0
  20. package/dist/cjs/types/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -0
  21. package/dist/cjs/types/data-structures/binary-tree/index.d.ts +2 -1
  22. package/dist/cjs/types/data-structures/binary-tree/index.js +2 -1
  23. package/dist/cjs/types/data-structures/binary-tree/index.js.map +1 -1
  24. package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.d.ts +5 -0
  25. package/dist/cjs/types/data-structures/binary-tree/{tree-multimap.js → tree-multi-map.js} +1 -1
  26. package/dist/cjs/types/data-structures/binary-tree/tree-multi-map.js.map +1 -0
  27. package/dist/mjs/data-structures/binary-tree/{tree-multimap.d.ts → avl-tree-multi-map.d.ts} +11 -11
  28. package/dist/mjs/data-structures/binary-tree/{tree-multimap.js → avl-tree-multi-map.js} +11 -11
  29. package/dist/mjs/data-structures/binary-tree/bst.js +5 -7
  30. package/dist/mjs/data-structures/binary-tree/index.d.ts +2 -1
  31. package/dist/mjs/data-structures/binary-tree/index.js +2 -1
  32. package/dist/mjs/data-structures/binary-tree/rb-tree.js +17 -9
  33. package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +200 -0
  34. package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +397 -0
  35. package/dist/mjs/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -0
  36. package/dist/mjs/types/data-structures/binary-tree/index.d.ts +2 -1
  37. package/dist/mjs/types/data-structures/binary-tree/index.js +2 -1
  38. package/dist/mjs/types/data-structures/binary-tree/tree-multi-map.d.ts +5 -0
  39. package/dist/mjs/types/data-structures/binary-tree/tree-multi-map.js +1 -0
  40. package/dist/umd/data-structure-typed.js +399 -29
  41. package/dist/umd/data-structure-typed.min.js +2 -2
  42. package/dist/umd/data-structure-typed.min.js.map +1 -1
  43. package/package.json +1 -1
  44. package/src/data-structures/binary-tree/{tree-multimap.ts → avl-tree-multi-map.ts} +20 -20
  45. package/src/data-structures/binary-tree/bst.ts +5 -6
  46. package/src/data-structures/binary-tree/index.ts +2 -1
  47. package/src/data-structures/binary-tree/rb-tree.ts +20 -10
  48. package/src/data-structures/binary-tree/tree-multi-map.ts +463 -0
  49. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +8 -0
  50. package/src/types/data-structures/binary-tree/index.ts +2 -1
  51. package/src/types/data-structures/binary-tree/tree-multi-map.ts +8 -0
  52. package/test/integration/index.html +2 -2
  53. package/test/unit/data-structures/binary-tree/{tree-multimap.test.ts → avl-tree-multi-map.test.ts} +55 -55
  54. package/test/unit/data-structures/binary-tree/overall.test.ts +24 -25
  55. package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +706 -0
  56. package/test/unit/data-structures/graph/undirected-graph.test.ts +4 -4
  57. package/test/unit/unrestricted-interconversion.test.ts +3 -3
  58. package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +0 -1
  59. package/dist/cjs/types/data-structures/binary-tree/tree-multimap.d.ts +0 -5
  60. package/dist/cjs/types/data-structures/binary-tree/tree-multimap.js.map +0 -1
  61. package/dist/mjs/types/data-structures/binary-tree/tree-multimap.d.ts +0 -5
  62. package/src/types/data-structures/binary-tree/tree-multimap.ts +0 -8
  63. /package/dist/mjs/types/data-structures/binary-tree/{tree-multimap.js → avl-tree-multi-map.js} +0 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "data-structure-typed",
3
- "version": "1.50.3",
3
+ "version": "1.50.4",
4
4
  "description": "Javascript Data Structure. Heap, Binary Tree, Red Black Tree, Linked List, Deque, Trie, HashMap, Directed Graph, Undirected Graph, Binary Search Tree(BST), AVL Tree, Priority Queue, Graph, Queue, Tree Multiset, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue, Stack. Benchmark compared with C++ STL. API aligned with ES6 and Java.util. Usability is comparable to Python",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/mjs/index.js",
@@ -6,22 +6,22 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import type {
9
+ AVLTreeMultiMapNested,
10
+ AVLTreeMultiMapNodeNested,
11
+ AVLTreeMultiMapOptions,
9
12
  BinaryTreeDeleteResult,
10
13
  BSTNKeyOrNode,
11
14
  BTNCallback,
12
- KeyOrNodeOrEntry,
13
- TreeMultimapNested,
14
- TreeMultimapNodeNested,
15
- TreeMultimapOptions
15
+ KeyOrNodeOrEntry
16
16
  } from '../../types';
17
17
  import { FamilyPosition, IterationType } from '../../types';
18
18
  import { IBinaryTree } from '../../interfaces';
19
19
  import { AVLTree, AVLTreeNode } from './avl-tree';
20
20
 
21
- export class TreeMultimapNode<
21
+ export class AVLTreeMultiMapNode<
22
22
  K = any,
23
23
  V = any,
24
- NODE extends TreeMultimapNode<K, V, NODE> = TreeMultimapNodeNested<K, V>
24
+ NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNodeNested<K, V>
25
25
  > extends AVLTreeNode<K, V, NODE> {
26
26
  /**
27
27
  * The constructor function initializes a BinaryTreeNode object with a key, value, and count.
@@ -59,17 +59,17 @@ export class TreeMultimapNode<
59
59
  }
60
60
 
61
61
  /**
62
- * The only distinction between a TreeMultimap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
62
+ * The only distinction between a AVLTreeMultiMap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
63
63
  */
64
- export class TreeMultimap<
64
+ export class AVLTreeMultiMap<
65
65
  K = any,
66
66
  V = any,
67
- NODE extends TreeMultimapNode<K, V, NODE> = TreeMultimapNode<K, V, TreeMultimapNodeNested<K, V>>,
68
- TREE extends TreeMultimap<K, V, NODE, TREE> = TreeMultimap<K, V, NODE, TreeMultimapNested<K, V, NODE>>
67
+ NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNodeNested<K, V>>,
68
+ TREE extends AVLTreeMultiMap<K, V, NODE, TREE> = AVLTreeMultiMap<K, V, NODE, AVLTreeMultiMapNested<K, V, NODE>>
69
69
  >
70
70
  extends AVLTree<K, V, NODE, TREE>
71
71
  implements IBinaryTree<K, V, NODE, TREE> {
72
- constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: TreeMultimapOptions<K>) {
72
+ constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: AVLTreeMultiMapOptions<K>) {
73
73
  super([], options);
74
74
  if (keysOrNodesOrEntries) this.addMany(keysOrNodesOrEntries);
75
75
  }
@@ -98,20 +98,20 @@ export class TreeMultimap<
98
98
  * @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
99
99
  */
100
100
  override createNode(key: K, value?: V, count?: number): NODE {
101
- return new TreeMultimapNode(key, value, count) as NODE;
101
+ return new AVLTreeMultiMapNode(key, value, count) as NODE;
102
102
  }
103
103
 
104
104
  /**
105
- * The function creates a new TreeMultimap object with the specified options and returns it.
105
+ * The function creates a new AVLTreeMultiMap object with the specified options and returns it.
106
106
  * @param [options] - The `options` parameter is an optional object that contains additional
107
- * configuration options for creating the `TreeMultimap` object. It can include properties such as
107
+ * configuration options for creating the `AVLTreeMultiMap` object. It can include properties such as
108
108
  * `iterationType` and `variant`, which are used to specify the type of iteration and the variant of
109
109
  * the tree, respectively. These properties can be
110
- * @returns a new instance of the `TreeMultimap` class, with the provided options merged with the
110
+ * @returns a new instance of the `AVLTreeMultiMap` class, with the provided options merged with the
111
111
  * default options. The returned value is casted as `TREE`.
112
112
  */
113
- override createTree(options?: TreeMultimapOptions<K>): TREE {
114
- return new TreeMultimap<K, V, NODE, TREE>([], {
113
+ override createTree(options?: AVLTreeMultiMapOptions<K>): TREE {
114
+ return new AVLTreeMultiMap<K, V, NODE, TREE>([], {
115
115
  iterationType: this.iterationType,
116
116
  variant: this.variant,
117
117
  ...options
@@ -155,13 +155,13 @@ export class TreeMultimap<
155
155
  }
156
156
 
157
157
  /**
158
- * The function checks if an keyOrNodeOrEntry is an instance of the TreeMultimapNode class.
158
+ * The function checks if an keyOrNodeOrEntry is an instance of the AVLTreeMultiMapNode class.
159
159
  * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
160
- * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the TreeMultimapNode
160
+ * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeMultiMapNode
161
161
  * class.
162
162
  */
163
163
  override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE {
164
- return keyOrNodeOrEntry instanceof TreeMultimapNode;
164
+ return keyOrNodeOrEntry instanceof AVLTreeMultiMapNode;
165
165
  }
166
166
 
167
167
  /**
@@ -708,9 +708,8 @@ export class BST<
708
708
  const compared = this._compare(cur.key, targetKey);
709
709
  if (compared === lesserOrGreater) ans.push(callback(cur));
710
710
 
711
- if (!cur.left && !cur.right) return;
712
- if (cur.left && this._compare(cur.left.key, targetKey) === lesserOrGreater) _traverse(cur.left);
713
- if (cur.right && this._compare(cur.right.key, targetKey) === lesserOrGreater) _traverse(cur.right);
711
+ if (this.isRealNode(cur.left)) _traverse(cur.left);
712
+ if (this.isRealNode(cur.right)) _traverse(cur.right);
714
713
  };
715
714
 
716
715
  _traverse(this.root);
@@ -719,12 +718,12 @@ export class BST<
719
718
  const queue = new Queue<NODE>([this.root]);
720
719
  while (queue.size > 0) {
721
720
  const cur = queue.shift();
722
- if (cur) {
721
+ if (this.isRealNode(cur)) {
723
722
  const compared = this._compare(cur.key, targetKey);
724
723
  if (compared === lesserOrGreater) ans.push(callback(cur));
725
724
 
726
- if (cur.left && this._compare(cur.left.key, targetKey) === lesserOrGreater) queue.push(cur.left);
727
- if (cur.right && this._compare(cur.right.key, targetKey) === lesserOrGreater) queue.push(cur.right);
725
+ if (this.isRealNode(cur.left)) queue.push(cur.left);
726
+ if (this.isRealNode(cur.right)) queue.push(cur.right);
728
727
  }
729
728
  }
730
729
  return ans;
@@ -4,4 +4,5 @@ export * from './binary-indexed-tree';
4
4
  export * from './segment-tree';
5
5
  export * from './avl-tree';
6
6
  export * from './rb-tree';
7
- export * from './tree-multimap';
7
+ export * from './avl-tree-multi-map';
8
+ export * from './tree-multi-map';
@@ -527,12 +527,14 @@ export class RedBlackTree<
527
527
  */
528
528
  protected _fixInsert(k: NODE): void {
529
529
  let u: NODE | undefined;
530
- while (k.parent && k.parent.color === 1) {
530
+ while (k.parent && k.parent.color === RBTNColor.RED) {
531
531
  if (k.parent.parent && k.parent === k.parent.parent.right) {
532
532
  u = k.parent.parent.left;
533
- if (u && u.color === 1) {
534
- u.color = RBTNColor.BLACK;
533
+
534
+ if (u && u.color === RBTNColor.RED) {
535
+ // Delay color flip
535
536
  k.parent.color = RBTNColor.BLACK;
537
+ u.color = RBTNColor.BLACK;
536
538
  k.parent.parent.color = RBTNColor.RED;
537
539
  k = k.parent.parent;
538
540
  } else {
@@ -541,16 +543,20 @@ export class RedBlackTree<
541
543
  this._rightRotate(k);
542
544
  }
543
545
 
544
- k.parent!.color = RBTNColor.BLACK;
545
- k.parent!.parent!.color = RBTNColor.RED;
546
+ // Check color before rotation
547
+ if (k.parent!.color === RBTNColor.RED) {
548
+ k.parent!.color = RBTNColor.BLACK;
549
+ k.parent!.parent!.color = RBTNColor.RED;
550
+ }
546
551
  this._leftRotate(k.parent!.parent!);
547
552
  }
548
553
  } else {
549
- u = k.parent.parent!.right;
554
+ u = k.parent!.parent!.right;
550
555
 
551
- if (u && u.color === 1) {
552
- u.color = RBTNColor.BLACK;
556
+ if (u && u.color === RBTNColor.RED) {
557
+ // Delay color flip
553
558
  k.parent.color = RBTNColor.BLACK;
559
+ u.color = RBTNColor.BLACK;
554
560
  k.parent.parent!.color = RBTNColor.RED;
555
561
  k = k.parent.parent!;
556
562
  } else {
@@ -559,11 +565,15 @@ export class RedBlackTree<
559
565
  this._leftRotate(k);
560
566
  }
561
567
 
562
- k.parent!.color = RBTNColor.BLACK;
563
- k.parent!.parent!.color = RBTNColor.RED;
568
+ // Check color before rotation
569
+ if (k.parent!.color === RBTNColor.RED) {
570
+ k.parent!.color = RBTNColor.BLACK;
571
+ k.parent!.parent!.color = RBTNColor.RED;
572
+ }
564
573
  this._rightRotate(k.parent!.parent!);
565
574
  }
566
575
  }
576
+
567
577
  if (k === this.root) {
568
578
  break;
569
579
  }
@@ -0,0 +1,463 @@
1
+ /**
2
+ * data-structure-typed
3
+ *
4
+ * @author Tyler Zeng
5
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
+ * @license MIT License
7
+ */
8
+ import type {
9
+ BinaryTreeDeleteResult,
10
+ BSTNKeyOrNode,
11
+ BTNCallback,
12
+ KeyOrNodeOrEntry,
13
+ TreeMultiMapNested,
14
+ TreeMultiMapNodeNested,
15
+ TreeMultiMapOptions
16
+ } from '../../types';
17
+ import { IterationType, RBTNColor } from '../../types';
18
+ import { IBinaryTree } from '../../interfaces';
19
+ import { RedBlackTree, RedBlackTreeNode } from './rb-tree';
20
+
21
+ export class TreeMultiMapNode<
22
+ K = any,
23
+ V = any,
24
+ NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNodeNested<K, V>
25
+ > extends RedBlackTreeNode<K, V, NODE> {
26
+ /**
27
+ * The constructor function initializes an instance of a class with a key, value, and count.
28
+ * @param {K} key - The key parameter is of type K, which represents the type of the key for the
29
+ * constructor. It is required and must be provided when creating an instance of the class.
30
+ * @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the
31
+ * value associated with the key in the constructor. If no value is provided, it will be `undefined`.
32
+ * @param [count=1] - The "count" parameter is an optional parameter that specifies the number of
33
+ * times the key-value pair should be repeated. If no value is provided for "count", it defaults to
34
+ * 1.
35
+ */
36
+ constructor(key: K, value?: V, count = 1) {
37
+ super(key, value);
38
+ this.count = count;
39
+ }
40
+
41
+ protected _count: number = 1;
42
+
43
+ /**
44
+ * The function returns the value of the private variable _count.
45
+ * @returns The count property of the object, which is of type number.
46
+ */
47
+ get count(): number {
48
+ return this._count;
49
+ }
50
+
51
+ /**
52
+ * The above function sets the value of the count property.
53
+ * @param {number} value - The value parameter is of type number, which means it can accept any
54
+ * numeric value.
55
+ */
56
+ set count(value: number) {
57
+ this._count = value;
58
+ }
59
+ }
60
+
61
+ export class TreeMultiMap<
62
+ K = any,
63
+ V = any,
64
+ NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNode<K, V, TreeMultiMapNodeNested<K, V>>,
65
+ TREE extends TreeMultiMap<K, V, NODE, TREE> = TreeMultiMap<K, V, NODE, TreeMultiMapNested<K, V, NODE>>
66
+ >
67
+ extends RedBlackTree<K, V, NODE, TREE>
68
+ implements IBinaryTree<K, V, NODE, TREE> {
69
+ /**
70
+ * The constructor function initializes a new instance of the TreeMultiMap class with optional
71
+ * initial keys, nodes, or entries.
72
+ * @param keysOrNodesOrEntries - The `keysOrNodesOrEntries` parameter is an iterable object that can
73
+ * contain keys, nodes, or entries. It is used to initialize the TreeMultiMap with the provided keys,
74
+ * nodes, or entries.
75
+ * @param [options] - The `options` parameter is an optional object that can be passed to the
76
+ * constructor. It allows you to customize the behavior of the `TreeMultiMap` instance.
77
+ */
78
+ constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: TreeMultiMapOptions<K>) {
79
+ super([], options);
80
+ if (keysOrNodesOrEntries) this.addMany(keysOrNodesOrEntries);
81
+ }
82
+
83
+ protected _count = 0;
84
+
85
+ // TODO the _count is not accurate after nodes count modified
86
+ /**
87
+ * The function calculates the sum of the count property of all nodes in a tree structure.
88
+ * @returns the sum of the count property of all nodes in the tree.
89
+ */
90
+ get count(): number {
91
+ let sum = 0;
92
+ this.dfs(node => (sum += node.count));
93
+ return sum;
94
+ // return this._count;
95
+ }
96
+
97
+ /**
98
+ * The function creates a new TreeMultiMapNode object with the specified key, value, and count.
99
+ * @param {K} key - The key parameter represents the key of the node being created. It is of type K,
100
+ * which is a generic type that can be replaced with any specific type when using the function.
101
+ * @param {V} [value] - The `value` parameter is an optional parameter that represents the value
102
+ * associated with the key in the node. It is of type `V`, which can be any data type.
103
+ * @param {number} [count] - The `count` parameter represents the number of occurrences of a
104
+ * key-value pair in the TreeMultiMap. It is an optional parameter, so if it is not provided, it will
105
+ * default to 1.
106
+ * @returns a new instance of the TreeMultiMapNode class, casted as NODE.
107
+ */
108
+ override createNode(key: K, value?: V, count?: number): NODE {
109
+ return new TreeMultiMapNode(key, value, count) as NODE;
110
+ }
111
+
112
+ /**
113
+ * The function creates a new instance of a TreeMultiMap with the specified options and returns it.
114
+ * @param [options] - The `options` parameter is an optional object that contains additional
115
+ * configuration options for creating the `TreeMultiMap`. It can include properties such as
116
+ * `keyComparator`, `valueComparator`, `allowDuplicates`, etc.
117
+ * @returns a new instance of the `TreeMultiMap` class, with the provided options merged with the
118
+ * existing `iterationType` option. The returned value is casted as `TREE`.
119
+ */
120
+ override createTree(options?: TreeMultiMapOptions<K>): TREE {
121
+ return new TreeMultiMap<K, V, NODE, TREE>([], {
122
+ iterationType: this.iterationType,
123
+ ...options
124
+ }) as TREE;
125
+ }
126
+
127
+ /**
128
+ * The function `keyValueOrEntryToNode` takes a key, value, and count and returns a node if the input
129
+ * is valid.
130
+ * @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be of type `KeyOrNodeOrEntry<K, V,
131
+ * NODE>`. It can accept three types of values:
132
+ * @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
133
+ * value associated with a key in a key-value pair.
134
+ * @param [count=1] - The count parameter is an optional parameter that specifies the number of times
135
+ * the key-value pair should be added to the node. If not provided, it defaults to 1.
136
+ * @returns a NODE object or undefined.
137
+ */
138
+ override keyValueOrEntryToNode(
139
+ keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>,
140
+ value?: V,
141
+ count = 1
142
+ ): NODE | undefined {
143
+ let node: NODE | undefined;
144
+ if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) {
145
+ return;
146
+ } else if (this.isNode(keyOrNodeOrEntry)) {
147
+ node = keyOrNodeOrEntry;
148
+ } else if (this.isEntry(keyOrNodeOrEntry)) {
149
+ const [key, value] = keyOrNodeOrEntry;
150
+ if (key === undefined || key === null) {
151
+ return;
152
+ } else {
153
+ node = this.createNode(key, value, count);
154
+ }
155
+ } else if (!this.isNode(keyOrNodeOrEntry)) {
156
+ node = this.createNode(keyOrNodeOrEntry, value, count);
157
+ } else {
158
+ return;
159
+ }
160
+ return node;
161
+ }
162
+
163
+ /**
164
+ * The function "isNode" checks if a given key, node, or entry is an instance of the TreeMultiMapNode
165
+ * class.
166
+ * @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be of type `KeyOrNodeOrEntry<K, V,
167
+ * NODE>`.
168
+ * @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntry` is an instance
169
+ * of the `TreeMultiMapNode` class.
170
+ */
171
+ override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE {
172
+ return keyOrNodeOrEntry instanceof TreeMultiMapNode;
173
+ }
174
+
175
+ /**
176
+ * Time Complexity: O(log n)
177
+ * Space Complexity: O(1)
178
+ */
179
+
180
+ /**
181
+ * Time Complexity: O(log n)
182
+ * Space Complexity: O(1)
183
+ *
184
+ * The function overrides the add method in TypeScript and adds a new node to the data structure.
185
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can accept three types of values:
186
+ * @param {V} [value] - The `value` parameter represents the value associated with the key in the
187
+ * data structure.
188
+ * @param [count=1] - The `count` parameter represents the number of times the key-value pair should
189
+ * be added to the data structure. By default, it is set to 1, meaning that the key-value pair will
190
+ * be added once. However, you can specify a different value for `count` if you want to add
191
+ * @returns a boolean value.
192
+ */
193
+ override add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V, count = 1): boolean {
194
+ const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value, count);
195
+ if (newNode === undefined) return false;
196
+
197
+ const orgNodeCount = newNode?.count || 0;
198
+ const inserted = super.add(newNode);
199
+ if (inserted) {
200
+ this._count += orgNodeCount;
201
+ }
202
+ return true;
203
+ }
204
+
205
+ /**
206
+ * Time Complexity: O(log n)
207
+ * Space Complexity: O(1)
208
+ */
209
+
210
+ /**
211
+ * Time Complexity: O(log n)
212
+ * Space Complexity: O(1)
213
+ *
214
+ * The `delete` function in a TypeScript class is used to delete nodes from a binary tree based on a
215
+ * given identifier, and it returns an array of results containing information about the deleted
216
+ * nodes.
217
+ * @param {ReturnType<C> | null | undefined} identifier - The identifier parameter is the value used
218
+ * to identify the node to be deleted. It can be of any type that is returned by the callback
219
+ * function. It can also be null or undefined if no node needs to be deleted.
220
+ * @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as
221
+ * input and returns a value of type `ReturnType<C>`. It is used to determine if a node matches the
222
+ * identifier for deletion. If no callback is provided, the `_defaultOneParamCallback` function is
223
+ * used
224
+ * @param [ignoreCount=false] - A boolean value indicating whether to ignore the count of the target
225
+ * node when performing deletion. If set to true, the count of the target node will not be considered
226
+ * and the node will be deleted regardless of its count. If set to false (default), the count of the
227
+ * target node will be decremented
228
+ * @returns an array of BinaryTreeDeleteResult<NODE> objects.
229
+ */
230
+ override delete<C extends BTNCallback<NODE>>(
231
+ identifier: ReturnType<C> | null | undefined,
232
+ callback: C = this._defaultOneParamCallback as C,
233
+ ignoreCount = false
234
+ ): BinaryTreeDeleteResult<NODE>[] {
235
+ const deleteResults: BinaryTreeDeleteResult<NODE>[] = [];
236
+ if (identifier === null) return deleteResults;
237
+
238
+ // Helper function to perform deletion
239
+ const deleteHelper = (node: NODE | undefined): void => {
240
+ // Initialize targetNode to the sentinel node
241
+ let targetNode: NODE = this._Sentinel;
242
+ let currentNode: NODE | undefined;
243
+
244
+ // Find the node to be deleted based on the identifier
245
+ while (node !== this._Sentinel) {
246
+ // Update targetNode if the current node matches the identifier
247
+ if (node && callback(node) === identifier) {
248
+ targetNode = node;
249
+ }
250
+
251
+ // Move to the right or left based on the comparison with the identifier
252
+ if (node && identifier && callback(node) <= identifier) {
253
+ node = node.right;
254
+ } else {
255
+ node = node?.left;
256
+ }
257
+ }
258
+
259
+ // If the target node is not found, decrement size and return
260
+ if (targetNode === this._Sentinel) {
261
+ return;
262
+ }
263
+
264
+ if (ignoreCount || targetNode.count <= 1) {
265
+ // Store the parent of the target node and its original color
266
+ let parentNode = targetNode;
267
+ let parentNodeOriginalColor: number = parentNode.color;
268
+
269
+ // Handle deletion based on the number of children of the target node
270
+ if (targetNode.left === this._Sentinel) {
271
+ // Target node has no left child - deletion case 1
272
+ currentNode = targetNode.right;
273
+ this._rbTransplant(targetNode, targetNode.right!);
274
+ } else if (targetNode.right === this._Sentinel) {
275
+ // Target node has no right child - deletion case 2
276
+ currentNode = targetNode.left;
277
+ this._rbTransplant(targetNode, targetNode.left!);
278
+ } else {
279
+ // Target node has both left and right children - deletion case 3
280
+ parentNode = this.getLeftMost(targetNode.right)!;
281
+ parentNodeOriginalColor = parentNode.color;
282
+ currentNode = parentNode.right;
283
+
284
+ if (parentNode.parent === targetNode) {
285
+ // Target node's right child becomes its parent's left child
286
+ currentNode!.parent = parentNode;
287
+ } else {
288
+ // Replace parentNode with its right child and update connections
289
+ this._rbTransplant(parentNode, parentNode.right!);
290
+ parentNode.right = targetNode.right;
291
+ parentNode.right!.parent = parentNode;
292
+ }
293
+
294
+ // Replace the target node with its in-order successor
295
+ this._rbTransplant(targetNode, parentNode);
296
+ parentNode.left = targetNode.left;
297
+ parentNode.left!.parent = parentNode;
298
+ parentNode.color = targetNode.color;
299
+ }
300
+
301
+ // Fix the Red-Black Tree properties after deletion
302
+ if (parentNodeOriginalColor === RBTNColor.BLACK) {
303
+ this._fixDelete(currentNode!);
304
+ }
305
+
306
+ // Decrement the size and store information about the deleted node
307
+ this._size--;
308
+ this._count -= targetNode.count;
309
+ deleteResults.push({ deleted: targetNode, needBalanced: undefined });
310
+ } else {
311
+ targetNode.count--;
312
+ this._count--;
313
+ }
314
+ };
315
+
316
+ // Call the helper function with the root of the tree
317
+ deleteHelper(this.root);
318
+
319
+ // Return the result array
320
+ return deleteResults;
321
+ }
322
+
323
+ /**
324
+ * Time Complexity: O(1)
325
+ * Space Complexity: O(1)
326
+ */
327
+
328
+ /**
329
+ * Time Complexity: O(1)
330
+ * Space Complexity: O(1)
331
+ *
332
+ * The "clear" function overrides the parent class's "clear" function and also resets the count to
333
+ * zero.
334
+ */
335
+ override clear() {
336
+ super.clear();
337
+ this._count = 0;
338
+ }
339
+
340
+ /**
341
+ * Time Complexity: O(n log n)
342
+ * Space Complexity: O(log n)
343
+ */
344
+
345
+ /**
346
+ * Time Complexity: O(n log n)
347
+ * Space Complexity: O(log n)
348
+ *
349
+ * The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
350
+ * tree using either a recursive or iterative approach.
351
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
352
+ * type of iteration to use when building the balanced binary search tree. It can have two possible
353
+ * values:
354
+ * @returns a boolean value.
355
+ */
356
+ override perfectlyBalance(iterationType = this.iterationType): boolean {
357
+ const sorted = this.dfs(node => node, 'in'),
358
+ n = sorted.length;
359
+ if (sorted.length < 1) return false;
360
+
361
+ this.clear();
362
+
363
+ if (iterationType === IterationType.RECURSIVE) {
364
+ const buildBalanceBST = (l: number, r: number) => {
365
+ if (l > r) return;
366
+ const m = l + Math.floor((r - l) / 2);
367
+ const midNode = sorted[m];
368
+ this.add(midNode.key, midNode.value, midNode.count);
369
+ buildBalanceBST(l, m - 1);
370
+ buildBalanceBST(m + 1, r);
371
+ };
372
+
373
+ buildBalanceBST(0, n - 1);
374
+ return true;
375
+ } else {
376
+ const stack: [[number, number]] = [[0, n - 1]];
377
+ while (stack.length > 0) {
378
+ const popped = stack.pop();
379
+ if (popped) {
380
+ const [l, r] = popped;
381
+ if (l <= r) {
382
+ const m = l + Math.floor((r - l) / 2);
383
+ const midNode = sorted[m];
384
+ this.add(midNode.key, midNode.value, midNode.count);
385
+ stack.push([m + 1, r]);
386
+ stack.push([l, m - 1]);
387
+ }
388
+ }
389
+ }
390
+ return true;
391
+ }
392
+ }
393
+
394
+ /**
395
+ * Time complexity: O(n)
396
+ * Space complexity: O(n)
397
+ */
398
+
399
+ /**
400
+ * Time complexity: O(n)
401
+ * Space complexity: O(n)
402
+ *
403
+ * The function overrides the clone method to create a deep copy of a tree object.
404
+ * @returns The `clone()` method is returning a cloned instance of the `TREE` object.
405
+ */
406
+ override clone(): TREE {
407
+ const cloned = this.createTree();
408
+ this.bfs(node => cloned.add(node.key, node.value, node.count));
409
+ return cloned;
410
+ }
411
+
412
+ /**
413
+ * The function swaps the properties of two nodes in a binary search tree.
414
+ * @param srcNode - The source node that needs to be swapped with the destination node. It can be
415
+ * either a key or a node object.
416
+ * @param destNode - The `destNode` parameter is the node in the binary search tree where the
417
+ * properties will be swapped with the `srcNode`.
418
+ * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
419
+ * If both `srcNode` and `destNode` are valid nodes, the method swaps their `key`, `value`, `count`,
420
+ * and `color` properties. If the swapping is successful, the method returns the modified `destNode`.
421
+ * If either `srcNode` or `destNode` is
422
+ */
423
+ protected override _swapProperties(
424
+ srcNode: BSTNKeyOrNode<K, NODE>,
425
+ destNode: BSTNKeyOrNode<K, NODE>
426
+ ): NODE | undefined {
427
+ srcNode = this.ensureNode(srcNode);
428
+ destNode = this.ensureNode(destNode);
429
+ if (srcNode && destNode) {
430
+ const { key, value, count, color } = destNode;
431
+ const tempNode = this.createNode(key, value, count);
432
+ if (tempNode) {
433
+ tempNode.color = color;
434
+
435
+ destNode.key = srcNode.key;
436
+ destNode.value = srcNode.value;
437
+ destNode.count = srcNode.count;
438
+ destNode.color = srcNode.color;
439
+
440
+ srcNode.key = tempNode.key;
441
+ srcNode.value = tempNode.value;
442
+ srcNode.count = tempNode.count;
443
+ srcNode.color = tempNode.color;
444
+ }
445
+
446
+ return destNode;
447
+ }
448
+ return undefined;
449
+ }
450
+
451
+ /**
452
+ * The function replaces an old node with a new node and updates the count property of the new node.
453
+ * @param {NODE} oldNode - The `oldNode` parameter is of type `NODE` and represents the node that
454
+ * needs to be replaced in the data structure.
455
+ * @param {NODE} newNode - The `newNode` parameter is an object of type `NODE`.
456
+ * @returns The method is returning the result of calling the `_replaceNode` method from the
457
+ * superclass, after updating the `count` property of the `newNode` object.
458
+ */
459
+ protected _replaceNode(oldNode: NODE, newNode: NODE): NODE {
460
+ newNode.count = oldNode.count + newNode.count;
461
+ return super._replaceNode(oldNode, newNode);
462
+ }
463
+ }
@@ -0,0 +1,8 @@
1
+ import { AVLTreeMultiMap, AVLTreeMultiMapNode } from '../../../data-structures';
2
+ import type { AVLTreeOptions } from './avl-tree';
3
+
4
+ export type AVLTreeMultiMapNodeNested<K, V> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
5
+
6
+ export type AVLTreeMultiMapNested<K, V, N extends AVLTreeMultiMapNode<K, V, N>> = AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, AVLTreeMultiMap<K, V, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
7
+
8
+ export type AVLTreeMultiMapOptions<K> = AVLTreeOptions<K> & {}