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.
- package/dist/data-structures/base/iterable-base.d.ts +10 -8
- package/dist/data-structures/base/iterable-base.js +8 -12
- package/dist/data-structures/binary-tree/{tree-multimap.d.ts → avl-tree-multi-map.d.ts} +11 -11
- package/dist/data-structures/binary-tree/{tree-multimap.js → avl-tree-multi-map.js} +14 -14
- package/dist/data-structures/binary-tree/bst.js +5 -7
- package/dist/data-structures/binary-tree/index.d.ts +2 -1
- package/dist/data-structures/binary-tree/index.js +2 -1
- package/dist/data-structures/binary-tree/rb-tree.js +17 -9
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +200 -0
- package/dist/data-structures/binary-tree/tree-multi-map.js +399 -0
- package/dist/data-structures/graph/abstract-graph.d.ts +1 -0
- package/dist/data-structures/graph/abstract-graph.js +3 -0
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +14 -76
- package/dist/data-structures/linked-list/doubly-linked-list.js +16 -86
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +27 -69
- package/dist/data-structures/linked-list/singly-linked-list.js +35 -79
- package/dist/data-structures/queue/deque.d.ts +0 -53
- package/dist/data-structures/queue/deque.js +0 -61
- package/dist/data-structures/queue/queue.d.ts +0 -70
- package/dist/data-structures/queue/queue.js +0 -87
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -0
- package/dist/types/data-structures/binary-tree/index.d.ts +2 -1
- package/dist/types/data-structures/binary-tree/index.js +2 -1
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +5 -0
- package/dist/types/data-structures/binary-tree/tree-multi-map.js +2 -0
- package/package.json +2 -2
- package/src/data-structures/base/iterable-base.ts +14 -10
- package/src/data-structures/binary-tree/{tree-multimap.ts → avl-tree-multi-map.ts} +20 -20
- package/src/data-structures/binary-tree/bst.ts +5 -6
- package/src/data-structures/binary-tree/index.ts +2 -1
- package/src/data-structures/binary-tree/rb-tree.ts +20 -10
- package/src/data-structures/binary-tree/tree-multi-map.ts +463 -0
- package/src/data-structures/graph/abstract-graph.ts +4 -0
- package/src/data-structures/heap/heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +16 -94
- package/src/data-structures/linked-list/singly-linked-list.ts +35 -87
- package/src/data-structures/queue/deque.ts +0 -67
- package/src/data-structures/queue/queue.ts +0 -98
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +8 -0
- package/src/types/data-structures/binary-tree/index.ts +2 -1
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +8 -0
- package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +0 -5
- package/src/types/data-structures/binary-tree/tree-multimap.ts +0 -8
- /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> & {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "min-heap-typed",
|
|
3
|
-
"version": "1.50.
|
|
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.
|
|
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
|
|
21
|
+
export class AVLTreeMultiMapNode<
|
|
22
22
|
K = any,
|
|
23
23
|
V = any,
|
|
24
|
-
NODE extends
|
|
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
|
|
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
|
|
64
|
+
export class AVLTreeMultiMap<
|
|
65
65
|
K = any,
|
|
66
66
|
V = any,
|
|
67
|
-
NODE extends
|
|
68
|
-
TREE extends
|
|
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?:
|
|
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
|
|
101
|
+
return new AVLTreeMultiMapNode(key, value, count) as NODE;
|
|
102
102
|
}
|
|
103
103
|
|
|
104
104
|
/**
|
|
105
|
-
* The function creates a new
|
|
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 `
|
|
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 `
|
|
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?:
|
|
114
|
-
return new
|
|
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
|
|
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
|
|
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
|
|
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 (
|
|
712
|
-
if (
|
|
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 (
|
|
727
|
-
if (
|
|
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;
|
|
@@ -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 ===
|
|
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
|
-
|
|
534
|
-
|
|
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
|
-
|
|
545
|
-
k.parent!.
|
|
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
|
|
554
|
+
u = k.parent!.parent!.right;
|
|
550
555
|
|
|
551
|
-
if (u && u.color ===
|
|
552
|
-
|
|
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
|
-
|
|
563
|
-
k.parent!.
|
|
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
|
}
|