min-heap-typed 1.42.5 → 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, IterationType } from '../../types';
9
+ import { BiTreeDeleteResult, BTNCallback, IterationType } from '../../types';
10
10
  import { IBinaryTree } from '../../interfaces';
11
11
  import { AVLTree, AVLTreeNode } from './avl-tree';
12
12
  export declare class TreeMultimapNode<V = any, N extends TreeMultimapNode<V, N> = TreeMultimapNodeNested<V>> extends AVLTreeNode<V, N> {
@@ -68,7 +68,7 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
68
68
  * be added as a child.
69
69
  * @returns The method `_addTo` returns either the `parent.left`, `parent.right`, or `undefined`.
70
70
  */
71
- _addTo(newNode: N | undefined, parent: N): N | undefined;
71
+ protected _addTo(newNode: N | undefined, parent: BTNKey | N | undefined): N | undefined;
72
72
  /**
73
73
  * The `addMany` function adds multiple keys or nodes to a TreeMultimap and returns an array of the
74
74
  * inserted nodes.
@@ -79,7 +79,7 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
79
79
  * each key or node.
80
80
  * @returns The function `addMany` returns an array of `N`, `undefined`, or `undefined` values.
81
81
  */
82
- addMany(keysOrNodes: (BTNKey | undefined)[] | (N | undefined)[], data?: V[]): (N | undefined)[];
82
+ addMany(keysOrNodes: (BTNKey | N | undefined)[], data?: V[]): (N | undefined)[];
83
83
  /**
84
84
  * The `perfectlyBalance` function in TypeScript takes a sorted array of nodes and builds a balanced
85
85
  * binary search tree using either a recursive or iterative approach.
@@ -98,14 +98,14 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
98
98
  * @param callback - The `callback` parameter is a function that takes a node as input and returns a
99
99
  * value. This value is compared with the `identifier` parameter to determine if the node should be
100
100
  * included in the result. The `callback` parameter has a default value of
101
- * `this.defaultOneParamCallback`
101
+ * `this._defaultOneParamCallback`
102
102
  * @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
103
103
  * being deleted. If set to true, the count of the node will not be considered and the node will be
104
104
  * deleted regardless of its count. If set to false (default), the count of the node will be
105
105
  * decremented by 1 and
106
- * @returns The method `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
106
+ * @returns The method `delete` returns an array of `BiTreeDeleteResult<N>` objects.
107
107
  */
108
- delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback?: C, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[];
108
+ delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback?: C, ignoreCount?: boolean): BiTreeDeleteResult<N>[];
109
109
  /**
110
110
  * The clear() function clears the contents of a data structure and sets the count to zero.
111
111
  */
@@ -117,10 +117,5 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
117
117
  * from `srcNode` will be swapped into.
118
118
  * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
119
119
  */
120
- protected _swap(srcNode: N, destNode: N): N;
121
- /**
122
- * The function sets the value of the "_count" property.
123
- * @param {number} v - number
124
- */
125
- protected _setCount(v: number): void;
120
+ protected _swap(srcNode: BTNKey | N | undefined, destNode: BTNKey | N | undefined): N | undefined;
126
121
  }
@@ -78,7 +78,8 @@ class TreeMultimap extends avl_tree_1.AVLTree {
78
78
  if (!this.root) {
79
79
  this._setRoot(newNode);
80
80
  this._size = this.size + 1;
81
- newNode && this._setCount(this.count + newNode.count);
81
+ if (newNode)
82
+ this._count += newNode.count;
82
83
  inserted = this.root;
83
84
  }
84
85
  else {
@@ -90,7 +91,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
90
91
  if (this._compare(cur.key, newNode.key) === types_1.CP.eq) {
91
92
  cur.value = newNode.value;
92
93
  cur.count += newNode.count;
93
- this._setCount(this.count + newNode.count);
94
+ this._count += newNode.count;
94
95
  traversing = false;
95
96
  inserted = cur;
96
97
  }
@@ -100,7 +101,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
100
101
  //Add to the left of the current node
101
102
  cur.left = newNode;
102
103
  this._size = this.size + 1;
103
- this._setCount(this.count + newNode.count);
104
+ this._count += newNode.count;
104
105
  traversing = false;
105
106
  inserted = cur.left;
106
107
  }
@@ -116,7 +117,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
116
117
  //Add to the right of the current node
117
118
  cur.right = newNode;
118
119
  this._size = this.size + 1;
119
- this._setCount(this.count + newNode.count);
120
+ this._count += newNode.count;
120
121
  traversing = false;
121
122
  inserted = cur.right;
122
123
  }
@@ -149,12 +150,13 @@ class TreeMultimap extends avl_tree_1.AVLTree {
149
150
  * @returns The method `_addTo` returns either the `parent.left`, `parent.right`, or `undefined`.
150
151
  */
151
152
  _addTo(newNode, parent) {
153
+ parent = this.ensureNotKey(parent);
152
154
  if (parent) {
153
155
  if (parent.left === undefined) {
154
156
  parent.left = newNode;
155
157
  if (newNode !== undefined) {
156
158
  this._size = this.size + 1;
157
- this._setCount(this.count + newNode.count);
159
+ this._count += newNode.count;
158
160
  }
159
161
  return parent.left;
160
162
  }
@@ -162,7 +164,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
162
164
  parent.right = newNode;
163
165
  if (newNode !== undefined) {
164
166
  this._size = this.size + 1;
165
- this._setCount(this.count + newNode.count);
167
+ this._count += newNode.count;
166
168
  }
167
169
  return parent.right;
168
170
  }
@@ -253,26 +255,26 @@ class TreeMultimap extends avl_tree_1.AVLTree {
253
255
  * @param callback - The `callback` parameter is a function that takes a node as input and returns a
254
256
  * value. This value is compared with the `identifier` parameter to determine if the node should be
255
257
  * included in the result. The `callback` parameter has a default value of
256
- * `this.defaultOneParamCallback`
258
+ * `this._defaultOneParamCallback`
257
259
  * @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
258
260
  * being deleted. If set to true, the count of the node will not be considered and the node will be
259
261
  * deleted regardless of its count. If set to false (default), the count of the node will be
260
262
  * decremented by 1 and
261
- * @returns The method `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
263
+ * @returns The method `delete` returns an array of `BiTreeDeleteResult<N>` objects.
262
264
  */
263
- delete(identifier, callback = this.defaultOneParamCallback, ignoreCount = false) {
265
+ delete(identifier, callback = this._defaultOneParamCallback, ignoreCount = false) {
264
266
  var _a;
265
- const bstDeletedResult = [];
267
+ const deletedResult = [];
266
268
  if (!this.root)
267
- return bstDeletedResult;
269
+ return deletedResult;
268
270
  const curr = (_a = this.getNode(identifier, callback)) !== null && _a !== void 0 ? _a : undefined;
269
271
  if (!curr)
270
- return bstDeletedResult;
272
+ return deletedResult;
271
273
  const parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent : undefined;
272
274
  let needBalanced = undefined, orgCurrent = curr;
273
275
  if (curr.count > 1 && !ignoreCount) {
274
276
  curr.count--;
275
- this._setCount(this.count - 1);
277
+ this._count--;
276
278
  }
277
279
  else {
278
280
  if (!curr.left) {
@@ -309,20 +311,21 @@ class TreeMultimap extends avl_tree_1.AVLTree {
309
311
  }
310
312
  this._size = this.size - 1;
311
313
  // TODO How to handle when the count of target node is lesser than current node's count
312
- this._setCount(this.count - orgCurrent.count);
314
+ if (orgCurrent)
315
+ this._count -= orgCurrent.count;
313
316
  }
314
- bstDeletedResult.push({ deleted: orgCurrent, needBalanced });
317
+ deletedResult.push({ deleted: orgCurrent, needBalanced });
315
318
  if (needBalanced) {
316
319
  this._balancePath(needBalanced);
317
320
  }
318
- return bstDeletedResult;
321
+ return deletedResult;
319
322
  }
320
323
  /**
321
324
  * The clear() function clears the contents of a data structure and sets the count to zero.
322
325
  */
323
326
  clear() {
324
327
  super.clear();
325
- this._setCount(0);
328
+ this._count = 0;
326
329
  }
327
330
  /**
328
331
  * The function swaps the values of two nodes in a binary tree.
@@ -332,27 +335,25 @@ class TreeMultimap extends avl_tree_1.AVLTree {
332
335
  * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
333
336
  */
334
337
  _swap(srcNode, destNode) {
335
- const { key, value, count, height } = destNode;
336
- const tempNode = this.createNode(key, value, count);
337
- if (tempNode) {
338
- tempNode.height = height;
339
- destNode.key = srcNode.key;
340
- destNode.value = srcNode.value;
341
- destNode.count = srcNode.count;
342
- destNode.height = srcNode.height;
343
- srcNode.key = tempNode.key;
344
- srcNode.value = tempNode.value;
345
- srcNode.count = tempNode.count;
346
- srcNode.height = tempNode.height;
338
+ srcNode = this.ensureNotKey(srcNode);
339
+ destNode = this.ensureNotKey(destNode);
340
+ if (srcNode && destNode) {
341
+ const { key, value, count, height } = destNode;
342
+ const tempNode = this.createNode(key, value, count);
343
+ if (tempNode) {
344
+ tempNode.height = height;
345
+ destNode.key = srcNode.key;
346
+ destNode.value = srcNode.value;
347
+ destNode.count = srcNode.count;
348
+ destNode.height = srcNode.height;
349
+ srcNode.key = tempNode.key;
350
+ srcNode.value = tempNode.value;
351
+ srcNode.count = tempNode.count;
352
+ srcNode.height = tempNode.height;
353
+ }
354
+ return destNode;
347
355
  }
348
- return destNode;
349
- }
350
- /**
351
- * The function sets the value of the "_count" property.
352
- * @param {number} v - number
353
- */
354
- _setCount(v) {
355
- this._count = v;
356
+ return undefined;
356
357
  }
357
358
  }
358
359
  exports.TreeMultimap = TreeMultimap;
@@ -1,7 +1,7 @@
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
  export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>> {
4
4
  createNode(key: BTNKey, value?: N['value']): N;
5
5
  add(keyOrNode: BTNKey | N | null, value?: N['value']): N | null | undefined;
6
- delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeletedResult<N>[];
6
+ delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BiTreeDeleteResult<N>[];
7
7
  }
@@ -19,7 +19,7 @@ export declare enum FamilyPosition {
19
19
  MAL_NODE = "MAL_NODE"
20
20
  }
21
21
  export type BTNKey = number;
22
- export type BinaryTreeDeletedResult<N> = {
22
+ export type BiTreeDeleteResult<N> = {
23
23
  deleted: N | null | undefined;
24
24
  needBalanced: N | null | undefined;
25
25
  };
@@ -22,3 +22,9 @@ var FamilyPosition;
22
22
  FamilyPosition["ISOLATED"] = "ISOLATED";
23
23
  FamilyPosition["MAL_NODE"] = "MAL_NODE";
24
24
  })(FamilyPosition = exports.FamilyPosition || (exports.FamilyPosition = {}));
25
+ //
26
+ // export type BTNIdentifierOrNU<N> = BTNKey | N | null | undefined;
27
+ //
28
+ // export type BTNIdentifierOrU<N> = BTNKey | N | undefined;
29
+ //
30
+ // 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
  export declare enum RBTNColor {
4
4
  RED = 1,
5
5
  BLACK = 0
6
6
  }
7
- 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>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
7
+ 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>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
8
8
  export type RBTreeOptions = BSTOptions & {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "min-heap-typed",
3
- "version": "1.42.5",
3
+ "version": "1.42.7",
4
4
  "description": "Min Heap. Javascript & Typescript Data Structure.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -14,7 +14,7 @@
14
14
  },
15
15
  "repository": {
16
16
  "type": "git",
17
- "url": "git+https://github.com/zrwusa/data-structure-typed"
17
+ "url": "git+https://github.com/zrwusa/data-structure-typed.git"
18
18
  },
19
19
  "keywords": [
20
20
  "Min Heap",
@@ -132,6 +132,6 @@
132
132
  "typescript": "^4.9.5"
133
133
  },
134
134
  "dependencies": {
135
- "data-structure-typed": "^1.42.5"
135
+ "data-structure-typed": "^1.42.7"
136
136
  }
137
137
  }
@@ -6,7 +6,7 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import {BST, BSTNode} from './bst';
9
- import type {AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeletedResult, BTNKey} from '../../types';
9
+ import type {AVLTreeNodeNested, AVLTreeOptions, BiTreeDeleteResult, BTNKey} from '../../types';
10
10
  import {BTNCallback} from '../../types';
11
11
  import {IBinaryTree} from '../../interfaces';
12
12
 
@@ -71,13 +71,13 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
71
71
  * @param callback - The `callback` parameter is a function that takes a node as input and returns a
72
72
  * value. This value is compared with the `identifier` parameter to determine if the node should be
73
73
  * included in the result. The `callback` parameter has a default value of
74
- * `this.defaultOneParamCallback`
75
- * @returns The method is returning an array of `BinaryTreeDeletedResult<N>` objects.
74
+ * `this._defaultOneParamCallback`
75
+ * @returns The method is returning an array of `BiTreeDeleteResult<N>` objects.
76
76
  */
77
77
  override delete<C extends BTNCallback<N>>(
78
78
  identifier: ReturnType<C>,
79
- callback: C = this.defaultOneParamCallback as C
80
- ): BinaryTreeDeletedResult<N>[] {
79
+ callback: C = this._defaultOneParamCallback as C
80
+ ): BiTreeDeleteResult<N>[] {
81
81
  if ((identifier as any) instanceof AVLTreeNode) callback = (node => node) as C;
82
82
  const deletedResults = super.delete(identifier, callback);
83
83
  for (const {needBalanced} of deletedResults) {
@@ -96,23 +96,29 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
96
96
  * from the source node (`srcNode`) will be swapped to.
97
97
  * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
98
98
  */
99
- protected override _swap(srcNode: N, destNode: N): N {
100
- const {key, value, height} = destNode;
101
- const tempNode = this.createNode(key, value);
99
+ protected override _swap(srcNode: BTNKey | N | undefined, destNode: BTNKey | N | undefined): N | undefined {
100
+ srcNode = this.ensureNotKey(srcNode);
101
+ destNode = this.ensureNotKey(destNode);
102
102
 
103
- if (tempNode) {
104
- tempNode.height = height;
103
+ if (srcNode && destNode) {
104
+ const {key, value, height} = destNode;
105
+ const tempNode = this.createNode(key, value);
105
106
 
106
- destNode.key = srcNode.key;
107
- destNode.value = srcNode.value;
108
- destNode.height = srcNode.height;
107
+ if (tempNode) {
108
+ tempNode.height = height;
109
109
 
110
- srcNode.key = tempNode.key;
111
- srcNode.value = tempNode.value;
112
- srcNode.height = tempNode.height;
113
- }
110
+ destNode.key = srcNode.key;
111
+ destNode.value = srcNode.value;
112
+ destNode.height = srcNode.height;
113
+
114
+ srcNode.key = tempNode.key;
115
+ srcNode.value = tempNode.value;
116
+ srcNode.height = tempNode.height;
117
+ }
114
118
 
115
- return destNode;
119
+ return destNode;
120
+ }
121
+ return undefined;
116
122
  }
117
123
 
118
124
  /**