heap-typed 1.50.6 → 1.50.8

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.
@@ -126,7 +126,7 @@ export class BST<
126
126
  return this._root;
127
127
  }
128
128
 
129
- protected _variant = BSTVariant.STANDARD;
129
+ protected _variant: BSTVariant = 'STANDARD';
130
130
 
131
131
  /**
132
132
  * The function returns the value of the _variant property.
@@ -207,13 +207,10 @@ export class BST<
207
207
  * @param {K | NODE | undefined} keyOrNodeOrEntry - The `key` parameter can be of type `K`, `NODE`, or
208
208
  * `undefined`.
209
209
  * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
210
- * type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
210
+ * type of iteration to be performed. It has a default value of `'ITERATIVE'`.
211
211
  * @returns either a node object (NODE) or undefined.
212
212
  */
213
- override ensureNode(
214
- keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>,
215
- iterationType = IterationType.ITERATIVE
216
- ): NODE | undefined {
213
+ override ensureNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, iterationType = 'ITERATIVE'): NODE | undefined {
217
214
  let res: NODE | undefined;
218
215
  if (this.isRealNode(keyOrNodeOrEntry)) {
219
216
  res = keyOrNodeOrEntry;
@@ -263,7 +260,7 @@ export class BST<
263
260
 
264
261
  let current = this.root;
265
262
  while (current !== undefined) {
266
- if (this._compare(current.key, newNode.key) === CP.eq) {
263
+ if (this._compare(current.key, newNode.key) === 'EQ') {
267
264
  // if (current !== newNode) {
268
265
  // The key value is the same but the reference is different, update the value of the existing node
269
266
  this._replaceNode(current, newNode);
@@ -275,7 +272,7 @@ export class BST<
275
272
 
276
273
  // return;
277
274
  // }
278
- } else if (this._compare(current.key, newNode.key) === CP.gt) {
275
+ } else if (this._compare(current.key, newNode.key) === 'GT') {
279
276
  if (current.left === undefined) {
280
277
  current.left = newNode;
281
278
  this._size++;
@@ -397,7 +394,7 @@ export class BST<
397
394
  }
398
395
  };
399
396
 
400
- if (iterationType === IterationType.RECURSIVE) {
397
+ if (iterationType === 'RECURSIVE') {
401
398
  _dfs(sorted);
402
399
  } else {
403
400
  _iterate();
@@ -425,15 +422,15 @@ export class BST<
425
422
  * @returns The function `getNodeByKey` returns a node (`NODE`) if a node with the specified key is
426
423
  * found in the binary tree. If no node is found, it returns `undefined`.
427
424
  */
428
- override getNodeByKey(key: K, iterationType = IterationType.ITERATIVE): NODE | undefined {
429
- if (!this.root) return undefined;
430
- if (iterationType === IterationType.RECURSIVE) {
425
+ override getNodeByKey(key: K, iterationType = 'ITERATIVE'): NODE | undefined {
426
+ if (!this.isRealNode(this.root)) return undefined;
427
+ if (iterationType === 'RECURSIVE') {
431
428
  const _dfs = (cur: NODE): NODE | undefined => {
432
429
  if (cur.key === key) return cur;
433
- if (!cur.left && !cur.right) return;
430
+ if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
434
431
 
435
- if (this._compare(cur.key, key) === CP.gt && cur.left) return _dfs(cur.left);
436
- if (this._compare(cur.key, key) === CP.lt && cur.right) return _dfs(cur.right);
432
+ if (this._compare(cur.key, key) === 'GT' && this.isRealNode(cur.left)) return _dfs(cur.left);
433
+ if (this._compare(cur.key, key) === 'LT' && this.isRealNode(cur.right)) return _dfs(cur.right);
437
434
  };
438
435
 
439
436
  return _dfs(this.root);
@@ -441,10 +438,10 @@ export class BST<
441
438
  const queue = new Queue<NODE>([this.root]);
442
439
  while (queue.size > 0) {
443
440
  const cur = queue.shift();
444
- if (cur) {
445
- if (this._compare(cur.key, key) === CP.eq) return cur;
446
- if (this._compare(cur.key, key) === CP.gt) cur.left && queue.push(cur.left);
447
- if (this._compare(cur.key, key) === CP.lt) cur.right && queue.push(cur.right);
441
+ if (this.isRealNode(cur)) {
442
+ if (this._compare(cur.key, key) === 'EQ') return cur;
443
+ if (this._compare(cur.key, key) === 'GT') this.isRealNode(cur.left) && queue.push(cur.left);
444
+ if (this._compare(cur.key, key) === 'LT') this.isRealNode(cur.right) && queue.push(cur.right);
448
445
  }
449
446
  }
450
447
  }
@@ -489,7 +486,7 @@ export class BST<
489
486
  if (!beginRoot) return [];
490
487
  const ans: NODE[] = [];
491
488
 
492
- if (iterationType === IterationType.RECURSIVE) {
489
+ if (iterationType === 'RECURSIVE') {
493
490
  const _traverse = (cur: NODE) => {
494
491
  const callbackResult = callback(cur);
495
492
  if (callbackResult === identifier) {
@@ -497,23 +494,23 @@ export class BST<
497
494
  if (onlyOne) return;
498
495
  }
499
496
 
500
- if (!cur.left && !cur.right) return;
497
+ if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
501
498
  // TODO potential bug
502
499
  if (callback === this._defaultOneParamCallback) {
503
- if (this._compare(cur.key, identifier as K) === CP.gt) cur.left && _traverse(cur.left);
504
- if (this._compare(cur.key, identifier as K) === CP.lt) cur.right && _traverse(cur.right);
500
+ if (this.isRealNode(cur.left) && this._compare(cur.key, identifier as K) === 'GT') _traverse(cur.left);
501
+ if (this.isRealNode(cur.right) && this._compare(cur.key, identifier as K) === 'LT') _traverse(cur.right);
505
502
  } else {
506
- cur.left && _traverse(cur.left);
507
- cur.right && _traverse(cur.right);
503
+ this.isRealNode(cur.left) && _traverse(cur.left);
504
+ this.isRealNode(cur.right) && _traverse(cur.right);
508
505
  }
509
506
  };
510
507
 
511
508
  _traverse(beginRoot);
512
509
  } else {
513
- const queue = new Queue<NODE>([beginRoot]);
514
- while (queue.size > 0) {
515
- const cur = queue.shift();
516
- if (cur) {
510
+ const stack = [beginRoot];
511
+ while (stack.length > 0) {
512
+ const cur = stack.pop();
513
+ if (this.isRealNode(cur)) {
517
514
  const callbackResult = callback(cur);
518
515
  if (callbackResult === identifier) {
519
516
  ans.push(cur);
@@ -521,11 +518,19 @@ export class BST<
521
518
  }
522
519
  // TODO potential bug
523
520
  if (callback === this._defaultOneParamCallback) {
524
- if (this._compare(cur.key, identifier as K) === CP.gt) cur.left && queue.push(cur.left);
525
- if (this._compare(cur.key, identifier as K) === CP.lt) cur.right && queue.push(cur.right);
521
+ if (this.isRealNode(cur.right) && this._compare(cur.key, identifier as K) === 'LT') stack.push(cur.right);
522
+ if (this.isRealNode(cur.left) && this._compare(cur.key, identifier as K) === 'GT') stack.push(cur.left);
523
+
524
+ // if (this.isRealNode(cur.right) && this._lt(cur.key, identifier as K)) stack.push(cur.right);
525
+ // if (this.isRealNode(cur.left) && this._gt(cur.key, identifier as K)) stack.push(cur.left);
526
+
527
+ // // @ts-ignore
528
+ // if (this.isRealNode(cur.right) && cur.key > identifier) stack.push(cur.right);
529
+ // // @ts-ignore
530
+ // if (this.isRealNode(cur.left) && cur.key < identifier) stack.push(cur.left);
526
531
  } else {
527
- cur.left && queue.push(cur.left);
528
- cur.right && queue.push(cur.right);
532
+ this.isRealNode(cur.right) && stack.push(cur.right);
533
+ this.isRealNode(cur.left) && stack.push(cur.left);
529
534
  }
530
535
  }
531
536
  }
@@ -562,7 +567,7 @@ export class BST<
562
567
  callback: C = this._defaultOneParamCallback as C,
563
568
  pattern: DFSOrderPattern = 'in',
564
569
  beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
565
- iterationType: IterationType = IterationType.ITERATIVE
570
+ iterationType: IterationType = 'ITERATIVE'
566
571
  ): ReturnType<C>[] {
567
572
  return super.dfs(callback, pattern, beginRoot, iterationType, false);
568
573
  }
@@ -650,7 +655,7 @@ export class BST<
650
655
  let current = this.ensureNode(beginRoot);
651
656
  if (!current) return undefined;
652
657
 
653
- if (this._variant === BSTVariant.STANDARD) {
658
+ if (this._variant === 'STANDARD') {
654
659
  // For BSTVariant.MIN, find the rightmost node
655
660
  while (current.right !== undefined) {
656
661
  current = current.right;
@@ -692,7 +697,7 @@ export class BST<
692
697
  */
693
698
  lesserOrGreaterTraverse<C extends BTNCallback<NODE>>(
694
699
  callback: C = this._defaultOneParamCallback as C,
695
- lesserOrGreater: CP = CP.lt,
700
+ lesserOrGreater: CP = 'LT',
696
701
  targetNode: KeyOrNodeOrEntry<K, V, NODE> = this.root,
697
702
  iterationType = this.iterationType
698
703
  ): ReturnType<C>[] {
@@ -703,7 +708,7 @@ export class BST<
703
708
 
704
709
  const targetKey = targetNode.key;
705
710
 
706
- if (iterationType === IterationType.RECURSIVE) {
711
+ if (iterationType === 'RECURSIVE') {
707
712
  const _traverse = (cur: NODE) => {
708
713
  const compared = this._compare(cur.key, targetKey);
709
714
  if (compared === lesserOrGreater) ans.push(callback(cur));
@@ -752,7 +757,7 @@ export class BST<
752
757
  this.clear();
753
758
 
754
759
  if (sorted.length < 1) return false;
755
- if (iterationType === IterationType.RECURSIVE) {
760
+ if (iterationType === 'RECURSIVE') {
756
761
  const buildBalanceBST = (l: number, r: number) => {
757
762
  if (l > r) return;
758
763
  const m = l + Math.floor((r - l) / 2);
@@ -812,7 +817,7 @@ export class BST<
812
817
 
813
818
  let balanced = true;
814
819
 
815
- if (iterationType === IterationType.RECURSIVE) {
820
+ if (iterationType === 'RECURSIVE') {
816
821
  const _height = (cur: NODE | undefined): number => {
817
822
  if (!cur) return 0;
818
823
  const leftHeight = _height(cur.left),
@@ -856,7 +861,7 @@ export class BST<
856
861
  * @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. This means that it
857
862
  * can either be an object of type `NODE` or it can be `undefined`.
858
863
  */
859
- protected _setRoot(v: NODE | undefined) {
864
+ protected override _setRoot(v: NODE | undefined) {
860
865
  if (v) {
861
866
  v.parent = undefined;
862
867
  }
@@ -868,14 +873,32 @@ export class BST<
868
873
  * is greater than, less than, or equal to the second value.
869
874
  * @param {K} a - The parameter "a" is of type K.
870
875
  * @param {K} b - The parameter "b" in the above code represents a K.
871
- * @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater
872
- * than), CP.lt (less than), or CP.eq (equal).
876
+ * @returns a value of type CP (ComparisonResult). The possible return values are 'GT' (greater
877
+ * than), 'LT' (less than), or 'EQ' (equal).
873
878
  */
874
879
  protected _compare(a: K, b: K): CP {
875
880
  const extractedA = this.extractor(a);
876
881
  const extractedB = this.extractor(b);
877
- const compared = this.variant === BSTVariant.STANDARD ? extractedA - extractedB : extractedB - extractedA;
882
+ const compared = this.variant === 'STANDARD' ? extractedA - extractedB : extractedB - extractedA;
878
883
 
879
- return compared > 0 ? CP.gt : compared < 0 ? CP.lt : CP.eq;
884
+ return compared > 0 ? 'GT' : compared < 0 ? 'LT' : 'EQ';
885
+ }
886
+
887
+ protected _lt(a: K, b: K): boolean {
888
+ const extractedA = this.extractor(a);
889
+ const extractedB = this.extractor(b);
890
+ // return this.variant === BSTVariant.STANDARD ? extractedA < extractedB : extractedA > extractedB;
891
+ return this.variant === 'STANDARD' ? extractedA < extractedB : extractedA > extractedB;
892
+ // return extractedA < extractedB;
893
+ // return a < b;
894
+ }
895
+
896
+ protected _gt(a: K, b: K): boolean {
897
+ const extractedA = this.extractor(a);
898
+ const extractedB = this.extractor(b);
899
+ // return this.variant === BSTVariant.STANDARD ? extractedA > extractedB : extractedA < extractedB;
900
+ return this.variant === 'STANDARD' ? extractedA > extractedB : extractedA < extractedB;
901
+ // return extractedA > extractedB;
902
+ // return a > b;
880
903
  }
881
904
  }
@@ -7,7 +7,7 @@ import type {
7
7
  RedBlackTreeNested,
8
8
  RedBlackTreeNodeNested
9
9
  } from '../../types';
10
- import { RBTNColor } from '../../types';
10
+ import { CRUD, RBTNColor } from '../../types';
11
11
  import { BST, BSTNode } from './bst';
12
12
  import { IBinaryTree } from '../../interfaces';
13
13
 
@@ -89,26 +89,16 @@ export class RedBlackTree<
89
89
  return this._SENTINEL;
90
90
  }
91
91
 
92
- protected _root: NODE | undefined;
92
+ protected override _root: NODE | undefined;
93
93
 
94
94
  /**
95
95
  * The function returns the root node of a tree or undefined if there is no root.
96
96
  * @returns The root node of the tree structure, or undefined if there is no root node.
97
97
  */
98
- get root(): NODE | undefined {
98
+ override get root(): NODE | undefined {
99
99
  return this._root;
100
100
  }
101
101
 
102
- protected _size: number = 0;
103
-
104
- /**
105
- * The function returns the size of an object.
106
- * @returns The size of the object, which is a number.
107
- */
108
- get size(): number {
109
- return this._size;
110
- }
111
-
112
102
  /**
113
103
  * The function creates a new Red-Black Tree node with the specified key, value, and color.
114
104
  * @param {K} key - The key parameter represents the key of the node being created. It is of type K,
@@ -208,7 +198,7 @@ export class RedBlackTree<
208
198
  * @returns a boolean value.
209
199
  */
210
200
  override isRealNode(node: NODE | undefined): node is NODE {
211
- if (node === this._SENTINEL || node === undefined) return false;
201
+ if (node === this.SENTINEL || node === undefined) return false;
212
202
  return node instanceof RedBlackTreeNode;
213
203
  }
214
204
 
@@ -244,8 +234,7 @@ export class RedBlackTree<
244
234
  beginRoot: BSTNKeyOrNode<K, NODE> = this.root,
245
235
  iterationType = this.iterationType
246
236
  ): NODE | null | undefined {
247
- if ((identifier as any) instanceof RedBlackTreeNode) callback = (node => node) as C;
248
- beginRoot = this.ensureNode(beginRoot);
237
+ // if ((identifier as any) instanceof RedBlackTreeNode) callback = (node => node) as C;
249
238
  return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
250
239
  }
251
240
 
@@ -262,8 +251,8 @@ export class RedBlackTree<
262
251
  * size counter to zero.
263
252
  */
264
253
  override clear() {
254
+ super.clear();
265
255
  this._root = this.SENTINEL;
266
- this._size = 0;
267
256
  }
268
257
 
269
258
  /**
@@ -290,7 +279,7 @@ export class RedBlackTree<
290
279
 
291
280
  const insertStatus = this._insert(newNode);
292
281
 
293
- if (insertStatus === 'inserted') {
282
+ if (insertStatus === 'CREATED') {
294
283
  // Ensure the root is black
295
284
  if (this.isRealNode(this._root)) {
296
285
  this._root.color = RBTNColor.BLACK;
@@ -299,7 +288,7 @@ export class RedBlackTree<
299
288
  }
300
289
  this._size++;
301
290
  return true;
302
- } else return insertStatus === 'updated';
291
+ } else return insertStatus === 'UPDATED';
303
292
  }
304
293
 
305
294
  /**
@@ -434,7 +423,7 @@ export class RedBlackTree<
434
423
  * node in the tree.
435
424
  * @returns {'inserted' | 'updated'} - The result of the insertion.
436
425
  */
437
- protected _insert(node: NODE): 'inserted' | 'updated' {
426
+ protected _insert(node: NODE): CRUD {
438
427
  let current = this.root;
439
428
  let parent: NODE | undefined = undefined;
440
429
 
@@ -446,7 +435,7 @@ export class RedBlackTree<
446
435
  current = current.right ?? this.SENTINEL;
447
436
  } else {
448
437
  this._replaceNode(current, node);
449
- return 'updated';
438
+ return 'UPDATED';
450
439
  }
451
440
  }
452
441
 
@@ -465,7 +454,7 @@ export class RedBlackTree<
465
454
  node.color = RBTNColor.RED;
466
455
 
467
456
  this._insertFixup(node);
468
- return 'inserted';
457
+ return 'CREATED';
469
458
  }
470
459
 
471
460
  /**
@@ -14,7 +14,7 @@ import type {
14
14
  TreeMultiMapNodeNested,
15
15
  TreeMultiMapOptions
16
16
  } from '../../types';
17
- import { IterationType, RBTNColor } from '../../types';
17
+ import { RBTNColor } from '../../types';
18
18
  import { IBinaryTree } from '../../interfaces';
19
19
  import { RedBlackTree, RedBlackTreeNode } from './rb-tree';
20
20
 
@@ -363,7 +363,7 @@ export class TreeMultiMap<
363
363
 
364
364
  this.clear();
365
365
 
366
- if (iterationType === IterationType.RECURSIVE) {
366
+ if (iterationType === 'RECURSIVE') {
367
367
  const buildBalanceBST = (l: number, r: number) => {
368
368
  if (l > r) return;
369
369
  const m = l + Math.floor((r - l) / 2);
@@ -1,13 +1,5 @@
1
- export enum BSTVariant {
2
- STANDARD = 'STANDARD',
3
- INVERSE = 'INVERSE'
4
- }
5
-
6
- export enum CP {
7
- lt = 'lt',
8
- eq = 'eq',
9
- gt = 'gt'
10
- }
1
+ export type BSTVariant = 'STANDARD' | 'INVERSE';
2
+ export type CP = 'LT' | 'EQ' | 'GT';
11
3
 
12
4
  /**
13
5
  * Enum representing different loop types.
@@ -15,20 +7,9 @@ export enum CP {
15
7
  * - `iterative`: Indicates the iterative loop type (with loops that use iterations).
16
8
  * - `recursive`: Indicates the recursive loop type (with loops that call themselves).
17
9
  */
18
- export enum IterationType {
19
- ITERATIVE = 'ITERATIVE',
20
- RECURSIVE = 'RECURSIVE'
21
- }
10
+ export type IterationType = 'ITERATIVE' | 'RECURSIVE';
22
11
 
23
- export enum FamilyPosition {
24
- ROOT = 'ROOT',
25
- LEFT = 'LEFT',
26
- RIGHT = 'RIGHT',
27
- ROOT_LEFT = 'ROOT_LEFT',
28
- ROOT_RIGHT = 'ROOT_RIGHT',
29
- ISOLATED = 'ISOLATED',
30
- MAL_NODE = 'MAL_NODE'
31
- }
12
+ export type FamilyPosition = 'ROOT' | 'LEFT' | 'RIGHT' | 'ROOT_LEFT' | 'ROOT_RIGHT' | 'ISOLATED' | 'MAL_NODE';
32
13
 
33
14
  export type Comparator<K> = (a: K, b: K) => number;
34
15
 
@@ -63,3 +44,5 @@ export type BTNodePureExemplar<K, V, N> = [K, V | undefined] | BTNodePureKeyOrNo
63
44
  export type BSTNKeyOrNode<K, N> = K | undefined | N;
64
45
 
65
46
  export type BinaryTreeDeleteResult<N> = { deleted: N | null | undefined; needBalanced: N | null | undefined };
47
+
48
+ export type CRUD = 'CREATED' | 'READ' | 'UPDATED' | 'DELETED';