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
|
@@ -4,6 +4,7 @@ export declare abstract class IterableEntryBase<K = any, V = any> {
|
|
|
4
4
|
* Time Complexity: O(n)
|
|
5
5
|
* Space Complexity: O(1)
|
|
6
6
|
*/
|
|
7
|
+
abstract get size(): number;
|
|
7
8
|
/**
|
|
8
9
|
* Time Complexity: O(n)
|
|
9
10
|
* Space Complexity: O(1)
|
|
@@ -91,6 +92,10 @@ export declare abstract class IterableEntryBase<K = any, V = any> {
|
|
|
91
92
|
* Time Complexity: O(n)
|
|
92
93
|
* Space Complexity: O(1)
|
|
93
94
|
*/
|
|
95
|
+
/**
|
|
96
|
+
* Time Complexity: O(n)
|
|
97
|
+
* Space Complexity: O(1)
|
|
98
|
+
*/
|
|
94
99
|
/**
|
|
95
100
|
* Time Complexity: O(n)
|
|
96
101
|
* Space Complexity: O(1)
|
|
@@ -171,10 +176,6 @@ export declare abstract class IterableEntryBase<K = any, V = any> {
|
|
|
171
176
|
* collection, otherwise it returns `undefined`.
|
|
172
177
|
*/
|
|
173
178
|
get(key: K): V | undefined;
|
|
174
|
-
/**
|
|
175
|
-
* Time Complexity: O(n)
|
|
176
|
-
* Space Complexity: O(1)
|
|
177
|
-
*/
|
|
178
179
|
/**
|
|
179
180
|
* Time Complexity: O(n)
|
|
180
181
|
* Space Complexity: O(1)
|
|
@@ -205,6 +206,7 @@ export declare abstract class IterableEntryBase<K = any, V = any> {
|
|
|
205
206
|
protected abstract _getIterator(...args: any[]): IterableIterator<[K, V]>;
|
|
206
207
|
}
|
|
207
208
|
export declare abstract class IterableElementBase<E = any, C = any> {
|
|
209
|
+
abstract get size(): number;
|
|
208
210
|
/**
|
|
209
211
|
* Time Complexity: O(n)
|
|
210
212
|
* Space Complexity: O(1)
|
|
@@ -253,6 +255,10 @@ export declare abstract class IterableElementBase<E = any, C = any> {
|
|
|
253
255
|
* Time Complexity: O(n)
|
|
254
256
|
* Space Complexity: O(1)
|
|
255
257
|
*/
|
|
258
|
+
/**
|
|
259
|
+
* Time Complexity: O(n)
|
|
260
|
+
* Space Complexity: O(1)
|
|
261
|
+
*/
|
|
256
262
|
/**
|
|
257
263
|
* Time Complexity: O(n)
|
|
258
264
|
* Space Complexity: O(1)
|
|
@@ -307,10 +313,6 @@ export declare abstract class IterableElementBase<E = any, C = any> {
|
|
|
307
313
|
* callback function. If no element satisfies the callback function, `undefined` is returned.
|
|
308
314
|
*/
|
|
309
315
|
find(callbackfn: ElementCallback<E, boolean>, thisArg?: any): E | undefined;
|
|
310
|
-
/**
|
|
311
|
-
* Time Complexity: O(n)
|
|
312
|
-
* Space Complexity: O(1)
|
|
313
|
-
*/
|
|
314
316
|
/**
|
|
315
317
|
* Time Complexity: O(n)
|
|
316
318
|
* Space Complexity: O(1)
|
|
@@ -2,10 +2,6 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.IterableElementBase = exports.IterableEntryBase = void 0;
|
|
4
4
|
class IterableEntryBase {
|
|
5
|
-
/**
|
|
6
|
-
* Time Complexity: O(n)
|
|
7
|
-
* Space Complexity: O(1)
|
|
8
|
-
*/
|
|
9
5
|
/**
|
|
10
6
|
* Time Complexity: O(n)
|
|
11
7
|
* Space Complexity: O(1)
|
|
@@ -123,6 +119,10 @@ class IterableEntryBase {
|
|
|
123
119
|
* Time Complexity: O(n)
|
|
124
120
|
* Space Complexity: O(1)
|
|
125
121
|
*/
|
|
122
|
+
/**
|
|
123
|
+
* Time Complexity: O(n)
|
|
124
|
+
* Space Complexity: O(1)
|
|
125
|
+
*/
|
|
126
126
|
/**
|
|
127
127
|
* Time Complexity: O(n)
|
|
128
128
|
* Space Complexity: O(1)
|
|
@@ -237,10 +237,6 @@ class IterableEntryBase {
|
|
|
237
237
|
}
|
|
238
238
|
return;
|
|
239
239
|
}
|
|
240
|
-
/**
|
|
241
|
-
* Time Complexity: O(n)
|
|
242
|
-
* Space Complexity: O(1)
|
|
243
|
-
*/
|
|
244
240
|
/**
|
|
245
241
|
* Time Complexity: O(n)
|
|
246
242
|
* Space Complexity: O(1)
|
|
@@ -338,6 +334,10 @@ class IterableElementBase {
|
|
|
338
334
|
* Time Complexity: O(n)
|
|
339
335
|
* Space Complexity: O(1)
|
|
340
336
|
*/
|
|
337
|
+
/**
|
|
338
|
+
* Time Complexity: O(n)
|
|
339
|
+
* Space Complexity: O(1)
|
|
340
|
+
*/
|
|
341
341
|
/**
|
|
342
342
|
* Time Complexity: O(n)
|
|
343
343
|
* Space Complexity: O(1)
|
|
@@ -412,10 +412,6 @@ class IterableElementBase {
|
|
|
412
412
|
}
|
|
413
413
|
return;
|
|
414
414
|
}
|
|
415
|
-
/**
|
|
416
|
-
* Time Complexity: O(n)
|
|
417
|
-
* Space Complexity: O(1)
|
|
418
|
-
*/
|
|
419
415
|
/**
|
|
420
416
|
* Time Complexity: O(n)
|
|
421
417
|
* Space Complexity: O(1)
|
|
@@ -5,11 +5,11 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type {
|
|
8
|
+
import type { AVLTreeMultiMapNested, AVLTreeMultiMapNodeNested, AVLTreeMultiMapOptions, BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, KeyOrNodeOrEntry } from '../../types';
|
|
9
9
|
import { IterationType } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
12
|
-
export declare class
|
|
12
|
+
export declare class AVLTreeMultiMapNode<K = any, V = any, NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNodeNested<K, V>> extends AVLTreeNode<K, V, NODE> {
|
|
13
13
|
/**
|
|
14
14
|
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
15
15
|
* @param {K} key - The `key` parameter is of type `K` and represents the unique identifier
|
|
@@ -35,10 +35,10 @@ export declare class TreeMultimapNode<K = any, V = any, NODE extends TreeMultima
|
|
|
35
35
|
set count(value: number);
|
|
36
36
|
}
|
|
37
37
|
/**
|
|
38
|
-
* The only distinction between a
|
|
38
|
+
* 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.
|
|
39
39
|
*/
|
|
40
|
-
export declare class
|
|
41
|
-
constructor(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V, NODE>>, options?:
|
|
40
|
+
export declare class AVLTreeMultiMap<K = any, V = any, NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNodeNested<K, V>>, TREE extends AVLTreeMultiMap<K, V, NODE, TREE> = AVLTreeMultiMap<K, V, NODE, AVLTreeMultiMapNested<K, V, NODE>>> extends AVLTree<K, V, NODE, TREE> implements IBinaryTree<K, V, NODE, TREE> {
|
|
41
|
+
constructor(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V, NODE>>, options?: AVLTreeMultiMapOptions<K>);
|
|
42
42
|
protected _count: number;
|
|
43
43
|
/**
|
|
44
44
|
* The function calculates the sum of the count property of all nodes in a tree using depth-first
|
|
@@ -57,15 +57,15 @@ export declare class TreeMultimap<K = any, V = any, NODE extends TreeMultimapNod
|
|
|
57
57
|
*/
|
|
58
58
|
createNode(key: K, value?: V, count?: number): NODE;
|
|
59
59
|
/**
|
|
60
|
-
* The function creates a new
|
|
60
|
+
* The function creates a new AVLTreeMultiMap object with the specified options and returns it.
|
|
61
61
|
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
62
|
-
* configuration options for creating the `
|
|
62
|
+
* configuration options for creating the `AVLTreeMultiMap` object. It can include properties such as
|
|
63
63
|
* `iterationType` and `variant`, which are used to specify the type of iteration and the variant of
|
|
64
64
|
* the tree, respectively. These properties can be
|
|
65
|
-
* @returns a new instance of the `
|
|
65
|
+
* @returns a new instance of the `AVLTreeMultiMap` class, with the provided options merged with the
|
|
66
66
|
* default options. The returned value is casted as `TREE`.
|
|
67
67
|
*/
|
|
68
|
-
createTree(options?:
|
|
68
|
+
createTree(options?: AVLTreeMultiMapOptions<K>): TREE;
|
|
69
69
|
/**
|
|
70
70
|
* The function `keyValueOrEntryToNode` converts an keyOrNodeOrEntry object into a node object.
|
|
71
71
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`, which means it
|
|
@@ -79,9 +79,9 @@ export declare class TreeMultimap<K = any, V = any, NODE extends TreeMultimapNod
|
|
|
79
79
|
*/
|
|
80
80
|
keyValueOrEntryToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V, count?: number): NODE | undefined;
|
|
81
81
|
/**
|
|
82
|
-
* The function checks if an keyOrNodeOrEntry is an instance of the
|
|
82
|
+
* The function checks if an keyOrNodeOrEntry is an instance of the AVLTreeMultiMapNode class.
|
|
83
83
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
84
|
-
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the
|
|
84
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeMultiMapNode
|
|
85
85
|
* class.
|
|
86
86
|
*/
|
|
87
87
|
isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.AVLTreeMultiMap = exports.AVLTreeMultiMapNode = void 0;
|
|
4
4
|
const types_1 = require("../../types");
|
|
5
5
|
const avl_tree_1 = require("./avl-tree");
|
|
6
|
-
class
|
|
6
|
+
class AVLTreeMultiMapNode extends avl_tree_1.AVLTreeNode {
|
|
7
7
|
/**
|
|
8
8
|
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
9
9
|
* @param {K} key - The `key` parameter is of type `K` and represents the unique identifier
|
|
@@ -35,11 +35,11 @@ class TreeMultimapNode extends avl_tree_1.AVLTreeNode {
|
|
|
35
35
|
this._count = value;
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
|
-
exports.
|
|
38
|
+
exports.AVLTreeMultiMapNode = AVLTreeMultiMapNode;
|
|
39
39
|
/**
|
|
40
|
-
* The only distinction between a
|
|
40
|
+
* 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.
|
|
41
41
|
*/
|
|
42
|
-
class
|
|
42
|
+
class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
43
43
|
constructor(keysOrNodesOrEntries = [], options) {
|
|
44
44
|
super([], options);
|
|
45
45
|
this._count = 0;
|
|
@@ -67,19 +67,19 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
67
67
|
* @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
|
|
68
68
|
*/
|
|
69
69
|
createNode(key, value, count) {
|
|
70
|
-
return new
|
|
70
|
+
return new AVLTreeMultiMapNode(key, value, count);
|
|
71
71
|
}
|
|
72
72
|
/**
|
|
73
|
-
* The function creates a new
|
|
73
|
+
* The function creates a new AVLTreeMultiMap object with the specified options and returns it.
|
|
74
74
|
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
75
|
-
* configuration options for creating the `
|
|
75
|
+
* configuration options for creating the `AVLTreeMultiMap` object. It can include properties such as
|
|
76
76
|
* `iterationType` and `variant`, which are used to specify the type of iteration and the variant of
|
|
77
77
|
* the tree, respectively. These properties can be
|
|
78
|
-
* @returns a new instance of the `
|
|
78
|
+
* @returns a new instance of the `AVLTreeMultiMap` class, with the provided options merged with the
|
|
79
79
|
* default options. The returned value is casted as `TREE`.
|
|
80
80
|
*/
|
|
81
81
|
createTree(options) {
|
|
82
|
-
return new
|
|
82
|
+
return new AVLTreeMultiMap([], Object.assign({ iterationType: this.iterationType, variant: this.variant }, options));
|
|
83
83
|
}
|
|
84
84
|
/**
|
|
85
85
|
* The function `keyValueOrEntryToNode` converts an keyOrNodeOrEntry object into a node object.
|
|
@@ -118,13 +118,13 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
118
118
|
return node;
|
|
119
119
|
}
|
|
120
120
|
/**
|
|
121
|
-
* The function checks if an keyOrNodeOrEntry is an instance of the
|
|
121
|
+
* The function checks if an keyOrNodeOrEntry is an instance of the AVLTreeMultiMapNode class.
|
|
122
122
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, NODE>`.
|
|
123
|
-
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the
|
|
123
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the AVLTreeMultiMapNode
|
|
124
124
|
* class.
|
|
125
125
|
*/
|
|
126
126
|
isNode(keyOrNodeOrEntry) {
|
|
127
|
-
return keyOrNodeOrEntry instanceof
|
|
127
|
+
return keyOrNodeOrEntry instanceof AVLTreeMultiMapNode;
|
|
128
128
|
}
|
|
129
129
|
/**
|
|
130
130
|
* Time Complexity: O(log n)
|
|
@@ -362,4 +362,4 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
362
362
|
return super._replaceNode(oldNode, newNode);
|
|
363
363
|
}
|
|
364
364
|
}
|
|
365
|
-
exports.
|
|
365
|
+
exports.AVLTreeMultiMap = AVLTreeMultiMap;
|
|
@@ -624,11 +624,9 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
624
624
|
const compared = this._compare(cur.key, targetKey);
|
|
625
625
|
if (compared === lesserOrGreater)
|
|
626
626
|
ans.push(callback(cur));
|
|
627
|
-
if (
|
|
628
|
-
return;
|
|
629
|
-
if (cur.left && this._compare(cur.left.key, targetKey) === lesserOrGreater)
|
|
627
|
+
if (this.isRealNode(cur.left))
|
|
630
628
|
_traverse(cur.left);
|
|
631
|
-
if (
|
|
629
|
+
if (this.isRealNode(cur.right))
|
|
632
630
|
_traverse(cur.right);
|
|
633
631
|
};
|
|
634
632
|
_traverse(this.root);
|
|
@@ -638,13 +636,13 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
638
636
|
const queue = new queue_1.Queue([this.root]);
|
|
639
637
|
while (queue.size > 0) {
|
|
640
638
|
const cur = queue.shift();
|
|
641
|
-
if (cur) {
|
|
639
|
+
if (this.isRealNode(cur)) {
|
|
642
640
|
const compared = this._compare(cur.key, targetKey);
|
|
643
641
|
if (compared === lesserOrGreater)
|
|
644
642
|
ans.push(callback(cur));
|
|
645
|
-
if (
|
|
643
|
+
if (this.isRealNode(cur.left))
|
|
646
644
|
queue.push(cur.left);
|
|
647
|
-
if (
|
|
645
|
+
if (this.isRealNode(cur.right))
|
|
648
646
|
queue.push(cur.right);
|
|
649
647
|
}
|
|
650
648
|
}
|
|
@@ -20,4 +20,5 @@ __exportStar(require("./binary-indexed-tree"), exports);
|
|
|
20
20
|
__exportStar(require("./segment-tree"), exports);
|
|
21
21
|
__exportStar(require("./avl-tree"), exports);
|
|
22
22
|
__exportStar(require("./rb-tree"), exports);
|
|
23
|
-
__exportStar(require("./tree-
|
|
23
|
+
__exportStar(require("./avl-tree-multi-map"), exports);
|
|
24
|
+
__exportStar(require("./tree-multi-map"), exports);
|
|
@@ -473,12 +473,13 @@ class RedBlackTree extends bst_1.BST {
|
|
|
473
473
|
*/
|
|
474
474
|
_fixInsert(k) {
|
|
475
475
|
let u;
|
|
476
|
-
while (k.parent && k.parent.color ===
|
|
476
|
+
while (k.parent && k.parent.color === types_1.RBTNColor.RED) {
|
|
477
477
|
if (k.parent.parent && k.parent === k.parent.parent.right) {
|
|
478
478
|
u = k.parent.parent.left;
|
|
479
|
-
if (u && u.color ===
|
|
480
|
-
|
|
479
|
+
if (u && u.color === types_1.RBTNColor.RED) {
|
|
480
|
+
// Delay color flip
|
|
481
481
|
k.parent.color = types_1.RBTNColor.BLACK;
|
|
482
|
+
u.color = types_1.RBTNColor.BLACK;
|
|
482
483
|
k.parent.parent.color = types_1.RBTNColor.RED;
|
|
483
484
|
k = k.parent.parent;
|
|
484
485
|
}
|
|
@@ -487,16 +488,20 @@ class RedBlackTree extends bst_1.BST {
|
|
|
487
488
|
k = k.parent;
|
|
488
489
|
this._rightRotate(k);
|
|
489
490
|
}
|
|
490
|
-
|
|
491
|
-
k.parent.
|
|
491
|
+
// Check color before rotation
|
|
492
|
+
if (k.parent.color === types_1.RBTNColor.RED) {
|
|
493
|
+
k.parent.color = types_1.RBTNColor.BLACK;
|
|
494
|
+
k.parent.parent.color = types_1.RBTNColor.RED;
|
|
495
|
+
}
|
|
492
496
|
this._leftRotate(k.parent.parent);
|
|
493
497
|
}
|
|
494
498
|
}
|
|
495
499
|
else {
|
|
496
500
|
u = k.parent.parent.right;
|
|
497
|
-
if (u && u.color ===
|
|
498
|
-
|
|
501
|
+
if (u && u.color === types_1.RBTNColor.RED) {
|
|
502
|
+
// Delay color flip
|
|
499
503
|
k.parent.color = types_1.RBTNColor.BLACK;
|
|
504
|
+
u.color = types_1.RBTNColor.BLACK;
|
|
500
505
|
k.parent.parent.color = types_1.RBTNColor.RED;
|
|
501
506
|
k = k.parent.parent;
|
|
502
507
|
}
|
|
@@ -505,8 +510,11 @@ class RedBlackTree extends bst_1.BST {
|
|
|
505
510
|
k = k.parent;
|
|
506
511
|
this._leftRotate(k);
|
|
507
512
|
}
|
|
508
|
-
|
|
509
|
-
k.parent.
|
|
513
|
+
// Check color before rotation
|
|
514
|
+
if (k.parent.color === types_1.RBTNColor.RED) {
|
|
515
|
+
k.parent.color = types_1.RBTNColor.BLACK;
|
|
516
|
+
k.parent.parent.color = types_1.RBTNColor.RED;
|
|
517
|
+
}
|
|
510
518
|
this._rightRotate(k.parent.parent);
|
|
511
519
|
}
|
|
512
520
|
}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
import type { BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, KeyOrNodeOrEntry, TreeMultiMapNested, TreeMultiMapNodeNested, TreeMultiMapOptions } from '../../types';
|
|
9
|
+
import { IterationType } from '../../types';
|
|
10
|
+
import { IBinaryTree } from '../../interfaces';
|
|
11
|
+
import { RedBlackTree, RedBlackTreeNode } from './rb-tree';
|
|
12
|
+
export declare class TreeMultiMapNode<K = any, V = any, NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNodeNested<K, V>> extends RedBlackTreeNode<K, V, NODE> {
|
|
13
|
+
/**
|
|
14
|
+
* The constructor function initializes an instance of a class with a key, value, and count.
|
|
15
|
+
* @param {K} key - The key parameter is of type K, which represents the type of the key for the
|
|
16
|
+
* constructor. It is required and must be provided when creating an instance of the class.
|
|
17
|
+
* @param {V} [value] - The `value` parameter is an optional parameter of type `V`. It represents the
|
|
18
|
+
* value associated with the key in the constructor. If no value is provided, it will be `undefined`.
|
|
19
|
+
* @param [count=1] - The "count" parameter is an optional parameter that specifies the number of
|
|
20
|
+
* times the key-value pair should be repeated. If no value is provided for "count", it defaults to
|
|
21
|
+
* 1.
|
|
22
|
+
*/
|
|
23
|
+
constructor(key: K, value?: V, count?: number);
|
|
24
|
+
protected _count: number;
|
|
25
|
+
/**
|
|
26
|
+
* The function returns the value of the private variable _count.
|
|
27
|
+
* @returns The count property of the object, which is of type number.
|
|
28
|
+
*/
|
|
29
|
+
get count(): number;
|
|
30
|
+
/**
|
|
31
|
+
* The above function sets the value of the count property.
|
|
32
|
+
* @param {number} value - The value parameter is of type number, which means it can accept any
|
|
33
|
+
* numeric value.
|
|
34
|
+
*/
|
|
35
|
+
set count(value: number);
|
|
36
|
+
}
|
|
37
|
+
export declare class TreeMultiMap<K = any, V = any, NODE extends TreeMultiMapNode<K, V, NODE> = TreeMultiMapNode<K, V, TreeMultiMapNodeNested<K, V>>, TREE extends TreeMultiMap<K, V, NODE, TREE> = TreeMultiMap<K, V, NODE, TreeMultiMapNested<K, V, NODE>>> extends RedBlackTree<K, V, NODE, TREE> implements IBinaryTree<K, V, NODE, TREE> {
|
|
38
|
+
/**
|
|
39
|
+
* The constructor function initializes a new instance of the TreeMultiMap class with optional
|
|
40
|
+
* initial keys, nodes, or entries.
|
|
41
|
+
* @param keysOrNodesOrEntries - The `keysOrNodesOrEntries` parameter is an iterable object that can
|
|
42
|
+
* contain keys, nodes, or entries. It is used to initialize the TreeMultiMap with the provided keys,
|
|
43
|
+
* nodes, or entries.
|
|
44
|
+
* @param [options] - The `options` parameter is an optional object that can be passed to the
|
|
45
|
+
* constructor. It allows you to customize the behavior of the `TreeMultiMap` instance.
|
|
46
|
+
*/
|
|
47
|
+
constructor(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V, NODE>>, options?: TreeMultiMapOptions<K>);
|
|
48
|
+
protected _count: number;
|
|
49
|
+
/**
|
|
50
|
+
* The function calculates the sum of the count property of all nodes in a tree structure.
|
|
51
|
+
* @returns the sum of the count property of all nodes in the tree.
|
|
52
|
+
*/
|
|
53
|
+
get count(): number;
|
|
54
|
+
/**
|
|
55
|
+
* The function creates a new TreeMultiMapNode object with the specified key, value, and count.
|
|
56
|
+
* @param {K} key - The key parameter represents the key of the node being created. It is of type K,
|
|
57
|
+
* which is a generic type that can be replaced with any specific type when using the function.
|
|
58
|
+
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
59
|
+
* associated with the key in the node. It is of type `V`, which can be any data type.
|
|
60
|
+
* @param {number} [count] - The `count` parameter represents the number of occurrences of a
|
|
61
|
+
* key-value pair in the TreeMultiMap. It is an optional parameter, so if it is not provided, it will
|
|
62
|
+
* default to 1.
|
|
63
|
+
* @returns a new instance of the TreeMultiMapNode class, casted as NODE.
|
|
64
|
+
*/
|
|
65
|
+
createNode(key: K, value?: V, count?: number): NODE;
|
|
66
|
+
/**
|
|
67
|
+
* The function creates a new instance of a TreeMultiMap with the specified options and returns it.
|
|
68
|
+
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
69
|
+
* configuration options for creating the `TreeMultiMap`. It can include properties such as
|
|
70
|
+
* `keyComparator`, `valueComparator`, `allowDuplicates`, etc.
|
|
71
|
+
* @returns a new instance of the `TreeMultiMap` class, with the provided options merged with the
|
|
72
|
+
* existing `iterationType` option. The returned value is casted as `TREE`.
|
|
73
|
+
*/
|
|
74
|
+
createTree(options?: TreeMultiMapOptions<K>): TREE;
|
|
75
|
+
/**
|
|
76
|
+
* The function `keyValueOrEntryToNode` takes a key, value, and count and returns a node if the input
|
|
77
|
+
* is valid.
|
|
78
|
+
* @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be of type `KeyOrNodeOrEntry<K, V,
|
|
79
|
+
* NODE>`. It can accept three types of values:
|
|
80
|
+
* @param {V} [value] - The `value` parameter is an optional value of type `V`. It represents the
|
|
81
|
+
* value associated with a key in a key-value pair.
|
|
82
|
+
* @param [count=1] - The count parameter is an optional parameter that specifies the number of times
|
|
83
|
+
* the key-value pair should be added to the node. If not provided, it defaults to 1.
|
|
84
|
+
* @returns a NODE object or undefined.
|
|
85
|
+
*/
|
|
86
|
+
keyValueOrEntryToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V, count?: number): NODE | undefined;
|
|
87
|
+
/**
|
|
88
|
+
* The function "isNode" checks if a given key, node, or entry is an instance of the TreeMultiMapNode
|
|
89
|
+
* class.
|
|
90
|
+
* @param keyOrNodeOrEntry - The parameter `keyOrNodeOrEntry` can be of type `KeyOrNodeOrEntry<K, V,
|
|
91
|
+
* NODE>`.
|
|
92
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntry` is an instance
|
|
93
|
+
* of the `TreeMultiMapNode` class.
|
|
94
|
+
*/
|
|
95
|
+
isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is NODE;
|
|
96
|
+
/**
|
|
97
|
+
* Time Complexity: O(log n)
|
|
98
|
+
* Space Complexity: O(1)
|
|
99
|
+
*/
|
|
100
|
+
/**
|
|
101
|
+
* Time Complexity: O(log n)
|
|
102
|
+
* Space Complexity: O(1)
|
|
103
|
+
*
|
|
104
|
+
* The function overrides the add method in TypeScript and adds a new node to the data structure.
|
|
105
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can accept three types of values:
|
|
106
|
+
* @param {V} [value] - The `value` parameter represents the value associated with the key in the
|
|
107
|
+
* data structure.
|
|
108
|
+
* @param [count=1] - The `count` parameter represents the number of times the key-value pair should
|
|
109
|
+
* be added to the data structure. By default, it is set to 1, meaning that the key-value pair will
|
|
110
|
+
* be added once. However, you can specify a different value for `count` if you want to add
|
|
111
|
+
* @returns a boolean value.
|
|
112
|
+
*/
|
|
113
|
+
add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V, count?: number): boolean;
|
|
114
|
+
/**
|
|
115
|
+
* Time Complexity: O(log n)
|
|
116
|
+
* Space Complexity: O(1)
|
|
117
|
+
*/
|
|
118
|
+
/**
|
|
119
|
+
* Time Complexity: O(log n)
|
|
120
|
+
* Space Complexity: O(1)
|
|
121
|
+
*
|
|
122
|
+
* The `delete` function in a TypeScript class is used to delete nodes from a binary tree based on a
|
|
123
|
+
* given identifier, and it returns an array of results containing information about the deleted
|
|
124
|
+
* nodes.
|
|
125
|
+
* @param {ReturnType<C> | null | undefined} identifier - The identifier parameter is the value used
|
|
126
|
+
* to identify the node to be deleted. It can be of any type that is returned by the callback
|
|
127
|
+
* function. It can also be null or undefined if no node needs to be deleted.
|
|
128
|
+
* @param {C} callback - The `callback` parameter is a function that takes a node of type `NODE` as
|
|
129
|
+
* input and returns a value of type `ReturnType<C>`. It is used to determine if a node matches the
|
|
130
|
+
* identifier for deletion. If no callback is provided, the `_defaultOneParamCallback` function is
|
|
131
|
+
* used
|
|
132
|
+
* @param [ignoreCount=false] - A boolean value indicating whether to ignore the count of the target
|
|
133
|
+
* node when performing deletion. If set to true, the count of the target node will not be considered
|
|
134
|
+
* and the node will be deleted regardless of its count. If set to false (default), the count of the
|
|
135
|
+
* target node will be decremented
|
|
136
|
+
* @returns an array of BinaryTreeDeleteResult<NODE> objects.
|
|
137
|
+
*/
|
|
138
|
+
delete<C extends BTNCallback<NODE>>(identifier: ReturnType<C> | null | undefined, callback?: C, ignoreCount?: boolean): BinaryTreeDeleteResult<NODE>[];
|
|
139
|
+
/**
|
|
140
|
+
* Time Complexity: O(1)
|
|
141
|
+
* Space Complexity: O(1)
|
|
142
|
+
*/
|
|
143
|
+
/**
|
|
144
|
+
* Time Complexity: O(1)
|
|
145
|
+
* Space Complexity: O(1)
|
|
146
|
+
*
|
|
147
|
+
* The "clear" function overrides the parent class's "clear" function and also resets the count to
|
|
148
|
+
* zero.
|
|
149
|
+
*/
|
|
150
|
+
clear(): void;
|
|
151
|
+
/**
|
|
152
|
+
* Time Complexity: O(n log n)
|
|
153
|
+
* Space Complexity: O(log n)
|
|
154
|
+
*/
|
|
155
|
+
/**
|
|
156
|
+
* Time Complexity: O(n log n)
|
|
157
|
+
* Space Complexity: O(log n)
|
|
158
|
+
*
|
|
159
|
+
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
|
|
160
|
+
* tree using either a recursive or iterative approach.
|
|
161
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
162
|
+
* type of iteration to use when building the balanced binary search tree. It can have two possible
|
|
163
|
+
* values:
|
|
164
|
+
* @returns a boolean value.
|
|
165
|
+
*/
|
|
166
|
+
perfectlyBalance(iterationType?: IterationType): boolean;
|
|
167
|
+
/**
|
|
168
|
+
* Time complexity: O(n)
|
|
169
|
+
* Space complexity: O(n)
|
|
170
|
+
*/
|
|
171
|
+
/**
|
|
172
|
+
* Time complexity: O(n)
|
|
173
|
+
* Space complexity: O(n)
|
|
174
|
+
*
|
|
175
|
+
* The function overrides the clone method to create a deep copy of a tree object.
|
|
176
|
+
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
177
|
+
*/
|
|
178
|
+
clone(): TREE;
|
|
179
|
+
/**
|
|
180
|
+
* The function swaps the properties of two nodes in a binary search tree.
|
|
181
|
+
* @param srcNode - The source node that needs to be swapped with the destination node. It can be
|
|
182
|
+
* either a key or a node object.
|
|
183
|
+
* @param destNode - The `destNode` parameter is the node in the binary search tree where the
|
|
184
|
+
* properties will be swapped with the `srcNode`.
|
|
185
|
+
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
186
|
+
* If both `srcNode` and `destNode` are valid nodes, the method swaps their `key`, `value`, `count`,
|
|
187
|
+
* and `color` properties. If the swapping is successful, the method returns the modified `destNode`.
|
|
188
|
+
* If either `srcNode` or `destNode` is
|
|
189
|
+
*/
|
|
190
|
+
protected _swapProperties(srcNode: BSTNKeyOrNode<K, NODE>, destNode: BSTNKeyOrNode<K, NODE>): NODE | undefined;
|
|
191
|
+
/**
|
|
192
|
+
* The function replaces an old node with a new node and updates the count property of the new node.
|
|
193
|
+
* @param {NODE} oldNode - The `oldNode` parameter is of type `NODE` and represents the node that
|
|
194
|
+
* needs to be replaced in the data structure.
|
|
195
|
+
* @param {NODE} newNode - The `newNode` parameter is an object of type `NODE`.
|
|
196
|
+
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
197
|
+
* superclass, after updating the `count` property of the `newNode` object.
|
|
198
|
+
*/
|
|
199
|
+
protected _replaceNode(oldNode: NODE, newNode: NODE): NODE;
|
|
200
|
+
}
|