data-structure-typed 1.19.7 → 1.19.9

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 (34) hide show
  1. package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +41 -58
  2. package/dist/data-structures/binary-tree/abstract-binary-tree.js +107 -145
  3. package/dist/data-structures/binary-tree/avl-tree.d.ts +1 -0
  4. package/dist/data-structures/binary-tree/avl-tree.js +4 -0
  5. package/dist/data-structures/binary-tree/binary-tree.d.ts +2 -1
  6. package/dist/data-structures/binary-tree/binary-tree.js +3 -0
  7. package/dist/data-structures/binary-tree/bst.d.ts +1 -0
  8. package/dist/data-structures/binary-tree/bst.js +4 -0
  9. package/dist/data-structures/binary-tree/rb-tree.d.ts +1 -1
  10. package/dist/data-structures/binary-tree/rb-tree.js +2 -3
  11. package/dist/data-structures/binary-tree/tree-multiset.d.ts +22 -16
  12. package/dist/data-structures/binary-tree/tree-multiset.js +138 -122
  13. package/dist/data-structures/heap/heap.d.ts +4 -3
  14. package/dist/data-structures/heap/heap.js +12 -33
  15. package/dist/data-structures/interfaces/abstract-binary-tree.d.ts +6 -9
  16. package/dist/data-structures/types/abstract-binary-tree.d.ts +1 -2
  17. package/dist/data-structures/types/tree-multiset.d.ts +2 -2
  18. package/package.json +22 -5
  19. package/src/data-structures/binary-tree/abstract-binary-tree.ts +112 -161
  20. package/src/data-structures/binary-tree/avl-tree.ts +4 -0
  21. package/src/data-structures/binary-tree/binary-tree.ts +4 -2
  22. package/src/data-structures/binary-tree/bst.ts +4 -1
  23. package/src/data-structures/binary-tree/rb-tree.ts +3 -3
  24. package/src/data-structures/binary-tree/tree-multiset.ts +136 -118
  25. package/src/data-structures/graph/abstract-graph.ts +1 -0
  26. package/src/data-structures/heap/heap.ts +12 -38
  27. package/src/data-structures/interfaces/abstract-binary-tree.ts +6 -43
  28. package/src/data-structures/types/abstract-binary-tree.ts +1 -2
  29. package/src/data-structures/types/tree-multiset.ts +2 -2
  30. package/tsconfig.json +1 -2
  31. package/src/assets/complexities-diff.jpg +0 -0
  32. package/src/assets/data-structure-complexities.jpg +0 -0
  33. package/src/assets/logo.png +0 -0
  34. package/src/assets/overview-diagram-of-data-structures.png +0 -0
@@ -10,6 +10,9 @@ exports.AVLTree = exports.AVLTreeNode = void 0;
10
10
  */
11
11
  const bst_1 = require("./bst");
12
12
  class AVLTreeNode extends bst_1.BSTNode {
13
+ constructor(id, val) {
14
+ super(id, val);
15
+ }
13
16
  }
14
17
  exports.AVLTreeNode = AVLTreeNode;
15
18
  class AVLTree extends bst_1.BST {
@@ -41,6 +44,7 @@ class AVLTree extends bst_1.BST {
41
44
  * @returns The method is returning the inserted node, or null or undefined if the insertion was not successful.
42
45
  */
43
46
  add(id, val) {
47
+ // TODO support node as a param
44
48
  const inserted = super.add(id, val);
45
49
  if (inserted)
46
50
  this.balancePath(inserted);
@@ -7,8 +7,9 @@
7
7
  */
8
8
  import type { BinaryTreeNodeId, BinaryTreeNodeNested, BinaryTreeOptions } from '../types';
9
9
  import { AbstractBinaryTree, AbstractBinaryTreeNode } from './abstract-binary-tree';
10
- import { IBinaryTree, IBinaryTreeNode } from '../interfaces/binary-tree';
10
+ import { IBinaryTree, IBinaryTreeNode } from '../interfaces';
11
11
  export declare class BinaryTreeNode<T = any, NEIGHBOR extends BinaryTreeNode<T, NEIGHBOR> = BinaryTreeNodeNested<T>> extends AbstractBinaryTreeNode<T, NEIGHBOR> implements IBinaryTreeNode<T, NEIGHBOR> {
12
+ constructor(id: BinaryTreeNodeId, val?: T);
12
13
  }
13
14
  export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode> extends AbstractBinaryTree<N> implements IBinaryTree<N> {
14
15
  /**
@@ -10,6 +10,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
10
10
  exports.BinaryTree = exports.BinaryTreeNode = void 0;
11
11
  const abstract_binary_tree_1 = require("./abstract-binary-tree");
12
12
  class BinaryTreeNode extends abstract_binary_tree_1.AbstractBinaryTreeNode {
13
+ constructor(id, val) {
14
+ super(id, val);
15
+ }
13
16
  }
14
17
  exports.BinaryTreeNode = BinaryTreeNode;
15
18
  class BinaryTree extends abstract_binary_tree_1.AbstractBinaryTree {
@@ -10,6 +10,7 @@ import { CP } from '../types';
10
10
  import { BinaryTree, BinaryTreeNode } from './binary-tree';
11
11
  import { IBST, IBSTNode } from '../interfaces';
12
12
  export declare class BSTNode<T = any, NEIGHBOR extends BSTNode<T, NEIGHBOR> = BSTNodeNested<T>> extends BinaryTreeNode<T, NEIGHBOR> implements IBSTNode<T, NEIGHBOR> {
13
+ constructor(id: BinaryTreeNodeId, val?: T);
13
14
  }
14
15
  export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N> implements IBST<N> {
15
16
  /**
@@ -4,6 +4,9 @@ exports.BST = exports.BSTNode = void 0;
4
4
  const types_1 = require("../types");
5
5
  const binary_tree_1 = require("./binary-tree");
6
6
  class BSTNode extends binary_tree_1.BinaryTreeNode {
7
+ constructor(id, val) {
8
+ super(id, val);
9
+ }
7
10
  }
8
11
  exports.BSTNode = BSTNode;
9
12
  class BST extends binary_tree_1.BinaryTree {
@@ -42,6 +45,7 @@ class BST extends binary_tree_1.BinaryTree {
42
45
  * If the node was not added (e.g., due to a duplicate ID), it returns `null` or `undefined`.
43
46
  */
44
47
  add(id, val) {
48
+ // TODO support node as a param
45
49
  let inserted = null;
46
50
  const newNode = this.createNode(id, val);
47
51
  if (this.root === null) {
@@ -2,7 +2,7 @@ import { BinaryTreeNodeId, RBColor, RBTreeNodeNested, RBTreeOptions } from '../t
2
2
  import { IRBTree, IRBTreeNode } from '../interfaces/rb-tree';
3
3
  import { BST, BSTNode } from './bst';
4
4
  export declare class RBTreeNode<T = any, NEIGHBOR extends RBTreeNode<T, NEIGHBOR> = RBTreeNodeNested<T>> extends BSTNode<T, NEIGHBOR> implements IRBTreeNode<T, NEIGHBOR> {
5
- constructor(id: BinaryTreeNodeId, color: RBColor, val?: T);
5
+ constructor(id: BinaryTreeNodeId, val?: T, color?: RBColor);
6
6
  private _color;
7
7
  get color(): RBColor;
8
8
  set color(value: RBColor);
@@ -4,9 +4,8 @@ exports.RBTree = exports.RBTreeNode = void 0;
4
4
  const types_1 = require("../types");
5
5
  const bst_1 = require("./bst");
6
6
  class RBTreeNode extends bst_1.BSTNode {
7
- constructor(id, color, val) {
7
+ constructor(id, val, color = types_1.RBColor.RED) {
8
8
  super(id, val);
9
- this._color = types_1.RBColor.RED;
10
9
  this._color = color;
11
10
  }
12
11
  get color() {
@@ -22,7 +21,7 @@ class RBTree extends bst_1.BST {
22
21
  super(options);
23
22
  }
24
23
  createNode(id, val) {
25
- return new RBTreeNode(id, types_1.RBColor.RED, val);
24
+ return new RBTreeNode(id, val, types_1.RBColor.RED);
26
25
  }
27
26
  // private override _root: BinaryTreeNode<N> | null = null;
28
27
  //
@@ -57,17 +57,16 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
57
57
  */
58
58
  swapLocation(srcNode: N, destNode: N): N;
59
59
  /**
60
- * The `add` function adds a new node to a binary tree, updating the size and count properties accordingly, and
61
- * balancing the tree if necessary.
62
- * @param {BinaryTreeNodeId} id - The id parameter represents the identifier of the binary tree node that we want to
63
- * add. It is of type BinaryTreeNodeId.
64
- * @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. If no value is
65
- * provided, it will default to `undefined`.
66
- * @param {number} [count] - The `count` parameter is an optional parameter that specifies the number of times the node
67
- * with the given `id` should be added to the binary tree. If the `count` parameter is not provided, it defaults to 1.
68
- * @returns The `add` method returns the inserted node (`N`), `null`, or `undefined`.
69
- */
70
- add(id: BinaryTreeNodeId, val?: N['val'], count?: number): N | null | undefined;
60
+ * The `add` function adds a new node to a binary search tree, maintaining the tree's properties and balancing if
61
+ * necessary.
62
+ * @param {BinaryTreeNodeId | N} idOrNode - The `idOrNode` parameter can be either a `BinaryTreeNodeId` or a `N` (which
63
+ * represents a `BinaryTreeNode`).
64
+ * @param [val] - The `val` parameter represents the value to be added to the binary tree node.
65
+ * @param {number} [count] - The `count` parameter is an optional parameter that specifies the number of times the
66
+ * value should be added to the binary tree. If the `count` parameter is not provided, it defaults to 1.
67
+ * @returns The method `add` returns either the inserted node (`N`), `null`, or `undefined`.
68
+ */
69
+ add(idOrNode: BinaryTreeNodeId | N | null, val?: N['val'], count?: number): N | null | undefined;
71
70
  /**
72
71
  * The function adds a new node to a binary tree as the left or right child of a given parent node.
73
72
  * @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to the tree. It can be
@@ -79,13 +78,20 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
79
78
  */
80
79
  addTo(newNode: N | null, parent: N): N | null | undefined;
81
80
  /**
82
- * The `addMany` function inserts multiple items into a binary tree and returns an array of the inserted nodes or
83
- * null/undefined values.
84
- * @param {N[] | N[]} data - The `data` parameter can be either an array of elements of type `N` or an
85
- * array of `N` objects.
81
+ * The `addMany` function adds multiple nodes to a binary tree and returns an array of the inserted nodes.
82
+ * @param {BinaryTreeNodeId[] | N[]} idsOrNodes - An array of BinaryTreeNodeId objects or N objects. These objects
83
+ * represent the IDs or nodes of the binary tree where the values will be added.
84
+ * @param {N['val'][]} [data] - Optional array of values to be associated with each node being added. If provided, the
85
+ * length of the `data` array should be equal to the length of the `idsOrNodes` array.
86
86
  * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
87
87
  */
88
- addMany(data: N[] | Array<N['val']>): (N | null | undefined)[];
88
+ addMany(idsOrNodes: (BinaryTreeNodeId | N)[], data?: N['val'][]): (N | null | undefined)[];
89
+ /**
90
+ * The `perfectlyBalance` function takes a binary tree, performs a depth-first search to sort the nodes, and then
91
+ * constructs a balanced binary search tree using either a recursive or iterative approach.
92
+ * @returns The function `perfectlyBalance()` returns a boolean value.
93
+ */
94
+ perfectlyBalance(): boolean;
89
95
  /**
90
96
  * The `remove` function removes a node from a binary search tree and returns the deleted node along with the parent
91
97
  * node that needs to be balanced.
@@ -16,7 +16,6 @@ class TreeMultisetNode extends avl_tree_1.AVLTreeNode {
16
16
  */
17
17
  constructor(id, val, count = 1) {
18
18
  super(id, val);
19
- this._count = 1;
20
19
  this._count = count;
21
20
  }
22
21
  get count() {
@@ -38,7 +37,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
38
37
  * TreeMultiset.
39
38
  */
40
39
  constructor(options) {
41
- super(Object.assign(Object.assign({}, options), { isMergeDuplicatedVal: true }));
40
+ super(Object.assign(Object.assign({}, options), { isMergeDuplicatedNodeById: true }));
42
41
  this._count = 0;
43
42
  }
44
43
  get count() {
@@ -64,97 +63,98 @@ class TreeMultiset extends avl_tree_1.AVLTree {
64
63
  * @returns the `destNode` after swapping its values with the `srcNode`.
65
64
  */
66
65
  swapLocation(srcNode, destNode) {
67
- const { val, count, height, id } = destNode;
66
+ const { id, val, count, height } = destNode;
68
67
  const tempNode = this.createNode(id, val, count);
69
68
  if (tempNode) {
70
69
  tempNode.height = height;
71
- if (tempNode instanceof TreeMultisetNode) {
72
- destNode.id = srcNode.id;
73
- destNode.val = srcNode.val;
74
- destNode.count = srcNode.count;
75
- destNode.height = srcNode.height;
76
- srcNode.id = tempNode.id;
77
- srcNode.val = tempNode.val;
78
- srcNode.count = tempNode.count;
79
- srcNode.height = tempNode.height;
80
- }
70
+ destNode.id = srcNode.id;
71
+ destNode.val = srcNode.val;
72
+ destNode.count = srcNode.count;
73
+ destNode.height = srcNode.height;
74
+ srcNode.id = tempNode.id;
75
+ srcNode.val = tempNode.val;
76
+ srcNode.count = tempNode.count;
77
+ srcNode.height = tempNode.height;
81
78
  }
82
79
  return destNode;
83
80
  }
84
81
  /**
85
- * The `add` function adds a new node to a binary tree, updating the size and count properties accordingly, and
86
- * balancing the tree if necessary.
87
- * @param {BinaryTreeNodeId} id - The id parameter represents the identifier of the binary tree node that we want to
88
- * add. It is of type BinaryTreeNodeId.
89
- * @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. If no value is
90
- * provided, it will default to `undefined`.
91
- * @param {number} [count] - The `count` parameter is an optional parameter that specifies the number of times the node
92
- * with the given `id` should be added to the binary tree. If the `count` parameter is not provided, it defaults to 1.
93
- * @returns The `add` method returns the inserted node (`N`), `null`, or `undefined`.
82
+ * The `add` function adds a new node to a binary search tree, maintaining the tree's properties and balancing if
83
+ * necessary.
84
+ * @param {BinaryTreeNodeId | N} idOrNode - The `idOrNode` parameter can be either a `BinaryTreeNodeId` or a `N` (which
85
+ * represents a `BinaryTreeNode`).
86
+ * @param [val] - The `val` parameter represents the value to be added to the binary tree node.
87
+ * @param {number} [count] - The `count` parameter is an optional parameter that specifies the number of times the
88
+ * value should be added to the binary tree. If the `count` parameter is not provided, it defaults to 1.
89
+ * @returns The method `add` returns either the inserted node (`N`), `null`, or `undefined`.
94
90
  */
95
- add(id, val, count) {
91
+ add(idOrNode, val, count) {
96
92
  count = count !== null && count !== void 0 ? count : 1;
97
- let inserted = null;
98
- const newNode = this.createNode(id, val, count);
99
- if (this.root === null) {
93
+ let inserted = undefined, newNode;
94
+ if (idOrNode instanceof TreeMultisetNode) {
95
+ newNode = this.createNode(idOrNode.id, idOrNode.val, idOrNode.count);
96
+ }
97
+ else if (idOrNode === null) {
98
+ newNode = null;
99
+ }
100
+ else {
101
+ newNode = this.createNode(idOrNode, val, count);
102
+ }
103
+ if (!this.root) {
100
104
  this._setRoot(newNode);
101
105
  this._setSize(this.size + 1);
102
- this._setCount(this.count + count);
103
- inserted = (this.root);
106
+ newNode && this._setCount(this.count + newNode.count);
107
+ inserted = this.root;
104
108
  }
105
109
  else {
106
110
  let cur = this.root;
107
111
  let traversing = true;
108
112
  while (traversing) {
109
- if (cur !== null && newNode !== null) {
110
- if (this._compare(cur.id, id) === types_1.CP.eq) {
111
- if (newNode) {
113
+ if (cur) {
114
+ if (newNode) {
115
+ if (this._compare(cur.id, newNode.id) === types_1.CP.eq) {
112
116
  cur.val = newNode.val;
113
- cur.count += count;
114
- this._setCount(this.count + newNode.count);
115
- }
116
- //Duplicates are not accepted.
117
- traversing = false;
118
- inserted = cur;
119
- }
120
- else if (this._compare(cur.id, id) === types_1.CP.gt) {
121
- // Traverse left of the node
122
- if (cur.left === undefined) {
123
- if (newNode) {
124
- newNode.parent = cur;
125
- }
126
- //Add to the left of the current node
127
- cur.left = newNode;
128
- this._setSize(this.size + 1);
117
+ cur.count += newNode.count;
129
118
  this._setCount(this.count + newNode.count);
130
119
  traversing = false;
131
- inserted = cur.left;
120
+ inserted = cur;
132
121
  }
133
- else {
134
- //Traverse the left of the current node
135
- if (cur.left)
136
- cur = cur.left;
137
- }
138
- }
139
- else if (this._compare(cur.id, id) === types_1.CP.lt) {
140
- // Traverse right of the node
141
- if (cur.right === undefined) {
142
- if (newNode) {
143
- newNode.parent = cur;
122
+ else if (this._compare(cur.id, newNode.id) === types_1.CP.gt) {
123
+ // Traverse left of the node
124
+ if (cur.left === undefined) {
125
+ //Add to the left of the current node
126
+ cur.left = newNode;
127
+ this._setSize(this.size + 1);
128
+ this._setCount(this.count + newNode.count);
129
+ traversing = false;
130
+ inserted = cur.left;
131
+ }
132
+ else {
133
+ //Traverse the left of the current node
134
+ if (cur.left)
135
+ cur = cur.left;
144
136
  }
145
- //Add to the right of the current node
146
- cur.right = newNode;
147
- this._setSize(this.size + 1);
148
- this._setCount(this.count + newNode.count);
149
- traversing = false;
150
- inserted = (cur.right);
151
137
  }
152
- else {
153
- //Traverse the left of the current node
154
- if (cur.right)
155
- cur = cur.right;
138
+ else if (this._compare(cur.id, newNode.id) === types_1.CP.lt) {
139
+ // Traverse right of the node
140
+ if (cur.right === undefined) {
141
+ //Add to the right of the current node
142
+ cur.right = newNode;
143
+ this._setSize(this.size + 1);
144
+ this._setCount(this.count + newNode.count);
145
+ traversing = false;
146
+ inserted = (cur.right);
147
+ }
148
+ else {
149
+ //Traverse the left of the current node
150
+ if (cur.right)
151
+ cur = cur.right;
152
+ }
156
153
  }
157
154
  }
155
+ else {
156
+ // TODO may need to support null inserted
157
+ }
158
158
  }
159
159
  else {
160
160
  traversing = false;
@@ -175,27 +175,20 @@ class TreeMultiset extends avl_tree_1.AVLTree {
175
175
  * `undefined` in certain cases.
176
176
  */
177
177
  addTo(newNode, parent) {
178
- var _a, _b;
179
178
  if (parent) {
180
179
  if (parent.left === undefined) {
181
- if (newNode) {
182
- newNode.parent = parent;
183
- }
184
180
  parent.left = newNode;
185
181
  if (newNode !== null) {
186
182
  this._setSize(this.size + 1);
187
- this._setCount((_a = this.count + newNode.count) !== null && _a !== void 0 ? _a : 0);
183
+ this._setCount(this.count + newNode.count);
188
184
  }
189
185
  return parent.left;
190
186
  }
191
187
  else if (parent.right === undefined) {
192
- if (newNode) {
193
- newNode.parent = parent;
194
- }
195
188
  parent.right = newNode;
196
189
  if (newNode !== null) {
197
190
  this._setSize(this.size + 1);
198
- this._setCount((_b = this.count + newNode.count) !== null && _b !== void 0 ? _b : 0);
191
+ this._setCount(this.count + newNode.count);
199
192
  }
200
193
  return parent.right;
201
194
  }
@@ -208,66 +201,86 @@ class TreeMultiset extends avl_tree_1.AVLTree {
208
201
  }
209
202
  }
210
203
  /**
211
- * The `addMany` function inserts multiple items into a binary tree and returns an array of the inserted nodes or
212
- * null/undefined values.
213
- * @param {N[] | N[]} data - The `data` parameter can be either an array of elements of type `N` or an
214
- * array of `N` objects.
204
+ * The `addMany` function adds multiple nodes to a binary tree and returns an array of the inserted nodes.
205
+ * @param {BinaryTreeNodeId[] | N[]} idsOrNodes - An array of BinaryTreeNodeId objects or N objects. These objects
206
+ * represent the IDs or nodes of the binary tree where the values will be added.
207
+ * @param {N['val'][]} [data] - Optional array of values to be associated with each node being added. If provided, the
208
+ * length of the `data` array should be equal to the length of the `idsOrNodes` array.
215
209
  * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
216
210
  */
217
- addMany(data) {
211
+ addMany(idsOrNodes, data) {
218
212
  var _a;
219
213
  // TODO not sure addMany not be run multi times
220
214
  const inserted = [];
221
215
  const map = new Map();
222
- if (this.isMergeDuplicatedVal) {
223
- for (const nodeOrId of data)
224
- map.set(nodeOrId, ((_a = map.get(nodeOrId)) !== null && _a !== void 0 ? _a : 0) + 1);
216
+ if (this.isMergeDuplicatedNodeById) {
217
+ for (const idOrNode of idsOrNodes)
218
+ map.set(idOrNode, ((_a = map.get(idOrNode)) !== null && _a !== void 0 ? _a : 0) + 1);
225
219
  }
226
- for (const nodeOrId of data) {
227
- if (nodeOrId instanceof TreeMultisetNode) {
228
- inserted.push(this.add(nodeOrId.id, nodeOrId.val, nodeOrId.count));
220
+ for (let i = 0; i < idsOrNodes.length; i++) {
221
+ const idOrNode = idsOrNodes[i];
222
+ if (idOrNode instanceof TreeMultisetNode) {
223
+ inserted.push(this.add(idOrNode.id, idOrNode.val, idOrNode.count));
229
224
  continue;
230
225
  }
231
- if (nodeOrId === null) {
226
+ if (idOrNode === null) {
232
227
  inserted.push(this.add(NaN, null, 0));
233
228
  continue;
234
229
  }
235
- // TODO will this cause an issue?
236
- const count = this.isMergeDuplicatedVal ? map.get(nodeOrId) : 1;
237
- let newId;
238
- if (typeof nodeOrId === 'number') {
239
- newId = this.autoIncrementId ? this.maxId + 1 : nodeOrId;
240
- }
241
- else if (nodeOrId instanceof Object) {
242
- if (this.autoIncrementId) {
243
- newId = this.maxId + 1;
244
- }
245
- else {
246
- if (Object.keys(nodeOrId).includes('id')) {
247
- newId = nodeOrId.id;
248
- }
249
- else {
250
- console.warn(nodeOrId, 'Object value must has an id property when the autoIncrementId is false');
251
- continue;
252
- }
230
+ const count = this.isMergeDuplicatedNodeById ? map.get(idOrNode) : 1;
231
+ const val = data === null || data === void 0 ? void 0 : data[i];
232
+ if (this.isMergeDuplicatedNodeById) {
233
+ if (map.has(idOrNode)) {
234
+ inserted.push(this.add(idOrNode, val, count));
235
+ map.delete(idOrNode);
253
236
  }
254
237
  }
255
238
  else {
256
- console.warn(nodeOrId, ` is not added`);
257
- continue;
239
+ inserted.push(this.add(idOrNode, val, 1));
258
240
  }
259
- if (this.isMergeDuplicatedVal) {
260
- if (map.has(nodeOrId)) {
261
- inserted.push(this.add(newId, nodeOrId, count));
262
- map.delete(nodeOrId);
241
+ }
242
+ return inserted;
243
+ }
244
+ /**
245
+ * The `perfectlyBalance` function takes a binary tree, performs a depth-first search to sort the nodes, and then
246
+ * constructs a balanced binary search tree using either a recursive or iterative approach.
247
+ * @returns The function `perfectlyBalance()` returns a boolean value.
248
+ */
249
+ perfectlyBalance() {
250
+ const sorted = this.DFS('in', 'node'), n = sorted.length;
251
+ if (sorted.length < 1)
252
+ return false;
253
+ this.clear();
254
+ if (this.loopType === types_1.LoopType.RECURSIVE) {
255
+ const buildBalanceBST = (l, r) => {
256
+ if (l > r)
257
+ return;
258
+ const m = l + Math.floor((r - l) / 2);
259
+ const midNode = sorted[m];
260
+ this.add(midNode.id, midNode.val, midNode.count);
261
+ buildBalanceBST(l, m - 1);
262
+ buildBalanceBST(m + 1, r);
263
+ };
264
+ buildBalanceBST(0, n - 1);
265
+ return true;
266
+ }
267
+ else {
268
+ const stack = [[0, n - 1]];
269
+ while (stack.length > 0) {
270
+ const popped = stack.pop();
271
+ if (popped) {
272
+ const [l, r] = popped;
273
+ if (l <= r) {
274
+ const m = l + Math.floor((r - l) / 2);
275
+ const midNode = sorted[m];
276
+ this.add(midNode.id, midNode.val, midNode.count);
277
+ stack.push([m + 1, r]);
278
+ stack.push([l, m - 1]);
279
+ }
263
280
  }
264
281
  }
265
- else {
266
- inserted.push(this.add(newId, nodeOrId, 1));
267
- }
268
- this._setMaxId(newId);
282
+ return true;
269
283
  }
270
- return inserted;
271
284
  }
272
285
  /**
273
286
  * The `remove` function removes a node from a binary search tree and returns the deleted node along with the parent
@@ -282,7 +295,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
282
295
  const bstDeletedResult = [];
283
296
  if (!this.root)
284
297
  return bstDeletedResult;
285
- const curr = (typeof nodeOrId === 'number') ? this.get(nodeOrId) : nodeOrId;
298
+ const curr = this.get(nodeOrId);
286
299
  if (!curr)
287
300
  return bstDeletedResult;
288
301
  const parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent : null;
@@ -314,15 +327,18 @@ class TreeMultiset extends avl_tree_1.AVLTree {
314
327
  const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
315
328
  orgCurrent = this.swapLocation(curr, leftSubTreeRightMost);
316
329
  if (parentOfLeftSubTreeMax) {
317
- if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
330
+ if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost) {
318
331
  parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
319
- else
332
+ }
333
+ else {
320
334
  parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
335
+ }
321
336
  needBalanced = parentOfLeftSubTreeMax;
322
337
  }
323
338
  }
324
339
  }
325
340
  this._setSize(this.size - 1);
341
+ // TODO How to handle when the count of target node is lesser than current node's count
326
342
  this._setCount(this.count - orgCurrent.count);
327
343
  }
328
344
  bstDeletedResult.push({ deleted: orgCurrent, needBalanced });
@@ -25,7 +25,8 @@ export declare class HeapItem<T = number> {
25
25
  }
26
26
  export declare abstract class Heap<T = number> {
27
27
  /**
28
- * The constructor function initializes a priority queue with an optional priority extractor function.
28
+ * The function is a constructor for a class that initializes a priority callback function based on the
29
+ * options provided.
29
30
  * @param [options] - An optional object that contains configuration options for the Heap.
30
31
  */
31
32
  protected constructor(options?: HeapOptions<T>);
@@ -64,8 +65,8 @@ export declare abstract class Heap<T = number> {
64
65
  poll(isItem: false): T | undefined;
65
66
  poll(isItem: true): HeapItem<T> | null;
66
67
  /**
67
- * The `has` function checks if a given node or value exists in the priority queue.
68
- * @param {T | HeapItem<T>} node - The `node` parameter can be of type `T` or `HeapItem<T>`.
68
+ * The function checks if a given node or value exists in the priority queue.
69
+ * @param {T | HeapItem<T>} node - The parameter `node` can be of type `T` or `HeapItem<T>`.
69
70
  * @returns a boolean value.
70
71
  */
71
72
  has(node: T | HeapItem<T>): boolean;
@@ -29,7 +29,8 @@ class HeapItem {
29
29
  exports.HeapItem = HeapItem;
30
30
  class Heap {
31
31
  /**
32
- * The constructor function initializes a priority queue with an optional priority extractor function.
32
+ * The function is a constructor for a class that initializes a priority callback function based on the
33
+ * options provided.
33
34
  * @param [options] - An optional object that contains configuration options for the Heap.
34
35
  */
35
36
  constructor(options) {
@@ -65,14 +66,8 @@ class Heap {
65
66
  return this._pq.size < 1;
66
67
  }
67
68
  /**
68
- * The `peek` function returns the top item or value in a priority queue, depending on the value of the `isItem`
69
- * parameter.
70
- * @param {boolean} [isItem] - The `isItem` parameter is an optional boolean parameter that determines whether the
71
- * method should return the entire `HeapItem` object or just the value of the item. If `isItem` is set to `true`, the
72
- * method will return the `HeapItem` object. If `isItem`
73
- * @returns The `peek` method returns either a `HeapItem<T>` object, `null`, `T`, or `undefined`. The specific return
74
- * type depends on the value of the `isItem` parameter. If `isItem` is `true`, then the method returns a `HeapItem<T>`
75
- * object or `null` if the heap is empty. If `isItem` is `false`
69
+ * The `peek` function returns the top item in the priority queue without removing it.
70
+ * @returns The `peek()` method is returning either a `HeapItem<T>` object or `null`.Returns an val with the highest priority in the queue
76
71
  */
77
72
  peek(isItem) {
78
73
  isItem = isItem !== null && isItem !== void 0 ? isItem : false;
@@ -80,14 +75,8 @@ class Heap {
80
75
  return isItem ? peeked : peeked === null || peeked === void 0 ? void 0 : peeked.val;
81
76
  }
82
77
  /**
83
- * The `peekLast` function returns the last item in the heap, either as a `HeapItem` object or just the value depending
84
- * on the `isItem` parameter.
85
- * @param {boolean} [isItem] - A boolean parameter that indicates whether the method should return the HeapItem object
86
- * or just the value of the last item in the heap. If isItem is true, the method will return the HeapItem object. If
87
- * isItem is false or not provided, the method will return the value of the last item
88
- * @returns The method `peekLast` returns either a `HeapItem<T>` object, `null`, `T`, or `undefined`. The specific
89
- * return type depends on the value of the `isItem` parameter. If `isItem` is `true`, then the method returns a
90
- * `HeapItem<T>` object or `null` if there are no items in the heap. If `isItem`
78
+ * The `peekLast` function returns the last item in the heap.
79
+ * @returns The method `peekLast()` returns either a `HeapItem<T>` object or `null`.Returns an val with the lowest priority in the queue
91
80
  */
92
81
  peekLast(isItem) {
93
82
  isItem = isItem !== null && isItem !== void 0 ? isItem : false;
@@ -110,13 +99,8 @@ class Heap {
110
99
  return this;
111
100
  }
112
101
  /**
113
- * The `poll` function returns the top item from a priority queue, either as a HeapItem object or its value, depending
114
- * on the value of the `isItem` parameter.
115
- * @param {boolean} [isItem] - The `isItem` parameter is a boolean flag that indicates whether the returned value
116
- * should be a `HeapItem<T>` object or just the value `T` itself. If `isItem` is `true`, the method will return the
117
- * `HeapItem<T>` object, otherwise it will return just
118
- * @returns The function `poll` returns either a `HeapItem<T>` object, `null`, or `T` (the value of the `val` property
119
- * of the `HeapItem<T>` object).
102
+ * The `poll` function returns the top item from a priority queue or null if the queue is empty.Removes and returns an val with the highest priority in the queue
103
+ * @returns either a HeapItem<T> object or null.
120
104
  */
121
105
  poll(isItem) {
122
106
  isItem = isItem !== null && isItem !== void 0 ? isItem : false;
@@ -127,8 +111,8 @@ class Heap {
127
111
  return isItem ? top : top.val;
128
112
  }
129
113
  /**
130
- * The `has` function checks if a given node or value exists in the priority queue.
131
- * @param {T | HeapItem<T>} node - The `node` parameter can be of type `T` or `HeapItem<T>`.
114
+ * The function checks if a given node or value exists in the priority queue.
115
+ * @param {T | HeapItem<T>} node - The parameter `node` can be of type `T` or `HeapItem<T>`.
132
116
  * @returns a boolean value.
133
117
  */
134
118
  has(node) {
@@ -142,13 +126,8 @@ class Heap {
142
126
  }
143
127
  }
144
128
  /**
145
- * The `toArray` function returns an array of either HeapItem objects or their values, depending on the value of the
146
- * `isItem` parameter.
147
- * @param {boolean} [isItem] - isItem is an optional boolean parameter that determines whether the returned array
148
- * should contain the HeapItem objects or just the values of the HeapItem objects. If isItem is true, the array will
149
- * contain the HeapItem objects. If isItem is false or not provided, the array will contain only the values
150
- * @returns The method `toArray` returns an array of `HeapItem<T>` objects, or an array of `T` values if the `isItem`
151
- * parameter is set to `false`.
129
+ * The `toArray` function returns an array of `HeapItem<T>` objects.
130
+ * @returns An array of HeapItem<T> objects.Returns a sorted list of vals
152
131
  */
153
132
  toArray(isItem) {
154
133
  isItem = isItem !== null && isItem !== void 0 ? isItem : false;