min-heap-typed 1.42.6 → 1.42.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.
@@ -6,7 +6,7 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import type {BTNKey, TreeMultimapNodeNested, TreeMultimapOptions} from '../../types';
9
- import {BinaryTreeDeletedResult, BTNCallback, CP, FamilyPosition, IterationType} from '../../types';
9
+ import {BiTreeDeleteResult, BTNCallback, CP, FamilyPosition, IterationType} from '../../types';
10
10
  import {IBinaryTree} from '../../interfaces';
11
11
  import {AVLTree, AVLTreeNode} from './avl-tree';
12
12
 
@@ -37,8 +37,7 @@ export class TreeMultimapNode<
37
37
  */
38
38
  export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultimapNode<V, TreeMultimapNodeNested<V>>>
39
39
  extends AVLTree<V, N>
40
- implements IBinaryTree<V, N>
41
- {
40
+ implements IBinaryTree<V, N> {
42
41
  /**
43
42
  * The constructor function for a TreeMultimap class in TypeScript, which extends another class and sets an option to
44
43
  * merge duplicated values.
@@ -82,8 +81,8 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
82
81
  * @returns The function `add` returns a value of type `N | undefined | undefined`.
83
82
  */
84
83
  override add(keyOrNode: BTNKey | N | null | undefined, value?: V, count = 1): N | undefined {
85
- if(keyOrNode === null) return undefined;
86
- let inserted: N | undefined = undefined,
84
+ if (keyOrNode === null) return undefined;
85
+ let inserted: N | undefined = undefined,
87
86
  newNode: N | undefined;
88
87
  if (keyOrNode instanceof TreeMultimapNode) {
89
88
  newNode = this.createNode(keyOrNode.key, keyOrNode.value, keyOrNode.count);
@@ -95,7 +94,7 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
95
94
  if (!this.root) {
96
95
  this._setRoot(newNode);
97
96
  this._size = this.size + 1;
98
- newNode && this._setCount(this.count + newNode.count);
97
+ if (newNode) this._count += newNode.count;
99
98
  inserted = this.root;
100
99
  } else {
101
100
  let cur = this.root;
@@ -106,7 +105,7 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
106
105
  if (this._compare(cur.key, newNode.key) === CP.eq) {
107
106
  cur.value = newNode.value;
108
107
  cur.count += newNode.count;
109
- this._setCount(this.count + newNode.count);
108
+ this._count += newNode.count;
110
109
  traversing = false;
111
110
  inserted = cur;
112
111
  } else if (this._compare(cur.key, newNode.key) === CP.gt) {
@@ -115,7 +114,7 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
115
114
  //Add to the left of the current node
116
115
  cur.left = newNode;
117
116
  this._size = this.size + 1;
118
- this._setCount(this.count + newNode.count);
117
+ this._count += newNode.count;
119
118
 
120
119
  traversing = false;
121
120
  inserted = cur.left;
@@ -129,7 +128,7 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
129
128
  //Add to the right of the current node
130
129
  cur.right = newNode;
131
130
  this._size = this.size + 1;
132
- this._setCount(this.count + newNode.count);
131
+ this._count += newNode.count;
133
132
 
134
133
  traversing = false;
135
134
  inserted = cur.right;
@@ -158,13 +157,14 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
158
157
  * be added as a child.
159
158
  * @returns The method `_addTo` returns either the `parent.left`, `parent.right`, or `undefined`.
160
159
  */
161
- override _addTo(newNode: N | undefined, parent: N): N | undefined {
160
+ protected override _addTo(newNode: N | undefined, parent: BTNKey | N | undefined): N | undefined {
161
+ parent = this.ensureNotKey(parent);
162
162
  if (parent) {
163
163
  if (parent.left === undefined) {
164
164
  parent.left = newNode;
165
165
  if (newNode !== undefined) {
166
166
  this._size = this.size + 1;
167
- this._setCount(this.count + newNode.count);
167
+ this._count += newNode.count;
168
168
  }
169
169
 
170
170
  return parent.left;
@@ -172,7 +172,7 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
172
172
  parent.right = newNode;
173
173
  if (newNode !== undefined) {
174
174
  this._size = this.size + 1;
175
- this._setCount(this.count + newNode.count);
175
+ this._count += newNode.count;
176
176
  }
177
177
  return parent.right;
178
178
  } else {
@@ -193,8 +193,8 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
193
193
  * each key or node.
194
194
  * @returns The function `addMany` returns an array of `N`, `undefined`, or `undefined` values.
195
195
  */
196
- override addMany(keysOrNodes: (BTNKey | undefined)[] | (N | undefined)[], data?: V[]): (N | undefined)[] {
197
- const inserted: (N | undefined | undefined)[] = [];
196
+ override addMany(keysOrNodes: (BTNKey | N | undefined)[], data?: V[]): (N | undefined)[] {
197
+ const inserted: (N | undefined)[] = [];
198
198
 
199
199
  for (let i = 0; i < keysOrNodes.length; i++) {
200
200
  const keyOrNode = keysOrNodes[i];
@@ -269,31 +269,30 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
269
269
  * @param callback - The `callback` parameter is a function that takes a node as input and returns a
270
270
  * value. This value is compared with the `identifier` parameter to determine if the node should be
271
271
  * included in the result. The `callback` parameter has a default value of
272
- * `this.defaultOneParamCallback`
272
+ * `this._defaultOneParamCallback`
273
273
  * @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
274
274
  * being deleted. If set to true, the count of the node will not be considered and the node will be
275
275
  * deleted regardless of its count. If set to false (default), the count of the node will be
276
276
  * decremented by 1 and
277
- * @returns The method `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
277
+ * @returns The method `delete` returns an array of `BiTreeDeleteResult<N>` objects.
278
278
  */
279
279
  override delete<C extends BTNCallback<N>>(
280
280
  identifier: ReturnType<C>,
281
- callback: C = this.defaultOneParamCallback as C,
281
+ callback: C = this._defaultOneParamCallback as C,
282
282
  ignoreCount = false
283
- ): BinaryTreeDeletedResult<N>[] {
284
- const bstDeletedResult: BinaryTreeDeletedResult<N>[] = [];
285
- if (!this.root) return bstDeletedResult;
283
+ ): BiTreeDeleteResult<N>[] {
284
+ const deletedResult: BiTreeDeleteResult<N>[] = [];
285
+ if (!this.root) return deletedResult;
286
286
 
287
287
  const curr: N | undefined = this.getNode(identifier, callback) ?? undefined;
288
- if (!curr) return bstDeletedResult;
288
+ if (!curr) return deletedResult;
289
289
 
290
290
  const parent: N | undefined = curr?.parent ? curr.parent : undefined;
291
- let needBalanced: N | undefined = undefined,
292
- orgCurrent = curr;
291
+ let needBalanced: N | undefined = undefined,orgCurrent: N | undefined = curr;
293
292
 
294
293
  if (curr.count > 1 && !ignoreCount) {
295
294
  curr.count--;
296
- this._setCount(this.count - 1);
295
+ this._count--;
297
296
  } else {
298
297
  if (!curr.left) {
299
298
  if (!parent) {
@@ -324,24 +323,24 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
324
323
  }
325
324
  this._size = this.size - 1;
326
325
  // TODO How to handle when the count of target node is lesser than current node's count
327
- this._setCount(this.count - orgCurrent.count);
326
+ if (orgCurrent) this._count -= orgCurrent.count;
328
327
  }
329
328
 
330
- bstDeletedResult.push({deleted: orgCurrent, needBalanced});
329
+ deletedResult.push({deleted: orgCurrent, needBalanced});
331
330
 
332
331
  if (needBalanced) {
333
332
  this._balancePath(needBalanced);
334
333
  }
335
334
 
336
- return bstDeletedResult;
335
+ return deletedResult;
337
336
  }
338
337
 
339
338
  /**
340
339
  * The clear() function clears the contents of a data structure and sets the count to zero.
341
340
  */
342
- clear() {
341
+ override clear() {
343
342
  super.clear();
344
- this._setCount(0);
343
+ this._count = 0;
345
344
  }
346
345
 
347
346
  /**
@@ -351,31 +350,28 @@ export class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultim
351
350
  * from `srcNode` will be swapped into.
352
351
  * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
353
352
  */
354
- protected override _swap(srcNode: N, destNode: N): N {
355
- const {key, value, count, height} = destNode;
356
- const tempNode = this.createNode(key, value, count);
357
- if (tempNode) {
358
- tempNode.height = height;
359
-
360
- destNode.key = srcNode.key;
361
- destNode.value = srcNode.value;
362
- destNode.count = srcNode.count;
363
- destNode.height = srcNode.height;
364
-
365
- srcNode.key = tempNode.key;
366
- srcNode.value = tempNode.value;
367
- srcNode.count = tempNode.count;
368
- srcNode.height = tempNode.height;
369
- }
370
-
371
- return destNode;
372
- }
353
+ protected _swap(srcNode: BTNKey | N | undefined, destNode:BTNKey | N | undefined): N | undefined{
354
+ srcNode = this.ensureNotKey(srcNode);
355
+ destNode = this.ensureNotKey(destNode);
356
+ if (srcNode && destNode) {
357
+ const {key, value, count, height} = destNode;
358
+ const tempNode = this.createNode(key, value, count);
359
+ if (tempNode) {
360
+ tempNode.height = height;
361
+
362
+ destNode.key = srcNode.key;
363
+ destNode.value = srcNode.value;
364
+ destNode.count = srcNode.count;
365
+ destNode.height = srcNode.height;
366
+
367
+ srcNode.key = tempNode.key;
368
+ srcNode.value = tempNode.value;
369
+ srcNode.count = tempNode.count;
370
+ srcNode.height = tempNode.height;
371
+ }
373
372
 
374
- /**
375
- * The function sets the value of the "_count" property.
376
- * @param {number} v - number
377
- */
378
- protected _setCount(v: number) {
379
- this._count = v;
373
+ return destNode;
374
+ }
375
+ return undefined;
380
376
  }
381
377
  }
@@ -1,10 +1,10 @@
1
1
  import {BinaryTreeNode} from '../data-structures';
2
- import {BinaryTreeDeletedResult, BinaryTreeNodeNested, BTNCallback, BTNKey} from '../types';
2
+ import {BiTreeDeleteResult, BinaryTreeNodeNested, BTNCallback, BTNKey} from '../types';
3
3
 
4
4
  export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>> {
5
5
  createNode(key: BTNKey, value?: N['value']): N;
6
6
 
7
7
  add(keyOrNode: BTNKey | N | null, value?: N['value']): N | null | undefined;
8
8
 
9
- delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeletedResult<N>[];
9
+ delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BiTreeDeleteResult<N>[];
10
10
  }
@@ -24,8 +24,14 @@ export enum FamilyPosition {
24
24
 
25
25
  export type BTNKey = number;
26
26
 
27
- export type BinaryTreeDeletedResult<N> = { deleted: N | null | undefined; needBalanced: N | null | undefined };
27
+ export type BiTreeDeleteResult<N> = { deleted: N | null | undefined; needBalanced: N | null | undefined };
28
28
 
29
29
  export type BinaryTreeNodeNested<T> = BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
30
30
 
31
31
  export type BinaryTreeOptions = { iterationType?: IterationType }
32
+ //
33
+ // export type BTNIdentifierOrNU<N> = BTNKey | N | null | undefined;
34
+ //
35
+ // export type BTNIdentifierOrU<N> = BTNKey | N | undefined;
36
+ //
37
+ // export type BTNOrNU<N> = N | null | undefined;
@@ -1,8 +1,8 @@
1
- import {RBTreeNode} from '../../../data-structures';
1
+ import {RedBlackTreeNode} from '../../../data-structures';
2
2
  import {BSTOptions} from "./bst";
3
3
 
4
4
  export enum RBTNColor { RED = 1, BLACK = 0}
5
5
 
6
- export type RBTreeNodeNested<T> = RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, RBTreeNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
6
+ export type RedBlackTreeNodeNested<T> = RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
7
7
 
8
8
  export type RBTreeOptions = BSTOptions & {};