doubly-linked-list-typed 2.0.5 → 2.1.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 (102) hide show
  1. package/README.md +14 -14
  2. package/dist/data-structures/base/iterable-element-base.d.ts +186 -83
  3. package/dist/data-structures/base/iterable-element-base.js +149 -107
  4. package/dist/data-structures/base/iterable-entry-base.d.ts +95 -119
  5. package/dist/data-structures/base/iterable-entry-base.js +59 -116
  6. package/dist/data-structures/base/linear-base.d.ts +250 -192
  7. package/dist/data-structures/base/linear-base.js +137 -274
  8. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
  9. package/dist/data-structures/binary-tree/avl-tree-counter.js +171 -205
  10. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
  11. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +135 -87
  12. package/dist/data-structures/binary-tree/avl-tree.d.ts +138 -149
  13. package/dist/data-structures/binary-tree/avl-tree.js +208 -195
  14. package/dist/data-structures/binary-tree/binary-tree.d.ts +476 -632
  15. package/dist/data-structures/binary-tree/binary-tree.js +602 -873
  16. package/dist/data-structures/binary-tree/bst.d.ts +258 -306
  17. package/dist/data-structures/binary-tree/bst.js +505 -481
  18. package/dist/data-structures/binary-tree/red-black-tree.d.ts +107 -179
  19. package/dist/data-structures/binary-tree/red-black-tree.js +114 -209
  20. package/dist/data-structures/binary-tree/tree-counter.d.ts +132 -154
  21. package/dist/data-structures/binary-tree/tree-counter.js +172 -203
  22. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
  23. package/dist/data-structures/binary-tree/tree-multi-map.js +105 -85
  24. package/dist/data-structures/graph/abstract-graph.d.ts +238 -233
  25. package/dist/data-structures/graph/abstract-graph.js +267 -237
  26. package/dist/data-structures/graph/directed-graph.d.ts +108 -224
  27. package/dist/data-structures/graph/directed-graph.js +146 -233
  28. package/dist/data-structures/graph/map-graph.d.ts +49 -55
  29. package/dist/data-structures/graph/map-graph.js +56 -59
  30. package/dist/data-structures/graph/undirected-graph.d.ts +103 -146
  31. package/dist/data-structures/graph/undirected-graph.js +129 -149
  32. package/dist/data-structures/hash/hash-map.d.ts +164 -338
  33. package/dist/data-structures/hash/hash-map.js +270 -457
  34. package/dist/data-structures/heap/heap.d.ts +214 -289
  35. package/dist/data-structures/heap/heap.js +340 -349
  36. package/dist/data-structures/heap/max-heap.d.ts +11 -47
  37. package/dist/data-structures/heap/max-heap.js +11 -66
  38. package/dist/data-structures/heap/min-heap.d.ts +12 -47
  39. package/dist/data-structures/heap/min-heap.js +11 -66
  40. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
  41. package/dist/data-structures/linked-list/doubly-linked-list.js +368 -494
  42. package/dist/data-structures/linked-list/singly-linked-list.d.ts +261 -310
  43. package/dist/data-structures/linked-list/singly-linked-list.js +447 -466
  44. package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -107
  45. package/dist/data-structures/linked-list/skip-linked-list.js +0 -100
  46. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
  47. package/dist/data-structures/priority-queue/max-priority-queue.js +11 -78
  48. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
  49. package/dist/data-structures/priority-queue/min-priority-queue.js +10 -79
  50. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -61
  51. package/dist/data-structures/priority-queue/priority-queue.js +8 -83
  52. package/dist/data-structures/queue/deque.d.ts +227 -254
  53. package/dist/data-structures/queue/deque.js +309 -348
  54. package/dist/data-structures/queue/queue.d.ts +180 -201
  55. package/dist/data-structures/queue/queue.js +265 -248
  56. package/dist/data-structures/stack/stack.d.ts +124 -102
  57. package/dist/data-structures/stack/stack.js +181 -125
  58. package/dist/data-structures/trie/trie.d.ts +164 -165
  59. package/dist/data-structures/trie/trie.js +189 -172
  60. package/dist/interfaces/binary-tree.d.ts +56 -6
  61. package/dist/interfaces/graph.d.ts +16 -0
  62. package/dist/types/data-structures/base/base.d.ts +1 -1
  63. package/dist/types/data-structures/graph/abstract-graph.d.ts +4 -0
  64. package/dist/types/utils/utils.d.ts +1 -0
  65. package/dist/utils/utils.d.ts +1 -1
  66. package/dist/utils/utils.js +2 -1
  67. package/package.json +2 -2
  68. package/src/data-structures/base/iterable-element-base.ts +238 -115
  69. package/src/data-structures/base/iterable-entry-base.ts +96 -120
  70. package/src/data-structures/base/linear-base.ts +271 -277
  71. package/src/data-structures/binary-tree/avl-tree-counter.ts +196 -217
  72. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +188 -102
  73. package/src/data-structures/binary-tree/avl-tree.ts +237 -206
  74. package/src/data-structures/binary-tree/binary-tree.ts +665 -896
  75. package/src/data-structures/binary-tree/bst.ts +565 -572
  76. package/src/data-structures/binary-tree/red-black-tree.ts +157 -223
  77. package/src/data-structures/binary-tree/tree-counter.ts +195 -219
  78. package/src/data-structures/binary-tree/tree-multi-map.ts +127 -98
  79. package/src/data-structures/graph/abstract-graph.ts +339 -264
  80. package/src/data-structures/graph/directed-graph.ts +146 -236
  81. package/src/data-structures/graph/map-graph.ts +63 -60
  82. package/src/data-structures/graph/undirected-graph.ts +129 -152
  83. package/src/data-structures/hash/hash-map.ts +274 -496
  84. package/src/data-structures/heap/heap.ts +389 -402
  85. package/src/data-structures/heap/max-heap.ts +12 -76
  86. package/src/data-structures/heap/min-heap.ts +13 -76
  87. package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
  88. package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
  89. package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
  90. package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
  91. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
  92. package/src/data-structures/priority-queue/priority-queue.ts +3 -92
  93. package/src/data-structures/queue/deque.ts +381 -357
  94. package/src/data-structures/queue/queue.ts +310 -264
  95. package/src/data-structures/stack/stack.ts +217 -131
  96. package/src/data-structures/trie/trie.ts +240 -175
  97. package/src/interfaces/binary-tree.ts +240 -6
  98. package/src/interfaces/graph.ts +37 -0
  99. package/src/types/data-structures/base/base.ts +5 -5
  100. package/src/types/data-structures/graph/abstract-graph.ts +5 -0
  101. package/src/types/utils/utils.ts +2 -0
  102. package/src/utils/utils.ts +9 -14
@@ -5,100 +5,131 @@
5
5
  * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import { AVLTreeMultiMapOptions } from '../../types';
8
+ import type { AVLTreeMultiMapOptions, AVLTreeOptions, ElemOf, EntryCallback, IterationType } from '../../types';
9
9
  import { AVLTree, AVLTreeNode } from './avl-tree';
10
10
  import { IBinaryTree } from '../../interfaces';
11
+ /**
12
+ * Node used by AVLTreeMultiMap; stores the key with a bucket of values (array).
13
+ * @remarks Time O(1), Space O(1)
14
+ * @template K
15
+ * @template V
16
+ */
11
17
  export declare class AVLTreeMultiMapNode<K = any, V = any> extends AVLTreeNode<K, V[]> {
12
18
  parent?: AVLTreeMultiMapNode<K, V>;
13
19
  /**
14
- * This TypeScript constructor initializes an object with a key of type K and an array of values of
15
- * type V.
16
- * @param {K} key - The `key` parameter is typically used to store a unique identifier or key for the
17
- * data being stored in the data structure. It helps in quickly accessing or retrieving the
18
- * associated value in the data structure.
19
- * @param {V[]} value - The `value` parameter in the constructor represents an array of values of
20
- * type `V`.
20
+ * Create an AVLTreeMultiMap node with a value bucket.
21
+ * @remarks Time O(1), Space O(1)
22
+ * @param key - Key of the node.
23
+ * @param value - Initial array of values.
24
+ * @returns New AVLTreeMultiMapNode instance.
21
25
  */
22
26
  constructor(key: K, value: V[]);
23
27
  _left?: AVLTreeMultiMapNode<K, V> | null | undefined;
28
+ /**
29
+ * Get the left child pointer.
30
+ * @remarks Time O(1), Space O(1)
31
+ * @returns Left child node, or null/undefined.
32
+ */
24
33
  get left(): AVLTreeMultiMapNode<K, V> | null | undefined;
34
+ /**
35
+ * Set the left child and update its parent pointer.
36
+ * @remarks Time O(1), Space O(1)
37
+ * @param v - New left child node, or null/undefined.
38
+ * @returns void
39
+ */
25
40
  set left(v: AVLTreeMultiMapNode<K, V> | null | undefined);
26
41
  _right?: AVLTreeMultiMapNode<K, V> | null | undefined;
42
+ /**
43
+ * Get the right child pointer.
44
+ * @remarks Time O(1), Space O(1)
45
+ * @returns Right child node, or null/undefined.
46
+ */
27
47
  get right(): AVLTreeMultiMapNode<K, V> | null | undefined;
48
+ /**
49
+ * Set the right child and update its parent pointer.
50
+ * @remarks Time O(1), Space O(1)
51
+ * @param v - New right child node, or null/undefined.
52
+ * @returns void
53
+ */
28
54
  set right(v: AVLTreeMultiMapNode<K, V> | null | undefined);
29
55
  }
30
56
  /**
31
- *
57
+ * AVL-tree–based multimap (key → array of values). Preserves O(log N) updates and supports map-like mode.
58
+ * @remarks Time O(1), Space O(1)
59
+ * @template K
60
+ * @template V
61
+ * @template R
32
62
  */
33
- export declare class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR = object> extends AVLTree<K, V[], R, MK, MV[], MR> implements IBinaryTree<K, V[], R, MK, MV, MR> {
63
+ export declare class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<K, V[], R> implements IBinaryTree<K, V[], R> {
34
64
  /**
35
- * The constructor initializes an AVLTreeMultiMap with the provided keys, nodes, entries, or raw data
36
- * and options.
37
- * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
38
- * iterable that can contain either key-value pairs represented as `BTNRep<K, V[],
39
- * AVLTreeMultiMapNode<K, V>>` or raw data represented as `R`. This parameter is used to initialize
40
- * the AVLTreeMulti
41
- * @param [options] - The `options` parameter in the constructor is of type
42
- * `AVLTreeMultiMapOptions<K, V[], R>`. It is an optional parameter that allows you to specify
43
- * additional options for configuring the AVLTreeMultiMap instance.
65
+ * Create an AVLTreeMultiMap and optionally bulk-insert items.
66
+ * @remarks Time O(N log N), Space O(N)
67
+ * @param [keysNodesEntriesOrRaws] - Iterable of keys/nodes/entries/raw items to insert.
68
+ * @param [options] - Options for AVLTreeMultiMap (comparator, reverse, map mode).
69
+ * @returns New AVLTreeMultiMap instance.
44
70
  */
45
71
  constructor(keysNodesEntriesOrRaws?: Iterable<K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | R>, options?: AVLTreeMultiMapOptions<K, V[], R>);
72
+ _createNode(key: K, value?: V[]): AVLTreeMultiMapNode<K, V>;
73
+ add(keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined): boolean;
74
+ add(key: K, value: V): boolean;
46
75
  /**
47
- * Time Complexity: O(1)
48
- * Space Complexity: O(1)
49
- *
50
- * The function `createTree` in TypeScript overrides the creation of an AVLTreeMultiMap with
51
- * specified options.
52
- * @param [options] - The `options` parameter in the `createTree` function is of type
53
- * `AVLTreeMultiMapOptions<K, V[], R>`. This means it is an object that can have properties of type
54
- * `K`, `V[]`, and `R`. The function creates a new `AVL
55
- * @returns The `createTree` method is returning a new instance of `AVLTreeMultiMap` with the
56
- * provided options.
76
+ * Delete a single value from the bucket at a given key. Removes the key if the bucket becomes empty.
77
+ * @remarks Time O(log N), Space O(1)
78
+ * @param keyNodeOrEntry - Key, node, or [key, values] entry to locate the bucket.
79
+ * @param value - Value to remove from the bucket.
80
+ * @returns True if the value was removed; false if not found.
57
81
  */
58
- createTree(options?: AVLTreeMultiMapOptions<K, V[], R>): AVLTreeMultiMap<K, V, R, MK, MV, MR>;
82
+ deleteValue(keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined, value: V): boolean;
59
83
  /**
60
- * Time Complexity: O(1)
61
- * Space Complexity: O(1)
62
- *
63
- * The `createNode` function in TypeScript overrides the default implementation to create a new
64
- * AVLTreeMultiMapNode with a specified key and value array.
65
- * @param {K} key - The `key` parameter represents the key of the node being created in the
66
- * AVLTreeMultiMap.
67
- * @param {V[]} value - The `value` parameter in the `createNode` method represents an array of
68
- * values associated with a specific key in the AVLTreeMultiMapNode. If no value is provided when
69
- * calling the method, an empty array `[]` is used as the default value.
70
- * @returns An AVLTreeMultiMapNode object is being returned, with the specified key and value. If the
71
- * AVLTreeMultiMap is in map mode, an empty array is used as the value, otherwise the provided value
72
- * array is used.
84
+ * Rebuild the tree into a perfectly balanced form using in-order nodes.
85
+ * @remarks Time O(N), Space O(N)
86
+ * @param [iterationType] - Traversal style to use when constructing the balanced tree.
87
+ * @returns True if rebalancing succeeded (tree not empty).
73
88
  */
74
- createNode(key: K, value?: V[]): AVLTreeMultiMapNode<K, V>;
75
- add(keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined): boolean;
76
- add(key: K, value: V): boolean;
89
+ perfectlyBalance(iterationType?: IterationType): boolean;
90
+ /**
91
+ * Create a new tree by mapping each [key, values] bucket.
92
+ * @remarks Time O(N log N), Space O(N)
93
+ * @template MK
94
+ * @template MVArr
95
+ * @template MR
96
+ * @param callback - Function mapping (key, values, index, tree) → [newKey, newValue].
97
+ * @param [options] - Options for the output tree.
98
+ * @param [thisArg] - Value for `this` inside the callback.
99
+ * @returns A new AVLTreeMultiMap when mapping to array values; see overloads.
100
+ */
101
+ map<MK = K, MVArr extends unknown[] = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MVArr]>, options?: Partial<AVLTreeOptions<MK, MVArr, MR>>, thisArg?: unknown): AVLTreeMultiMap<MK, ElemOf<MVArr>, MR>;
102
+ /**
103
+ * Create a new tree by mapping each [key, values] bucket.
104
+ * @remarks Time O(N log N), Space O(N)
105
+ * @template MK
106
+ * @template MV
107
+ * @template MR
108
+ * @param callback - Function mapping (key, values, index, tree) → [newKey, newValue].
109
+ * @param [options] - Options for the output tree.
110
+ * @param [thisArg] - Value for `this` inside the callback.
111
+ * @returns A new AVLTree when mapping to non-array values; see overloads.
112
+ */
113
+ map<MK = K, MV = V[], MR = any>(callback: EntryCallback<K, V[] | undefined, [MK, MV]>, options?: Partial<AVLTreeOptions<MK, MV, MR>>, thisArg?: unknown): AVLTree<MK, MV, MR>;
77
114
  /**
78
- * Time Complexity: O(log n)
79
- * Space Complexity: O(log n)
80
- *
81
- * The function `deleteValue` removes a specific value from a key in an AVLTreeMultiMap data
82
- * structure and deletes the entire node if no values are left for that key.
83
- * @param {K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | K} keyNodeOrEntry - The `keyNodeOrEntry`
84
- * parameter in the `deleteValue` function can be either a `BTNRep` object representing a key-value
85
- * pair in the AVLTreeMultiMapNode, or just the key itself.
86
- * @param {V} value - The `value` parameter in the `deleteValue` function represents the specific
87
- * value that you want to delete from the multi-map data structure associated with a particular key.
88
- * The function checks if the value exists in the array of values associated with the key, and if
89
- * found, removes it from the array.
90
- * @returns The `deleteValue` function returns a boolean value. It returns `true` if the specified
91
- * `value` was successfully deleted from the array of values associated with the `keyNodeOrEntry`. If
92
- * the value was not found in the array, it returns `false`.
115
+ * (Protected) Create an empty instance of the same concrete class.
116
+ * @remarks Time O(1), Space O(1)
117
+ * @template TK
118
+ * @template TV
119
+ * @template TR
120
+ * @param [options] - Optional constructor options for the like-kind instance.
121
+ * @returns An empty like-kind instance.
93
122
  */
94
- deleteValue(keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | K, value: V): boolean;
123
+ protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<AVLTreeOptions<TK, TV, TR>>): this;
95
124
  /**
96
- * Time Complexity: O(n)
97
- * Space Complexity: O(n)
98
- *
99
- * The function `clone` overrides the default cloning behavior to create a deep copy of a tree
100
- * structure.
101
- * @returns A cloned tree object is being returned.
125
+ * (Protected) Create a like-kind instance and seed it from an iterable.
126
+ * @remarks Time O(N log N), Space O(N)
127
+ * @template TK
128
+ * @template TV
129
+ * @template TR
130
+ * @param iter - Iterable used to seed the new tree.
131
+ * @param [options] - Options merged with the current snapshot.
132
+ * @returns A like-kind AVLTree built from the iterable.
102
133
  */
103
- clone(): AVLTreeMultiMap<K, V, R, MK, MV, MR>;
134
+ protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<AVLTreeOptions<TK, TV, TR>>): AVLTree<TK, TV, TR>;
104
135
  }
@@ -1,16 +1,27 @@
1
1
  "use strict";
2
+ /**
3
+ * data-structure-typed
4
+ *
5
+ * @author Pablo Zeng
6
+ * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
7
+ * @license MIT License
8
+ */
2
9
  Object.defineProperty(exports, "__esModule", { value: true });
3
10
  exports.AVLTreeMultiMap = exports.AVLTreeMultiMapNode = void 0;
4
11
  const avl_tree_1 = require("./avl-tree");
12
+ /**
13
+ * Node used by AVLTreeMultiMap; stores the key with a bucket of values (array).
14
+ * @remarks Time O(1), Space O(1)
15
+ * @template K
16
+ * @template V
17
+ */
5
18
  class AVLTreeMultiMapNode extends avl_tree_1.AVLTreeNode {
6
19
  /**
7
- * This TypeScript constructor initializes an object with a key of type K and an array of values of
8
- * type V.
9
- * @param {K} key - The `key` parameter is typically used to store a unique identifier or key for the
10
- * data being stored in the data structure. It helps in quickly accessing or retrieving the
11
- * associated value in the data structure.
12
- * @param {V[]} value - The `value` parameter in the constructor represents an array of values of
13
- * type `V`.
20
+ * Create an AVLTreeMultiMap node with a value bucket.
21
+ * @remarks Time O(1), Space O(1)
22
+ * @param key - Key of the node.
23
+ * @param value - Initial array of values.
24
+ * @returns New AVLTreeMultiMapNode instance.
14
25
  */
15
26
  constructor(key, value) {
16
27
  super(key, value);
@@ -18,18 +29,40 @@ class AVLTreeMultiMapNode extends avl_tree_1.AVLTreeNode {
18
29
  this._left = undefined;
19
30
  this._right = undefined;
20
31
  }
32
+ /**
33
+ * Get the left child pointer.
34
+ * @remarks Time O(1), Space O(1)
35
+ * @returns Left child node, or null/undefined.
36
+ */
21
37
  get left() {
22
38
  return this._left;
23
39
  }
40
+ /**
41
+ * Set the left child and update its parent pointer.
42
+ * @remarks Time O(1), Space O(1)
43
+ * @param v - New left child node, or null/undefined.
44
+ * @returns void
45
+ */
24
46
  set left(v) {
25
47
  if (v) {
26
48
  v.parent = this;
27
49
  }
28
50
  this._left = v;
29
51
  }
52
+ /**
53
+ * Get the right child pointer.
54
+ * @remarks Time O(1), Space O(1)
55
+ * @returns Right child node, or null/undefined.
56
+ */
30
57
  get right() {
31
58
  return this._right;
32
59
  }
60
+ /**
61
+ * Set the right child and update its parent pointer.
62
+ * @remarks Time O(1), Space O(1)
63
+ * @param v - New right child node, or null/undefined.
64
+ * @returns void
65
+ */
33
66
  set right(v) {
34
67
  if (v) {
35
68
  v.parent = this;
@@ -39,19 +72,19 @@ class AVLTreeMultiMapNode extends avl_tree_1.AVLTreeNode {
39
72
  }
40
73
  exports.AVLTreeMultiMapNode = AVLTreeMultiMapNode;
41
74
  /**
42
- *
75
+ * AVL-tree–based multimap (key → array of values). Preserves O(log N) updates and supports map-like mode.
76
+ * @remarks Time O(1), Space O(1)
77
+ * @template K
78
+ * @template V
79
+ * @template R
43
80
  */
44
81
  class AVLTreeMultiMap extends avl_tree_1.AVLTree {
45
82
  /**
46
- * The constructor initializes an AVLTreeMultiMap with the provided keys, nodes, entries, or raw data
47
- * and options.
48
- * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
49
- * iterable that can contain either key-value pairs represented as `BTNRep<K, V[],
50
- * AVLTreeMultiMapNode<K, V>>` or raw data represented as `R`. This parameter is used to initialize
51
- * the AVLTreeMulti
52
- * @param [options] - The `options` parameter in the constructor is of type
53
- * `AVLTreeMultiMapOptions<K, V[], R>`. It is an optional parameter that allows you to specify
54
- * additional options for configuring the AVLTreeMultiMap instance.
83
+ * Create an AVLTreeMultiMap and optionally bulk-insert items.
84
+ * @remarks Time O(N log N), Space O(N)
85
+ * @param [keysNodesEntriesOrRaws] - Iterable of keys/nodes/entries/raw items to insert.
86
+ * @param [options] - Options for AVLTreeMultiMap (comparator, reverse, map mode).
87
+ * @returns New AVLTreeMultiMap instance.
55
88
  */
56
89
  constructor(keysNodesEntriesOrRaws = [], options) {
57
90
  super([], Object.assign(Object.assign({}, options), { isMapMode: true }));
@@ -59,53 +92,15 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
59
92
  this.addMany(keysNodesEntriesOrRaws);
60
93
  }
61
94
  }
62
- /**
63
- * Time Complexity: O(1)
64
- * Space Complexity: O(1)
65
- *
66
- * The function `createTree` in TypeScript overrides the creation of an AVLTreeMultiMap with
67
- * specified options.
68
- * @param [options] - The `options` parameter in the `createTree` function is of type
69
- * `AVLTreeMultiMapOptions<K, V[], R>`. This means it is an object that can have properties of type
70
- * `K`, `V[]`, and `R`. The function creates a new `AVL
71
- * @returns The `createTree` method is returning a new instance of `AVLTreeMultiMap` with the
72
- * provided options.
73
- */
74
- createTree(options) {
75
- return new AVLTreeMultiMap([], Object.assign({ iterationType: this.iterationType, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse, isMapMode: this._isMapMode }, options));
76
- }
77
- /**
78
- * Time Complexity: O(1)
79
- * Space Complexity: O(1)
80
- *
81
- * The `createNode` function in TypeScript overrides the default implementation to create a new
82
- * AVLTreeMultiMapNode with a specified key and value array.
83
- * @param {K} key - The `key` parameter represents the key of the node being created in the
84
- * AVLTreeMultiMap.
85
- * @param {V[]} value - The `value` parameter in the `createNode` method represents an array of
86
- * values associated with a specific key in the AVLTreeMultiMapNode. If no value is provided when
87
- * calling the method, an empty array `[]` is used as the default value.
88
- * @returns An AVLTreeMultiMapNode object is being returned, with the specified key and value. If the
89
- * AVLTreeMultiMap is in map mode, an empty array is used as the value, otherwise the provided value
90
- * array is used.
91
- */
92
- createNode(key, value = []) {
95
+ _createNode(key, value = []) {
93
96
  return new AVLTreeMultiMapNode(key, this._isMapMode ? [] : value);
94
97
  }
95
98
  /**
96
- * Time Complexity: O(log n)
97
- * Space Complexity: O(log n)
98
- *
99
- * The function `add` in this TypeScript code overrides the superclass method to add key-value pairs
100
- * to an AVLTreeMultiMap, handling different input types and scenarios.
101
- * @param [key] - The `key` parameter in the `override add` method represents the key of the entry to
102
- * be added to the AVLTreeMultiMap. It can be of type `K`, which is the key type of the map. The key
103
- * can be a single key value, a node of the AVLTree
104
- * @param {V[]} [values] - The `values` parameter in the `add` method represents an array of values
105
- * that you want to add to the AVLTreeMultiMap. It can contain one or more values associated with a
106
- * specific key.
107
- * @returns The `add` method is returning a boolean value, which indicates whether the operation was
108
- * successful or not.
99
+ * Insert a value or a list of values into the multimap. If the key exists, values are appended.
100
+ * @remarks Time O(log N + M), Space O(1)
101
+ * @param keyNodeOrEntry - Key, node, or [key, values] entry.
102
+ * @param [value] - Single value to add when a bare key is provided.
103
+ * @returns True if inserted or appended; false if ignored.
109
104
  */
110
105
  add(keyNodeOrEntry, value) {
111
106
  if (this.isRealNode(keyNodeOrEntry))
@@ -155,21 +150,11 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
155
150
  return _commonAdd(keyNodeOrEntry, value !== undefined ? [value] : undefined);
156
151
  }
157
152
  /**
158
- * Time Complexity: O(log n)
159
- * Space Complexity: O(log n)
160
- *
161
- * The function `deleteValue` removes a specific value from a key in an AVLTreeMultiMap data
162
- * structure and deletes the entire node if no values are left for that key.
163
- * @param {K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | K} keyNodeOrEntry - The `keyNodeOrEntry`
164
- * parameter in the `deleteValue` function can be either a `BTNRep` object representing a key-value
165
- * pair in the AVLTreeMultiMapNode, or just the key itself.
166
- * @param {V} value - The `value` parameter in the `deleteValue` function represents the specific
167
- * value that you want to delete from the multi-map data structure associated with a particular key.
168
- * The function checks if the value exists in the array of values associated with the key, and if
169
- * found, removes it from the array.
170
- * @returns The `deleteValue` function returns a boolean value. It returns `true` if the specified
171
- * `value` was successfully deleted from the array of values associated with the `keyNodeOrEntry`. If
172
- * the value was not found in the array, it returns `false`.
153
+ * Delete a single value from the bucket at a given key. Removes the key if the bucket becomes empty.
154
+ * @remarks Time O(log N), Space O(1)
155
+ * @param keyNodeOrEntry - Key, node, or [key, values] entry to locate the bucket.
156
+ * @param value - Value to remove from the bucket.
157
+ * @returns True if the value was removed; false if not found.
173
158
  */
174
159
  deleteValue(keyNodeOrEntry, value) {
175
160
  const values = this.get(keyNodeOrEntry);
@@ -178,7 +163,6 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
178
163
  if (index === -1)
179
164
  return false;
180
165
  values.splice(index, 1);
181
- // If no values left, remove the entire node
182
166
  if (values.length === 0)
183
167
  this.delete(keyNodeOrEntry);
184
168
  return true;
@@ -186,17 +170,81 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
186
170
  return false;
187
171
  }
188
172
  /**
189
- * Time Complexity: O(n)
190
- * Space Complexity: O(n)
191
- *
192
- * The function `clone` overrides the default cloning behavior to create a deep copy of a tree
193
- * structure.
194
- * @returns A cloned tree object is being returned.
173
+ * Rebuild the tree into a perfectly balanced form using in-order nodes.
174
+ * @remarks Time O(N), Space O(N)
175
+ * @param [iterationType] - Traversal style to use when constructing the balanced tree.
176
+ * @returns True if rebalancing succeeded (tree not empty).
177
+ */
178
+ perfectlyBalance(iterationType = this.iterationType) {
179
+ const nodes = this.dfs(node => node, 'IN', false, this._root, iterationType);
180
+ const n = nodes.length;
181
+ if (n === 0)
182
+ return false;
183
+ this._clearNodes();
184
+ const build = (l, r, parent) => {
185
+ if (l > r)
186
+ return undefined;
187
+ const m = l + ((r - l) >> 1);
188
+ const root = nodes[m];
189
+ root.left = build(l, m - 1, root);
190
+ root.right = build(m + 1, r, root);
191
+ root.parent = parent;
192
+ const lh = root.left ? root.left.height : -1;
193
+ const rh = root.right ? root.right.height : -1;
194
+ root.height = Math.max(lh, rh) + 1;
195
+ return root;
196
+ };
197
+ const newRoot = build(0, n - 1, undefined);
198
+ this._setRoot(newRoot);
199
+ this._size = n;
200
+ return true;
201
+ }
202
+ /**
203
+ * Create a new tree by mapping each [key, values] bucket.
204
+ * @remarks Time O(N log N), Space O(N)
205
+ * @template MK
206
+ * @template MV
207
+ * @template MR
208
+ * @param callback - Function mapping (key, values, index, tree) → [newKey, newValue].
209
+ * @param [options] - Options for the output tree.
210
+ * @param [thisArg] - Value for `this` inside the callback.
211
+ * @returns The mapped AVLTree or AVLTreeMultiMap depending on MV; see overloads.
212
+ */
213
+ map(callback, options, thisArg) {
214
+ const out = this._createLike([], options);
215
+ let i = 0;
216
+ for (const [k, v] of this)
217
+ out.add(callback.call(thisArg, k, v, i++, this));
218
+ return out;
219
+ }
220
+ /**
221
+ * (Protected) Create an empty instance of the same concrete class.
222
+ * @remarks Time O(1), Space O(1)
223
+ * @template TK
224
+ * @template TV
225
+ * @template TR
226
+ * @param [options] - Optional constructor options for the like-kind instance.
227
+ * @returns An empty like-kind instance.
228
+ */
229
+ _createInstance(options) {
230
+ var _a, _b;
231
+ const Ctor = this.constructor;
232
+ return new Ctor([], Object.assign(Object.assign({}, ((_b = (_a = this._snapshotOptions) === null || _a === void 0 ? void 0 : _a.call(this)) !== null && _b !== void 0 ? _b : {})), (options !== null && options !== void 0 ? options : {})));
233
+ }
234
+ /**
235
+ * (Protected) Create a like-kind instance and seed it from an iterable.
236
+ * @remarks Time O(N log N), Space O(N)
237
+ * @template TK
238
+ * @template TV
239
+ * @template TR
240
+ * @param iter - Iterable used to seed the new tree.
241
+ * @param [options] - Options merged with the current snapshot.
242
+ * @returns A like-kind AVLTree built from the iterable.
195
243
  */
196
- clone() {
197
- const cloned = this.createTree();
198
- this._clone(cloned);
199
- return cloned;
244
+ _createLike(iter = [], options) {
245
+ var _a, _b;
246
+ const Ctor = this.constructor;
247
+ return new Ctor(iter, Object.assign(Object.assign({}, ((_b = (_a = this._snapshotOptions) === null || _a === void 0 ? void 0 : _a.call(this)) !== null && _b !== void 0 ? _b : {})), (options !== null && options !== void 0 ? options : {})));
200
248
  }
201
249
  }
202
250
  exports.AVLTreeMultiMap = AVLTreeMultiMap;