priority-queue-typed 1.47.6 → 1.47.7

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 (28) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +40 -22
  2. package/dist/data-structures/binary-tree/avl-tree.js +45 -36
  3. package/dist/data-structures/binary-tree/binary-tree.d.ts +105 -113
  4. package/dist/data-structures/binary-tree/binary-tree.js +133 -119
  5. package/dist/data-structures/binary-tree/bst.d.ts +53 -44
  6. package/dist/data-structures/binary-tree/bst.js +137 -154
  7. package/dist/data-structures/binary-tree/rb-tree.d.ts +48 -15
  8. package/dist/data-structures/binary-tree/rb-tree.js +70 -33
  9. package/dist/data-structures/binary-tree/tree-multimap.d.ts +26 -37
  10. package/dist/data-structures/binary-tree/tree-multimap.js +58 -137
  11. package/dist/data-structures/hash/hash-map.d.ts +2 -6
  12. package/dist/data-structures/hash/hash-map.js +5 -8
  13. package/dist/data-structures/trie/trie.d.ts +3 -0
  14. package/dist/data-structures/trie/trie.js +19 -4
  15. package/dist/interfaces/binary-tree.d.ts +3 -3
  16. package/dist/types/common.d.ts +6 -1
  17. package/dist/types/data-structures/hash/hash-map.d.ts +1 -2
  18. package/package.json +2 -2
  19. package/src/data-structures/binary-tree/avl-tree.ts +59 -39
  20. package/src/data-structures/binary-tree/binary-tree.ts +192 -180
  21. package/src/data-structures/binary-tree/bst.ts +157 -154
  22. package/src/data-structures/binary-tree/rb-tree.ts +78 -37
  23. package/src/data-structures/binary-tree/tree-multimap.ts +67 -145
  24. package/src/data-structures/hash/hash-map.ts +8 -8
  25. package/src/data-structures/trie/trie.ts +23 -4
  26. package/src/interfaces/binary-tree.ts +3 -3
  27. package/src/types/common.ts +11 -1
  28. package/src/types/data-structures/hash/hash-map.ts +1 -2
@@ -24,20 +24,17 @@ exports.TreeMultimapNode = TreeMultimapNode;
24
24
  * 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.
25
25
  */
26
26
  class TreeMultimap extends avl_tree_1.AVLTree {
27
- /**
28
- * The constructor function for a TreeMultimap class in TypeScript, which extends another class and sets an option to
29
- * merge duplicated values.
30
- * @param {TreeMultimapOptions} [options] - An optional object that contains additional configuration options for the
31
- * TreeMultimap.
32
- */
33
27
  constructor(elements, options) {
34
28
  super([], options);
35
29
  this._count = 0;
36
30
  if (elements)
37
- this.init(elements);
31
+ this.addMany(elements);
38
32
  }
33
+ // TODO the _count is not accurate after nodes count modified
39
34
  get count() {
40
- return this._count;
35
+ let sum = 0;
36
+ this.subTreeTraverse(node => sum += node.count);
37
+ return sum;
41
38
  }
42
39
  /**
43
40
  * The function creates a new BSTNode with the given key, value, and count.
@@ -54,131 +51,68 @@ class TreeMultimap extends avl_tree_1.AVLTree {
54
51
  createTree(options) {
55
52
  return new TreeMultimap([], Object.assign({ iterationType: this.iterationType, comparator: this.comparator }, options));
56
53
  }
54
+ /**
55
+ * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
56
+ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
57
+ */
57
58
  /**
58
59
  * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
59
60
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
60
61
  *
61
- * The `add` function adds a new node to the tree multimap, updating the count if the key already
62
- * exists, and balances the tree if necessary.
63
- * @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be one of the
64
- * following types:
65
- * @param {V} [value] - The `value` parameter represents the value associated with the key that is
66
- * being added to the tree. It is an optional parameter, so it can be omitted if not needed.
62
+ * The `add` function overrides the base class `add` function to add a new node to the tree multimap
63
+ * and update the count.
64
+ * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be one of the following:
67
65
  * @param [count=1] - The `count` parameter is an optional parameter that specifies the number of
68
- * times the key-value pair should be added to the multimap. If not provided, the default value is 1.
69
- * @returns a node (`N`) or `undefined`.
66
+ * times the key or node or entry should be added to the multimap. If not provided, the default value
67
+ * is 1.
68
+ * @returns either a node (`N`) or `undefined`.
70
69
  */
71
- add(keyOrNode, value, count = 1) {
72
- if (keyOrNode === null)
73
- return undefined;
74
- let inserted = undefined, newNode;
75
- if (keyOrNode instanceof TreeMultimapNode) {
76
- newNode = this.createNode(keyOrNode.key, keyOrNode.value, keyOrNode.count);
70
+ add(keyOrNodeOrEntry, count = 1) {
71
+ let newNode;
72
+ if (keyOrNodeOrEntry === undefined || keyOrNodeOrEntry === null) {
73
+ return;
77
74
  }
78
- else if (keyOrNode === undefined) {
79
- newNode = undefined;
75
+ else if (keyOrNodeOrEntry instanceof TreeMultimapNode) {
76
+ newNode = keyOrNodeOrEntry;
80
77
  }
81
- else {
82
- newNode = this.createNode(keyOrNode, value, count);
78
+ else if (this.isNodeKey(keyOrNodeOrEntry)) {
79
+ newNode = this.createNode(keyOrNodeOrEntry, undefined, count);
83
80
  }
84
- if (!this.root) {
85
- this._setRoot(newNode);
86
- this._size = this.size + 1;
87
- if (newNode)
88
- this._count += newNode.count;
89
- inserted = this.root;
81
+ else if (this.isEntry(keyOrNodeOrEntry)) {
82
+ const [key, value] = keyOrNodeOrEntry;
83
+ if (key === undefined || key === null) {
84
+ return;
85
+ }
86
+ else {
87
+ newNode = this.createNode(key, value, count);
88
+ }
90
89
  }
91
90
  else {
92
- let cur = this.root;
93
- let traversing = true;
94
- while (traversing) {
95
- if (cur) {
96
- if (newNode) {
97
- if (this._compare(cur.key, newNode.key) === types_1.CP.eq) {
98
- cur.value = newNode.value;
99
- cur.count += newNode.count;
100
- this._count += newNode.count;
101
- traversing = false;
102
- inserted = cur;
103
- }
104
- else if (this._compare(cur.key, newNode.key) === types_1.CP.gt) {
105
- // Traverse left of the node
106
- if (cur.left === undefined) {
107
- //Add to the left of the current node
108
- cur.left = newNode;
109
- this._size = this.size + 1;
110
- this._count += newNode.count;
111
- traversing = false;
112
- inserted = cur.left;
113
- }
114
- else {
115
- //Traverse the left of the current node
116
- if (cur.left)
117
- cur = cur.left;
118
- }
119
- }
120
- else if (this._compare(cur.key, newNode.key) === types_1.CP.lt) {
121
- // Traverse right of the node
122
- if (cur.right === undefined) {
123
- //Add to the right of the current node
124
- cur.right = newNode;
125
- this._size = this.size + 1;
126
- this._count += newNode.count;
127
- traversing = false;
128
- inserted = cur.right;
129
- }
130
- else {
131
- //Traverse the left of the current node
132
- if (cur.right)
133
- cur = cur.right;
134
- }
135
- }
136
- }
137
- else {
138
- // TODO may need to support undefined inserted
139
- }
140
- }
141
- else {
142
- traversing = false;
143
- }
144
- }
91
+ return;
92
+ }
93
+ const orgNodeCount = (newNode === null || newNode === void 0 ? void 0 : newNode.count) || 0;
94
+ const inserted = super.add(newNode);
95
+ if (inserted) {
96
+ this._count += orgNodeCount;
145
97
  }
146
- if (inserted)
147
- this._balancePath(inserted);
148
98
  return inserted;
149
99
  }
150
100
  /**
151
- * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
101
+ * Time Complexity: O(k log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
152
102
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
153
103
  */
154
104
  /**
155
- * Time Complexity: O(k log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each.
105
+ * Time Complexity: O(k log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
156
106
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
157
107
  *
158
- * The function `addMany` takes an array of keys or nodes and adds them to the TreeMultimap,
159
- * returning an array of the inserted nodes.
160
- * @param {(BTNKey | N | undefined)[]} keysOrNodes - An array of keys or nodes. Each element can be
161
- * of type BTNKey, N, or undefined.
162
- * @param {V[]} [data] - The `data` parameter is an optional array of values that correspond to the
163
- * keys or nodes being added. It is used to associate data with each key or node being added to the
164
- * TreeMultimap. If provided, the length of the `data` array should be the same as the length of the
165
- * @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
108
+ * The function overrides the addMany method to add multiple keys, nodes, or entries to a data
109
+ * structure.
110
+ * @param keysOrNodesOrEntries - The parameter `keysOrNodesOrEntries` is an iterable that can contain
111
+ * either keys, nodes, or entries.
112
+ * @returns The method is returning an array of type `N | undefined`.
166
113
  */
167
- addMany(keysOrNodes, data) {
168
- const inserted = [];
169
- for (let i = 0; i < keysOrNodes.length; i++) {
170
- const keyOrNode = keysOrNodes[i];
171
- if (keyOrNode instanceof TreeMultimapNode) {
172
- inserted.push(this.add(keyOrNode.key, keyOrNode.value, keyOrNode.count));
173
- continue;
174
- }
175
- if (keyOrNode === undefined) {
176
- inserted.push(this.add(NaN, undefined, 0));
177
- continue;
178
- }
179
- inserted.push(this.add(keyOrNode, data === null || data === void 0 ? void 0 : data[i], 1));
180
- }
181
- return inserted;
114
+ addMany(keysOrNodesOrEntries) {
115
+ return super.addMany(keysOrNodesOrEntries);
182
116
  }
183
117
  /**
184
118
  * Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
@@ -206,7 +140,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
206
140
  return;
207
141
  const m = l + Math.floor((r - l) / 2);
208
142
  const midNode = sorted[m];
209
- this.add(midNode.key, midNode.value, midNode.count);
143
+ this.add([midNode.key, midNode.value], midNode.count);
210
144
  buildBalanceBST(l, m - 1);
211
145
  buildBalanceBST(m + 1, r);
212
146
  };
@@ -222,7 +156,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
222
156
  if (l <= r) {
223
157
  const m = l + Math.floor((r - l) / 2);
224
158
  const midNode = sorted[m];
225
- this.add(midNode.key, midNode.value, midNode.count);
159
+ this.add([midNode.key, midNode.value], midNode.count);
226
160
  stack.push([m + 1, r]);
227
161
  stack.push([l, m - 1]);
228
162
  }
@@ -289,7 +223,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
289
223
  const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : undefined;
290
224
  if (leftSubTreeRightMost) {
291
225
  const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
292
- orgCurrent = this._swap(curr, leftSubTreeRightMost);
226
+ orgCurrent = this._swapProperties(curr, leftSubTreeRightMost);
293
227
  if (parentOfLeftSubTreeMax) {
294
228
  if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost) {
295
229
  parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
@@ -323,23 +257,6 @@ class TreeMultimap extends avl_tree_1.AVLTree {
323
257
  super.clear();
324
258
  this._count = 0;
325
259
  }
326
- /**
327
- * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (AVLTree) has logarithmic time complexity.
328
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
329
- */
330
- init(elements) {
331
- if (elements) {
332
- for (const entryOrKey of elements) {
333
- if (Array.isArray(entryOrKey)) {
334
- const [key, value] = entryOrKey;
335
- this.add(key, value);
336
- }
337
- else {
338
- this.add(entryOrKey);
339
- }
340
- }
341
- }
342
- }
343
260
  /**
344
261
  * Time Complexity: O(1) - constant time, as it performs basic pointer assignments.
345
262
  * Space Complexity: O(1) - constant space, as it only uses a constant amount of memory.
@@ -356,7 +273,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
356
273
  * added, or `undefined` if no node was added.
357
274
  */
358
275
  _addTo(newNode, parent) {
359
- parent = this.ensureNotKey(parent);
276
+ parent = this.ensureNode(parent);
360
277
  if (parent) {
361
278
  if (parent.left === undefined) {
362
279
  parent.left = newNode;
@@ -383,7 +300,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
383
300
  }
384
301
  }
385
302
  /**
386
- * The `_swap` function swaps the key, value, count, and height properties between two nodes.
303
+ * The `_swapProperties` function swaps the key, value, count, and height properties between two nodes.
387
304
  * @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node from
388
305
  * which the values will be swapped. It can be of type `BTNKey`, `N`, or `undefined`.
389
306
  * @param {BTNKey | N | undefined} destNode - The `destNode` parameter represents the destination
@@ -391,9 +308,9 @@ class TreeMultimap extends avl_tree_1.AVLTree {
391
308
  * @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
392
309
  * if either `srcNode` or `destNode` is undefined.
393
310
  */
394
- _swap(srcNode, destNode) {
395
- srcNode = this.ensureNotKey(srcNode);
396
- destNode = this.ensureNotKey(destNode);
311
+ _swapProperties(srcNode, destNode) {
312
+ srcNode = this.ensureNode(srcNode);
313
+ destNode = this.ensureNode(destNode);
397
314
  if (srcNode && destNode) {
398
315
  const { key, value, count, height } = destNode;
399
316
  const tempNode = this.createNode(key, value, count);
@@ -412,5 +329,9 @@ class TreeMultimap extends avl_tree_1.AVLTree {
412
329
  }
413
330
  return undefined;
414
331
  }
332
+ _replaceNode(oldNode, newNode) {
333
+ newNode.count = oldNode.count + newNode.count;
334
+ return super._replaceNode(oldNode, newNode);
335
+ }
415
336
  }
416
337
  exports.TreeMultimap = TreeMultimap;
@@ -14,12 +14,7 @@ export declare class HashMap<K = any, V = any> {
14
14
  protected readonly _sentinel: HashMapLinkedNode<K, V | undefined>;
15
15
  protected _hashFn: (key: K) => string;
16
16
  protected _objHashFn: (key: K) => object;
17
- /**
18
- * The constructor initializes a HashMapLinkedNode with an optional iterable of key-value pairs.
19
- * @param options - The `options` parameter is an object that contains the `elements` property. The
20
- * `elements` property is an iterable that contains key-value pairs represented as arrays `[K, V]`.
21
- */
22
- constructor(options?: HashMapOptions<K, V>);
17
+ constructor(elements?: Iterable<[K, V]>, options?: HashMapOptions<K>);
23
18
  protected _size: number;
24
19
  get size(): number;
25
20
  /**
@@ -172,6 +167,7 @@ export declare class HashMap<K = any, V = any> {
172
167
  * The above function is an iterator that yields key-value pairs from a linked list.
173
168
  */
174
169
  [Symbol.iterator](): Generator<[K, V], void, unknown>;
170
+ print(): void;
175
171
  /**
176
172
  * Time Complexity: O(1)
177
173
  * Space Complexity: O(1)
@@ -10,13 +10,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
10
10
  exports.HashMap = void 0;
11
11
  const utils_1 = require("../../utils");
12
12
  class HashMap {
13
- /**
14
- * The constructor initializes a HashMapLinkedNode with an optional iterable of key-value pairs.
15
- * @param options - The `options` parameter is an object that contains the `elements` property. The
16
- * `elements` property is an iterable that contains key-value pairs represented as arrays `[K, V]`.
17
- */
18
- constructor(options = {
19
- elements: [],
13
+ constructor(elements, options = {
20
14
  hashFn: (key) => String(key),
21
15
  objHashFn: (key) => key
22
16
  }) {
@@ -25,7 +19,7 @@ class HashMap {
25
19
  this._size = 0;
26
20
  this._sentinel = {};
27
21
  this._sentinel.prev = this._sentinel.next = this._head = this._tail = this._sentinel;
28
- const { elements, hashFn, objHashFn } = options;
22
+ const { hashFn, objHashFn } = options;
29
23
  this._hashFn = hashFn;
30
24
  this._objHashFn = objHashFn;
31
25
  if (elements) {
@@ -347,6 +341,9 @@ class HashMap {
347
341
  node = node.next;
348
342
  }
349
343
  }
344
+ print() {
345
+ console.log([...this]);
346
+ }
350
347
  /**
351
348
  * Time Complexity: O(1)
352
349
  * Space Complexity: O(1)
@@ -20,6 +20,8 @@ export declare class TrieNode {
20
20
  */
21
21
  export declare class Trie {
22
22
  constructor(words?: string[], caseSensitive?: boolean);
23
+ protected _size: number;
24
+ get size(): number;
23
25
  protected _caseSensitive: boolean;
24
26
  get caseSensitive(): boolean;
25
27
  protected _root: TrieNode;
@@ -145,6 +147,7 @@ export declare class Trie {
145
147
  filter(predicate: (word: string, index: number, trie: this) => boolean): string[];
146
148
  map(callback: (word: string, index: number, trie: this) => string): Trie;
147
149
  reduce<T>(callback: (accumulator: T, word: string, index: number, trie: this) => T, initialValue: T): T;
150
+ print(): void;
148
151
  /**
149
152
  * Time Complexity: O(M), where M is the length of the input string.
150
153
  * Space Complexity: O(1) - Constant space.
@@ -27,12 +27,16 @@ class Trie {
27
27
  constructor(words, caseSensitive = true) {
28
28
  this._root = new TrieNode('');
29
29
  this._caseSensitive = caseSensitive;
30
+ this._size = 0;
30
31
  if (words) {
31
- for (const i of words) {
32
- this.add(i);
32
+ for (const word of words) {
33
+ this.add(word);
33
34
  }
34
35
  }
35
36
  }
37
+ get size() {
38
+ return this._size;
39
+ }
36
40
  get caseSensitive() {
37
41
  return this._caseSensitive;
38
42
  }
@@ -54,6 +58,7 @@ class Trie {
54
58
  add(word) {
55
59
  word = this._caseProcess(word);
56
60
  let cur = this.root;
61
+ let isNewWord = false;
57
62
  for (const c of word) {
58
63
  let nodeC = cur.children.get(c);
59
64
  if (!nodeC) {
@@ -62,8 +67,12 @@ class Trie {
62
67
  }
63
68
  cur = nodeC;
64
69
  }
65
- cur.isEnd = true;
66
- return true;
70
+ if (!cur.isEnd) {
71
+ isNewWord = true;
72
+ cur.isEnd = true;
73
+ this._size++;
74
+ }
75
+ return isNewWord;
67
76
  }
68
77
  /**
69
78
  * Time Complexity: O(M), where M is the length of the input word.
@@ -130,6 +139,9 @@ class Trie {
130
139
  return false;
131
140
  };
132
141
  dfs(this.root, 0);
142
+ if (isDeleted) {
143
+ this._size--;
144
+ }
133
145
  return isDeleted;
134
146
  }
135
147
  /**
@@ -352,6 +364,9 @@ class Trie {
352
364
  }
353
365
  return accumulator;
354
366
  }
367
+ print() {
368
+ console.log([...this]);
369
+ }
355
370
  /**
356
371
  * Time Complexity: O(M), where M is the length of the input string.
357
372
  * Space Complexity: O(1) - Constant space.
@@ -1,9 +1,9 @@
1
1
  import { BinaryTree, BinaryTreeNode } from '../data-structures';
2
- import { BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BiTreeDeleteResult, BTNCallback, BTNKey, IterableEntriesOrKeys } from '../types';
2
+ import { BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BiTreeDeleteResult, BTNCallback, BTNKey, BTNodeExemplar } from '../types';
3
3
  export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>, TREE extends BinaryTree<V, N, TREE> = BinaryTreeNested<V, N>> {
4
4
  createNode(key: BTNKey, value?: N['value']): N;
5
5
  createTree(options?: Partial<BinaryTreeOptions>): TREE;
6
- init(elements: IterableEntriesOrKeys<V>): void;
7
- add(keyOrNode: BTNKey | N | null, value?: N['value']): N | null | undefined;
6
+ add(keyOrNodeOrEntry: BTNodeExemplar<V, N>, count?: number): N | null | undefined;
7
+ addMany(nodes: Iterable<BTNodeExemplar<V, N>>): (N | null | undefined)[];
8
8
  delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BiTreeDeleteResult<N>[];
9
9
  }
@@ -19,4 +19,9 @@ export type BinaryTreePrintOptions = {
19
19
  isShowNull?: boolean;
20
20
  isShowRedBlackNIL?: boolean;
21
21
  };
22
- export type IterableEntriesOrKeys<T> = Iterable<[BTNKey, T | undefined] | BTNKey>;
22
+ export type BTNodeEntry<T> = [BTNKey | null | undefined, T | undefined];
23
+ export type BTNodeKeyOrNode<N> = BTNKey | null | undefined | N;
24
+ export type BTNodeExemplar<T, N> = BTNodeEntry<T> | BTNodeKeyOrNode<N>;
25
+ export type BTNodePureExemplar<T, N> = [BTNKey, T | undefined] | BTNodePureKeyOrNode<N>;
26
+ export type BTNodePureKeyOrNode<N> = BTNKey | N;
27
+ export type BSTNodeKeyOrNode<N> = BTNKey | undefined | N;
@@ -4,8 +4,7 @@ export type HashMapLinkedNode<K, V> = {
4
4
  next: HashMapLinkedNode<K, V>;
5
5
  prev: HashMapLinkedNode<K, V>;
6
6
  };
7
- export type HashMapOptions<K, V> = {
8
- elements: Iterable<[K, V]>;
7
+ export type HashMapOptions<K> = {
9
8
  hashFn: (key: K) => string;
10
9
  objHashFn: (key: K) => object;
11
10
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "priority-queue-typed",
3
- "version": "1.47.6",
3
+ "version": "1.47.7",
4
4
  "description": "Priority Queue, Min Priority Queue, Max Priority Queue. Javascript & Typescript Data Structure.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -120,6 +120,6 @@
120
120
  "typedoc": "^0.25.1"
121
121
  },
122
122
  "dependencies": {
123
- "data-structure-typed": "^1.47.6"
123
+ "data-structure-typed": "^1.47.7"
124
124
  }
125
125
  }
@@ -6,8 +6,16 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import { BST, BSTNode } from './bst';
9
- import type { AVLTreeNested, AVLTreeNodeNested, AVLTreeOptions, BiTreeDeleteResult, BTNKey } from '../../types';
10
- import { BTNCallback, IterableEntriesOrKeys } from '../../types';
9
+ import type {
10
+ AVLTreeNested,
11
+ AVLTreeNodeNested,
12
+ AVLTreeOptions,
13
+ BiTreeDeleteResult,
14
+ BSTNodeKeyOrNode,
15
+ BTNKey,
16
+ BTNodeExemplar
17
+ } from '../../types';
18
+ import { BTNCallback } from '../../types';
11
19
  import { IBinaryTree } from '../../interfaces';
12
20
 
13
21
  export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNested<V>> extends BSTNode<V, N> {
@@ -19,19 +27,32 @@ export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNeste
19
27
  }
20
28
  }
21
29
 
30
+ /**
31
+ * 1. Height-Balanced: Each node's left and right subtrees differ in height by no more than one.
32
+ * 2. Automatic Rebalancing: AVL trees rebalance themselves automatically during insertions and deletions.
33
+ * 3. Rotations for Balancing: Utilizes rotations (single or double) to maintain balance after updates.
34
+ * 4. Order Preservation: Maintains the binary search tree property where left child values are less than the parent, and right child values are greater.
35
+ * 5. Efficient Lookups: Offers O(log n) search time, where 'n' is the number of nodes, due to its balanced nature.
36
+ * 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
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
+ * 8. Memory Overhead: Stores balance factors (or heights) at each node, leading to slightly higher memory usage compared to a regular BST.
39
+ */
22
40
  export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTreeNodeNested<V>>, TREE extends AVLTree<V, N, TREE> = AVLTree<V, N, AVLTreeNested<V, N>>>
23
41
  extends BST<V, N, TREE>
24
42
  implements IBinaryTree<V, N, TREE> {
25
43
 
26
44
  /**
27
- * This is a constructor function for an AVL tree data structure in TypeScript.
28
- * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
29
- * constructor of the AVLTree class. It allows you to customize the behavior of the AVL tree by providing different
30
- * options.
45
+ * The constructor function initializes an AVLTree object with optional elements and options.
46
+ * @param [elements] - The `elements` parameter is an optional iterable of `BTNodeExemplar<V, N>`
47
+ * objects. It represents a collection of elements that will be added to the AVL tree during
48
+ * initialization.
49
+ * @param [options] - The `options` parameter is an optional object that allows you to customize the
50
+ * behavior of the AVL tree. It is of type `Partial<AVLTreeOptions>`, which means that you can
51
+ * provide only a subset of the properties defined in the `AVLTreeOptions` interface.
31
52
  */
32
- constructor(elements?: IterableEntriesOrKeys<V>, options?: Partial<AVLTreeOptions>) {
53
+ constructor(elements?: Iterable<BTNodeExemplar<V, N>>, options?: Partial<AVLTreeOptions>) {
33
54
  super([], options);
34
- if (elements) this.init(elements);
55
+ if (elements) super.addMany(elements);
35
56
  }
36
57
 
37
58
  /**
@@ -47,6 +68,13 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
47
68
  return new AVLTreeNode<V, N>(key, value) as N;
48
69
  }
49
70
 
71
+ /**
72
+ * The function creates a new AVL tree with the specified options and returns it.
73
+ * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be
74
+ * passed to the `createTree` function. It is used to customize the behavior of the AVL tree that is
75
+ * being created.
76
+ * @returns a new AVLTree object.
77
+ */
50
78
  override createTree(options?: AVLTreeOptions): TREE {
51
79
  return new AVLTree<V, N, TREE>([], {
52
80
  iterationType: this.iterationType,
@@ -54,21 +82,24 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
54
82
  }) as TREE;
55
83
  }
56
84
 
85
+ /**
86
+ * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
87
+ * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
88
+ */
89
+
57
90
  /**
58
91
  * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
59
92
  * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
60
93
  *
61
- * The function overrides the add method of a class, adds a key-value pair to a data structure, and
62
- * balances the structure if necessary.
63
- * @param {BTNKey | N | null | undefined} keyOrNode - The `keyOrNode` parameter can be of type
64
- * `BTNKey`, `N`, `null`, or `undefined`.
65
- * @param {V} [value] - The `value` parameter is the value associated with the key that is being
66
- * added to the binary search tree.
67
- * @returns The method is returning either a node (N) or undefined.
94
+ * The function overrides the add method of a binary tree node and balances the tree after inserting
95
+ * a new node.
96
+ * @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be either a key, a node, or an
97
+ * entry.
98
+ * @returns The method is returning either the inserted node or `undefined`.
68
99
  */
69
- override add(keyOrNode: BTNKey | N | null | undefined, value?: V): N | undefined {
70
- if (keyOrNode === null) return undefined;
71
- const inserted = super.add(keyOrNode, value);
100
+ override add(keyOrNodeOrEntry: BTNodeExemplar<V, N>): N | undefined {
101
+ if (keyOrNodeOrEntry === null) return undefined;
102
+ const inserted = super.add(keyOrNodeOrEntry);
72
103
  if (inserted) this._balancePath(inserted);
73
104
  return inserted;
74
105
  }
@@ -107,26 +138,9 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
107
138
  return deletedResults;
108
139
  }
109
140
 
110
- /**
111
- * Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The delete method of the superclass (BST) has logarithmic time complexity.
112
- * Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
113
- */
114
-
115
- init(elements: IterableEntriesOrKeys<V>): void {
116
- if (elements) {
117
- for (const entryOrKey of elements) {
118
- if (Array.isArray(entryOrKey)) {
119
- const [key, value] = entryOrKey;
120
- this.add(key, value);
121
- } else {
122
- this.add(entryOrKey);
123
- }
124
- }
125
- }
126
- }
127
141
 
128
142
  /**
129
- * The `_swap` function swaps the key, value, and height properties between two nodes in a binary
143
+ * The `_swapProperties` function swaps the key, value, and height properties between two nodes in a binary
130
144
  * tree.
131
145
  * @param {BTNKey | N | undefined} srcNode - The `srcNode` parameter represents the source node that
132
146
  * needs to be swapped with the destination node. It can be of type `BTNKey`, `N`, or `undefined`.
@@ -135,9 +149,9 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
135
149
  * @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
136
150
  * if either `srcNode` or `destNode` is undefined.
137
151
  */
138
- protected override _swap(srcNode: BTNKey | N | undefined, destNode: BTNKey | N | undefined): N | undefined {
139
- srcNode = this.ensureNotKey(srcNode);
140
- destNode = this.ensureNotKey(destNode);
152
+ protected override _swapProperties(srcNode: BSTNodeKeyOrNode<N>, destNode: BSTNodeKeyOrNode<N>): N | undefined {
153
+ srcNode = this.ensureNode(srcNode);
154
+ destNode = this.ensureNode(destNode);
141
155
 
142
156
  if (srcNode && destNode) {
143
157
  const { key, value, height } = destNode;
@@ -450,4 +464,10 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
450
464
  B && this._updateHeight(B);
451
465
  C && this._updateHeight(C);
452
466
  }
467
+
468
+ protected _replaceNode(oldNode: N, newNode: N): N {
469
+ newNode.height = oldNode.height;
470
+
471
+ return super._replaceNode(oldNode, newNode)
472
+ }
453
473
  }