stack-typed 1.54.2 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/dist/data-structures/base/iterable-element-base.d.ts +14 -40
  2. package/dist/data-structures/base/iterable-element-base.js +14 -11
  3. package/dist/data-structures/base/linear-base.d.ts +277 -0
  4. package/dist/data-structures/base/linear-base.js +552 -0
  5. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +21 -20
  6. package/dist/data-structures/binary-tree/avl-tree-counter.js +8 -7
  7. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +23 -19
  8. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +51 -38
  9. package/dist/data-structures/binary-tree/avl-tree.d.ts +89 -21
  10. package/dist/data-structures/binary-tree/avl-tree.js +76 -8
  11. package/dist/data-structures/binary-tree/binary-tree.d.ts +173 -225
  12. package/dist/data-structures/binary-tree/binary-tree.js +244 -149
  13. package/dist/data-structures/binary-tree/bst.d.ts +62 -56
  14. package/dist/data-structures/binary-tree/bst.js +89 -133
  15. package/dist/data-structures/binary-tree/red-black-tree.d.ts +19 -25
  16. package/dist/data-structures/binary-tree/red-black-tree.js +7 -13
  17. package/dist/data-structures/binary-tree/tree-counter.d.ts +19 -19
  18. package/dist/data-structures/binary-tree/tree-counter.js +12 -12
  19. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +186 -25
  20. package/dist/data-structures/binary-tree/tree-multi-map.js +211 -41
  21. package/dist/data-structures/graph/abstract-graph.js +2 -2
  22. package/dist/data-structures/heap/heap.d.ts +3 -11
  23. package/dist/data-structures/heap/heap.js +0 -10
  24. package/dist/data-structures/heap/max-heap.d.ts +2 -2
  25. package/dist/data-structures/heap/min-heap.d.ts +2 -2
  26. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +65 -94
  27. package/dist/data-structures/linked-list/doubly-linked-list.js +131 -146
  28. package/dist/data-structures/linked-list/singly-linked-list.d.ts +79 -75
  29. package/dist/data-structures/linked-list/singly-linked-list.js +217 -169
  30. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +2 -2
  31. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +2 -2
  32. package/dist/data-structures/priority-queue/priority-queue.d.ts +2 -2
  33. package/dist/data-structures/queue/deque.d.ts +130 -91
  34. package/dist/data-structures/queue/deque.js +269 -169
  35. package/dist/data-structures/queue/queue.d.ts +84 -40
  36. package/dist/data-structures/queue/queue.js +134 -50
  37. package/dist/data-structures/stack/stack.d.ts +3 -11
  38. package/dist/data-structures/stack/stack.js +0 -10
  39. package/dist/data-structures/trie/trie.d.ts +4 -3
  40. package/dist/data-structures/trie/trie.js +3 -0
  41. package/dist/types/data-structures/base/base.d.ts +9 -4
  42. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +1 -1
  43. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -0
  44. package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
  45. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +1 -1
  46. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -2
  47. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +2 -2
  48. package/dist/types/data-structures/queue/deque.d.ts +2 -3
  49. package/dist/types/data-structures/queue/queue.d.ts +2 -2
  50. package/dist/utils/utils.d.ts +2 -2
  51. package/package.json +2 -2
  52. package/src/data-structures/base/iterable-element-base.ts +29 -20
  53. package/src/data-structures/base/linear-base.ts +649 -0
  54. package/src/data-structures/binary-tree/avl-tree-counter.ts +30 -23
  55. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +74 -49
  56. package/src/data-structures/binary-tree/avl-tree.ts +99 -29
  57. package/src/data-structures/binary-tree/binary-tree.ts +474 -257
  58. package/src/data-structures/binary-tree/bst.ts +150 -152
  59. package/src/data-structures/binary-tree/red-black-tree.ts +27 -35
  60. package/src/data-structures/binary-tree/tree-counter.ts +33 -27
  61. package/src/data-structures/binary-tree/tree-multi-map.ts +235 -53
  62. package/src/data-structures/graph/abstract-graph.ts +2 -2
  63. package/src/data-structures/heap/heap.ts +3 -14
  64. package/src/data-structures/heap/max-heap.ts +2 -2
  65. package/src/data-structures/heap/min-heap.ts +2 -2
  66. package/src/data-structures/linked-list/doubly-linked-list.ts +144 -160
  67. package/src/data-structures/linked-list/singly-linked-list.ts +241 -185
  68. package/src/data-structures/priority-queue/max-priority-queue.ts +2 -5
  69. package/src/data-structures/priority-queue/min-priority-queue.ts +2 -5
  70. package/src/data-structures/priority-queue/priority-queue.ts +2 -2
  71. package/src/data-structures/queue/deque.ts +286 -183
  72. package/src/data-structures/queue/queue.ts +149 -63
  73. package/src/data-structures/stack/stack.ts +3 -18
  74. package/src/data-structures/trie/trie.ts +7 -3
  75. package/src/types/data-structures/base/base.ts +17 -8
  76. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
  77. package/src/types/data-structures/binary-tree/binary-tree.ts +1 -0
  78. package/src/types/data-structures/binary-tree/bst.ts +1 -1
  79. package/src/types/data-structures/binary-tree/tree-multi-map.ts +1 -1
  80. package/src/types/data-structures/linked-list/doubly-linked-list.ts +2 -2
  81. package/src/types/data-structures/linked-list/singly-linked-list.ts +2 -2
  82. package/src/types/data-structures/queue/deque.ts +2 -3
  83. package/src/types/data-structures/queue/queue.ts +2 -2
  84. package/src/utils/utils.ts +2 -2
@@ -105,8 +105,8 @@ class AVLTreeCounter extends avl_tree_1.AVLTree {
105
105
  }
106
106
  /**
107
107
  * The function checks if the input is an instance of AVLTreeCounterNode.
108
- * @param {BTNRep<K, V, AVLTreeCounterNode<K, V>>} keyNodeOrEntry - The parameter
109
- * `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, AVLTreeCounterNode<K, V>>`.
108
+ * @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The parameter
109
+ * `keyNodeOrEntry` can be of type `R` or `K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`.
110
110
  * @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
111
111
  * an instance of the `AVLTreeCounterNode` class.
112
112
  */
@@ -119,9 +119,9 @@ class AVLTreeCounter extends avl_tree_1.AVLTree {
119
119
  *
120
120
  * The function overrides the add method of a TypeScript class to add a new node to a data structure
121
121
  * and update the count.
122
- * @param {BTNRep<K, V, AVLTreeCounterNode<K, V>>} keyNodeOrEntry - The
122
+ * @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The
123
123
  * `keyNodeOrEntry` parameter can accept a value of type `R`, which can be any type. It
124
- * can also accept a value of type `BTNRep<K, V, AVLTreeCounterNode<K, V>>`, which represents a key, node,
124
+ * can also accept a value of type `K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`, which represents a key, node,
125
125
  * entry, or raw element
126
126
  * @param {V} [value] - The `value` parameter represents the value associated with the key in the
127
127
  * data structure. It is an optional parameter, so it can be omitted if not needed.
@@ -147,7 +147,7 @@ class AVLTreeCounter extends avl_tree_1.AVLTree {
147
147
  *
148
148
  * The function overrides the delete method in a binary tree data structure, handling deletion of
149
149
  * nodes and maintaining balance in the tree.
150
- * @param {BTNRep<K, V, AVLTreeCounterNode<K, V>>} keyNodeOrEntry - The `predicate`
150
+ * @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The `predicate`
151
151
  * parameter in the `delete` method is used to specify the condition for deleting a node from the
152
152
  * binary tree. It can be a key, node, or entry that determines which
153
153
  * node(s) should be deleted.
@@ -232,6 +232,7 @@ class AVLTreeCounter extends avl_tree_1.AVLTree {
232
232
  /**
233
233
  * Time Complexity: O(n log n)
234
234
  * Space Complexity: O(log n)
235
+ *
235
236
  * The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
236
237
  * tree using either a recursive or iterative approach.
237
238
  * @param {IterationType} iterationType - The `iterationType` parameter is an optional parameter that
@@ -329,8 +330,8 @@ class AVLTreeCounter extends avl_tree_1.AVLTree {
329
330
  /**
330
331
  * The function `keyValueNodeEntryRawToNodeAndValue` converts a key, value, entry, or raw element into
331
332
  * a node object.
332
- * @param {BTNRep<K, V, AVLTreeCounterNode<K, V>>} keyNodeOrEntry - The
333
- * `keyNodeOrEntry` parameter can be of type `R` or `BTNRep<K, V, AVLTreeCounterNode<K, V>>`.
333
+ * @param {K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined} keyNodeOrEntry - The
334
+ * `keyNodeOrEntry` parameter can be of type `R` or `K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined`.
334
335
  * @param {V} [value] - The `value` parameter is an optional value that can be passed to the
335
336
  * `override` function. It represents the value associated with the key in the data structure. If no
336
337
  * value is provided, it will default to `undefined`.
@@ -5,10 +5,11 @@
5
5
  * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import { AVLTreeMultiMapOptions, BTNRep, OptNodeOrNull } from '../../types';
8
+ import { AVLTreeMultiMapOptions } from '../../types';
9
9
  import { AVLTree, AVLTreeNode } from './avl-tree';
10
10
  import { IBinaryTree } from '../../interfaces';
11
11
  export declare class AVLTreeMultiMapNode<K = any, V = any> extends AVLTreeNode<K, V[]> {
12
+ parent?: AVLTreeMultiMapNode<K, V>;
12
13
  /**
13
14
  * This TypeScript constructor initializes an object with a key of type K and an array of values of
14
15
  * type V.
@@ -19,13 +20,12 @@ export declare class AVLTreeMultiMapNode<K = any, V = any> extends AVLTreeNode<K
19
20
  * type `V`.
20
21
  */
21
22
  constructor(key: K, value: V[]);
22
- parent?: AVLTreeMultiMapNode<K, V>;
23
- _left?: OptNodeOrNull<AVLTreeMultiMapNode<K, V>>;
24
- get left(): OptNodeOrNull<AVLTreeMultiMapNode<K, V>>;
25
- set left(v: OptNodeOrNull<AVLTreeMultiMapNode<K, V>>);
26
- _right?: OptNodeOrNull<AVLTreeMultiMapNode<K, V>>;
27
- get right(): OptNodeOrNull<AVLTreeMultiMapNode<K, V>>;
28
- set right(v: OptNodeOrNull<AVLTreeMultiMapNode<K, V>>);
23
+ _left?: AVLTreeMultiMapNode<K, V> | null | undefined;
24
+ get left(): AVLTreeMultiMapNode<K, V> | null | undefined;
25
+ set left(v: AVLTreeMultiMapNode<K, V> | null | undefined);
26
+ _right?: AVLTreeMultiMapNode<K, V> | null | undefined;
27
+ get right(): AVLTreeMultiMapNode<K, V> | null | undefined;
28
+ set right(v: AVLTreeMultiMapNode<K, V> | null | undefined);
29
29
  }
30
30
  /**
31
31
  *
@@ -42,7 +42,7 @@ export declare class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV
42
42
  * `AVLTreeMultiMapOptions<K, V[], R>`. It is an optional parameter that allows you to specify
43
43
  * additional options for configuring the AVLTreeMultiMap instance.
44
44
  */
45
- constructor(keysNodesEntriesOrRaws?: Iterable<BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | R>, options?: AVLTreeMultiMapOptions<K, V[], R>);
45
+ constructor(keysNodesEntriesOrRaws?: Iterable<K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | R>, options?: AVLTreeMultiMapOptions<K, V[], R>);
46
46
  /**
47
47
  * Time Complexity: O(1)
48
48
  * Space Complexity: O(1)
@@ -60,15 +60,19 @@ export declare class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV
60
60
  * Time Complexity: O(1)
61
61
  * Space Complexity: O(1)
62
62
  *
63
- * The function `createNode` overrides the method to create a new AVLTreeMultiMapNode with a
64
- * specified key and an empty array of values.
65
- * @param {K} key - The `key` parameter in the `createNode` method represents the key of the node
66
- * that will be created in the AVLTreeMultiMap.
67
- * @returns An AVLTreeMultiMapNode object is being returned, initialized with the provided key and an
68
- * empty array.
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.
69
73
  */
70
- createNode(key: K): AVLTreeMultiMapNode<K, V>;
71
- add(node: BTNRep<K, V[], AVLTreeMultiMapNode<K, V>>): boolean;
74
+ createNode(key: K, value?: V[]): AVLTreeMultiMapNode<K, V>;
75
+ add(keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined): boolean;
72
76
  add(key: K, value: V): boolean;
73
77
  /**
74
78
  * Time Complexity: O(log n)
@@ -76,7 +80,7 @@ export declare class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV
76
80
  *
77
81
  * The function `deleteValue` removes a specific value from a key in an AVLTreeMultiMap data
78
82
  * structure and deletes the entire node if no values are left for that key.
79
- * @param {BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry`
83
+ * @param {K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | K} keyNodeOrEntry - The `keyNodeOrEntry`
80
84
  * parameter in the `deleteValue` function can be either a `BTNRep` object representing a key-value
81
85
  * pair in the AVLTreeMultiMapNode, or just the key itself.
82
86
  * @param {V} value - The `value` parameter in the `deleteValue` function represents the specific
@@ -87,7 +91,7 @@ export declare class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV
87
91
  * `value` was successfully deleted from the array of values associated with the `keyNodeOrEntry`. If
88
92
  * the value was not found in the array, it returns `false`.
89
93
  */
90
- deleteValue(keyNodeOrEntry: BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K, value: V): boolean;
94
+ deleteValue(keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | K, value: V): boolean;
91
95
  /**
92
96
  * Time Complexity: O(n)
93
97
  * Space Complexity: O(n)
@@ -72,37 +72,40 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
72
72
  * provided options.
73
73
  */
74
74
  createTree(options) {
75
- return new AVLTreeMultiMap([], Object.assign({ iterationType: this.iterationType, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options));
75
+ return new AVLTreeMultiMap([], Object.assign({ iterationType: this.iterationType, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse, isMapMode: this._isMapMode }, options));
76
76
  }
77
77
  /**
78
78
  * Time Complexity: O(1)
79
79
  * Space Complexity: O(1)
80
80
  *
81
- * The function `createNode` overrides the method to create a new AVLTreeMultiMapNode with a
82
- * specified key and an empty array of values.
83
- * @param {K} key - The `key` parameter in the `createNode` method represents the key of the node
84
- * that will be created in the AVLTreeMultiMap.
85
- * @returns An AVLTreeMultiMapNode object is being returned, initialized with the provided key and an
86
- * empty array.
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.
87
91
  */
88
- createNode(key) {
89
- return new AVLTreeMultiMapNode(key, []);
92
+ createNode(key, value = []) {
93
+ return new AVLTreeMultiMapNode(key, this._isMapMode ? [] : value);
90
94
  }
91
95
  /**
92
96
  * Time Complexity: O(log n)
93
97
  * Space Complexity: O(log n)
94
98
  *
95
- * The function `add` in TypeScript overrides the superclass method to add key-value pairs to an AVL
96
- * tree multi-map.
97
- * @param {BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry`
98
- * parameter in the `override add` method can be either a key-value pair entry or just a key. If it
99
- * is a key-value pair entry, it will be in the format `[key, values]`, where `key` is the key and
100
- * `values`
101
- * @param {V} [value] - The `value` parameter in the `override add` method represents the value that
102
- * you want to add to the AVLTreeMultiMap. It can be a single value or an array of values associated
103
- * with a specific key.
104
- * @returns The `override add` method is returning a boolean value, which indicates whether the
105
- * addition operation was successful or not.
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.
106
109
  */
107
110
  add(keyNodeOrEntry, value) {
108
111
  if (this.isRealNode(keyNodeOrEntry))
@@ -110,30 +113,40 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
110
113
  const _commonAdd = (key, values) => {
111
114
  if (key === undefined || key === null)
112
115
  return false;
113
- const existingValues = this.get(key);
114
- if (existingValues !== undefined && values !== undefined) {
115
- for (const value of values)
116
- existingValues.push(value);
117
- return true;
118
- }
119
- const existingNode = this.getNode(key);
120
- if (this.isRealNode(existingNode)) {
121
- if (existingValues === undefined) {
122
- super.add(key, values);
123
- return true;
124
- }
125
- if (values !== undefined) {
116
+ const _addToValues = () => {
117
+ const existingValues = this.get(key);
118
+ if (existingValues !== undefined && values !== undefined) {
126
119
  for (const value of values)
127
120
  existingValues.push(value);
128
121
  return true;
129
122
  }
123
+ return false;
124
+ };
125
+ const _addByNode = () => {
126
+ const existingNode = this.getNode(key);
127
+ if (this.isRealNode(existingNode)) {
128
+ const existingValues = this.get(existingNode);
129
+ if (existingValues === undefined) {
130
+ super.add(key, values);
131
+ return true;
132
+ }
133
+ if (values !== undefined) {
134
+ for (const value of values)
135
+ existingValues.push(value);
136
+ return true;
137
+ }
138
+ else {
139
+ return false;
140
+ }
141
+ }
130
142
  else {
131
- return false;
143
+ return super.add(key, values);
132
144
  }
145
+ };
146
+ if (this._isMapMode) {
147
+ return _addByNode() || _addToValues();
133
148
  }
134
- else {
135
- return super.add(key, values);
136
- }
149
+ return _addToValues() || _addByNode();
137
150
  };
138
151
  if (this.isEntry(keyNodeOrEntry)) {
139
152
  const [key, values] = keyNodeOrEntry;
@@ -147,7 +160,7 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
147
160
  *
148
161
  * The function `deleteValue` removes a specific value from a key in an AVLTreeMultiMap data
149
162
  * structure and deletes the entire node if no values are left for that key.
150
- * @param {BTNRep<K, V[], AVLTreeMultiMapNode<K, V>> | K} keyNodeOrEntry - The `keyNodeOrEntry`
163
+ * @param {K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined | K} keyNodeOrEntry - The `keyNodeOrEntry`
151
164
  * parameter in the `deleteValue` function can be either a `BTNRep` object representing a key-value
152
165
  * pair in the AVLTreeMultiMapNode, or just the key itself.
153
166
  * @param {V} value - The `value` parameter in the `deleteValue` function represents the specific
@@ -6,9 +6,10 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import { BST, BSTNode } from './bst';
9
- import type { AVLTreeOptions, BinaryTreeDeleteResult, BSTNOptKeyOrNode, BTNRep, EntryCallback, OptNodeOrNull } from '../../types';
9
+ import type { AVLTreeOptions, BinaryTreeDeleteResult, BSTNOptKeyOrNode, EntryCallback } from '../../types';
10
10
  import { IBinaryTree } from '../../interfaces';
11
11
  export declare class AVLTreeNode<K = any, V = any> extends BSTNode<K, V> {
12
+ parent?: AVLTreeNode<K, V>;
12
13
  /**
13
14
  * This TypeScript constructor function initializes an instance with a key and an optional value.
14
15
  * @param {K} key - The `key` parameter is typically used to uniquely identify an object or element
@@ -19,13 +20,12 @@ export declare class AVLTreeNode<K = any, V = any> extends BSTNode<K, V> {
19
20
  * default to `undefined`.
20
21
  */
21
22
  constructor(key: K, value?: V);
22
- parent?: AVLTreeNode<K, V>;
23
- _left?: OptNodeOrNull<AVLTreeNode<K, V>>;
24
- get left(): OptNodeOrNull<AVLTreeNode<K, V>>;
25
- set left(v: OptNodeOrNull<AVLTreeNode<K, V>>);
26
- _right?: OptNodeOrNull<AVLTreeNode<K, V>>;
27
- get right(): OptNodeOrNull<AVLTreeNode<K, V>>;
28
- set right(v: OptNodeOrNull<AVLTreeNode<K, V>>);
23
+ _left?: AVLTreeNode<K, V> | null | undefined;
24
+ get left(): AVLTreeNode<K, V> | null | undefined;
25
+ set left(v: AVLTreeNode<K, V> | null | undefined);
26
+ _right?: AVLTreeNode<K, V> | null | undefined;
27
+ get right(): AVLTreeNode<K, V> | null | undefined;
28
+ set right(v: AVLTreeNode<K, V> | null | undefined);
29
29
  }
30
30
  /**
31
31
  * 1. Height-Balanced: Each node's left and right subtrees differ in height by no more than one.
@@ -35,20 +35,85 @@ export declare class AVLTreeNode<K = any, V = any> extends BSTNode<K, V> {
35
35
  * 5. Efficient Lookups: Offers O(log n) search time, where 'n' is the number of nodes, due to its balanced nature.
36
36
  * 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
37
37
  * 7. Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes.
38
+ * @example
39
+ * // Find elements in a range
40
+ * // In interval queries, AVL trees, with their strictly balanced structure and lower height, offer better query efficiency, making them ideal for frequent and high-performance interval queries. In contrast, Red-Black trees, with lower update costs, are more suitable for scenarios involving frequent insertions and deletions where the requirements for interval queries are less demanding.
41
+ * type Datum = { timestamp: Date; temperature: number };
42
+ * // Fixed dataset of CPU temperature readings
43
+ * const cpuData: Datum[] = [
44
+ * { timestamp: new Date('2024-12-02T00:00:00'), temperature: 55.1 },
45
+ * { timestamp: new Date('2024-12-02T00:01:00'), temperature: 56.3 },
46
+ * { timestamp: new Date('2024-12-02T00:02:00'), temperature: 54.8 },
47
+ * { timestamp: new Date('2024-12-02T00:03:00'), temperature: 57.2 },
48
+ * { timestamp: new Date('2024-12-02T00:04:00'), temperature: 58.0 },
49
+ * { timestamp: new Date('2024-12-02T00:05:00'), temperature: 59.4 },
50
+ * { timestamp: new Date('2024-12-02T00:06:00'), temperature: 60.1 },
51
+ * { timestamp: new Date('2024-12-02T00:07:00'), temperature: 61.3 },
52
+ * { timestamp: new Date('2024-12-02T00:08:00'), temperature: 62.0 },
53
+ * { timestamp: new Date('2024-12-02T00:09:00'), temperature: 63.5 },
54
+ * { timestamp: new Date('2024-12-02T00:10:00'), temperature: 64.0 },
55
+ * { timestamp: new Date('2024-12-02T00:11:00'), temperature: 62.8 },
56
+ * { timestamp: new Date('2024-12-02T00:12:00'), temperature: 61.5 },
57
+ * { timestamp: new Date('2024-12-02T00:13:00'), temperature: 60.2 },
58
+ * { timestamp: new Date('2024-12-02T00:14:00'), temperature: 59.8 },
59
+ * { timestamp: new Date('2024-12-02T00:15:00'), temperature: 58.6 },
60
+ * { timestamp: new Date('2024-12-02T00:16:00'), temperature: 57.4 },
61
+ * { timestamp: new Date('2024-12-02T00:17:00'), temperature: 56.2 },
62
+ * { timestamp: new Date('2024-12-02T00:18:00'), temperature: 55.7 },
63
+ * { timestamp: new Date('2024-12-02T00:19:00'), temperature: 54.5 },
64
+ * { timestamp: new Date('2024-12-02T00:20:00'), temperature: 53.2 },
65
+ * { timestamp: new Date('2024-12-02T00:21:00'), temperature: 52.8 },
66
+ * { timestamp: new Date('2024-12-02T00:22:00'), temperature: 51.9 },
67
+ * { timestamp: new Date('2024-12-02T00:23:00'), temperature: 50.5 },
68
+ * { timestamp: new Date('2024-12-02T00:24:00'), temperature: 49.8 },
69
+ * { timestamp: new Date('2024-12-02T00:25:00'), temperature: 48.7 },
70
+ * { timestamp: new Date('2024-12-02T00:26:00'), temperature: 47.5 },
71
+ * { timestamp: new Date('2024-12-02T00:27:00'), temperature: 46.3 },
72
+ * { timestamp: new Date('2024-12-02T00:28:00'), temperature: 45.9 },
73
+ * { timestamp: new Date('2024-12-02T00:29:00'), temperature: 45.0 }
74
+ * ];
75
+ *
76
+ * // Create an AVL tree to store CPU temperature data
77
+ * const cpuTemperatureTree = new AVLTree<Date, number, Datum>(cpuData, {
78
+ * toEntryFn: ({ timestamp, temperature }) => [timestamp, temperature]
79
+ * });
80
+ *
81
+ * // Query a specific time range (e.g., from 00:05 to 00:15)
82
+ * const rangeStart = new Date('2024-12-02T00:05:00');
83
+ * const rangeEnd = new Date('2024-12-02T00:15:00');
84
+ * const rangeResults = cpuTemperatureTree.rangeSearch([rangeStart, rangeEnd], node => ({
85
+ * minute: node ? node.key.getMinutes() : 0,
86
+ * temperature: cpuTemperatureTree.get(node ? node.key : undefined)
87
+ * }));
88
+ *
89
+ * console.log(rangeResults); // [
90
+ * // { minute: 5, temperature: 59.4 },
91
+ * // { minute: 6, temperature: 60.1 },
92
+ * // { minute: 7, temperature: 61.3 },
93
+ * // { minute: 8, temperature: 62 },
94
+ * // { minute: 9, temperature: 63.5 },
95
+ * // { minute: 10, temperature: 64 },
96
+ * // { minute: 11, temperature: 62.8 },
97
+ * // { minute: 12, temperature: 61.5 },
98
+ * // { minute: 13, temperature: 60.2 },
99
+ * // { minute: 14, temperature: 59.8 },
100
+ * // { minute: 15, temperature: 58.6 }
101
+ * // ]
38
102
  */
39
103
  export declare class AVLTree<K = any, V = any, R = object, MK = any, MV = any, MR = object> extends BST<K, V, R, MK, MV, MR> implements IBinaryTree<K, V, R, MK, MV, MR> {
40
104
  /**
41
105
  * This TypeScript constructor initializes an AVLTree with keys, nodes, entries, or raw data provided
42
106
  * in an iterable format.
43
107
  * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
44
- * iterable that can contain either `BTNRep<K, V, AVLTreeNode<K, V>>` objects or `R` objects. It is
108
+ * iterable that can contain either `
109
+ K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` objects or `R` objects. It is
45
110
  * used to initialize the AVLTree with key-value pairs or raw data entries. If provided
46
111
  * @param [options] - The `options` parameter in the constructor is of type `AVLTreeOptions<K, V,
47
112
  * R>`. It is an optional parameter that allows you to specify additional options for configuring the
48
113
  * AVL tree. These options could include things like custom comparators, initial capacity, or any
49
114
  * other configuration settings specific
50
115
  */
51
- constructor(keysNodesEntriesOrRaws?: Iterable<BTNRep<K, V, AVLTreeNode<K, V>> | R>, options?: AVLTreeOptions<K, V, R>);
116
+ constructor(keysNodesEntriesOrRaws?: Iterable<K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, options?: AVLTreeOptions<K, V, R>);
52
117
  /**
53
118
  * Time Complexity: O(1)
54
119
  * Space Complexity: O(1)
@@ -78,39 +143,41 @@ export declare class AVLTree<K = any, V = any, R = object, MK = any, MV = any, M
78
143
  * Space Complexity: O(1)
79
144
  *
80
145
  * The function checks if the input is an instance of AVLTreeNode.
81
- * @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The parameter
82
- * `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, AVLTreeNode<K, V>>`.
146
+ * @param {K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter
147
+ * `keyNodeOrEntry` can be of type `R` or `
148
+ K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `.
83
149
  * @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
84
150
  * an instance of the `AVLTreeNode` class.
85
151
  */
86
- isNode(keyNodeOrEntry: BTNRep<K, V, AVLTreeNode<K, V>>): keyNodeOrEntry is AVLTreeNode<K, V>;
152
+ isNode(keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): keyNodeOrEntry is AVLTreeNode<K, V>;
87
153
  /**
88
154
  * Time Complexity: O(log n)
89
155
  * Space Complexity: O(log n)
90
156
  *
91
157
  * The function overrides the add method of a class and inserts a key-value pair into a data
92
158
  * structure, then balances the path.
93
- * @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The parameter
94
- * `keyNodeOrEntry` can accept values of type `R`, `BTNRep<K, V, AVLTreeNode<K, V>>`
159
+ * @param { K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter
160
+ * `keyNodeOrEntry` can accept values of type `R`, `
161
+ K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `
95
162
  * @param {V} [value] - The `value` parameter is an optional value that you want to associate with
96
163
  * the key or node being added to the data structure.
97
164
  * @returns The method is returning a boolean value.
98
165
  */
99
- add(keyNodeOrEntry: BTNRep<K, V, AVLTreeNode<K, V>>, value?: V): boolean;
166
+ add(keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): boolean;
100
167
  /**
101
168
  * Time Complexity: O(log n)
102
169
  * Space Complexity: O(log n)
103
170
  *
104
171
  * The function overrides the delete method in a TypeScript class, performs deletion, and then
105
172
  * balances the tree if necessary.
106
- * @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry`
173
+ * @param { K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `keyNodeOrEntry`
107
174
  * parameter in the `override delete` method can be one of the following types:
108
175
  * @returns The `delete` method is being overridden in this code snippet. It first calls the `delete`
109
176
  * method from the superclass (presumably a parent class) with the provided `predicate`, which could
110
177
  * be a key, node, entry, or a custom predicate. The result of this deletion operation is stored in
111
178
  * `deletedResults`, which is an array of `BinaryTreeDeleteResult` objects.
112
179
  */
113
- delete(keyNodeOrEntry: BTNRep<K, V, AVLTreeNode<K, V>>): BinaryTreeDeleteResult<AVLTreeNode<K, V>>[];
180
+ delete(keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): BinaryTreeDeleteResult<AVLTreeNode<K, V>>[];
114
181
  /**
115
182
  * Time Complexity: O(n)
116
183
  * Space Complexity: O(n)
@@ -213,10 +280,11 @@ export declare class AVLTree<K = any, V = any, R = object, MK = any, MV = any, M
213
280
  *
214
281
  * The `_balancePath` function is used to update the heights of nodes and perform rotation operations
215
282
  * to restore balance in an AVL tree after inserting a node.
216
- * @param {BTNRep<K, V, AVLTreeNode<K, V>>} node - The `node` parameter can be of type `R` or
217
- * `BTNRep<K, V, AVLTreeNode<K, V>>`.
283
+ * @param { K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } node - The `node` parameter can be of type `R` or
284
+ * `
285
+ K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `.
218
286
  */
219
- protected _balancePath(node: BTNRep<K, V, AVLTreeNode<K, V>>): void;
287
+ protected _balancePath(node: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): void;
220
288
  /**
221
289
  * Time Complexity: O(1)
222
290
  * Space Complexity: O(1)
@@ -53,13 +53,78 @@ exports.AVLTreeNode = AVLTreeNode;
53
53
  * 5. Efficient Lookups: Offers O(log n) search time, where 'n' is the number of nodes, due to its balanced nature.
54
54
  * 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
55
55
  * 7. Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes.
56
+ * @example
57
+ * // Find elements in a range
58
+ * // In interval queries, AVL trees, with their strictly balanced structure and lower height, offer better query efficiency, making them ideal for frequent and high-performance interval queries. In contrast, Red-Black trees, with lower update costs, are more suitable for scenarios involving frequent insertions and deletions where the requirements for interval queries are less demanding.
59
+ * type Datum = { timestamp: Date; temperature: number };
60
+ * // Fixed dataset of CPU temperature readings
61
+ * const cpuData: Datum[] = [
62
+ * { timestamp: new Date('2024-12-02T00:00:00'), temperature: 55.1 },
63
+ * { timestamp: new Date('2024-12-02T00:01:00'), temperature: 56.3 },
64
+ * { timestamp: new Date('2024-12-02T00:02:00'), temperature: 54.8 },
65
+ * { timestamp: new Date('2024-12-02T00:03:00'), temperature: 57.2 },
66
+ * { timestamp: new Date('2024-12-02T00:04:00'), temperature: 58.0 },
67
+ * { timestamp: new Date('2024-12-02T00:05:00'), temperature: 59.4 },
68
+ * { timestamp: new Date('2024-12-02T00:06:00'), temperature: 60.1 },
69
+ * { timestamp: new Date('2024-12-02T00:07:00'), temperature: 61.3 },
70
+ * { timestamp: new Date('2024-12-02T00:08:00'), temperature: 62.0 },
71
+ * { timestamp: new Date('2024-12-02T00:09:00'), temperature: 63.5 },
72
+ * { timestamp: new Date('2024-12-02T00:10:00'), temperature: 64.0 },
73
+ * { timestamp: new Date('2024-12-02T00:11:00'), temperature: 62.8 },
74
+ * { timestamp: new Date('2024-12-02T00:12:00'), temperature: 61.5 },
75
+ * { timestamp: new Date('2024-12-02T00:13:00'), temperature: 60.2 },
76
+ * { timestamp: new Date('2024-12-02T00:14:00'), temperature: 59.8 },
77
+ * { timestamp: new Date('2024-12-02T00:15:00'), temperature: 58.6 },
78
+ * { timestamp: new Date('2024-12-02T00:16:00'), temperature: 57.4 },
79
+ * { timestamp: new Date('2024-12-02T00:17:00'), temperature: 56.2 },
80
+ * { timestamp: new Date('2024-12-02T00:18:00'), temperature: 55.7 },
81
+ * { timestamp: new Date('2024-12-02T00:19:00'), temperature: 54.5 },
82
+ * { timestamp: new Date('2024-12-02T00:20:00'), temperature: 53.2 },
83
+ * { timestamp: new Date('2024-12-02T00:21:00'), temperature: 52.8 },
84
+ * { timestamp: new Date('2024-12-02T00:22:00'), temperature: 51.9 },
85
+ * { timestamp: new Date('2024-12-02T00:23:00'), temperature: 50.5 },
86
+ * { timestamp: new Date('2024-12-02T00:24:00'), temperature: 49.8 },
87
+ * { timestamp: new Date('2024-12-02T00:25:00'), temperature: 48.7 },
88
+ * { timestamp: new Date('2024-12-02T00:26:00'), temperature: 47.5 },
89
+ * { timestamp: new Date('2024-12-02T00:27:00'), temperature: 46.3 },
90
+ * { timestamp: new Date('2024-12-02T00:28:00'), temperature: 45.9 },
91
+ * { timestamp: new Date('2024-12-02T00:29:00'), temperature: 45.0 }
92
+ * ];
93
+ *
94
+ * // Create an AVL tree to store CPU temperature data
95
+ * const cpuTemperatureTree = new AVLTree<Date, number, Datum>(cpuData, {
96
+ * toEntryFn: ({ timestamp, temperature }) => [timestamp, temperature]
97
+ * });
98
+ *
99
+ * // Query a specific time range (e.g., from 00:05 to 00:15)
100
+ * const rangeStart = new Date('2024-12-02T00:05:00');
101
+ * const rangeEnd = new Date('2024-12-02T00:15:00');
102
+ * const rangeResults = cpuTemperatureTree.rangeSearch([rangeStart, rangeEnd], node => ({
103
+ * minute: node ? node.key.getMinutes() : 0,
104
+ * temperature: cpuTemperatureTree.get(node ? node.key : undefined)
105
+ * }));
106
+ *
107
+ * console.log(rangeResults); // [
108
+ * // { minute: 5, temperature: 59.4 },
109
+ * // { minute: 6, temperature: 60.1 },
110
+ * // { minute: 7, temperature: 61.3 },
111
+ * // { minute: 8, temperature: 62 },
112
+ * // { minute: 9, temperature: 63.5 },
113
+ * // { minute: 10, temperature: 64 },
114
+ * // { minute: 11, temperature: 62.8 },
115
+ * // { minute: 12, temperature: 61.5 },
116
+ * // { minute: 13, temperature: 60.2 },
117
+ * // { minute: 14, temperature: 59.8 },
118
+ * // { minute: 15, temperature: 58.6 }
119
+ * // ]
56
120
  */
57
121
  class AVLTree extends bst_1.BST {
58
122
  /**
59
123
  * This TypeScript constructor initializes an AVLTree with keys, nodes, entries, or raw data provided
60
124
  * in an iterable format.
61
125
  * @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter in the constructor is an
62
- * iterable that can contain either `BTNRep<K, V, AVLTreeNode<K, V>>` objects or `R` objects. It is
126
+ * iterable that can contain either `
127
+ K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined ` objects or `R` objects. It is
63
128
  * used to initialize the AVLTree with key-value pairs or raw data entries. If provided
64
129
  * @param [options] - The `options` parameter in the constructor is of type `AVLTreeOptions<K, V,
65
130
  * R>`. It is an optional parameter that allows you to specify additional options for configuring the
@@ -104,8 +169,9 @@ class AVLTree extends bst_1.BST {
104
169
  * Space Complexity: O(1)
105
170
  *
106
171
  * The function checks if the input is an instance of AVLTreeNode.
107
- * @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The parameter
108
- * `keyNodeOrEntry` can be of type `R` or `BTNRep<K, V, AVLTreeNode<K, V>>`.
172
+ * @param {K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter
173
+ * `keyNodeOrEntry` can be of type `R` or `
174
+ K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `.
109
175
  * @returns a boolean value indicating whether the input parameter `keyNodeOrEntry` is
110
176
  * an instance of the `AVLTreeNode` class.
111
177
  */
@@ -118,8 +184,9 @@ class AVLTree extends bst_1.BST {
118
184
  *
119
185
  * The function overrides the add method of a class and inserts a key-value pair into a data
120
186
  * structure, then balances the path.
121
- * @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The parameter
122
- * `keyNodeOrEntry` can accept values of type `R`, `BTNRep<K, V, AVLTreeNode<K, V>>`
187
+ * @param { K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The parameter
188
+ * `keyNodeOrEntry` can accept values of type `R`, `
189
+ K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `
123
190
  * @param {V} [value] - The `value` parameter is an optional value that you want to associate with
124
191
  * the key or node being added to the data structure.
125
192
  * @returns The method is returning a boolean value.
@@ -138,7 +205,7 @@ class AVLTree extends bst_1.BST {
138
205
  *
139
206
  * The function overrides the delete method in a TypeScript class, performs deletion, and then
140
207
  * balances the tree if necessary.
141
- * @param {BTNRep<K, V, AVLTreeNode<K, V>>} keyNodeOrEntry - The `keyNodeOrEntry`
208
+ * @param { K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } keyNodeOrEntry - The `keyNodeOrEntry`
142
209
  * parameter in the `override delete` method can be one of the following types:
143
210
  * @returns The `delete` method is being overridden in this code snippet. It first calls the `delete`
144
211
  * method from the superclass (presumably a parent class) with the provided `predicate`, which could
@@ -464,8 +531,9 @@ class AVLTree extends bst_1.BST {
464
531
  *
465
532
  * The `_balancePath` function is used to update the heights of nodes and perform rotation operations
466
533
  * to restore balance in an AVL tree after inserting a node.
467
- * @param {BTNRep<K, V, AVLTreeNode<K, V>>} node - The `node` parameter can be of type `R` or
468
- * `BTNRep<K, V, AVLTreeNode<K, V>>`.
534
+ * @param { K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined } node - The `node` parameter can be of type `R` or
535
+ * `
536
+ K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined `.
469
537
  */
470
538
  _balancePath(node) {
471
539
  node = this.ensureNode(node);