data-structure-typed 1.50.7 → 1.50.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 (69) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +73 -67
  3. package/benchmark/report.html +1 -37
  4. package/benchmark/report.json +15 -393
  5. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +14 -3
  6. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +17 -6
  7. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
  8. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +1 -1
  9. package/dist/cjs/data-structures/binary-tree/binary-tree.js +33 -34
  10. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  11. package/dist/cjs/data-structures/binary-tree/bst.d.ts +22 -3
  12. package/dist/cjs/data-structures/binary-tree/bst.js +78 -39
  13. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  14. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +5 -5
  15. package/dist/cjs/data-structures/binary-tree/rb-tree.js +47 -50
  16. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  17. package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +40 -23
  18. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +44 -28
  19. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  20. package/dist/cjs/data-structures/heap/heap.d.ts +1 -1
  21. package/dist/cjs/data-structures/heap/heap.js +5 -5
  22. package/dist/cjs/types/common.d.ts +6 -29
  23. package/dist/cjs/types/common.js +0 -40
  24. package/dist/cjs/types/common.js.map +1 -1
  25. package/dist/cjs/types/data-structures/binary-tree/rb-tree.d.ts +1 -4
  26. package/dist/cjs/types/data-structures/binary-tree/rb-tree.js +0 -6
  27. package/dist/cjs/types/data-structures/binary-tree/rb-tree.js.map +1 -1
  28. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +14 -3
  29. package/dist/mjs/data-structures/binary-tree/avl-tree-multi-map.js +17 -6
  30. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +1 -1
  31. package/dist/mjs/data-structures/binary-tree/binary-tree.js +33 -34
  32. package/dist/mjs/data-structures/binary-tree/bst.d.ts +22 -3
  33. package/dist/mjs/data-structures/binary-tree/bst.js +78 -39
  34. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +5 -5
  35. package/dist/mjs/data-structures/binary-tree/rb-tree.js +47 -50
  36. package/dist/mjs/data-structures/binary-tree/tree-multi-map.d.ts +40 -23
  37. package/dist/mjs/data-structures/binary-tree/tree-multi-map.js +44 -28
  38. package/dist/mjs/data-structures/heap/heap.d.ts +1 -1
  39. package/dist/mjs/data-structures/heap/heap.js +5 -5
  40. package/dist/mjs/types/common.d.ts +6 -29
  41. package/dist/mjs/types/common.js +1 -39
  42. package/dist/mjs/types/data-structures/binary-tree/rb-tree.d.ts +1 -4
  43. package/dist/mjs/types/data-structures/binary-tree/rb-tree.js +1 -5
  44. package/dist/umd/data-structure-typed.js +212 -206
  45. package/dist/umd/data-structure-typed.min.js +2 -2
  46. package/dist/umd/data-structure-typed.min.js.map +1 -1
  47. package/package.json +6 -6
  48. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +20 -7
  49. package/src/data-structures/binary-tree/binary-tree.ts +54 -45
  50. package/src/data-structures/binary-tree/bst.ts +86 -42
  51. package/src/data-structures/binary-tree/rb-tree.ts +49 -49
  52. package/src/data-structures/binary-tree/tree-multi-map.ts +48 -29
  53. package/src/data-structures/heap/heap.ts +5 -5
  54. package/src/types/common.ts +6 -30
  55. package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
  56. package/test/integration/all-in-one.test.ts +4 -4
  57. package/test/integration/avl-tree.test.ts +1 -1
  58. package/test/integration/bst.test.ts +2 -2
  59. package/test/performance/data-structures/binary-tree/avl-tree.test.ts +20 -15
  60. package/test/performance/data-structures/binary-tree/binary-tree.test.ts +1 -1
  61. package/test/performance/data-structures/binary-tree/rb-tree.test.ts +13 -22
  62. package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +15 -23
  63. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +8 -8
  64. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +127 -74
  65. package/test/unit/data-structures/binary-tree/bst.test.ts +20 -20
  66. package/test/unit/data-structures/binary-tree/overall.test.ts +7 -7
  67. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +31 -26
  68. package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +26 -34
  69. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +3 -3
@@ -229,26 +229,26 @@ class Heap extends base_1.IterableElementBase {
229
229
  * Space Complexity: O(log n)
230
230
  *
231
231
  * Depth-first search (DFS) method, different traversal orders can be selected。
232
- * @param order - Traverse order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
232
+ * @param order - Traverse order parameter: 'IN' (in-order), 'PRE' (pre-order) or 'POST' (post-order).
233
233
  * @returns An array containing elements traversed in the specified order.
234
234
  */
235
- dfs(order = 'pre') {
235
+ dfs(order = 'PRE') {
236
236
  const result = [];
237
237
  // Auxiliary recursive function, traverses the binary heap according to the traversal order
238
238
  const _dfs = (index) => {
239
239
  const left = 2 * index + 1, right = left + 1;
240
240
  if (index < this.size) {
241
- if (order === 'in') {
241
+ if (order === 'IN') {
242
242
  _dfs(left);
243
243
  result.push(this.elements[index]);
244
244
  _dfs(right);
245
245
  }
246
- else if (order === 'pre') {
246
+ else if (order === 'PRE') {
247
247
  result.push(this.elements[index]);
248
248
  _dfs(left);
249
249
  _dfs(right);
250
250
  }
251
- else if (order === 'post') {
251
+ else if (order === 'POST') {
252
252
  _dfs(left);
253
253
  _dfs(right);
254
254
  result.push(this.elements[index]);
@@ -1,33 +1,15 @@
1
- export declare enum BSTVariant {
2
- STANDARD = "STANDARD",
3
- INVERSE = "INVERSE"
4
- }
5
- export declare enum CP {
6
- lt = "lt",
7
- eq = "eq",
8
- gt = "gt"
9
- }
1
+ export type BSTVariant = 'STANDARD' | 'INVERSE';
2
+ export type CP = 'LT' | 'EQ' | 'GT';
10
3
  /**
11
4
  * Enum representing different loop types.
12
5
  *
13
6
  * - `iterative`: Indicates the iterative loop type (with loops that use iterations).
14
7
  * - `recursive`: Indicates the recursive loop type (with loops that call themselves).
15
8
  */
16
- export declare enum IterationType {
17
- ITERATIVE = "ITERATIVE",
18
- RECURSIVE = "RECURSIVE"
19
- }
20
- export declare enum FamilyPosition {
21
- ROOT = "ROOT",
22
- LEFT = "LEFT",
23
- RIGHT = "RIGHT",
24
- ROOT_LEFT = "ROOT_LEFT",
25
- ROOT_RIGHT = "ROOT_RIGHT",
26
- ISOLATED = "ISOLATED",
27
- MAL_NODE = "MAL_NODE"
28
- }
9
+ export type IterationType = 'ITERATIVE' | 'RECURSIVE';
10
+ export type FamilyPosition = 'ROOT' | 'LEFT' | 'RIGHT' | 'ROOT_LEFT' | 'ROOT_RIGHT' | 'ISOLATED' | 'MAL_NODE';
29
11
  export type Comparator<K> = (a: K, b: K) => number;
30
- export type DFSOrderPattern = 'pre' | 'in' | 'post';
12
+ export type DFSOrderPattern = 'PRE' | 'IN' | 'POST';
31
13
  export type NodeDisplayLayout = [string[], number, number, number];
32
14
  export type BTNCallback<N, D = any> = (node: N) => D;
33
15
  export interface IterableWithSize<T> extends Iterable<T> {
@@ -52,9 +34,4 @@ export type BinaryTreeDeleteResult<N> = {
52
34
  deleted: N | null | undefined;
53
35
  needBalanced: N | null | undefined;
54
36
  };
55
- export declare enum CRUD {
56
- CREATED = "CREATED",
57
- READ = "READ",
58
- UPDATED = "UPDATED",
59
- DELETED = "DELETED"
60
- }
37
+ export type CRUD = 'CREATED' | 'READ' | 'UPDATED' | 'DELETED';
@@ -1,43 +1,3 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CRUD = exports.FamilyPosition = exports.IterationType = exports.CP = exports.BSTVariant = void 0;
4
- var BSTVariant;
5
- (function (BSTVariant) {
6
- BSTVariant["STANDARD"] = "STANDARD";
7
- BSTVariant["INVERSE"] = "INVERSE";
8
- })(BSTVariant || (exports.BSTVariant = BSTVariant = {}));
9
- var CP;
10
- (function (CP) {
11
- CP["lt"] = "lt";
12
- CP["eq"] = "eq";
13
- CP["gt"] = "gt";
14
- })(CP || (exports.CP = CP = {}));
15
- /**
16
- * Enum representing different loop types.
17
- *
18
- * - `iterative`: Indicates the iterative loop type (with loops that use iterations).
19
- * - `recursive`: Indicates the recursive loop type (with loops that call themselves).
20
- */
21
- var IterationType;
22
- (function (IterationType) {
23
- IterationType["ITERATIVE"] = "ITERATIVE";
24
- IterationType["RECURSIVE"] = "RECURSIVE";
25
- })(IterationType || (exports.IterationType = IterationType = {}));
26
- var FamilyPosition;
27
- (function (FamilyPosition) {
28
- FamilyPosition["ROOT"] = "ROOT";
29
- FamilyPosition["LEFT"] = "LEFT";
30
- FamilyPosition["RIGHT"] = "RIGHT";
31
- FamilyPosition["ROOT_LEFT"] = "ROOT_LEFT";
32
- FamilyPosition["ROOT_RIGHT"] = "ROOT_RIGHT";
33
- FamilyPosition["ISOLATED"] = "ISOLATED";
34
- FamilyPosition["MAL_NODE"] = "MAL_NODE";
35
- })(FamilyPosition || (exports.FamilyPosition = FamilyPosition = {}));
36
- var CRUD;
37
- (function (CRUD) {
38
- CRUD["CREATED"] = "CREATED";
39
- CRUD["READ"] = "READ";
40
- CRUD["UPDATED"] = "UPDATED";
41
- CRUD["DELETED"] = "DELETED";
42
- })(CRUD || (exports.CRUD = CRUD = {}));
43
3
  //# sourceMappingURL=common.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"common.js","sourceRoot":"","sources":["../../../src/types/common.ts"],"names":[],"mappings":";;;AAAA,IAAY,UAGX;AAHD,WAAY,UAAU;IACpB,mCAAqB,CAAA;IACrB,iCAAmB,CAAA;AACrB,CAAC,EAHW,UAAU,0BAAV,UAAU,QAGrB;AAED,IAAY,EAIX;AAJD,WAAY,EAAE;IACZ,eAAS,CAAA;IACT,eAAS,CAAA;IACT,eAAS,CAAA;AACX,CAAC,EAJW,EAAE,kBAAF,EAAE,QAIb;AAED;;;;;GAKG;AACH,IAAY,aAGX;AAHD,WAAY,aAAa;IACvB,wCAAuB,CAAA;IACvB,wCAAuB,CAAA;AACzB,CAAC,EAHW,aAAa,6BAAb,aAAa,QAGxB;AAED,IAAY,cAQX;AARD,WAAY,cAAc;IACxB,+BAAa,CAAA;IACb,+BAAa,CAAA;IACb,iCAAe,CAAA;IACf,yCAAuB,CAAA;IACvB,2CAAyB,CAAA;IACzB,uCAAqB,CAAA;IACrB,uCAAqB,CAAA;AACvB,CAAC,EARW,cAAc,8BAAd,cAAc,QAQzB;AAoCD,IAAY,IAKX;AALD,WAAY,IAAI;IACd,2BAAmB,CAAA;IACnB,qBAAa,CAAA;IACb,2BAAmB,CAAA;IACnB,2BAAmB,CAAA;AACrB,CAAC,EALW,IAAI,oBAAJ,IAAI,QAKf"}
1
+ {"version":3,"file":"common.js","sourceRoot":"","sources":["../../../src/types/common.ts"],"names":[],"mappings":""}
@@ -1,9 +1,6 @@
1
1
  import { RedBlackTree, RedBlackTreeNode } from '../../../data-structures';
2
2
  import type { BSTOptions } from "./bst";
3
- export declare enum RBTNColor {
4
- RED = 1,
5
- BLACK = 0
6
- }
3
+ export type RBTNColor = 'RED' | 'BLACK';
7
4
  export type RedBlackTreeNodeNested<K, V> = RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, RedBlackTreeNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
8
5
  export type RedBlackTreeNested<K, V, N extends RedBlackTreeNode<K, V, N>> = RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, RedBlackTree<K, V, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
9
6
  export type RBTreeOptions<K> = Omit<BSTOptions<K>, 'variant'> & {};
@@ -1,9 +1,3 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RBTNColor = void 0;
4
- var RBTNColor;
5
- (function (RBTNColor) {
6
- RBTNColor[RBTNColor["RED"] = 1] = "RED";
7
- RBTNColor[RBTNColor["BLACK"] = 0] = "BLACK";
8
- })(RBTNColor || (exports.RBTNColor = RBTNColor = {}));
9
3
  //# sourceMappingURL=rb-tree.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"rb-tree.js","sourceRoot":"","sources":["../../../../../src/types/data-structures/binary-tree/rb-tree.ts"],"names":[],"mappings":";;;AAGA,IAAY,SAA+B;AAA3C,WAAY,SAAS;IAAG,uCAAO,CAAA;IAAE,2CAAS,CAAA;AAAA,CAAC,EAA/B,SAAS,yBAAT,SAAS,QAAsB"}
1
+ {"version":3,"file":"rb-tree.js","sourceRoot":"","sources":["../../../../../src/types/data-structures/binary-tree/rb-tree.ts"],"names":[],"mappings":""}
@@ -5,8 +5,7 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { AVLTreeMultiMapNested, AVLTreeMultiMapNodeNested, AVLTreeMultiMapOptions, BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, KeyOrNodeOrEntry } from '../../types';
9
- import { IterationType } from '../../types';
8
+ import type { AVLTreeMultiMapNested, AVLTreeMultiMapNodeNested, AVLTreeMultiMapOptions, BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, IterationType, KeyOrNodeOrEntry } from '../../types';
10
9
  import { IBinaryTree } from '../../interfaces';
11
10
  import { AVLTree, AVLTreeNode } from './avl-tree';
12
11
  export declare class AVLTreeMultiMapNode<K = any, V = any, NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNodeNested<K, V>> extends AVLTreeNode<K, V, NODE> {
@@ -46,7 +45,19 @@ export declare class AVLTreeMultiMap<K = any, V = any, NODE extends AVLTreeMulti
46
45
  * @returns the sum of the count property of all nodes in the tree.
47
46
  */
48
47
  get count(): number;
49
- getMutableCount(): number;
48
+ /**
49
+ * Time Complexity: O(n)
50
+ * Space Complexity: O(1)
51
+ */
52
+ /**
53
+ * Time Complexity: O(n)
54
+ * Space Complexity: O(1)
55
+ *
56
+ * The function calculates the sum of the count property of all nodes in a tree using depth-first
57
+ * search.
58
+ * @returns the sum of the count property of all nodes in the tree.
59
+ */
60
+ getComputedCount(): number;
50
61
  /**
51
62
  * The function creates a new BSTNode with the given key, value, and count.
52
63
  * @param {K} key - The key parameter is the unique identifier for the binary tree node. It is used to
@@ -1,4 +1,3 @@
1
- import { FamilyPosition, IterationType } from '../../types';
2
1
  import { AVLTree, AVLTreeNode } from './avl-tree';
3
2
  export class AVLTreeMultiMapNode extends AVLTreeNode {
4
3
  /**
@@ -51,7 +50,19 @@ export class AVLTreeMultiMap extends AVLTree {
51
50
  get count() {
52
51
  return this._count;
53
52
  }
54
- getMutableCount() {
53
+ /**
54
+ * Time Complexity: O(n)
55
+ * Space Complexity: O(1)
56
+ */
57
+ /**
58
+ * Time Complexity: O(n)
59
+ * Space Complexity: O(1)
60
+ *
61
+ * The function calculates the sum of the count property of all nodes in a tree using depth-first
62
+ * search.
63
+ * @returns the sum of the count property of all nodes in the tree.
64
+ */
65
+ getComputedCount() {
55
66
  let sum = 0;
56
67
  this.dfs(node => (sum += node.count));
57
68
  return sum;
@@ -204,10 +215,10 @@ export class AVLTreeMultiMap extends AVLTree {
204
215
  }
205
216
  else {
206
217
  const { familyPosition: fp } = curr;
207
- if (fp === FamilyPosition.LEFT || fp === FamilyPosition.ROOT_LEFT) {
218
+ if (fp === 'LEFT' || fp === 'ROOT_LEFT') {
208
219
  parent.left = curr.right;
209
220
  }
210
- else if (fp === FamilyPosition.RIGHT || fp === FamilyPosition.ROOT_RIGHT) {
221
+ else if (fp === 'RIGHT' || fp === 'ROOT_RIGHT') {
211
222
  parent.right = curr.right;
212
223
  }
213
224
  needBalanced = parent;
@@ -270,11 +281,11 @@ export class AVLTreeMultiMap extends AVLTree {
270
281
  * @returns a boolean value.
271
282
  */
272
283
  perfectlyBalance(iterationType = this.iterationType) {
273
- const sorted = this.dfs(node => node, 'in'), n = sorted.length;
284
+ const sorted = this.dfs(node => node, 'IN'), n = sorted.length;
274
285
  if (sorted.length < 1)
275
286
  return false;
276
287
  this.clear();
277
- if (iterationType === IterationType.RECURSIVE) {
288
+ if (iterationType === 'RECURSIVE') {
278
289
  const buildBalanceBST = (l, r) => {
279
290
  if (l > r)
280
291
  return;
@@ -135,7 +135,7 @@ export declare class BinaryTree<K = any, V = any, NODE extends BinaryTreeNode<K,
135
135
  * `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
136
136
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
137
137
  * type of iteration to be used when searching for a node by key. It has a default value of
138
- * `IterationType.ITERATIVE`.
138
+ * `'ITERATIVE'`.
139
139
  * @returns either the node corresponding to the given key if it is a valid node key, or the key
140
140
  * itself if it is not a valid node key.
141
141
  */
@@ -5,7 +5,6 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import { FamilyPosition, IterationType } from '../../types';
9
8
  import { trampoline } from '../../utils';
10
9
  import { Queue } from '../queue';
11
10
  import { IterableEntryBase } from '../base';
@@ -77,15 +76,15 @@ export class BinaryTreeNode {
77
76
  get familyPosition() {
78
77
  const that = this;
79
78
  if (!this.parent) {
80
- return this.left || this.right ? FamilyPosition.ROOT : FamilyPosition.ISOLATED;
79
+ return this.left || this.right ? 'ROOT' : 'ISOLATED';
81
80
  }
82
81
  if (this.parent.left === that) {
83
- return this.left || this.right ? FamilyPosition.ROOT_LEFT : FamilyPosition.LEFT;
82
+ return this.left || this.right ? 'ROOT_LEFT' : 'LEFT';
84
83
  }
85
84
  else if (this.parent.right === that) {
86
- return this.left || this.right ? FamilyPosition.ROOT_RIGHT : FamilyPosition.RIGHT;
85
+ return this.left || this.right ? 'ROOT_RIGHT' : 'RIGHT';
87
86
  }
88
- return FamilyPosition.MAL_NODE;
87
+ return 'MAL_NODE';
89
88
  }
90
89
  }
91
90
  /**
@@ -96,7 +95,7 @@ export class BinaryTreeNode {
96
95
  * 5. Leaf Nodes: Nodes without children are leaves.
97
96
  */
98
97
  export class BinaryTree extends IterableEntryBase {
99
- iterationType = IterationType.ITERATIVE;
98
+ iterationType = 'ITERATIVE';
100
99
  /**
101
100
  * The constructor function initializes a binary tree object with optional keysOrNodesOrEntries and options.
102
101
  * @param [keysOrNodesOrEntries] - An optional iterable of KeyOrNodeOrEntry objects. These objects represent the
@@ -119,7 +118,7 @@ export class BinaryTree extends IterableEntryBase {
119
118
  if (keysOrNodesOrEntries)
120
119
  this.addMany(keysOrNodesOrEntries);
121
120
  }
122
- _extractor = (key) => Number(key);
121
+ _extractor = (key) => (typeof key === 'number' ? key : Number(key));
123
122
  /**
124
123
  * The function returns the value of the `_extractor` property.
125
124
  * @returns The `_extractor` property is being returned.
@@ -215,11 +214,11 @@ export class BinaryTree extends IterableEntryBase {
215
214
  * `null`, or `undefined`. It represents a key used to identify a node in a binary tree.
216
215
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
217
216
  * type of iteration to be used when searching for a node by key. It has a default value of
218
- * `IterationType.ITERATIVE`.
217
+ * `'ITERATIVE'`.
219
218
  * @returns either the node corresponding to the given key if it is a valid node key, or the key
220
219
  * itself if it is not a valid node key.
221
220
  */
222
- ensureNode(keyOrNodeOrEntry, iterationType = IterationType.ITERATIVE) {
221
+ ensureNode(keyOrNodeOrEntry, iterationType = 'ITERATIVE') {
223
222
  let res;
224
223
  if (this.isRealNode(keyOrNodeOrEntry)) {
225
224
  res = keyOrNodeOrEntry;
@@ -446,10 +445,10 @@ export class BinaryTree extends IterableEntryBase {
446
445
  }
447
446
  else if (parent) {
448
447
  const { familyPosition: fp } = curr;
449
- if (fp === FamilyPosition.LEFT || fp === FamilyPosition.ROOT_LEFT) {
448
+ if (fp === 'LEFT' || fp === 'ROOT_LEFT') {
450
449
  parent.left = curr.right;
451
450
  }
452
- else if (fp === FamilyPosition.RIGHT || fp === FamilyPosition.ROOT_RIGHT) {
451
+ else if (fp === 'RIGHT' || fp === 'ROOT_RIGHT') {
453
452
  parent.right = curr.right;
454
453
  }
455
454
  needBalanced = parent;
@@ -498,7 +497,7 @@ export class BinaryTree extends IterableEntryBase {
498
497
  if (!beginRoot)
499
498
  return [];
500
499
  const ans = [];
501
- if (iterationType === IterationType.RECURSIVE) {
500
+ if (iterationType === 'RECURSIVE') {
502
501
  const _traverse = (cur) => {
503
502
  if (callback(cur) === identifier) {
504
503
  ans.push(cur);
@@ -577,10 +576,10 @@ export class BinaryTree extends IterableEntryBase {
577
576
  * @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
578
577
  * found in the binary tree. If no node is found, it returns `undefined`.
579
578
  */
580
- getNodeByKey(key, iterationType = IterationType.ITERATIVE) {
579
+ getNodeByKey(key, iterationType = 'ITERATIVE') {
581
580
  if (!this.root)
582
581
  return undefined;
583
- if (iterationType === IterationType.RECURSIVE) {
582
+ if (iterationType === 'RECURSIVE') {
584
583
  const _dfs = (cur) => {
585
584
  if (cur.key === key)
586
585
  return cur;
@@ -734,7 +733,7 @@ export class BinaryTree extends IterableEntryBase {
734
733
  beginRoot = this.ensureNode(beginRoot);
735
734
  if (!beginRoot)
736
735
  return true;
737
- if (iterationType === IterationType.RECURSIVE) {
736
+ if (iterationType === 'RECURSIVE') {
738
737
  const dfs = (cur, min, max) => {
739
738
  if (!this.isRealNode(cur))
740
739
  return true;
@@ -823,7 +822,7 @@ export class BinaryTree extends IterableEntryBase {
823
822
  beginRoot = this.ensureNode(beginRoot);
824
823
  if (!this.isRealNode(beginRoot))
825
824
  return -1;
826
- if (iterationType === IterationType.RECURSIVE) {
825
+ if (iterationType === 'RECURSIVE') {
827
826
  const _getMaxHeight = (cur) => {
828
827
  if (!this.isRealNode(cur))
829
828
  return -1;
@@ -868,7 +867,7 @@ export class BinaryTree extends IterableEntryBase {
868
867
  beginRoot = this.ensureNode(beginRoot);
869
868
  if (!beginRoot)
870
869
  return -1;
871
- if (iterationType === IterationType.RECURSIVE) {
870
+ if (iterationType === 'RECURSIVE') {
872
871
  const _getMinHeight = (cur) => {
873
872
  if (!this.isRealNode(cur))
874
873
  return 0;
@@ -966,7 +965,7 @@ export class BinaryTree extends IterableEntryBase {
966
965
  beginRoot = this.ensureNode(beginRoot);
967
966
  if (!this.isRealNode(beginRoot))
968
967
  return beginRoot;
969
- if (iterationType === IterationType.RECURSIVE) {
968
+ if (iterationType === 'RECURSIVE') {
970
969
  const _traverse = (cur) => {
971
970
  if (!this.isRealNode(cur.left))
972
971
  return cur;
@@ -1010,7 +1009,7 @@ export class BinaryTree extends IterableEntryBase {
1010
1009
  beginRoot = this.ensureNode(beginRoot);
1011
1010
  if (!beginRoot)
1012
1011
  return beginRoot;
1013
- if (iterationType === IterationType.RECURSIVE) {
1012
+ if (iterationType === 'RECURSIVE') {
1014
1013
  const _traverse = (cur) => {
1015
1014
  if (!this.isRealNode(cur.right))
1016
1015
  return cur;
@@ -1110,15 +1109,15 @@ export class BinaryTree extends IterableEntryBase {
1110
1109
  * `false`, null or undefined
1111
1110
  * @returns an array of values that are the return values of the callback function.
1112
1111
  */
1113
- dfs(callback = this._defaultOneParamCallback, pattern = 'in', beginRoot = this.root, iterationType = IterationType.ITERATIVE, includeNull = false) {
1112
+ dfs(callback = this._defaultOneParamCallback, pattern = 'IN', beginRoot = this.root, iterationType = 'ITERATIVE', includeNull = false) {
1114
1113
  beginRoot = this.ensureNode(beginRoot);
1115
1114
  if (!beginRoot)
1116
1115
  return [];
1117
1116
  const ans = [];
1118
- if (iterationType === IterationType.RECURSIVE) {
1117
+ if (iterationType === 'RECURSIVE') {
1119
1118
  const _traverse = (node) => {
1120
1119
  switch (pattern) {
1121
- case 'in':
1120
+ case 'IN':
1122
1121
  if (includeNull) {
1123
1122
  if (this.isRealNode(node) && this.isNodeOrNull(node.left))
1124
1123
  _traverse(node.left);
@@ -1134,7 +1133,7 @@ export class BinaryTree extends IterableEntryBase {
1134
1133
  _traverse(node.right);
1135
1134
  }
1136
1135
  break;
1137
- case 'pre':
1136
+ case 'PRE':
1138
1137
  if (includeNull) {
1139
1138
  this.isNodeOrNull(node) && ans.push(callback(node));
1140
1139
  if (this.isRealNode(node) && this.isNodeOrNull(node.left))
@@ -1150,7 +1149,7 @@ export class BinaryTree extends IterableEntryBase {
1150
1149
  _traverse(node.right);
1151
1150
  }
1152
1151
  break;
1153
- case 'post':
1152
+ case 'POST':
1154
1153
  if (includeNull) {
1155
1154
  if (this.isRealNode(node) && this.isNodeOrNull(node.left))
1156
1155
  _traverse(node.left);
@@ -1190,17 +1189,17 @@ export class BinaryTree extends IterableEntryBase {
1190
1189
  }
1191
1190
  else {
1192
1191
  switch (pattern) {
1193
- case 'in':
1192
+ case 'IN':
1194
1193
  cur.node && stack.push({ opt: 0, node: cur.node.right });
1195
1194
  stack.push({ opt: 1, node: cur.node });
1196
1195
  cur.node && stack.push({ opt: 0, node: cur.node.left });
1197
1196
  break;
1198
- case 'pre':
1197
+ case 'PRE':
1199
1198
  cur.node && stack.push({ opt: 0, node: cur.node.right });
1200
1199
  cur.node && stack.push({ opt: 0, node: cur.node.left });
1201
1200
  stack.push({ opt: 1, node: cur.node });
1202
1201
  break;
1203
- case 'post':
1202
+ case 'POST':
1204
1203
  stack.push({ opt: 1, node: cur.node });
1205
1204
  cur.node && stack.push({ opt: 0, node: cur.node.right });
1206
1205
  cur.node && stack.push({ opt: 0, node: cur.node.left });
@@ -1246,7 +1245,7 @@ export class BinaryTree extends IterableEntryBase {
1246
1245
  if (!beginRoot)
1247
1246
  return [];
1248
1247
  const ans = [];
1249
- if (iterationType === IterationType.RECURSIVE) {
1248
+ if (iterationType === 'RECURSIVE') {
1250
1249
  const queue = new Queue([beginRoot]);
1251
1250
  const traverse = (level) => {
1252
1251
  if (queue.size === 0)
@@ -1323,7 +1322,7 @@ export class BinaryTree extends IterableEntryBase {
1323
1322
  const levelsNodes = [];
1324
1323
  if (!beginRoot)
1325
1324
  return levelsNodes;
1326
- if (iterationType === IterationType.RECURSIVE) {
1325
+ if (iterationType === 'RECURSIVE') {
1327
1326
  const _recursive = (node, level) => {
1328
1327
  if (!levelsNodes[level])
1329
1328
  levelsNodes[level] = [];
@@ -1390,7 +1389,7 @@ export class BinaryTree extends IterableEntryBase {
1390
1389
  * `callback` function on each node in the binary tree. The type of the array nodes is determined
1391
1390
  * by the return type of the `callback` function.
1392
1391
  */
1393
- morris(callback = this._defaultOneParamCallback, pattern = 'in', beginRoot = this.root) {
1392
+ morris(callback = this._defaultOneParamCallback, pattern = 'IN', beginRoot = this.root) {
1394
1393
  beginRoot = this.ensureNode(beginRoot);
1395
1394
  if (beginRoot === null)
1396
1395
  return [];
@@ -1417,7 +1416,7 @@ export class BinaryTree extends IterableEntryBase {
1417
1416
  _reverseEdge(tail);
1418
1417
  };
1419
1418
  switch (pattern) {
1420
- case 'in':
1419
+ case 'IN':
1421
1420
  while (cur) {
1422
1421
  if (cur.left) {
1423
1422
  const predecessor = this.getPredecessor(cur);
@@ -1434,7 +1433,7 @@ export class BinaryTree extends IterableEntryBase {
1434
1433
  cur = cur.right;
1435
1434
  }
1436
1435
  break;
1437
- case 'pre':
1436
+ case 'PRE':
1438
1437
  while (cur) {
1439
1438
  if (cur.left) {
1440
1439
  const predecessor = this.getPredecessor(cur);
@@ -1454,7 +1453,7 @@ export class BinaryTree extends IterableEntryBase {
1454
1453
  cur = cur.right;
1455
1454
  }
1456
1455
  break;
1457
- case 'post':
1456
+ case 'POST':
1458
1457
  while (cur) {
1459
1458
  if (cur.left) {
1460
1459
  const predecessor = this.getPredecessor(cur);
@@ -1610,7 +1609,7 @@ export class BinaryTree extends IterableEntryBase {
1610
1609
  *_getIterator(node = this.root) {
1611
1610
  if (!node)
1612
1611
  return;
1613
- if (this.iterationType === IterationType.ITERATIVE) {
1612
+ if (this.iterationType === 'ITERATIVE') {
1614
1613
  const stack = [];
1615
1614
  let current = node;
1616
1615
  while (current || stack.length > 0) {
@@ -109,7 +109,7 @@ export declare class BST<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BS
109
109
  * @param {K | NODE | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `NODE`, or
110
110
  * `undefined`.
111
111
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
112
- * type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
112
+ * type of iteration to be performed. It has a default value of `'ITERATIVE'`.
113
113
  * @returns either a node object (NODE) or undefined.
114
114
  */
115
115
  ensureNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, iterationType?: IterationType): NODE | undefined;
@@ -372,8 +372,27 @@ export declare class BST<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BS
372
372
  * is greater than, less than, or equal to the second value.
373
373
  * @param {K} a - The parameter "a" is of type K.
374
374
  * @param {K} b - The parameter "b" in the above code represents a K.
375
- * @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater
376
- * than), CP.lt (less than), or CP.eq (equal).
375
+ * @returns a value of type CP (ComparisonResult). The possible return values are 'GT' (greater
376
+ * than), 'LT' (less than), or 'EQ' (equal).
377
377
  */
378
378
  protected _compare(a: K, b: K): CP;
379
+ /**
380
+ * The function `_lt` compares two values `a` and `b` using an extractor function and returns true if
381
+ * `a` is less than `b` based on the specified variant.
382
+ * @param {K} a - The parameter "a" is of type "K", which means it can be any type. It represents the
383
+ * first value to be compared in the function.
384
+ * @param {K} b - The parameter `b` is of type `K`, which means it can be any type. It is used as one
385
+ * of the arguments for the comparison in the `_lt` function.
386
+ * @returns a boolean value.
387
+ */
388
+ protected _lt(a: K, b: K): boolean;
389
+ /**
390
+ * The function compares two values using a custom extractor function and returns true if the first
391
+ * value is greater than the second value.
392
+ * @param {K} a - The parameter "a" is of type K, which means it can be any type.
393
+ * @param {K} b - The parameter "b" is of type K, which means it can be any type. It is used as one
394
+ * of the arguments for the comparison in the function.
395
+ * @returns a boolean value.
396
+ */
397
+ protected _gt(a: K, b: K): boolean;
379
398
  }