linked-list-typed 1.49.5 → 1.49.7
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/binary-tree/avl-tree.d.ts +53 -48
- package/dist/data-structures/binary-tree/avl-tree.js +55 -49
- package/dist/data-structures/binary-tree/binary-tree.d.ts +153 -130
- package/dist/data-structures/binary-tree/binary-tree.js +194 -153
- package/dist/data-structures/binary-tree/bst.d.ts +83 -71
- package/dist/data-structures/binary-tree/bst.js +114 -91
- package/dist/data-structures/binary-tree/rb-tree.d.ts +37 -35
- package/dist/data-structures/binary-tree/rb-tree.js +62 -59
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +46 -39
- package/dist/data-structures/binary-tree/tree-multimap.js +58 -51
- package/dist/data-structures/hash/hash-map.d.ts +24 -27
- package/dist/data-structures/hash/hash-map.js +35 -35
- package/dist/data-structures/hash/index.d.ts +0 -1
- package/dist/data-structures/hash/index.js +0 -1
- package/dist/data-structures/heap/heap.d.ts +2 -1
- package/dist/data-structures/heap/heap.js +13 -13
- package/dist/data-structures/heap/max-heap.js +1 -1
- package/dist/data-structures/heap/min-heap.js +1 -1
- package/dist/data-structures/linked-list/doubly-linked-list.js +1 -1
- package/dist/data-structures/linked-list/singly-linked-list.js +1 -3
- package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -8
- package/dist/data-structures/linked-list/skip-linked-list.js +15 -18
- package/dist/data-structures/matrix/matrix.d.ts +2 -7
- package/dist/data-structures/matrix/matrix.js +0 -7
- package/dist/data-structures/priority-queue/max-priority-queue.js +1 -1
- package/dist/data-structures/priority-queue/min-priority-queue.js +1 -1
- package/dist/data-structures/priority-queue/priority-queue.js +1 -1
- package/dist/data-structures/queue/deque.d.ts +2 -11
- package/dist/data-structures/queue/deque.js +9 -13
- package/dist/data-structures/queue/queue.d.ts +13 -13
- package/dist/data-structures/queue/queue.js +29 -25
- package/dist/data-structures/stack/stack.js +2 -3
- package/dist/data-structures/trie/trie.d.ts +2 -2
- package/dist/data-structures/trie/trie.js +9 -5
- package/dist/interfaces/binary-tree.d.ts +3 -3
- package/dist/types/common.d.ts +3 -3
- package/dist/types/common.js +2 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/bst.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
- package/dist/types/data-structures/hash/hash-map.d.ts +5 -2
- package/dist/types/data-structures/hash/index.d.ts +0 -1
- package/dist/types/data-structures/hash/index.js +0 -1
- package/dist/types/data-structures/heap/heap.d.ts +1 -1
- package/dist/types/data-structures/linked-list/index.d.ts +1 -0
- package/dist/types/data-structures/linked-list/index.js +1 -0
- package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +4 -1
- package/dist/types/data-structures/matrix/index.d.ts +1 -0
- package/dist/types/data-structures/matrix/index.js +1 -0
- package/dist/types/data-structures/matrix/matrix.d.ts +7 -1
- package/dist/types/data-structures/queue/deque.d.ts +3 -1
- package/dist/types/data-structures/trie/trie.d.ts +3 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +58 -53
- package/src/data-structures/binary-tree/binary-tree.ts +255 -211
- package/src/data-structures/binary-tree/bst.ts +126 -107
- package/src/data-structures/binary-tree/rb-tree.ts +66 -64
- package/src/data-structures/binary-tree/tree-multimap.ts +62 -56
- package/src/data-structures/hash/hash-map.ts +46 -50
- package/src/data-structures/hash/index.ts +0 -1
- package/src/data-structures/heap/heap.ts +20 -19
- package/src/data-structures/heap/max-heap.ts +1 -1
- package/src/data-structures/heap/min-heap.ts +1 -1
- package/src/data-structures/linked-list/doubly-linked-list.ts +1 -1
- package/src/data-structures/linked-list/singly-linked-list.ts +2 -5
- package/src/data-structures/linked-list/skip-linked-list.ts +15 -16
- package/src/data-structures/matrix/matrix.ts +2 -10
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/data-structures/queue/deque.ts +11 -15
- package/src/data-structures/queue/queue.ts +29 -28
- package/src/data-structures/stack/stack.ts +3 -6
- package/src/data-structures/trie/trie.ts +10 -11
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +3 -3
- package/src/types/data-structures/binary-tree/binary-tree.ts +2 -2
- package/src/types/data-structures/binary-tree/bst.ts +1 -1
- package/src/types/data-structures/binary-tree/tree-multimap.ts +1 -1
- package/src/types/data-structures/hash/hash-map.ts +6 -2
- package/src/types/data-structures/hash/index.ts +0 -1
- package/src/types/data-structures/heap/heap.ts +1 -1
- package/src/types/data-structures/linked-list/index.ts +1 -0
- package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -1
- package/src/types/data-structures/matrix/index.ts +1 -0
- package/src/types/data-structures/matrix/matrix.ts +7 -1
- package/src/types/data-structures/queue/deque.ts +1 -1
- package/src/types/data-structures/trie/trie.ts +1 -1
- package/dist/data-structures/hash/hash-table.d.ts +0 -108
- package/dist/data-structures/hash/hash-table.js +0 -281
- package/dist/types/data-structures/hash/hash-table.d.ts +0 -1
- package/dist/types/data-structures/hash/hash-table.js +0 -2
- package/dist/types/data-structures/matrix/matrix2d.d.ts +0 -1
- package/dist/types/data-structures/matrix/matrix2d.js +0 -2
- package/dist/types/data-structures/matrix/vector2d.d.ts +0 -1
- package/dist/types/data-structures/matrix/vector2d.js +0 -2
- package/src/data-structures/hash/hash-table.ts +0 -318
- package/src/types/data-structures/hash/hash-table.ts +0 -1
- package/src/types/data-structures/matrix/matrix2d.ts +0 -1
- package/src/types/data-structures/matrix/vector2d.ts +0 -1
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { BinaryTreeDeleteResult, BTNCallback,
|
|
8
|
+
import { BinaryTreeDeleteResult, BTNCallback, IterationType, KeyOrNodeOrEntry, RBTNColor, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
|
|
9
9
|
import { BST, BSTNode } from './bst';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
export declare class RedBlackTreeNode<K = any, V = any, N extends RedBlackTreeNode<K, V, N> = RedBlackTreeNodeNested<K, V>> extends BSTNode<K, V, N> {
|
|
@@ -23,16 +23,16 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
|
|
|
23
23
|
Sentinel: N;
|
|
24
24
|
/**
|
|
25
25
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
|
26
|
-
* initializes the tree with optional
|
|
27
|
-
* @param [
|
|
28
|
-
* objects. It represents the initial
|
|
26
|
+
* initializes the tree with optional nodes and options.
|
|
27
|
+
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>`
|
|
28
|
+
* objects. It represents the initial nodes that will be added to the RBTree during its
|
|
29
29
|
* construction. If this parameter is provided, the `addMany` method is called to add all the
|
|
30
|
-
*
|
|
30
|
+
* nodes to the
|
|
31
31
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
32
32
|
* behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide
|
|
33
33
|
* only a subset of the properties defined in the `RBTreeOptions` interface.
|
|
34
34
|
*/
|
|
35
|
-
constructor(
|
|
35
|
+
constructor(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: RBTreeOptions<K>);
|
|
36
36
|
protected _root: N;
|
|
37
37
|
get root(): N;
|
|
38
38
|
protected _size: number;
|
|
@@ -59,34 +59,40 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
|
|
|
59
59
|
*/
|
|
60
60
|
createTree(options?: RBTreeOptions<K>): TREE;
|
|
61
61
|
/**
|
|
62
|
-
* The function
|
|
63
|
-
* @param
|
|
64
|
-
* @
|
|
62
|
+
* The function `exemplarToNode` takes an keyOrNodeOrEntry and converts it into a node object if possible.
|
|
63
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, where:
|
|
64
|
+
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
65
|
+
* `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If a value
|
|
66
|
+
* is provided, it will be used when creating the new node. If no value is provided, the new node
|
|
67
|
+
* @returns a node of type N or undefined.
|
|
68
|
+
*/
|
|
69
|
+
exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): N | undefined;
|
|
70
|
+
/**
|
|
71
|
+
* The function checks if an keyOrNodeOrEntry is an instance of the RedBlackTreeNode class.
|
|
72
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
73
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the RedBlackTreeNode
|
|
65
74
|
* class.
|
|
66
75
|
*/
|
|
67
|
-
isNode(
|
|
76
|
+
isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N;
|
|
77
|
+
/**
|
|
78
|
+
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
79
|
+
* Space Complexity: O(1)
|
|
80
|
+
*/
|
|
81
|
+
isRealNode(node: N | undefined): node is N;
|
|
68
82
|
/**
|
|
69
83
|
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
70
84
|
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
71
85
|
* data type.
|
|
72
86
|
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
73
87
|
*/
|
|
74
|
-
isNotNodeInstance(potentialKey:
|
|
88
|
+
isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K;
|
|
75
89
|
/**
|
|
76
|
-
*
|
|
77
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, where:
|
|
78
|
-
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
79
|
-
* `exemplarToNode` function. It represents the value associated with the exemplar node. If a value
|
|
80
|
-
* is provided, it will be used when creating the new node. If no value is provided, the new node
|
|
81
|
-
* @returns a node of type N or undefined.
|
|
82
|
-
*/
|
|
83
|
-
exemplarToNode(exemplar: BTNExemplar<K, V, N>, value?: V): N | undefined;
|
|
84
|
-
/**
|
|
85
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
90
|
+
* Time Complexity: O(log n)
|
|
86
91
|
* Space Complexity: O(1)
|
|
92
|
+
* on average (where n is the number of nodes in the tree)
|
|
87
93
|
*/
|
|
88
94
|
/**
|
|
89
|
-
* Time Complexity: O(log n)
|
|
95
|
+
* Time Complexity: O(log n)
|
|
90
96
|
* Space Complexity: O(1)
|
|
91
97
|
*
|
|
92
98
|
* The `add` function adds a new node to a binary search tree and performs necessary rotations and
|
|
@@ -97,13 +103,14 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
|
|
|
97
103
|
* being added to the binary search tree.
|
|
98
104
|
* @returns The method `add` returns either the newly added node (`N`) or `undefined`.
|
|
99
105
|
*/
|
|
100
|
-
add(keyOrNodeOrEntry:
|
|
106
|
+
add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V): boolean;
|
|
101
107
|
/**
|
|
102
|
-
* Time Complexity: O(log n)
|
|
108
|
+
* Time Complexity: O(log n)
|
|
103
109
|
* Space Complexity: O(1)
|
|
110
|
+
* on average (where n is the number of nodes in the tree)
|
|
104
111
|
*/
|
|
105
112
|
/**
|
|
106
|
-
* Time Complexity: O(log n)
|
|
113
|
+
* Time Complexity: O(log n)
|
|
107
114
|
* Space Complexity: O(1)
|
|
108
115
|
*
|
|
109
116
|
* The `delete` function removes a node from a binary tree based on a given identifier and updates
|
|
@@ -118,11 +125,6 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
|
|
|
118
125
|
* @returns an array of `BinaryTreeDeleteResult<N>`.
|
|
119
126
|
*/
|
|
120
127
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null | undefined, callback?: C): BinaryTreeDeleteResult<N>[];
|
|
121
|
-
/**
|
|
122
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
123
|
-
* Space Complexity: O(1)
|
|
124
|
-
*/
|
|
125
|
-
isRealNode(node: N | undefined): node is N;
|
|
126
128
|
getNode<C extends BTNCallback<N, K>>(identifier: K, callback?: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
|
|
127
129
|
getNode<C extends BTNCallback<N, N>>(identifier: N | undefined, callback?: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
|
|
128
130
|
getNode<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback: C, beginRoot?: N | undefined, iterationType?: IterationType): N | undefined;
|
|
@@ -141,7 +143,7 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
|
|
|
141
143
|
*/
|
|
142
144
|
getPredecessor(x: N): N;
|
|
143
145
|
/**
|
|
144
|
-
* Time Complexity: O(
|
|
146
|
+
* Time Complexity: O(1)
|
|
145
147
|
* Space Complexity: O(1)
|
|
146
148
|
*/
|
|
147
149
|
clear(): void;
|
|
@@ -172,11 +174,11 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
|
|
|
172
174
|
*/
|
|
173
175
|
protected _rightRotate(x: N): void;
|
|
174
176
|
/**
|
|
175
|
-
* Time Complexity: O(log n)
|
|
177
|
+
* Time Complexity: O(log n)
|
|
176
178
|
* Space Complexity: O(1)
|
|
177
179
|
*/
|
|
178
180
|
/**
|
|
179
|
-
* Time Complexity: O(log n)
|
|
181
|
+
* Time Complexity: O(log n)
|
|
180
182
|
* Space Complexity: O(1)
|
|
181
183
|
*
|
|
182
184
|
* The function `_fixDelete` is used to fix the red-black tree after a node deletion.
|
|
@@ -197,11 +199,11 @@ export declare class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K
|
|
|
197
199
|
*/
|
|
198
200
|
protected _rbTransplant(u: N, v: N): void;
|
|
199
201
|
/**
|
|
200
|
-
* Time Complexity: O(log n)
|
|
202
|
+
* Time Complexity: O(log n)
|
|
201
203
|
* Space Complexity: O(1)
|
|
202
204
|
*/
|
|
203
205
|
/**
|
|
204
|
-
* Time Complexity: O(log n)
|
|
206
|
+
* Time Complexity: O(log n)
|
|
205
207
|
* Space Complexity: O(1)
|
|
206
208
|
*
|
|
207
209
|
* The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
|
|
@@ -27,22 +27,22 @@ exports.RedBlackTreeNode = RedBlackTreeNode;
|
|
|
27
27
|
class RedBlackTree extends bst_1.BST {
|
|
28
28
|
/**
|
|
29
29
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
|
30
|
-
* initializes the tree with optional
|
|
31
|
-
* @param [
|
|
32
|
-
* objects. It represents the initial
|
|
30
|
+
* initializes the tree with optional nodes and options.
|
|
31
|
+
* @param [keysOrNodesOrEntries] - The `keysOrNodesOrEntries` parameter is an optional iterable of `KeyOrNodeOrEntry<K, V, N>`
|
|
32
|
+
* objects. It represents the initial nodes that will be added to the RBTree during its
|
|
33
33
|
* construction. If this parameter is provided, the `addMany` method is called to add all the
|
|
34
|
-
*
|
|
34
|
+
* nodes to the
|
|
35
35
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
36
36
|
* behavior of the RBTree. It is of type `Partial<RBTreeOptions>`, which means that you can provide
|
|
37
37
|
* only a subset of the properties defined in the `RBTreeOptions` interface.
|
|
38
38
|
*/
|
|
39
|
-
constructor(
|
|
39
|
+
constructor(keysOrNodesOrEntries = [], options) {
|
|
40
40
|
super([], options);
|
|
41
41
|
this.Sentinel = new RedBlackTreeNode(NaN);
|
|
42
42
|
this._size = 0;
|
|
43
43
|
this._root = this.Sentinel;
|
|
44
|
-
if (
|
|
45
|
-
super.addMany(
|
|
44
|
+
if (keysOrNodesOrEntries)
|
|
45
|
+
super.addMany(keysOrNodesOrEntries);
|
|
46
46
|
}
|
|
47
47
|
get root() {
|
|
48
48
|
return this._root;
|
|
@@ -76,41 +76,23 @@ class RedBlackTree extends bst_1.BST {
|
|
|
76
76
|
return new RedBlackTree([], Object.assign({ iterationType: this.iterationType, variant: this.variant }, options));
|
|
77
77
|
}
|
|
78
78
|
/**
|
|
79
|
-
* The function
|
|
80
|
-
* @param
|
|
81
|
-
* @returns a boolean value indicating whether the exemplar is an instance of the RedBlackTreeNode
|
|
82
|
-
* class.
|
|
83
|
-
*/
|
|
84
|
-
isNode(exemplar) {
|
|
85
|
-
return exemplar instanceof RedBlackTreeNode;
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
89
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
90
|
-
* data type.
|
|
91
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
92
|
-
*/
|
|
93
|
-
isNotNodeInstance(potentialKey) {
|
|
94
|
-
return !(potentialKey instanceof RedBlackTreeNode);
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* The function `exemplarToNode` takes an exemplar and converts it into a node object if possible.
|
|
98
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, where:
|
|
79
|
+
* The function `exemplarToNode` takes an keyOrNodeOrEntry and converts it into a node object if possible.
|
|
80
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, where:
|
|
99
81
|
* @param {V} [value] - The `value` parameter is an optional value that can be passed to the
|
|
100
|
-
* `exemplarToNode` function. It represents the value associated with the
|
|
82
|
+
* `exemplarToNode` function. It represents the value associated with the keyOrNodeOrEntry node. If a value
|
|
101
83
|
* is provided, it will be used when creating the new node. If no value is provided, the new node
|
|
102
84
|
* @returns a node of type N or undefined.
|
|
103
85
|
*/
|
|
104
|
-
exemplarToNode(
|
|
86
|
+
exemplarToNode(keyOrNodeOrEntry, value) {
|
|
105
87
|
let node;
|
|
106
|
-
if (
|
|
88
|
+
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
107
89
|
return;
|
|
108
90
|
}
|
|
109
|
-
else if (this.isNode(
|
|
110
|
-
node =
|
|
91
|
+
else if (this.isNode(keyOrNodeOrEntry)) {
|
|
92
|
+
node = keyOrNodeOrEntry;
|
|
111
93
|
}
|
|
112
|
-
else if (this.isEntry(
|
|
113
|
-
const [key, value] =
|
|
94
|
+
else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
95
|
+
const [key, value] = keyOrNodeOrEntry;
|
|
114
96
|
if (key === undefined || key === null) {
|
|
115
97
|
return;
|
|
116
98
|
}
|
|
@@ -118,20 +100,48 @@ class RedBlackTree extends bst_1.BST {
|
|
|
118
100
|
node = this.createNode(key, value, types_1.RBTNColor.RED);
|
|
119
101
|
}
|
|
120
102
|
}
|
|
121
|
-
else if (this.isNotNodeInstance(
|
|
122
|
-
node = this.createNode(
|
|
103
|
+
else if (this.isNotNodeInstance(keyOrNodeOrEntry)) {
|
|
104
|
+
node = this.createNode(keyOrNodeOrEntry, value, types_1.RBTNColor.RED);
|
|
123
105
|
}
|
|
124
106
|
else {
|
|
125
107
|
return;
|
|
126
108
|
}
|
|
127
109
|
return node;
|
|
128
110
|
}
|
|
111
|
+
/**
|
|
112
|
+
* The function checks if an keyOrNodeOrEntry is an instance of the RedBlackTreeNode class.
|
|
113
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
114
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the RedBlackTreeNode
|
|
115
|
+
* class.
|
|
116
|
+
*/
|
|
117
|
+
isNode(keyOrNodeOrEntry) {
|
|
118
|
+
return keyOrNodeOrEntry instanceof RedBlackTreeNode;
|
|
119
|
+
}
|
|
129
120
|
/**
|
|
130
121
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
131
122
|
* Space Complexity: O(1)
|
|
132
123
|
*/
|
|
124
|
+
isRealNode(node) {
|
|
125
|
+
if (node === this.Sentinel || node === undefined)
|
|
126
|
+
return false;
|
|
127
|
+
return node instanceof RedBlackTreeNode;
|
|
128
|
+
}
|
|
133
129
|
/**
|
|
134
|
-
*
|
|
130
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
131
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
132
|
+
* data type.
|
|
133
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
134
|
+
*/
|
|
135
|
+
isNotNodeInstance(potentialKey) {
|
|
136
|
+
return !(potentialKey instanceof RedBlackTreeNode);
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Time Complexity: O(log n)
|
|
140
|
+
* Space Complexity: O(1)
|
|
141
|
+
* on average (where n is the number of nodes in the tree)
|
|
142
|
+
*/
|
|
143
|
+
/**
|
|
144
|
+
* Time Complexity: O(log n)
|
|
135
145
|
* Space Complexity: O(1)
|
|
136
146
|
*
|
|
137
147
|
* The `add` function adds a new node to a binary search tree and performs necessary rotations and
|
|
@@ -145,7 +155,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
145
155
|
add(keyOrNodeOrEntry, value) {
|
|
146
156
|
const newNode = this.exemplarToNode(keyOrNodeOrEntry, value);
|
|
147
157
|
if (newNode === undefined)
|
|
148
|
-
return;
|
|
158
|
+
return false;
|
|
149
159
|
newNode.left = this.Sentinel;
|
|
150
160
|
newNode.right = this.Sentinel;
|
|
151
161
|
let y = undefined;
|
|
@@ -163,7 +173,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
163
173
|
if (newNode !== x) {
|
|
164
174
|
this._replaceNode(x, newNode);
|
|
165
175
|
}
|
|
166
|
-
return;
|
|
176
|
+
return false;
|
|
167
177
|
}
|
|
168
178
|
}
|
|
169
179
|
}
|
|
@@ -180,21 +190,23 @@ class RedBlackTree extends bst_1.BST {
|
|
|
180
190
|
if (newNode.parent === undefined) {
|
|
181
191
|
newNode.color = types_1.RBTNColor.BLACK;
|
|
182
192
|
this._size++;
|
|
183
|
-
return;
|
|
193
|
+
return false;
|
|
184
194
|
}
|
|
185
195
|
if (newNode.parent.parent === undefined) {
|
|
186
196
|
this._size++;
|
|
187
|
-
return;
|
|
197
|
+
return false;
|
|
188
198
|
}
|
|
189
199
|
this._fixInsert(newNode);
|
|
190
200
|
this._size++;
|
|
201
|
+
return true;
|
|
191
202
|
}
|
|
192
203
|
/**
|
|
193
|
-
* Time Complexity: O(log n)
|
|
204
|
+
* Time Complexity: O(log n)
|
|
194
205
|
* Space Complexity: O(1)
|
|
206
|
+
* on average (where n is the number of nodes in the tree)
|
|
195
207
|
*/
|
|
196
208
|
/**
|
|
197
|
-
* Time Complexity: O(log n)
|
|
209
|
+
* Time Complexity: O(log n)
|
|
198
210
|
* Space Complexity: O(1)
|
|
199
211
|
*
|
|
200
212
|
* The `delete` function removes a node from a binary tree based on a given identifier and updates
|
|
@@ -267,20 +279,11 @@ class RedBlackTree extends bst_1.BST {
|
|
|
267
279
|
return ans;
|
|
268
280
|
}
|
|
269
281
|
/**
|
|
270
|
-
* Time Complexity: O(log n)
|
|
271
|
-
* Space Complexity: O(1)
|
|
272
|
-
*/
|
|
273
|
-
isRealNode(node) {
|
|
274
|
-
if (node === this.Sentinel || node === undefined)
|
|
275
|
-
return false;
|
|
276
|
-
return node instanceof RedBlackTreeNode;
|
|
277
|
-
}
|
|
278
|
-
/**
|
|
279
|
-
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
282
|
+
* Time Complexity: O(log n)
|
|
280
283
|
* Space Complexity: O(1)
|
|
281
284
|
*/
|
|
282
285
|
/**
|
|
283
|
-
* Time Complexity: O(log n)
|
|
286
|
+
* Time Complexity: O(log n)
|
|
284
287
|
* Space Complexity: O(1)
|
|
285
288
|
*
|
|
286
289
|
* The function `getNode` retrieves a single node from a binary tree based on a given identifier and
|
|
@@ -332,7 +335,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
332
335
|
return y;
|
|
333
336
|
}
|
|
334
337
|
/**
|
|
335
|
-
* Time Complexity: O(
|
|
338
|
+
* Time Complexity: O(1)
|
|
336
339
|
* Space Complexity: O(1)
|
|
337
340
|
*/
|
|
338
341
|
clear() {
|
|
@@ -413,11 +416,11 @@ class RedBlackTree extends bst_1.BST {
|
|
|
413
416
|
}
|
|
414
417
|
}
|
|
415
418
|
/**
|
|
416
|
-
* Time Complexity: O(log n)
|
|
419
|
+
* Time Complexity: O(log n)
|
|
417
420
|
* Space Complexity: O(1)
|
|
418
421
|
*/
|
|
419
422
|
/**
|
|
420
|
-
* Time Complexity: O(log n)
|
|
423
|
+
* Time Complexity: O(log n)
|
|
421
424
|
* Space Complexity: O(1)
|
|
422
425
|
*
|
|
423
426
|
* The function `_fixDelete` is used to fix the red-black tree after a node deletion.
|
|
@@ -512,11 +515,11 @@ class RedBlackTree extends bst_1.BST {
|
|
|
512
515
|
v.parent = u.parent;
|
|
513
516
|
}
|
|
514
517
|
/**
|
|
515
|
-
* Time Complexity: O(log n)
|
|
518
|
+
* Time Complexity: O(log n)
|
|
516
519
|
* Space Complexity: O(1)
|
|
517
520
|
*/
|
|
518
521
|
/**
|
|
519
|
-
* Time Complexity: O(log n)
|
|
522
|
+
* Time Complexity: O(log n)
|
|
520
523
|
* Space Complexity: O(1)
|
|
521
524
|
*
|
|
522
525
|
* The `_fixInsert` function is used to fix the red-black tree after an insertion operation.
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback,
|
|
8
|
+
import type { BinaryTreeDeleteResult, BSTNKeyOrNode, BTNCallback, KeyOrNodeOrEntry, TreeMultimapNested, TreeMultimapNodeNested, TreeMultimapOptions } from '../../types';
|
|
9
9
|
import { IterationType } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
@@ -27,7 +27,7 @@ export declare class TreeMultimapNode<K = any, V = any, N extends TreeMultimapNo
|
|
|
27
27
|
* 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.
|
|
28
28
|
*/
|
|
29
29
|
export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N> = TreeMultimapNode<K, V, TreeMultimapNodeNested<K, V>>, TREE extends TreeMultimap<K, V, N, TREE> = TreeMultimap<K, V, N, TreeMultimapNested<K, V, N>>> extends AVLTree<K, V, N, TREE> implements IBinaryTree<K, V, N, TREE> {
|
|
30
|
-
constructor(
|
|
30
|
+
constructor(keysOrNodesOrEntries?: Iterable<KeyOrNodeOrEntry<K, V, N>>, options?: TreeMultimapOptions<K>);
|
|
31
31
|
private _count;
|
|
32
32
|
get count(): number;
|
|
33
33
|
/**
|
|
@@ -42,22 +42,8 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
|
|
|
42
42
|
createNode(key: K, value?: V, count?: number): N;
|
|
43
43
|
createTree(options?: TreeMultimapOptions<K>): TREE;
|
|
44
44
|
/**
|
|
45
|
-
* The function
|
|
46
|
-
* @param
|
|
47
|
-
* @returns a boolean value indicating whether the exemplar is an instance of the TreeMultimapNode
|
|
48
|
-
* class.
|
|
49
|
-
*/
|
|
50
|
-
isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N;
|
|
51
|
-
/**
|
|
52
|
-
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
53
|
-
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
54
|
-
* data type.
|
|
55
|
-
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
56
|
-
*/
|
|
57
|
-
isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K;
|
|
58
|
-
/**
|
|
59
|
-
* The function `exemplarToNode` converts an exemplar object into a node object.
|
|
60
|
-
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`, which means it
|
|
45
|
+
* The function `exemplarToNode` converts an keyOrNodeOrEntry object into a node object.
|
|
46
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`, which means it
|
|
61
47
|
* can be one of the following:
|
|
62
48
|
* @param {V} [value] - The `value` parameter is an optional argument that represents the value
|
|
63
49
|
* associated with the node. It is of type `V`, which can be any data type. If no value is provided,
|
|
@@ -66,14 +52,29 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
|
|
|
66
52
|
* times the value should be added to the node. If not provided, it defaults to 1.
|
|
67
53
|
* @returns a node of type `N` or `undefined`.
|
|
68
54
|
*/
|
|
69
|
-
exemplarToNode(
|
|
55
|
+
exemplarToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V, count?: number): N | undefined;
|
|
70
56
|
/**
|
|
71
|
-
*
|
|
72
|
-
*
|
|
57
|
+
* The function checks if an keyOrNodeOrEntry is an instance of the TreeMultimapNode class.
|
|
58
|
+
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is of type `KeyOrNodeOrEntry<K, V, N>`.
|
|
59
|
+
* @returns a boolean value indicating whether the keyOrNodeOrEntry is an instance of the TreeMultimapNode
|
|
60
|
+
* class.
|
|
73
61
|
*/
|
|
62
|
+
isNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>): keyOrNodeOrEntry is N;
|
|
74
63
|
/**
|
|
75
|
-
*
|
|
76
|
-
*
|
|
64
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
65
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
66
|
+
* data type.
|
|
67
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
68
|
+
*/
|
|
69
|
+
isNotNodeInstance(potentialKey: KeyOrNodeOrEntry<K, V, N>): potentialKey is K;
|
|
70
|
+
/**
|
|
71
|
+
* Time Complexity: O(log n)
|
|
72
|
+
* Space Complexity: O(1)
|
|
73
|
+
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
|
|
74
|
+
*/
|
|
75
|
+
/**
|
|
76
|
+
* Time Complexity: O(log n)
|
|
77
|
+
* Space Complexity: O(1)
|
|
77
78
|
*
|
|
78
79
|
* The function overrides the add method of a binary tree node and adds a new node to the tree.
|
|
79
80
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter can be either a key, a node, or an
|
|
@@ -87,14 +88,15 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
|
|
|
87
88
|
* @returns The method is returning either the newly inserted node or `undefined` if the insertion
|
|
88
89
|
* was not successful.
|
|
89
90
|
*/
|
|
90
|
-
add(keyOrNodeOrEntry:
|
|
91
|
+
add(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, N>, value?: V, count?: number): boolean;
|
|
91
92
|
/**
|
|
92
|
-
* Time Complexity: O(k log n)
|
|
93
|
-
* Space Complexity: O(1)
|
|
93
|
+
* Time Complexity: O(k log n)
|
|
94
|
+
* Space Complexity: O(1)
|
|
95
|
+
* logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity. constant space, as it doesn't use additional data structures that scale with input size.
|
|
94
96
|
*/
|
|
95
97
|
/**
|
|
96
|
-
* Time Complexity: O(k log n)
|
|
97
|
-
* Space Complexity: O(1)
|
|
98
|
+
* Time Complexity: O(k log n)
|
|
99
|
+
* Space Complexity: O(1)
|
|
98
100
|
*
|
|
99
101
|
* The function overrides the addMany method to add multiple keys, nodes, or entries to a data
|
|
100
102
|
* structure.
|
|
@@ -102,14 +104,15 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
|
|
|
102
104
|
* either keys, nodes, or entries.
|
|
103
105
|
* @returns The method is returning an array of type `N | undefined`.
|
|
104
106
|
*/
|
|
105
|
-
addMany(keysOrNodesOrEntries: Iterable<
|
|
107
|
+
addMany(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, N>>): boolean[];
|
|
106
108
|
/**
|
|
107
|
-
* Time Complexity: O(
|
|
108
|
-
* Space Complexity: O(
|
|
109
|
+
* Time Complexity: O(n log n)
|
|
110
|
+
* Space Complexity: O(n)
|
|
111
|
+
* logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node. linear space, as it creates an array to store the sorted nodes.
|
|
109
112
|
*/
|
|
110
113
|
/**
|
|
111
|
-
* Time Complexity: O(n log n)
|
|
112
|
-
* Space Complexity: O(n)
|
|
114
|
+
* Time Complexity: O(n log n)
|
|
115
|
+
* Space Complexity: O(n)
|
|
113
116
|
*
|
|
114
117
|
* The `perfectlyBalance` function takes a sorted array of nodes and builds a balanced binary search
|
|
115
118
|
* tree using either a recursive or iterative approach.
|
|
@@ -120,12 +123,13 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
|
|
|
120
123
|
*/
|
|
121
124
|
perfectlyBalance(iterationType?: IterationType): boolean;
|
|
122
125
|
/**
|
|
123
|
-
* Time Complexity: O(k log n)
|
|
124
|
-
* Space Complexity: O(1)
|
|
126
|
+
* Time Complexity: O(k log n)
|
|
127
|
+
* Space Complexity: O(1)
|
|
128
|
+
* logarithmic time for each insertion, where "n" is the number of nodes in the tree, and "k" is the number of keys to be inserted. This is because the method iterates through the keys and calls the add method for each. constant space, as it doesn't use additional data structures that scale with input size.
|
|
125
129
|
*/
|
|
126
130
|
/**
|
|
127
|
-
* Time Complexity: O(log n)
|
|
128
|
-
* Space Complexity: O(1)
|
|
131
|
+
* Time Complexity: O(k log n)
|
|
132
|
+
* Space Complexity: O(1)
|
|
129
133
|
*
|
|
130
134
|
* The `delete` function in TypeScript is used to remove a node from a binary tree, taking into
|
|
131
135
|
* account the count of the node and balancing the tree if necessary.
|
|
@@ -144,10 +148,13 @@ export declare class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K
|
|
|
144
148
|
*/
|
|
145
149
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C>, callback?: C, ignoreCount?: boolean): BinaryTreeDeleteResult<N>[];
|
|
146
150
|
/**
|
|
147
|
-
* Time Complexity: O(
|
|
148
|
-
* Space Complexity: O(
|
|
151
|
+
* Time Complexity: O(1)
|
|
152
|
+
* Space Complexity: O(1)
|
|
149
153
|
*/
|
|
150
154
|
/**
|
|
155
|
+
* Time Complexity: O(1)
|
|
156
|
+
* Space Complexity: O(1)
|
|
157
|
+
*
|
|
151
158
|
* The clear() function clears the contents of a data structure and sets the count to zero.
|
|
152
159
|
*/
|
|
153
160
|
clear(): void;
|