data-structure-typed 1.19.2 → 1.19.4

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 (155) hide show
  1. package/dist/data-structures/binary-tree/aa-tree.js +2 -5
  2. package/dist/data-structures/binary-tree/abstract-binary-tree.js +361 -488
  3. package/dist/data-structures/binary-tree/avl-tree.js +46 -90
  4. package/dist/data-structures/binary-tree/b-tree.js +2 -5
  5. package/dist/data-structures/binary-tree/binary-indexed-tree.js +17 -22
  6. package/dist/data-structures/binary-tree/binary-tree.js +9 -31
  7. package/dist/data-structures/binary-tree/bst.js +96 -139
  8. package/dist/data-structures/binary-tree/rb-tree.js +32 -56
  9. package/dist/data-structures/binary-tree/segment-tree.js +78 -120
  10. package/dist/data-structures/binary-tree/splay-tree.js +2 -5
  11. package/dist/data-structures/binary-tree/tree-multiset.js +176 -253
  12. package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
  13. package/dist/data-structures/graph/abstract-graph.js +340 -574
  14. package/dist/data-structures/graph/directed-graph.js +146 -276
  15. package/dist/data-structures/graph/undirected-graph.js +87 -176
  16. package/dist/data-structures/hash/coordinate-map.js +23 -45
  17. package/dist/data-structures/hash/coordinate-set.js +20 -42
  18. package/dist/data-structures/hash/hash-table.js +2 -5
  19. package/dist/data-structures/hash/pair.js +2 -5
  20. package/dist/data-structures/hash/tree-map.js +2 -5
  21. package/dist/data-structures/hash/tree-set.js +2 -5
  22. package/dist/data-structures/heap/heap.js +53 -77
  23. package/dist/data-structures/heap/max-heap.js +8 -26
  24. package/dist/data-structures/heap/min-heap.js +8 -26
  25. package/dist/data-structures/linked-list/doubly-linked-list.js +132 -197
  26. package/dist/data-structures/linked-list/singly-linked-list.js +112 -173
  27. package/dist/data-structures/linked-list/skip-linked-list.js +2 -5
  28. package/dist/data-structures/matrix/matrix.js +7 -8
  29. package/dist/data-structures/matrix/matrix2d.js +76 -93
  30. package/dist/data-structures/matrix/navigator.js +18 -37
  31. package/dist/data-structures/matrix/vector2d.js +80 -101
  32. package/dist/data-structures/priority-queue/max-priority-queue.js +11 -39
  33. package/dist/data-structures/priority-queue/min-priority-queue.js +11 -39
  34. package/dist/data-structures/priority-queue/priority-queue.js +93 -139
  35. package/dist/data-structures/queue/deque.js +82 -128
  36. package/dist/data-structures/queue/queue.js +24 -25
  37. package/dist/data-structures/stack/stack.js +21 -22
  38. package/dist/data-structures/tree/tree.js +32 -45
  39. package/dist/data-structures/trie/trie.js +93 -200
  40. package/dist/utils/utils.js +22 -107
  41. package/dist/utils/validate-type.js +2 -2
  42. package/package.json +1 -1
  43. package/src/assets/complexities-diff.jpg +0 -0
  44. package/src/assets/data-structure-complexities.jpg +0 -0
  45. package/src/assets/logo.png +0 -0
  46. package/src/assets/overview-diagram-of-data-structures.png +0 -0
  47. package/src/data-structures/binary-tree/aa-tree.ts +3 -0
  48. package/src/data-structures/binary-tree/abstract-binary-tree.ts +1528 -0
  49. package/src/data-structures/binary-tree/avl-tree.ts +297 -0
  50. package/src/data-structures/binary-tree/b-tree.ts +3 -0
  51. package/src/data-structures/binary-tree/binary-indexed-tree.ts +78 -0
  52. package/src/data-structures/binary-tree/binary-tree.ts +40 -0
  53. package/src/data-structures/binary-tree/bst.ts +435 -0
  54. package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  55. package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  56. package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  57. package/src/data-structures/binary-tree/index.ts +12 -0
  58. package/src/data-structures/binary-tree/rb-tree.ts +102 -0
  59. package/src/data-structures/binary-tree/segment-tree.ts +243 -0
  60. package/src/data-structures/binary-tree/splay-tree.ts +3 -0
  61. package/src/data-structures/binary-tree/tree-multiset.ts +694 -0
  62. package/src/data-structures/binary-tree/two-three-tree.ts +3 -0
  63. package/src/data-structures/diagrams/README.md +5 -0
  64. package/src/data-structures/graph/abstract-graph.ts +1032 -0
  65. package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
  66. package/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
  67. package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
  68. package/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
  69. package/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
  70. package/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
  71. package/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
  72. package/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
  73. package/src/data-structures/graph/diagrams/mst.jpg +0 -0
  74. package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  75. package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  76. package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  77. package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  78. package/src/data-structures/graph/diagrams/tarjan.webp +0 -0
  79. package/src/data-structures/graph/directed-graph.ts +472 -0
  80. package/src/data-structures/graph/index.ts +3 -0
  81. package/src/data-structures/graph/undirected-graph.ts +270 -0
  82. package/src/data-structures/hash/coordinate-map.ts +67 -0
  83. package/src/data-structures/hash/coordinate-set.ts +56 -0
  84. package/src/data-structures/hash/hash-table.ts +3 -0
  85. package/src/data-structures/hash/index.ts +6 -0
  86. package/src/data-structures/hash/pair.ts +3 -0
  87. package/src/data-structures/hash/tree-map.ts +3 -0
  88. package/src/data-structures/hash/tree-set.ts +3 -0
  89. package/src/data-structures/heap/heap.ts +183 -0
  90. package/src/data-structures/heap/index.ts +3 -0
  91. package/src/data-structures/heap/max-heap.ts +31 -0
  92. package/src/data-structures/heap/min-heap.ts +34 -0
  93. package/src/data-structures/index.ts +15 -0
  94. package/src/data-structures/interfaces/abstract-binary-tree.ts +231 -0
  95. package/src/data-structures/interfaces/abstract-graph.ts +40 -0
  96. package/src/data-structures/interfaces/avl-tree.ts +28 -0
  97. package/src/data-structures/interfaces/binary-tree.ts +8 -0
  98. package/src/data-structures/interfaces/bst.ts +32 -0
  99. package/src/data-structures/interfaces/directed-graph.ts +20 -0
  100. package/src/data-structures/interfaces/doubly-linked-list.ts +1 -0
  101. package/src/data-structures/interfaces/heap.ts +1 -0
  102. package/src/data-structures/interfaces/index.ts +15 -0
  103. package/src/data-structures/interfaces/navigator.ts +1 -0
  104. package/src/data-structures/interfaces/priority-queue.ts +1 -0
  105. package/src/data-structures/interfaces/rb-tree.ts +11 -0
  106. package/src/data-structures/interfaces/segment-tree.ts +1 -0
  107. package/src/data-structures/interfaces/singly-linked-list.ts +1 -0
  108. package/src/data-structures/interfaces/tree-multiset.ts +12 -0
  109. package/src/data-structures/interfaces/undirected-graph.ts +6 -0
  110. package/src/data-structures/linked-list/doubly-linked-list.ts +573 -0
  111. package/src/data-structures/linked-list/index.ts +3 -0
  112. package/src/data-structures/linked-list/singly-linked-list.ts +490 -0
  113. package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
  114. package/src/data-structures/matrix/index.ts +4 -0
  115. package/src/data-structures/matrix/matrix.ts +27 -0
  116. package/src/data-structures/matrix/matrix2d.ts +208 -0
  117. package/src/data-structures/matrix/navigator.ts +122 -0
  118. package/src/data-structures/matrix/vector2d.ts +316 -0
  119. package/src/data-structures/priority-queue/index.ts +3 -0
  120. package/src/data-structures/priority-queue/max-priority-queue.ts +49 -0
  121. package/src/data-structures/priority-queue/min-priority-queue.ts +50 -0
  122. package/src/data-structures/priority-queue/priority-queue.ts +354 -0
  123. package/src/data-structures/queue/deque.ts +251 -0
  124. package/src/data-structures/queue/index.ts +2 -0
  125. package/src/data-structures/queue/queue.ts +120 -0
  126. package/src/data-structures/stack/index.ts +1 -0
  127. package/src/data-structures/stack/stack.ts +98 -0
  128. package/src/data-structures/tree/index.ts +1 -0
  129. package/src/data-structures/tree/tree.ts +69 -0
  130. package/src/data-structures/trie/index.ts +1 -0
  131. package/src/data-structures/trie/trie.ts +227 -0
  132. package/src/data-structures/types/abstract-binary-tree.ts +42 -0
  133. package/src/data-structures/types/abstract-graph.ts +5 -0
  134. package/src/data-structures/types/avl-tree.ts +5 -0
  135. package/src/data-structures/types/binary-tree.ts +9 -0
  136. package/src/data-structures/types/bst.ts +12 -0
  137. package/src/data-structures/types/directed-graph.ts +8 -0
  138. package/src/data-structures/types/doubly-linked-list.ts +1 -0
  139. package/src/data-structures/types/heap.ts +5 -0
  140. package/src/data-structures/types/helpers.ts +1 -0
  141. package/src/data-structures/types/index.ts +15 -0
  142. package/src/data-structures/types/navigator.ts +13 -0
  143. package/src/data-structures/types/priority-queue.ts +9 -0
  144. package/src/data-structures/types/rb-tree.ts +8 -0
  145. package/src/data-structures/types/segment-tree.ts +1 -0
  146. package/src/data-structures/types/singly-linked-list.ts +1 -0
  147. package/src/data-structures/types/tree-multiset.ts +8 -0
  148. package/src/index.ts +2 -0
  149. package/src/utils/index.ts +3 -0
  150. package/src/utils/types/index.ts +2 -0
  151. package/src/utils/types/utils.ts +6 -0
  152. package/src/utils/types/validate-type.ts +25 -0
  153. package/src/utils/utils.ts +78 -0
  154. package/src/utils/validate-type.ts +69 -0
  155. package/tsconfig.json +1 -1
@@ -1,47 +1,9 @@
1
1
  "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
- var __assign = (this && this.__assign) || function () {
18
- __assign = Object.assign || function(t) {
19
- for (var s, i = 1, n = arguments.length; i < n; i++) {
20
- s = arguments[i];
21
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
22
- t[p] = s[p];
23
- }
24
- return t;
25
- };
26
- return __assign.apply(this, arguments);
27
- };
28
- var __values = (this && this.__values) || function(o) {
29
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
30
- if (m) return m.call(o);
31
- if (o && typeof o.length === "number") return {
32
- next: function () {
33
- if (o && i >= o.length) o = void 0;
34
- return { value: o && o[i++], done: !o };
35
- }
36
- };
37
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
38
- };
39
2
  Object.defineProperty(exports, "__esModule", { value: true });
40
3
  exports.TreeMultiset = exports.TreeMultisetNode = void 0;
41
- var types_1 = require("../types");
42
- var avl_tree_1 = require("./avl-tree");
43
- var TreeMultisetNode = /** @class */ (function (_super) {
44
- __extends(TreeMultisetNode, _super);
4
+ const types_1 = require("../types");
5
+ const avl_tree_1 = require("./avl-tree");
6
+ class TreeMultisetNode extends avl_tree_1.AVLTreeNode {
45
7
  /**
46
8
  * The constructor function initializes a BinaryTreeNode object with an id, value, and count.
47
9
  * @param {BinaryTreeNodeId} id - The `id` parameter is of type `BinaryTreeNodeId` and represents the unique identifier
@@ -52,49 +14,36 @@ var TreeMultisetNode = /** @class */ (function (_super) {
52
14
  * occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count`
53
15
  * parameter when creating a new instance of the `BinaryTreeNode` class,
54
16
  */
55
- function TreeMultisetNode(id, val, count) {
56
- if (count === void 0) { count = 1; }
57
- var _this = _super.call(this, id, val) || this;
58
- _this._count = 1;
59
- _this._count = count;
60
- return _this;
17
+ constructor(id, val, count = 1) {
18
+ super(id, val);
19
+ this._count = 1;
20
+ this._count = count;
21
+ }
22
+ get count() {
23
+ return this._count;
61
24
  }
62
- Object.defineProperty(TreeMultisetNode.prototype, "count", {
63
- get: function () {
64
- return this._count;
65
- },
66
- set: function (v) {
67
- this._count = v;
68
- },
69
- enumerable: false,
70
- configurable: true
71
- });
72
- return TreeMultisetNode;
73
- }(avl_tree_1.AVLTreeNode));
25
+ set count(v) {
26
+ this._count = v;
27
+ }
28
+ }
74
29
  exports.TreeMultisetNode = TreeMultisetNode;
75
30
  /**
76
31
  * The only distinction between a TreeMultiset and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
77
32
  */
78
- var TreeMultiset = /** @class */ (function (_super) {
79
- __extends(TreeMultiset, _super);
33
+ class TreeMultiset extends avl_tree_1.AVLTree {
80
34
  /**
81
35
  * The constructor function for a TreeMultiset class in TypeScript, which extends another class and sets an option to
82
36
  * merge duplicated values.
83
37
  * @param {TreeMultisetOptions} [options] - An optional object that contains additional configuration options for the
84
38
  * TreeMultiset.
85
39
  */
86
- function TreeMultiset(options) {
87
- var _this = _super.call(this, __assign(__assign({}, options), { isMergeDuplicatedVal: true })) || this;
88
- _this._count = 0;
89
- return _this;
40
+ constructor(options) {
41
+ super(Object.assign(Object.assign({}, options), { isMergeDuplicatedVal: true }));
42
+ this._count = 0;
43
+ }
44
+ get count() {
45
+ return this._count;
90
46
  }
91
- Object.defineProperty(TreeMultiset.prototype, "count", {
92
- get: function () {
93
- return this._count;
94
- },
95
- enumerable: false,
96
- configurable: true
97
- });
98
47
  /**
99
48
  * The function creates a new BSTNode with the given id, value, and count.
100
49
  * @param {BinaryTreeNodeId} id - The id parameter is the unique identifier for the binary tree node. It is used to
@@ -104,9 +53,9 @@ var TreeMultiset = /** @class */ (function (_super) {
104
53
  * occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
105
54
  * @returns A new instance of the BSTNode class with the specified id, value, and count (if provided).
106
55
  */
107
- TreeMultiset.prototype.createNode = function (id, val, count) {
56
+ createNode(id, val, count) {
108
57
  return new TreeMultisetNode(id, val, count);
109
- };
58
+ }
110
59
  /**
111
60
  * The function swaps the location of two nodes in a tree data structure.
112
61
  * @param {N} srcNode - The source node that we want to swap with the destination node.
@@ -114,9 +63,9 @@ var TreeMultiset = /** @class */ (function (_super) {
114
63
  * be swapped with.
115
64
  * @returns the `destNode` after swapping its values with the `srcNode`.
116
65
  */
117
- TreeMultiset.prototype.swapLocation = function (srcNode, destNode) {
118
- var val = destNode.val, count = destNode.count, height = destNode.height, id = destNode.id;
119
- var tempNode = this.createNode(id, val, count);
66
+ swapLocation(srcNode, destNode) {
67
+ const { val, count, height, id } = destNode;
68
+ const tempNode = this.createNode(id, val, count);
120
69
  if (tempNode) {
121
70
  tempNode.height = height;
122
71
  if (tempNode instanceof TreeMultisetNode) {
@@ -131,7 +80,7 @@ var TreeMultiset = /** @class */ (function (_super) {
131
80
  }
132
81
  }
133
82
  return destNode;
134
- };
83
+ }
135
84
  /**
136
85
  * The `add` function adds a new node to a binary tree, updating the size and count properties accordingly, and
137
86
  * balancing the tree if necessary.
@@ -143,10 +92,10 @@ var TreeMultiset = /** @class */ (function (_super) {
143
92
  * with the given `id` should be added to the binary tree. If the `count` parameter is not provided, it defaults to 1.
144
93
  * @returns The `add` method returns the inserted node (`N`), `null`, or `undefined`.
145
94
  */
146
- TreeMultiset.prototype.add = function (id, val, count) {
95
+ add(id, val, count) {
147
96
  count = count !== null && count !== void 0 ? count : 1;
148
- var inserted = null;
149
- var newNode = this.createNode(id, val, count);
97
+ let inserted = null;
98
+ const newNode = this.createNode(id, val, count);
150
99
  if (this.root === null) {
151
100
  this._setRoot(newNode);
152
101
  this._setSize(this.size + 1);
@@ -154,8 +103,8 @@ var TreeMultiset = /** @class */ (function (_super) {
154
103
  inserted = (this.root);
155
104
  }
156
105
  else {
157
- var cur = this.root;
158
- var traversing = true;
106
+ let cur = this.root;
107
+ let traversing = true;
159
108
  while (traversing) {
160
109
  if (cur !== null && newNode !== null) {
161
110
  if (this._compare(cur.id, id) === types_1.CP.eq) {
@@ -215,7 +164,7 @@ var TreeMultiset = /** @class */ (function (_super) {
215
164
  if (inserted)
216
165
  this.balancePath(inserted);
217
166
  return inserted;
218
- };
167
+ }
219
168
  /**
220
169
  * The function adds a new node to a binary tree as the left or right child of a given parent node.
221
170
  * @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to the tree. It can be
@@ -225,7 +174,7 @@ var TreeMultiset = /** @class */ (function (_super) {
225
174
  * @returns either the left or right child node that was added to the parent node. It can also return `null` or
226
175
  * `undefined` in certain cases.
227
176
  */
228
- TreeMultiset.prototype.addTo = function (newNode, parent) {
177
+ addTo(newNode, parent) {
229
178
  var _a, _b;
230
179
  if (parent) {
231
180
  if (parent.left === undefined) {
@@ -257,7 +206,7 @@ var TreeMultiset = /** @class */ (function (_super) {
257
206
  else {
258
207
  return;
259
208
  }
260
- };
209
+ }
261
210
  /**
262
211
  * The `addMany` function inserts multiple items into a binary tree and returns an array of the inserted nodes or
263
212
  * null/undefined values.
@@ -265,83 +214,61 @@ var TreeMultiset = /** @class */ (function (_super) {
265
214
  * array of `N` objects.
266
215
  * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
267
216
  */
268
- TreeMultiset.prototype.addMany = function (data) {
269
- var e_1, _a, e_2, _b;
270
- var _c;
217
+ addMany(data) {
218
+ var _a;
271
219
  // TODO not sure addMany not be run multi times
272
- var inserted = [];
273
- var map = new Map();
220
+ const inserted = [];
221
+ const map = new Map();
274
222
  if (this.isMergeDuplicatedVal) {
275
- try {
276
- for (var data_1 = __values(data), data_1_1 = data_1.next(); !data_1_1.done; data_1_1 = data_1.next()) {
277
- var nodeOrId = data_1_1.value;
278
- map.set(nodeOrId, ((_c = map.get(nodeOrId)) !== null && _c !== void 0 ? _c : 0) + 1);
279
- }
223
+ for (const nodeOrId of data)
224
+ map.set(nodeOrId, ((_a = map.get(nodeOrId)) !== null && _a !== void 0 ? _a : 0) + 1);
225
+ }
226
+ for (const nodeOrId of data) {
227
+ if (nodeOrId instanceof TreeMultisetNode) {
228
+ inserted.push(this.add(nodeOrId.id, nodeOrId.val, nodeOrId.count));
229
+ continue;
280
230
  }
281
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
282
- finally {
283
- try {
284
- if (data_1_1 && !data_1_1.done && (_a = data_1.return)) _a.call(data_1);
285
- }
286
- finally { if (e_1) throw e_1.error; }
231
+ if (nodeOrId === null) {
232
+ inserted.push(this.add(NaN, null, 0));
233
+ continue;
287
234
  }
288
- }
289
- try {
290
- for (var data_2 = __values(data), data_2_1 = data_2.next(); !data_2_1.done; data_2_1 = data_2.next()) {
291
- var nodeOrId = data_2_1.value;
292
- if (nodeOrId instanceof TreeMultisetNode) {
293
- inserted.push(this.add(nodeOrId.id, nodeOrId.val, nodeOrId.count));
294
- continue;
295
- }
296
- if (nodeOrId === null) {
297
- inserted.push(this.add(NaN, null, 0));
298
- continue;
299
- }
300
- // TODO will this cause an issue?
301
- var count = this.isMergeDuplicatedVal ? map.get(nodeOrId) : 1;
302
- var newId = void 0;
303
- if (typeof nodeOrId === 'number') {
304
- newId = this.autoIncrementId ? this.maxId + 1 : nodeOrId;
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;
305
244
  }
306
- else if (nodeOrId instanceof Object) {
307
- if (this.autoIncrementId) {
308
- newId = this.maxId + 1;
245
+ else {
246
+ if (Object.keys(nodeOrId).includes('id')) {
247
+ newId = nodeOrId.id;
309
248
  }
310
249
  else {
311
- if (Object.keys(nodeOrId).includes('id')) {
312
- newId = nodeOrId.id;
313
- }
314
- else {
315
- console.warn(nodeOrId, 'Object value must has an id property when the autoIncrementId is false');
316
- continue;
317
- }
318
- }
319
- }
320
- else {
321
- console.warn(nodeOrId, " is not added");
322
- continue;
323
- }
324
- if (this.isMergeDuplicatedVal) {
325
- if (map.has(nodeOrId)) {
326
- inserted.push(this.add(newId, nodeOrId, count));
327
- map.delete(nodeOrId);
250
+ console.warn(nodeOrId, 'Object value must has an id property when the autoIncrementId is false');
251
+ continue;
328
252
  }
329
253
  }
330
- else {
331
- inserted.push(this.add(newId, nodeOrId, 1));
254
+ }
255
+ else {
256
+ console.warn(nodeOrId, ` is not added`);
257
+ continue;
258
+ }
259
+ if (this.isMergeDuplicatedVal) {
260
+ if (map.has(nodeOrId)) {
261
+ inserted.push(this.add(newId, nodeOrId, count));
262
+ map.delete(nodeOrId);
332
263
  }
333
- this._setMaxId(newId);
334
264
  }
335
- }
336
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
337
- finally {
338
- try {
339
- if (data_2_1 && !data_2_1.done && (_b = data_2.return)) _b.call(data_2);
265
+ else {
266
+ inserted.push(this.add(newId, nodeOrId, 1));
340
267
  }
341
- finally { if (e_2) throw e_2.error; }
268
+ this._setMaxId(newId);
342
269
  }
343
270
  return inserted;
344
- };
271
+ }
345
272
  /**
346
273
  * The `remove` function removes a node from a binary search tree and returns the deleted node along with the parent
347
274
  * node that needs to be balanced.
@@ -351,15 +278,15 @@ var TreeMultiset = /** @class */ (function (_super) {
351
278
  * not be taken into account when removing it. If `ignoreCount` is set to `false
352
279
  * @returns The function `remove` returns an array of `BinaryTreeDeletedResult<N>` objects.
353
280
  */
354
- TreeMultiset.prototype.remove = function (nodeOrId, ignoreCount) {
355
- var bstDeletedResult = [];
281
+ remove(nodeOrId, ignoreCount) {
282
+ const bstDeletedResult = [];
356
283
  if (!this.root)
357
284
  return bstDeletedResult;
358
- var curr = (typeof nodeOrId === 'number') ? this.get(nodeOrId) : nodeOrId;
285
+ const curr = (typeof nodeOrId === 'number') ? this.get(nodeOrId) : nodeOrId;
359
286
  if (!curr)
360
287
  return bstDeletedResult;
361
- var parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent : null;
362
- var needBalanced = null, orgCurrent = curr;
288
+ const parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent : null;
289
+ let needBalanced = null, orgCurrent = curr;
363
290
  if (curr.count > 1 && !ignoreCount) {
364
291
  curr.count--;
365
292
  this._setCount(this.count - 1);
@@ -371,7 +298,7 @@ var TreeMultiset = /** @class */ (function (_super) {
371
298
  this._setRoot(curr.right);
372
299
  }
373
300
  else {
374
- var fp = curr.familyPosition;
301
+ const { familyPosition: fp } = curr;
375
302
  if (fp === types_1.FamilyPosition.LEFT || fp === types_1.FamilyPosition.ROOT_LEFT) {
376
303
  parent.left = curr.right;
377
304
  }
@@ -382,9 +309,9 @@ var TreeMultiset = /** @class */ (function (_super) {
382
309
  }
383
310
  }
384
311
  else {
385
- var leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : null;
312
+ const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : null;
386
313
  if (leftSubTreeRightMost) {
387
- var parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
314
+ const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
388
315
  orgCurrent = this.swapLocation(curr, leftSubTreeRightMost);
389
316
  if (parentOfLeftSubTreeMax) {
390
317
  if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost)
@@ -398,12 +325,12 @@ var TreeMultiset = /** @class */ (function (_super) {
398
325
  this._setSize(this.size - 1);
399
326
  this._setCount(this.count - orgCurrent.count);
400
327
  }
401
- bstDeletedResult.push({ deleted: orgCurrent, needBalanced: needBalanced });
328
+ bstDeletedResult.push({ deleted: orgCurrent, needBalanced });
402
329
  if (needBalanced) {
403
330
  this.balancePath(needBalanced);
404
331
  }
405
332
  return bstDeletedResult;
406
- };
333
+ }
407
334
  /**
408
335
  * The function `getSubTreeCount` calculates the number of nodes and the sum of their counts in a subtree, using either
409
336
  * recursive or iterative traversal.
@@ -411,24 +338,24 @@ var TreeMultiset = /** @class */ (function (_super) {
411
338
  * binary tree.
412
339
  * @returns The function `getSubTreeCount` returns an array `[number, number]`.
413
340
  */
414
- TreeMultiset.prototype.getSubTreeCount = function (subTreeRoot) {
415
- var res = [0, 0];
341
+ getSubTreeCount(subTreeRoot) {
342
+ const res = [0, 0];
416
343
  if (!subTreeRoot)
417
344
  return res;
418
345
  if (this.loopType === types_1.LoopType.RECURSIVE) {
419
- var _traverse_1 = function (cur) {
346
+ const _traverse = (cur) => {
420
347
  res[0]++;
421
348
  res[1] += cur.count;
422
- cur.left && _traverse_1(cur.left);
423
- cur.right && _traverse_1(cur.right);
349
+ cur.left && _traverse(cur.left);
350
+ cur.right && _traverse(cur.right);
424
351
  };
425
- _traverse_1(subTreeRoot);
352
+ _traverse(subTreeRoot);
426
353
  return res;
427
354
  }
428
355
  else {
429
- var stack = [subTreeRoot];
356
+ const stack = [subTreeRoot];
430
357
  while (stack.length > 0) {
431
- var cur = stack.pop();
358
+ const cur = stack.pop();
432
359
  res[0]++;
433
360
  res[1] += cur.count;
434
361
  cur.right && stack.push(cur.right);
@@ -436,7 +363,7 @@ var TreeMultiset = /** @class */ (function (_super) {
436
363
  }
437
364
  return res;
438
365
  }
439
- };
366
+ }
440
367
  /**
441
368
  * The function `subTreeSumCount` calculates the sum of the `count` property of each node in a subtree, either
442
369
  * recursively or iteratively.
@@ -445,31 +372,31 @@ var TreeMultiset = /** @class */ (function (_super) {
445
372
  * `null` if the subtree is empty.
446
373
  * @returns the sum of the count values of all nodes in the subtree rooted at `subTreeRoot`.
447
374
  */
448
- TreeMultiset.prototype.subTreeSumCount = function (subTreeRoot) {
375
+ subTreeSumCount(subTreeRoot) {
449
376
  if (typeof subTreeRoot === 'number')
450
377
  subTreeRoot = this.get(subTreeRoot, 'id');
451
378
  if (!subTreeRoot)
452
379
  return 0;
453
- var sum = 0;
380
+ let sum = 0;
454
381
  if (this.loopType === types_1.LoopType.RECURSIVE) {
455
- var _traverse_2 = function (cur) {
382
+ const _traverse = (cur) => {
456
383
  sum += cur.count;
457
- cur.left && _traverse_2(cur.left);
458
- cur.right && _traverse_2(cur.right);
384
+ cur.left && _traverse(cur.left);
385
+ cur.right && _traverse(cur.right);
459
386
  };
460
- _traverse_2(subTreeRoot);
387
+ _traverse(subTreeRoot);
461
388
  }
462
389
  else {
463
- var stack = [subTreeRoot];
390
+ const stack = [subTreeRoot];
464
391
  while (stack.length > 0) {
465
- var cur = stack.pop();
392
+ const cur = stack.pop();
466
393
  sum += cur.count;
467
394
  cur.right && stack.push(cur.right);
468
395
  cur.left && stack.push(cur.left);
469
396
  }
470
397
  }
471
398
  return sum;
472
- };
399
+ }
473
400
  /**
474
401
  * The function `subTreeAddCount` recursively or iteratively traverses a binary tree and adds a given delta value to
475
402
  * the `count` property of each node.
@@ -480,35 +407,34 @@ var TreeMultiset = /** @class */ (function (_super) {
480
407
  * in the subtree should be increased or decreased.
481
408
  * @returns a boolean value.
482
409
  */
483
- TreeMultiset.prototype.subTreeAddCount = function (subTreeRoot, delta) {
484
- var _this = this;
410
+ subTreeAddCount(subTreeRoot, delta) {
485
411
  if (typeof subTreeRoot === 'number')
486
412
  subTreeRoot = this.get(subTreeRoot, 'id');
487
413
  if (!subTreeRoot)
488
414
  return false;
489
- var _addByProperty = function (cur) {
415
+ const _addByProperty = (cur) => {
490
416
  cur.count += delta;
491
- _this._setCount(_this.count + delta);
417
+ this._setCount(this.count + delta);
492
418
  };
493
419
  if (this.loopType === types_1.LoopType.RECURSIVE) {
494
- var _traverse_3 = function (cur) {
420
+ const _traverse = (cur) => {
495
421
  _addByProperty(cur);
496
- cur.left && _traverse_3(cur.left);
497
- cur.right && _traverse_3(cur.right);
422
+ cur.left && _traverse(cur.left);
423
+ cur.right && _traverse(cur.right);
498
424
  };
499
- _traverse_3(subTreeRoot);
425
+ _traverse(subTreeRoot);
500
426
  }
501
427
  else {
502
- var stack = [subTreeRoot];
428
+ const stack = [subTreeRoot];
503
429
  while (stack.length > 0) {
504
- var cur = stack.pop();
430
+ const cur = stack.pop();
505
431
  _addByProperty(cur);
506
432
  cur.right && stack.push(cur.right);
507
433
  cur.left && stack.push(cur.left);
508
434
  }
509
435
  }
510
436
  return true;
511
- };
437
+ }
512
438
  /**
513
439
  * The function `getNodesByCount` returns an array of nodes that have a specific count property, either recursively or
514
440
  * using a queue.
@@ -519,12 +445,12 @@ var TreeMultiset = /** @class */ (function (_super) {
519
445
  * to `true`, the function will return only one node. If `onlyOne`
520
446
  * @returns an array of nodes that match the given nodeProperty.
521
447
  */
522
- TreeMultiset.prototype.getNodesByCount = function (nodeProperty, onlyOne) {
448
+ getNodesByCount(nodeProperty, onlyOne) {
523
449
  if (!this.root)
524
450
  return [];
525
- var result = [];
451
+ const result = [];
526
452
  if (this.loopType === types_1.LoopType.RECURSIVE) {
527
- var _traverse_4 = function (cur) {
453
+ const _traverse = (cur) => {
528
454
  if (cur.count === nodeProperty) {
529
455
  result.push(cur);
530
456
  if (onlyOne)
@@ -532,15 +458,15 @@ var TreeMultiset = /** @class */ (function (_super) {
532
458
  }
533
459
  if (!cur.left && !cur.right)
534
460
  return;
535
- cur.left && _traverse_4(cur.left);
536
- cur.right && _traverse_4(cur.right);
461
+ cur.left && _traverse(cur.left);
462
+ cur.right && _traverse(cur.right);
537
463
  };
538
- _traverse_4(this.root);
464
+ _traverse(this.root);
539
465
  }
540
466
  else {
541
- var queue = [this.root];
467
+ const queue = [this.root];
542
468
  while (queue.length > 0) {
543
- var cur = queue.shift();
469
+ const cur = queue.shift();
544
470
  if (cur) {
545
471
  if (cur.count === nodeProperty) {
546
472
  result.push(cur);
@@ -553,16 +479,16 @@ var TreeMultiset = /** @class */ (function (_super) {
553
479
  }
554
480
  }
555
481
  return result;
556
- };
482
+ }
557
483
  /**
558
484
  * The BFSCount function returns an array of counts from a breadth-first search of nodes.
559
485
  * @returns The BFSCount() function returns an array of numbers, specifically the count property of each node in the
560
486
  * BFS traversal.
561
487
  */
562
- TreeMultiset.prototype.BFSCount = function () {
563
- var nodes = _super.prototype.BFS.call(this, 'node');
564
- return nodes.map(function (node) { return node.count; });
565
- };
488
+ BFSCount() {
489
+ const nodes = super.BFS('node');
490
+ return nodes.map(node => node.count);
491
+ }
566
492
  /**
567
493
  * The function "listLevelsCount" takes a node and returns an array of arrays, where each inner array contains the
568
494
  * count property of each node at that level.
@@ -571,10 +497,10 @@ var TreeMultiset = /** @class */ (function (_super) {
571
497
  * @returns a 2D array of numbers. Each inner array represents a level in the binary tree, and each number in the inner
572
498
  * array represents the count property of a node in that level.
573
499
  */
574
- TreeMultiset.prototype.listLevelsCount = function (node) {
575
- var levels = _super.prototype.listLevels.call(this, node, 'node');
576
- return levels.map(function (level) { return level.map(function (node) { return node.count; }); });
577
- };
500
+ listLevelsCount(node) {
501
+ const levels = super.listLevels(node, 'node');
502
+ return levels.map(level => level.map(node => node.count));
503
+ }
578
504
  /**
579
505
  * The `morrisCount` function returns an array of counts for each node in a binary tree, based on a specified traversal
580
506
  * pattern.
@@ -582,11 +508,11 @@ var TreeMultiset = /** @class */ (function (_super) {
582
508
  * traversal pattern for the Morris traversal algorithm. It can have one of three values: 'in', 'pre', or 'post'.
583
509
  * @returns The function `morrisCount` returns an array of numbers.
584
510
  */
585
- TreeMultiset.prototype.morrisCount = function (pattern) {
511
+ morrisCount(pattern) {
586
512
  pattern = pattern || 'in';
587
- var nodes = _super.prototype.morris.call(this, pattern, 'node');
588
- return nodes.map(function (node) { return node.count; });
589
- };
513
+ const nodes = super.morris(pattern, 'node');
514
+ return nodes.map(node => node.count);
515
+ }
590
516
  /**
591
517
  * The function DFSIterativeCount performs a depth-first search iteratively and returns an array of count values for
592
518
  * each node.
@@ -597,11 +523,11 @@ var TreeMultiset = /** @class */ (function (_super) {
597
523
  * `'node'`, the function will return the nodes. If it is set to `'property'`, the function will return the property
598
524
  * @returns The DFSIterativeCount method returns an array of numbers.
599
525
  */
600
- TreeMultiset.prototype.DFSIterativeCount = function (pattern, nodeOrPropertyName) {
526
+ DFSIterativeCount(pattern, nodeOrPropertyName) {
601
527
  pattern = pattern !== null && pattern !== void 0 ? pattern : 'in';
602
- var nodes = _super.prototype.DFSIterative.call(this, pattern, 'node');
603
- return nodes.map(function (node) { return node.count; });
604
- };
528
+ const nodes = super.DFSIterative(pattern, 'node');
529
+ return nodes.map(node => node.count);
530
+ }
605
531
  /**
606
532
  * The DFSCount function returns an array of counts for each node in a depth-first search traversal.
607
533
  * @param {DFSOrderPattern} [pattern] - The `pattern` parameter is an optional parameter that specifies the order in
@@ -612,59 +538,58 @@ var TreeMultiset = /** @class */ (function (_super) {
612
538
  * @returns The DFSCount method returns an array of numbers representing the count property of each node in the DFS
613
539
  * traversal.
614
540
  */
615
- TreeMultiset.prototype.DFSCount = function (pattern, nodeOrPropertyName) {
541
+ DFSCount(pattern, nodeOrPropertyName) {
616
542
  pattern = pattern !== null && pattern !== void 0 ? pattern : 'in';
617
- var nodes = _super.prototype.DFS.call(this, pattern, 'node');
618
- return nodes.map(function (node) { return node.count; });
619
- };
543
+ const nodes = super.DFS(pattern, 'node');
544
+ return nodes.map(node => node.count);
545
+ }
620
546
  /**
621
547
  * The `lesserSumCount` function calculates the sum of the counts of all nodes in a binary tree that have a lesser
622
548
  * value than a given node.
623
549
  * @param {N | BinaryTreeNodeId | null} beginNode - The `beginNode` parameter can be one of the following:
624
550
  * @returns the sum of the counts of nodes in the binary tree that have a lesser value than the given beginNode.
625
551
  */
626
- TreeMultiset.prototype.lesserSumCount = function (beginNode) {
627
- var _this = this;
552
+ lesserSumCount(beginNode) {
628
553
  if (typeof beginNode === 'number')
629
554
  beginNode = this.get(beginNode, 'id');
630
555
  if (!beginNode)
631
556
  return 0;
632
557
  if (!this.root)
633
558
  return 0;
634
- var id = beginNode.id;
635
- var sum = 0;
559
+ const id = beginNode.id;
560
+ let sum = 0;
636
561
  if (this.loopType === types_1.LoopType.RECURSIVE) {
637
- var _traverse_5 = function (cur) {
638
- var compared = _this._compare(cur.id, id);
562
+ const _traverse = (cur) => {
563
+ const compared = this._compare(cur.id, id);
639
564
  if (compared === types_1.CP.eq) {
640
565
  if (cur.right)
641
- sum += _this.subTreeSumCount(cur.right);
566
+ sum += this.subTreeSumCount(cur.right);
642
567
  return;
643
568
  }
644
569
  else if (compared === types_1.CP.lt) {
645
570
  if (cur.left)
646
- sum += _this.subTreeSumCount(cur.left);
571
+ sum += this.subTreeSumCount(cur.left);
647
572
  sum += cur.count;
648
573
  if (cur.right)
649
- _traverse_5(cur.right);
574
+ _traverse(cur.right);
650
575
  else
651
576
  return;
652
577
  }
653
578
  else {
654
579
  if (cur.left)
655
- _traverse_5(cur.left);
580
+ _traverse(cur.left);
656
581
  else
657
582
  return;
658
583
  }
659
584
  };
660
- _traverse_5(this.root);
585
+ _traverse(this.root);
661
586
  }
662
587
  else {
663
- var queue = [this.root];
588
+ const queue = [this.root];
664
589
  while (queue.length > 0) {
665
- var cur = queue.shift();
590
+ const cur = queue.shift();
666
591
  if (cur) {
667
- var compared = this._compare(cur.id, id);
592
+ const compared = this._compare(cur.id, id);
668
593
  if (compared === types_1.CP.eq) {
669
594
  if (cur.right)
670
595
  sum += this.subTreeSumCount(cur.right);
@@ -689,7 +614,7 @@ var TreeMultiset = /** @class */ (function (_super) {
689
614
  }
690
615
  }
691
616
  return sum;
692
- };
617
+ }
693
618
  /**
694
619
  * The function `allGreaterNodesAddCount` updates the count property of all nodes in a binary tree that have an ID
695
620
  * greater than a given ID by a specified delta value.
@@ -698,36 +623,35 @@ var TreeMultiset = /** @class */ (function (_super) {
698
623
  * of each node should be increased.
699
624
  * @returns a boolean value.
700
625
  */
701
- TreeMultiset.prototype.allGreaterNodesAddCount = function (node, delta) {
702
- var _this = this;
626
+ allGreaterNodesAddCount(node, delta) {
703
627
  if (typeof node === 'number')
704
628
  node = this.get(node, 'id');
705
629
  if (!node)
706
630
  return false;
707
- var id = node.id;
631
+ const id = node.id;
708
632
  if (!this.root)
709
633
  return false;
710
634
  if (this.loopType === types_1.LoopType.RECURSIVE) {
711
- var _traverse_6 = function (cur) {
712
- var compared = _this._compare(cur.id, id);
635
+ const _traverse = (cur) => {
636
+ const compared = this._compare(cur.id, id);
713
637
  if (compared === types_1.CP.gt)
714
638
  cur.count += delta;
715
639
  if (!cur.left && !cur.right)
716
640
  return;
717
- if (cur.left && _this._compare(cur.left.id, id) === types_1.CP.gt)
718
- _traverse_6(cur.left);
719
- if (cur.right && _this._compare(cur.right.id, id) === types_1.CP.gt)
720
- _traverse_6(cur.right);
641
+ if (cur.left && this._compare(cur.left.id, id) === types_1.CP.gt)
642
+ _traverse(cur.left);
643
+ if (cur.right && this._compare(cur.right.id, id) === types_1.CP.gt)
644
+ _traverse(cur.right);
721
645
  };
722
- _traverse_6(this.root);
646
+ _traverse(this.root);
723
647
  return true;
724
648
  }
725
649
  else {
726
- var queue = [this.root];
650
+ const queue = [this.root];
727
651
  while (queue.length > 0) {
728
- var cur = queue.shift();
652
+ const cur = queue.shift();
729
653
  if (cur) {
730
- var compared = this._compare(cur.id, id);
654
+ const compared = this._compare(cur.id, id);
731
655
  if (compared === types_1.CP.gt)
732
656
  cur.count += delta;
733
657
  if (cur.left && this._compare(cur.left.id, id) === types_1.CP.gt)
@@ -738,21 +662,20 @@ var TreeMultiset = /** @class */ (function (_super) {
738
662
  }
739
663
  return true;
740
664
  }
741
- };
665
+ }
742
666
  /**
743
667
  * The clear() function clears the data and sets the count to 0.
744
668
  */
745
- TreeMultiset.prototype.clear = function () {
746
- _super.prototype.clear.call(this);
669
+ clear() {
670
+ super.clear();
747
671
  this._setCount(0);
748
- };
672
+ }
749
673
  /**
750
674
  * The function "_setCount" is used to set the value of the "_count" property.
751
675
  * @param {number} v - number
752
676
  */
753
- TreeMultiset.prototype._setCount = function (v) {
677
+ _setCount(v) {
754
678
  this._count = v;
755
- };
756
- return TreeMultiset;
757
- }(avl_tree_1.AVLTree));
679
+ }
680
+ }
758
681
  exports.TreeMultiset = TreeMultiset;