min-heap-typed 1.50.3 → 1.50.5

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 (44) hide show
  1. package/dist/data-structures/base/iterable-base.d.ts +10 -8
  2. package/dist/data-structures/base/iterable-base.js +8 -12
  3. package/dist/data-structures/binary-tree/{tree-multimap.d.ts → avl-tree-multi-map.d.ts} +11 -11
  4. package/dist/data-structures/binary-tree/{tree-multimap.js → avl-tree-multi-map.js} +14 -14
  5. package/dist/data-structures/binary-tree/bst.js +5 -7
  6. package/dist/data-structures/binary-tree/index.d.ts +2 -1
  7. package/dist/data-structures/binary-tree/index.js +2 -1
  8. package/dist/data-structures/binary-tree/rb-tree.js +17 -9
  9. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +200 -0
  10. package/dist/data-structures/binary-tree/tree-multi-map.js +399 -0
  11. package/dist/data-structures/graph/abstract-graph.d.ts +1 -0
  12. package/dist/data-structures/graph/abstract-graph.js +3 -0
  13. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +14 -76
  14. package/dist/data-structures/linked-list/doubly-linked-list.js +16 -86
  15. package/dist/data-structures/linked-list/singly-linked-list.d.ts +27 -69
  16. package/dist/data-structures/linked-list/singly-linked-list.js +35 -79
  17. package/dist/data-structures/queue/deque.d.ts +0 -53
  18. package/dist/data-structures/queue/deque.js +0 -61
  19. package/dist/data-structures/queue/queue.d.ts +0 -70
  20. package/dist/data-structures/queue/queue.js +0 -87
  21. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -0
  22. package/dist/types/data-structures/binary-tree/index.d.ts +2 -1
  23. package/dist/types/data-structures/binary-tree/index.js +2 -1
  24. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +5 -0
  25. package/dist/types/data-structures/binary-tree/tree-multi-map.js +2 -0
  26. package/package.json +2 -2
  27. package/src/data-structures/base/iterable-base.ts +14 -10
  28. package/src/data-structures/binary-tree/{tree-multimap.ts → avl-tree-multi-map.ts} +20 -20
  29. package/src/data-structures/binary-tree/bst.ts +5 -6
  30. package/src/data-structures/binary-tree/index.ts +2 -1
  31. package/src/data-structures/binary-tree/rb-tree.ts +20 -10
  32. package/src/data-structures/binary-tree/tree-multi-map.ts +463 -0
  33. package/src/data-structures/graph/abstract-graph.ts +4 -0
  34. package/src/data-structures/heap/heap.ts +1 -1
  35. package/src/data-structures/linked-list/doubly-linked-list.ts +16 -94
  36. package/src/data-structures/linked-list/singly-linked-list.ts +35 -87
  37. package/src/data-structures/queue/deque.ts +0 -67
  38. package/src/data-structures/queue/queue.ts +0 -98
  39. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +8 -0
  40. package/src/types/data-structures/binary-tree/index.ts +2 -1
  41. package/src/types/data-structures/binary-tree/tree-multi-map.ts +8 -0
  42. package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +0 -5
  43. package/src/types/data-structures/binary-tree/tree-multimap.ts +0 -8
  44. /package/dist/types/data-structures/binary-tree/{tree-multimap.js → avl-tree-multi-map.js} +0 -0
@@ -0,0 +1,5 @@
1
+ import { TreeMultiMap, TreeMultiMapNode } from '../../../data-structures';
2
+ import type { RBTreeOptions } from './rb-tree';
3
+ export type TreeMultiMapNodeNested<K, V> = TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, TreeMultiMapNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
4
+ export type TreeMultiMapNested<K, V, N extends TreeMultiMapNode<K, V, N>> = TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, TreeMultiMap<K, V, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
5
+ export type TreeMultiMapOptions<K> = RBTreeOptions<K> & {};
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "min-heap-typed",
3
- "version": "1.50.3",
3
+ "version": "1.50.5",
4
4
  "description": "Min Heap. Javascript & Typescript Data Structure.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -132,6 +132,6 @@
132
132
  "typescript": "^4.9.5"
133
133
  },
134
134
  "dependencies": {
135
- "data-structure-typed": "^1.50.3"
135
+ "data-structure-typed": "^1.50.4"
136
136
  }
137
137
  }
@@ -6,6 +6,8 @@ export abstract class IterableEntryBase<K = any, V = any> {
6
6
  * Space Complexity: O(1)
7
7
  */
8
8
 
9
+ abstract get size(): number;
10
+
9
11
  /**
10
12
  * Time Complexity: O(n)
11
13
  * Space Complexity: O(1)
@@ -125,6 +127,11 @@ export abstract class IterableEntryBase<K = any, V = any> {
125
127
  return false;
126
128
  }
127
129
 
130
+ /**
131
+ * Time Complexity: O(n)
132
+ * Space Complexity: O(1)
133
+ */
134
+
128
135
  /**
129
136
  * Time Complexity: O(n)
130
137
  * Space Complexity: O(1)
@@ -248,11 +255,6 @@ export abstract class IterableEntryBase<K = any, V = any> {
248
255
  return;
249
256
  }
250
257
 
251
- /**
252
- * Time Complexity: O(n)
253
- * Space Complexity: O(1)
254
- */
255
-
256
258
  /**
257
259
  * Time Complexity: O(n)
258
260
  * Space Complexity: O(1)
@@ -301,6 +303,8 @@ export abstract class IterableEntryBase<K = any, V = any> {
301
303
  }
302
304
 
303
305
  export abstract class IterableElementBase<E = any, C = any> {
306
+ abstract get size(): number;
307
+
304
308
  /**
305
309
  * Time Complexity: O(n)
306
310
  * Space Complexity: O(1)
@@ -362,6 +366,11 @@ export abstract class IterableElementBase<E = any, C = any> {
362
366
  return true;
363
367
  }
364
368
 
369
+ /**
370
+ * Time Complexity: O(n)
371
+ * Space Complexity: O(1)
372
+ */
373
+
365
374
  /**
366
375
  * Time Complexity: O(n)
367
376
  * Space Complexity: O(1)
@@ -445,11 +454,6 @@ export abstract class IterableElementBase<E = any, C = any> {
445
454
  return;
446
455
  }
447
456
 
448
- /**
449
- * Time Complexity: O(n)
450
- * Space Complexity: O(1)
451
- */
452
-
453
457
  /**
454
458
  * Time Complexity: O(n)
455
459
  * Space Complexity: O(1)
@@ -6,22 +6,22 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import type {
9
+ AVLTreeMultiMapNested,
10
+ AVLTreeMultiMapNodeNested,
11
+ AVLTreeMultiMapOptions,
9
12
  BinaryTreeDeleteResult,
10
13
  BSTNKeyOrNode,
11
14
  BTNCallback,
12
- KeyOrNodeOrEntry,
13
- TreeMultimapNested,
14
- TreeMultimapNodeNested,
15
- TreeMultimapOptions
15
+ KeyOrNodeOrEntry
16
16
  } from '../../types';
17
17
  import { FamilyPosition, IterationType } from '../../types';
18
18
  import { IBinaryTree } from '../../interfaces';
19
19
  import { AVLTree, AVLTreeNode } from './avl-tree';
20
20
 
21
- export class TreeMultimapNode<
21
+ export class AVLTreeMultiMapNode<
22
22
  K = any,
23
23
  V = any,
24
- NODE extends TreeMultimapNode<K, V, NODE> = TreeMultimapNodeNested<K, V>
24
+ NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNodeNested<K, V>
25
25
  > extends AVLTreeNode<K, V, NODE> {
26
26
  /**
27
27
  * The constructor function initializes a BinaryTreeNode object with a key, value, and count.
@@ -59,17 +59,17 @@ export class TreeMultimapNode<
59
59
  }
60
60
 
61
61
  /**
62
- * The only distinction between a TreeMultimap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
62
+ * The only distinction between a AVLTreeMultiMap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
63
63
  */
64
- export class TreeMultimap<
64
+ export class AVLTreeMultiMap<
65
65
  K = any,
66
66
  V = any,
67
- NODE extends TreeMultimapNode<K, V, NODE> = TreeMultimapNode<K, V, TreeMultimapNodeNested<K, V>>,
68
- TREE extends TreeMultimap<K, V, NODE, TREE> = TreeMultimap<K, V, NODE, TreeMultimapNested<K, V, NODE>>
67
+ NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNodeNested<K, V>>,
68
+ TREE extends AVLTreeMultiMap<K, V, NODE, TREE> = AVLTreeMultiMap<K, V, NODE, AVLTreeMultiMapNested<K, V, NODE>>
69
69
  >
70
70
  extends AVLTree<K, V, NODE, TREE>
71
71
  implements IBinaryTree<K, V, NODE, TREE> {
72
- constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: TreeMultimapOptions<K>) {
72
+ constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: AVLTreeMultiMapOptions<K>) {
73
73
  super([], options);
74
74
  if (keysOrNodesOrEntries) this.addMany(keysOrNodesOrEntries);
75
75
  }
@@ -98,20 +98,20 @@ export class TreeMultimap<
98
98
  * @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
99
99
  */
100
100
  override createNode(key: K, value?: V, count?: number): NODE {
101
- return new TreeMultimapNode(key, value, count) as NODE;
101
+ return new AVLTreeMultiMapNode(key, value, count) as NODE;
102
102
  }
103
103
 
104
104
  /**
105
- * The function creates a new TreeMultimap object with the specified options and returns it.
105
+ * The function creates a new AVLTreeMultiMap object with the specified options and returns it.
106
106
  * @param [options] - The `options` parameter is an optional object that contains additional
107
- * configuration options for creating the `TreeMultimap` object. It can include properties such as
107
+ * configuration options for creating the `AVLTreeMultiMap` object. It can include properties such as
108
108
  * `iterationType` and `variant`, which are used to specify the type of iteration and the variant of
109
109
  * the tree, respectively. These properties can be
110
- * @returns a new instance of the `TreeMultimap` class, with the provided options merged with the
110
+ * @returns a new instance of the `AVLTreeMultiMap` class, with the provided options merged with the
111
111
  * default options. The returned value is casted as `TREE`.
112
112
  */
113
- override createTree(options?: TreeMultimapOptions<K>): TREE {
114
- return new TreeMultimap<K, V, NODE, TREE>([], {
113
+ override createTree(options?: AVLTreeMultiMapOptions<K>): TREE {
114
+ return new AVLTreeMultiMap<K, V, NODE, TREE>([], {
115
115
  iterationType: this.iterationType,
116
116
  variant: this.variant,
117
117
  ...options
@@ -155,13 +155,13 @@ export class TreeMultimap<
155
155
  }
156
156
 
157
157
  /**
158
- * The function checks if an keyOrNodeOrEntry is an instance of the TreeMultimapNode class.
158
+ * The function checks if an keyOrNodeOrEntry is an instance of the AVLTreeMultiMapNode class.
159
159
  * @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
160
- * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the TreeMultimapNode
160
+ * @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeMultiMapNode
161
161
  * class.
162
162
  */
163
163
  override isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE {
164
- return keyOrNodeOrEntry instanceof TreeMultimapNode;
164
+ return keyOrNodeOrEntry instanceof AVLTreeMultiMapNode;
165
165
  }
166
166
 
167
167
  /**
@@ -708,9 +708,8 @@ export class BST<
708
708
  const compared = this._compare(cur.key, targetKey);
709
709
  if (compared === lesserOrGreater) ans.push(callback(cur));
710
710
 
711
- if (!cur.left && !cur.right) return;
712
- if (cur.left && this._compare(cur.left.key, targetKey) === lesserOrGreater) _traverse(cur.left);
713
- if (cur.right && this._compare(cur.right.key, targetKey) === lesserOrGreater) _traverse(cur.right);
711
+ if (this.isRealNode(cur.left)) _traverse(cur.left);
712
+ if (this.isRealNode(cur.right)) _traverse(cur.right);
714
713
  };
715
714
 
716
715
  _traverse(this.root);
@@ -719,12 +718,12 @@ export class BST<
719
718
  const queue = new Queue<NODE>([this.root]);
720
719
  while (queue.size > 0) {
721
720
  const cur = queue.shift();
722
- if (cur) {
721
+ if (this.isRealNode(cur)) {
723
722
  const compared = this._compare(cur.key, targetKey);
724
723
  if (compared === lesserOrGreater) ans.push(callback(cur));
725
724
 
726
- if (cur.left && this._compare(cur.left.key, targetKey) === lesserOrGreater) queue.push(cur.left);
727
- if (cur.right && this._compare(cur.right.key, targetKey) === lesserOrGreater) queue.push(cur.right);
725
+ if (this.isRealNode(cur.left)) queue.push(cur.left);
726
+ if (this.isRealNode(cur.right)) queue.push(cur.right);
728
727
  }
729
728
  }
730
729
  return ans;
@@ -4,4 +4,5 @@ export * from './binary-indexed-tree';
4
4
  export * from './segment-tree';
5
5
  export * from './avl-tree';
6
6
  export * from './rb-tree';
7
- export * from './tree-multimap';
7
+ export * from './avl-tree-multi-map';
8
+ export * from './tree-multi-map';
@@ -527,12 +527,14 @@ export class RedBlackTree<
527
527
  */
528
528
  protected _fixInsert(k: NODE): void {
529
529
  let u: NODE | undefined;
530
- while (k.parent && k.parent.color === 1) {
530
+ while (k.parent && k.parent.color === RBTNColor.RED) {
531
531
  if (k.parent.parent && k.parent === k.parent.parent.right) {
532
532
  u = k.parent.parent.left;
533
- if (u && u.color === 1) {
534
- u.color = RBTNColor.BLACK;
533
+
534
+ if (u && u.color === RBTNColor.RED) {
535
+ // Delay color flip
535
536
  k.parent.color = RBTNColor.BLACK;
537
+ u.color = RBTNColor.BLACK;
536
538
  k.parent.parent.color = RBTNColor.RED;
537
539
  k = k.parent.parent;
538
540
  } else {
@@ -541,16 +543,20 @@ export class RedBlackTree<
541
543
  this._rightRotate(k);
542
544
  }
543
545
 
544
- k.parent!.color = RBTNColor.BLACK;
545
- k.parent!.parent!.color = RBTNColor.RED;
546
+ // Check color before rotation
547
+ if (k.parent!.color === RBTNColor.RED) {
548
+ k.parent!.color = RBTNColor.BLACK;
549
+ k.parent!.parent!.color = RBTNColor.RED;
550
+ }
546
551
  this._leftRotate(k.parent!.parent!);
547
552
  }
548
553
  } else {
549
- u = k.parent.parent!.right;
554
+ u = k.parent!.parent!.right;
550
555
 
551
- if (u && u.color === 1) {
552
- u.color = RBTNColor.BLACK;
556
+ if (u && u.color === RBTNColor.RED) {
557
+ // Delay color flip
553
558
  k.parent.color = RBTNColor.BLACK;
559
+ u.color = RBTNColor.BLACK;
554
560
  k.parent.parent!.color = RBTNColor.RED;
555
561
  k = k.parent.parent!;
556
562
  } else {
@@ -559,11 +565,15 @@ export class RedBlackTree<
559
565
  this._leftRotate(k);
560
566
  }
561
567
 
562
- k.parent!.color = RBTNColor.BLACK;
563
- k.parent!.parent!.color = RBTNColor.RED;
568
+ // Check color before rotation
569
+ if (k.parent!.color === RBTNColor.RED) {
570
+ k.parent!.color = RBTNColor.BLACK;
571
+ k.parent!.parent!.color = RBTNColor.RED;
572
+ }
564
573
  this._rightRotate(k.parent!.parent!);
565
574
  }
566
575
  }
576
+
567
577
  if (k === this.root) {
568
578
  break;
569
579
  }