undirected-graph-typed 1.51.8 → 1.52.1
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/index.d.ts +2 -1
- package/dist/data-structures/base/index.js +2 -1
- package/dist/data-structures/base/iterable-element-base.d.ts +171 -0
- package/dist/data-structures/base/iterable-element-base.js +225 -0
- package/dist/data-structures/base/{iterable-base.d.ts → iterable-entry-base.d.ts} +4 -147
- package/dist/data-structures/base/{iterable-base.js → iterable-entry-base.js} +12 -189
- package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +106 -68
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +119 -87
- package/dist/data-structures/binary-tree/avl-tree.d.ts +82 -62
- package/dist/data-structures/binary-tree/avl-tree.js +78 -59
- package/dist/data-structures/binary-tree/binary-tree.d.ts +333 -241
- package/dist/data-structures/binary-tree/binary-tree.js +478 -366
- package/dist/data-structures/binary-tree/bst.d.ts +202 -212
- package/dist/data-structures/binary-tree/bst.js +208 -250
- package/dist/data-structures/binary-tree/rb-tree.d.ts +73 -74
- package/dist/data-structures/binary-tree/rb-tree.js +107 -98
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +92 -75
- package/dist/data-structures/binary-tree/tree-multi-map.js +105 -93
- package/dist/data-structures/graph/abstract-graph.d.ts +10 -15
- package/dist/data-structures/graph/abstract-graph.js +10 -15
- package/dist/data-structures/graph/directed-graph.js +2 -1
- package/dist/data-structures/hash/hash-map.d.ts +33 -40
- package/dist/data-structures/hash/hash-map.js +40 -55
- package/dist/data-structures/heap/heap.d.ts +43 -114
- package/dist/data-structures/heap/heap.js +59 -127
- package/dist/data-structures/heap/max-heap.d.ts +50 -4
- package/dist/data-structures/heap/max-heap.js +76 -10
- package/dist/data-structures/heap/min-heap.d.ts +51 -5
- package/dist/data-structures/heap/min-heap.js +68 -11
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +22 -28
- package/dist/data-structures/linked-list/doubly-linked-list.js +26 -28
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +22 -25
- package/dist/data-structures/linked-list/singly-linked-list.js +29 -26
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +50 -4
- package/dist/data-structures/priority-queue/max-priority-queue.js +79 -10
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +51 -5
- package/dist/data-structures/priority-queue/min-priority-queue.js +71 -11
- package/dist/data-structures/priority-queue/priority-queue.d.ts +50 -4
- package/dist/data-structures/priority-queue/priority-queue.js +70 -1
- package/dist/data-structures/queue/deque.d.ts +28 -20
- package/dist/data-structures/queue/deque.js +45 -24
- package/dist/data-structures/queue/queue.d.ts +8 -29
- package/dist/data-structures/queue/queue.js +15 -32
- package/dist/data-structures/stack/stack.d.ts +17 -22
- package/dist/data-structures/stack/stack.js +25 -24
- package/dist/data-structures/trie/trie.d.ts +19 -14
- package/dist/data-structures/trie/trie.js +27 -16
- package/dist/interfaces/binary-tree.d.ts +7 -7
- package/dist/types/common.d.ts +1 -23
- package/dist/types/data-structures/base/base.d.ts +5 -2
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -4
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -4
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +22 -5
- package/dist/types/data-structures/binary-tree/bst.d.ts +7 -5
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +3 -4
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -4
- package/dist/types/data-structures/heap/heap.d.ts +3 -2
- package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -1
- package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +2 -1
- package/dist/types/data-structures/priority-queue/priority-queue.d.ts +1 -1
- package/dist/types/data-structures/queue/deque.d.ts +4 -2
- package/dist/types/data-structures/queue/queue.d.ts +2 -1
- package/dist/types/data-structures/stack/stack.d.ts +2 -1
- package/dist/types/data-structures/trie/trie.d.ts +3 -2
- package/dist/utils/utils.js +3 -5
- package/package.json +2 -2
- package/src/data-structures/base/index.ts +2 -1
- package/src/data-structures/base/iterable-element-base.ts +250 -0
- package/src/data-structures/base/{iterable-base.ts → iterable-entry-base.ts} +22 -213
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +146 -97
- package/src/data-structures/binary-tree/avl-tree.ts +97 -70
- package/src/data-structures/binary-tree/binary-tree.ts +591 -455
- package/src/data-structures/binary-tree/bst.ts +266 -293
- package/src/data-structures/binary-tree/rb-tree.ts +124 -104
- package/src/data-structures/binary-tree/tree-multi-map.ts +128 -103
- package/src/data-structures/graph/abstract-graph.ts +10 -10
- package/src/data-structures/graph/directed-graph.ts +2 -1
- package/src/data-structures/hash/hash-map.ts +46 -53
- package/src/data-structures/heap/heap.ts +71 -152
- package/src/data-structures/heap/max-heap.ts +88 -13
- package/src/data-structures/heap/min-heap.ts +78 -15
- package/src/data-structures/linked-list/doubly-linked-list.ts +32 -32
- package/src/data-structures/linked-list/singly-linked-list.ts +37 -29
- package/src/data-structures/priority-queue/max-priority-queue.ts +94 -13
- package/src/data-structures/priority-queue/min-priority-queue.ts +84 -15
- package/src/data-structures/priority-queue/priority-queue.ts +81 -4
- package/src/data-structures/queue/deque.ts +52 -27
- package/src/data-structures/queue/queue.ts +23 -37
- package/src/data-structures/stack/stack.ts +31 -26
- package/src/data-structures/trie/trie.ts +35 -20
- package/src/interfaces/binary-tree.ts +10 -10
- package/src/types/common.ts +2 -25
- package/src/types/data-structures/base/base.ts +14 -6
- package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +3 -4
- package/src/types/data-structures/binary-tree/avl-tree.ts +3 -4
- package/src/types/data-structures/binary-tree/binary-tree.ts +26 -6
- package/src/types/data-structures/binary-tree/bst.ts +11 -5
- package/src/types/data-structures/binary-tree/rb-tree.ts +3 -4
- package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -4
- package/src/types/data-structures/heap/heap.ts +4 -1
- package/src/types/data-structures/linked-list/doubly-linked-list.ts +3 -1
- package/src/types/data-structures/linked-list/singly-linked-list.ts +3 -1
- package/src/types/data-structures/priority-queue/priority-queue.ts +1 -1
- package/src/types/data-structures/queue/deque.ts +6 -1
- package/src/types/data-structures/queue/queue.ts +3 -1
- package/src/types/data-structures/stack/stack.ts +3 -1
- package/src/types/data-structures/trie/trie.ts +3 -1
- package/src/utils/utils.ts +3 -3
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
BinaryTreeDeleteResult,
|
|
3
3
|
BTNCallback,
|
|
4
|
-
|
|
4
|
+
BTNKeyOrNodeOrEntry,
|
|
5
5
|
CRUD,
|
|
6
|
-
KeyOrNodeOrEntry,
|
|
7
6
|
RBTNColor,
|
|
8
7
|
RBTreeOptions,
|
|
9
8
|
RedBlackTreeNested,
|
|
10
9
|
RedBlackTreeNodeNested
|
|
11
10
|
} from '../../types';
|
|
11
|
+
import { BTNEntry } from '../../types';
|
|
12
12
|
import { BST, BSTNode } from './bst';
|
|
13
13
|
import { IBinaryTree } from '../../interfaces';
|
|
14
14
|
|
|
15
15
|
export class RedBlackTreeNode<
|
|
16
|
-
K
|
|
16
|
+
K = any,
|
|
17
17
|
V = any,
|
|
18
18
|
NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNodeNested<K, V>
|
|
19
19
|
> extends BSTNode<K, V, NODE> {
|
|
@@ -53,30 +53,34 @@ export class RedBlackTreeNode<
|
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
export class RedBlackTree<
|
|
56
|
-
K
|
|
56
|
+
K = any,
|
|
57
57
|
V = any,
|
|
58
|
+
R = BTNEntry<K, V>,
|
|
58
59
|
NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNode<K, V, RedBlackTreeNodeNested<K, V>>,
|
|
59
|
-
TREE extends RedBlackTree<K, V, NODE, TREE> = RedBlackTree<K, V, NODE, RedBlackTreeNested<K, V, NODE>>
|
|
60
|
+
TREE extends RedBlackTree<K, V, R, NODE, TREE> = RedBlackTree<K, V, R, NODE, RedBlackTreeNested<K, V, R, NODE>>
|
|
60
61
|
>
|
|
61
|
-
extends BST<K, V, NODE, TREE>
|
|
62
|
-
implements IBinaryTree<K, V, NODE, TREE> {
|
|
62
|
+
extends BST<K, V, R, NODE, TREE>
|
|
63
|
+
implements IBinaryTree<K, V, R, NODE, TREE> {
|
|
63
64
|
/**
|
|
64
65
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript.
|
|
65
|
-
* @param
|
|
66
|
-
* contain keys, nodes, or
|
|
67
|
-
*
|
|
66
|
+
* @param keysOrNodesOrEntriesOrRawElements - The `keysOrNodesOrEntriesOrRawElements` parameter is an
|
|
67
|
+
* iterable object that can contain either keys, nodes, entries, or raw elements. It is used to
|
|
68
|
+
* initialize the RBTree with the provided elements.
|
|
68
69
|
* @param [options] - The `options` parameter is an optional object that can be passed to the
|
|
69
|
-
* constructor. It
|
|
70
|
-
*
|
|
71
|
-
*
|
|
70
|
+
* constructor. It is of type `RBTreeOptions<K, V, R>`. This object can contain various options for
|
|
71
|
+
* configuring the behavior of the Red-Black Tree. The specific properties and their meanings would
|
|
72
|
+
* depend on the implementation
|
|
72
73
|
*/
|
|
73
|
-
constructor(
|
|
74
|
+
constructor(
|
|
75
|
+
keysOrNodesOrEntriesOrRawElements: Iterable<R | BTNKeyOrNodeOrEntry<K, V, NODE>> = [],
|
|
76
|
+
options?: RBTreeOptions<K, V, R>
|
|
77
|
+
) {
|
|
74
78
|
super([], options);
|
|
75
79
|
|
|
76
80
|
this._root = this.NIL;
|
|
77
81
|
|
|
78
|
-
if (
|
|
79
|
-
this.addMany(
|
|
82
|
+
if (keysOrNodesOrEntriesOrRawElements) {
|
|
83
|
+
this.addMany(keysOrNodesOrEntriesOrRawElements);
|
|
80
84
|
}
|
|
81
85
|
}
|
|
82
86
|
|
|
@@ -92,29 +96,30 @@ export class RedBlackTree<
|
|
|
92
96
|
|
|
93
97
|
/**
|
|
94
98
|
* The function creates a new Red-Black Tree node with the specified key, value, and color.
|
|
95
|
-
* @param {K} key - The key parameter represents the key of the node being created. It is of
|
|
96
|
-
* which is a generic type
|
|
99
|
+
* @param {K} key - The key parameter represents the key value of the node being created. It is of
|
|
100
|
+
* type K, which is a generic type that can be replaced with any specific type when using the
|
|
101
|
+
* function.
|
|
97
102
|
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
98
|
-
* associated with the key in the node. It is not required and can be omitted if
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
* can
|
|
102
|
-
*
|
|
103
|
-
* value, and color
|
|
103
|
+
* associated with the key in the node. It is not required and can be omitted if you only need to
|
|
104
|
+
* create a node with a key.
|
|
105
|
+
* @param {RBTNColor} [color=BLACK] - The "color" parameter is used to specify the color of the node
|
|
106
|
+
* in a Red-Black Tree. It can have two possible values: "RED" or "BLACK". By default, the color is
|
|
107
|
+
* set to "BLACK" if not specified.
|
|
108
|
+
* @returns A new instance of a RedBlackTreeNode with the specified key, value, and color is being
|
|
109
|
+
* returned.
|
|
104
110
|
*/
|
|
105
111
|
override createNode(key: K, value?: V, color: RBTNColor = 'BLACK'): NODE {
|
|
106
112
|
return new RedBlackTreeNode<K, V, NODE>(key, value, color) as NODE;
|
|
107
113
|
}
|
|
108
114
|
|
|
109
115
|
/**
|
|
110
|
-
* The function creates a Red-Black Tree with the
|
|
111
|
-
* @param [options] - The `options` parameter is an optional object that contains
|
|
112
|
-
* options for creating the Red-Black Tree. It
|
|
113
|
-
* the type of keys in the tree.
|
|
116
|
+
* The function creates a new Red-Black Tree with the specified options.
|
|
117
|
+
* @param [options] - The `options` parameter is an optional object that contains additional
|
|
118
|
+
* configuration options for creating the Red-Black Tree. It has the following properties:
|
|
114
119
|
* @returns a new instance of a RedBlackTree object.
|
|
115
120
|
*/
|
|
116
|
-
override createTree(options?: RBTreeOptions<K>): TREE {
|
|
117
|
-
return new RedBlackTree<K, V, NODE, TREE>([], {
|
|
121
|
+
override createTree(options?: RBTreeOptions<K, V, R>): TREE {
|
|
122
|
+
return new RedBlackTree<K, V, R, NODE, TREE>([], {
|
|
118
123
|
iterationType: this.iterationType,
|
|
119
124
|
...options
|
|
120
125
|
}) as TREE;
|
|
@@ -126,54 +131,57 @@ export class RedBlackTree<
|
|
|
126
131
|
*/
|
|
127
132
|
|
|
128
133
|
/**
|
|
129
|
-
* Time Complexity: O(1)
|
|
130
|
-
* Space Complexity: O(1)
|
|
131
|
-
*
|
|
132
|
-
* The function `keyValueOrEntryToNode` takes a key, value, or entry and returns a node if it is
|
|
133
|
-
* valid, otherwise it returns undefined.
|
|
134
|
-
* @param {KeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntry - The key, value, or entry to convert.
|
|
135
|
-
* @param {V} [value] - The value associated with the key (if `keyOrNodeOrEntry` is a key).
|
|
136
|
-
* @returns {NODE | undefined} - The corresponding Red-Black Tree node, or `undefined` if conversion fails.
|
|
137
|
-
*/
|
|
138
|
-
override keyValueOrEntryToNode(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>, value?: V): NODE | undefined {
|
|
139
|
-
let node: NODE | undefined;
|
|
140
|
-
|
|
141
|
-
if (keyOrNodeOrEntry === null || keyOrNodeOrEntry === undefined) {
|
|
142
|
-
return;
|
|
143
|
-
} else if (this.isNode(keyOrNodeOrEntry)) {
|
|
144
|
-
node = keyOrNodeOrEntry;
|
|
145
|
-
} else if (this.isEntry(keyOrNodeOrEntry)) {
|
|
146
|
-
const [key, value] = keyOrNodeOrEntry;
|
|
147
|
-
if (key === undefined || key === null) {
|
|
148
|
-
return;
|
|
149
|
-
} else {
|
|
150
|
-
node = this.createNode(key, value, 'RED');
|
|
151
|
-
}
|
|
152
|
-
} else if (!this.isNode(keyOrNodeOrEntry)) {
|
|
153
|
-
node = this.createNode(keyOrNodeOrEntry, value, 'RED');
|
|
154
|
-
} else {
|
|
155
|
-
return;
|
|
156
|
-
}
|
|
157
|
-
return node;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* Time Complexity: O(1)
|
|
162
|
-
* Space Complexity: O(1)
|
|
163
|
-
* /
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
134
|
* Time Complexity: O(1)
|
|
167
135
|
* Space Complexity: O(1)
|
|
168
136
|
*
|
|
169
137
|
* The function checks if the input is an instance of the RedBlackTreeNode class.
|
|
170
|
-
* @param {
|
|
171
|
-
*
|
|
138
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
139
|
+
* `keyOrNodeOrEntryOrRawElement` can be of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
140
|
+
* @returns a boolean value indicating whether the input parameter `keyOrNodeOrEntryOrRawElement` is
|
|
141
|
+
* an instance of the `RedBlackTreeNode` class.
|
|
172
142
|
*/
|
|
173
|
-
override isNode(
|
|
174
|
-
|
|
143
|
+
override isNode(
|
|
144
|
+
keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>
|
|
145
|
+
): keyOrNodeOrEntryOrRawElement is NODE {
|
|
146
|
+
return keyOrNodeOrEntryOrRawElement instanceof RedBlackTreeNode;
|
|
175
147
|
}
|
|
176
148
|
|
|
149
|
+
// /**
|
|
150
|
+
// * Time Complexity: O(1)
|
|
151
|
+
// * Space Complexity: O(1)
|
|
152
|
+
// */
|
|
153
|
+
//
|
|
154
|
+
// /**
|
|
155
|
+
// * Time Complexity: O(1)
|
|
156
|
+
// * Space Complexity: O(1)
|
|
157
|
+
// *
|
|
158
|
+
// * The function `keyValueOrEntryOrRawElementToNode` takes a key, value, or entry and returns a node if it is
|
|
159
|
+
// * valid, otherwise it returns undefined.
|
|
160
|
+
// * @param {BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The key, value, or entry to convert.
|
|
161
|
+
// * @param {V} [value] - The value associated with the key (if `keyOrNodeOrEntryOrRawElement` is a key).
|
|
162
|
+
// * @returns {NODE | undefined} - The corresponding Red-Black Tree node, or `undefined` if conversion fails.
|
|
163
|
+
// */
|
|
164
|
+
// override keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V): NODE | undefined {
|
|
165
|
+
//
|
|
166
|
+
// if (keyOrNodeOrEntryOrRawElement === null || keyOrNodeOrEntryOrRawElement === undefined) return;
|
|
167
|
+
// if (this.isNode(keyOrNodeOrEntryOrRawElement)) return keyOrNodeOrEntryOrRawElement;
|
|
168
|
+
//
|
|
169
|
+
// if (this.toEntryFn) {
|
|
170
|
+
// const [key, entryValue] = this.toEntryFn(keyOrNodeOrEntryOrRawElement as R);
|
|
171
|
+
// if (key) return this.createNode(key, entryValue ?? value, 'RED');
|
|
172
|
+
// }
|
|
173
|
+
//
|
|
174
|
+
// if (this.isEntry(keyOrNodeOrEntryOrRawElement)) {
|
|
175
|
+
// const [key, value] = keyOrNodeOrEntryOrRawElement;
|
|
176
|
+
// if (key === undefined || key === null) return;
|
|
177
|
+
// else return this.createNode(key, value, 'RED');
|
|
178
|
+
// }
|
|
179
|
+
//
|
|
180
|
+
// if (this.isKey(keyOrNodeOrEntryOrRawElement)) return this.createNode(keyOrNodeOrEntryOrRawElement, value, 'RED');
|
|
181
|
+
//
|
|
182
|
+
// return ;
|
|
183
|
+
// }
|
|
184
|
+
|
|
177
185
|
/**
|
|
178
186
|
* Time Complexity: O(1)
|
|
179
187
|
* Space Complexity: O(1)
|
|
@@ -200,17 +208,19 @@ export class RedBlackTree<
|
|
|
200
208
|
* Time Complexity: O(log n)
|
|
201
209
|
* Space Complexity: O(1)
|
|
202
210
|
*
|
|
203
|
-
* The function adds a new node to a
|
|
204
|
-
*
|
|
205
|
-
* @param
|
|
206
|
-
*
|
|
207
|
-
* @param {V} [value] - The `value` parameter is
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
+
* The function adds a new node to a binary search tree and returns true if the node was successfully
|
|
212
|
+
* added.
|
|
213
|
+
* @param {R | BTNKeyOrNodeOrEntry<K, V, NODE>} keyOrNodeOrEntryOrRawElement - The parameter
|
|
214
|
+
* `keyOrNodeOrEntryOrRawElement` can accept a value of type `R` or `BTNKeyOrNodeOrEntry<K, V, NODE>`.
|
|
215
|
+
* @param {V} [value] - The `value` parameter is an optional value that you want to associate with
|
|
216
|
+
* the key in the data structure. It represents the value that you want to add or update in the data
|
|
217
|
+
* structure.
|
|
218
|
+
* @returns The method is returning a boolean value. If a new node is successfully added to the tree,
|
|
219
|
+
* the method returns true. If the node already exists and its value is updated, the method also
|
|
220
|
+
* returns true. If the node cannot be added or updated, the method returns false.
|
|
211
221
|
*/
|
|
212
|
-
override add(
|
|
213
|
-
const newNode = this.
|
|
222
|
+
override add(keyOrNodeOrEntryOrRawElement: R | BTNKeyOrNodeOrEntry<K, V, NODE>, value?: V): boolean {
|
|
223
|
+
const newNode = this.keyValueOrEntryOrRawElementToNode(keyOrNodeOrEntryOrRawElement, value);
|
|
214
224
|
if (!this.isRealNode(newNode)) return false;
|
|
215
225
|
|
|
216
226
|
const insertStatus = this._insert(newNode);
|
|
@@ -236,16 +246,16 @@ export class RedBlackTree<
|
|
|
236
246
|
* Time Complexity: O(log n)
|
|
237
247
|
* Space Complexity: O(1)
|
|
238
248
|
*
|
|
239
|
-
* The function
|
|
240
|
-
*
|
|
241
|
-
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the
|
|
242
|
-
*
|
|
243
|
-
*
|
|
244
|
-
*
|
|
245
|
-
* @param {C} callback - The `callback` parameter is a function that is used to
|
|
246
|
-
*
|
|
247
|
-
* `_DEFAULT_CALLBACK
|
|
248
|
-
*
|
|
249
|
+
* The function overrides the delete method of a binary tree data structure, allowing for the
|
|
250
|
+
* deletion of a node and maintaining the balance of the tree.
|
|
251
|
+
* @param {ReturnType<C> | null | undefined} identifier - The `identifier` parameter is the value
|
|
252
|
+
* that identifies the node to be deleted from the binary tree. It can be of any type that is
|
|
253
|
+
* returned by the callback function `C`. It can also be `null` or `undefined` if there is no node to
|
|
254
|
+
* delete.
|
|
255
|
+
* @param {C} callback - The `callback` parameter is a function that is used to determine the
|
|
256
|
+
* equality of nodes in the binary tree. It is optional and has a default value of
|
|
257
|
+
* `this._DEFAULT_CALLBACK`. The type of the `callback` parameter is `C`, which is a generic type
|
|
258
|
+
* that extends the `BTNCallback
|
|
249
259
|
* @returns an array of BinaryTreeDeleteResult<NODE> objects.
|
|
250
260
|
*/
|
|
251
261
|
override delete<C extends BTNCallback<NODE>>(
|
|
@@ -309,6 +319,14 @@ export class RedBlackTree<
|
|
|
309
319
|
}
|
|
310
320
|
|
|
311
321
|
/**
|
|
322
|
+
* Time Complexity: O(1)
|
|
323
|
+
* Space Complexity: O(1)
|
|
324
|
+
*/
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Time Complexity: O(1)
|
|
328
|
+
* Space Complexity: O(1)
|
|
329
|
+
*
|
|
312
330
|
* The function sets the root of a tree-like structure and updates the parent property of the new
|
|
313
331
|
* root.
|
|
314
332
|
* @param {NODE | undefined} v - v is a parameter of type NODE or undefined.
|
|
@@ -332,8 +350,8 @@ export class RedBlackTree<
|
|
|
332
350
|
* The function replaces an old node with a new node while preserving the color of the old node.
|
|
333
351
|
* @param {NODE} oldNode - The `oldNode` parameter represents the node that needs to be replaced in
|
|
334
352
|
* the data structure.
|
|
335
|
-
* @param {NODE} newNode - The `newNode` parameter is
|
|
336
|
-
*
|
|
353
|
+
* @param {NODE} newNode - The `newNode` parameter is of type `NODE`, which represents a node in a
|
|
354
|
+
* data structure.
|
|
337
355
|
* @returns The method is returning the result of calling the `_replaceNode` method from the
|
|
338
356
|
* superclass, with the `oldNode` and `newNode` parameters.
|
|
339
357
|
*/
|
|
@@ -352,12 +370,13 @@ export class RedBlackTree<
|
|
|
352
370
|
* Time Complexity: O(log n)
|
|
353
371
|
* Space Complexity: O(1)
|
|
354
372
|
*
|
|
355
|
-
* The `_insert` function inserts
|
|
356
|
-
*
|
|
357
|
-
* @param {NODE} node - The `node` parameter represents the node that needs to be inserted into
|
|
358
|
-
* binary search tree.
|
|
359
|
-
*
|
|
360
|
-
*
|
|
373
|
+
* The `_insert` function inserts a node into a binary search tree and performs necessary fix-ups to
|
|
374
|
+
* maintain the red-black tree properties.
|
|
375
|
+
* @param {NODE} node - The `node` parameter represents the node that needs to be inserted into the
|
|
376
|
+
* binary search tree.
|
|
377
|
+
* @returns a string value indicating the result of the insertion operation. It can return either
|
|
378
|
+
* 'UPDATED' if the node with the same key already exists and was updated, or 'CREATED' if a new node
|
|
379
|
+
* was created and inserted into the tree.
|
|
361
380
|
*/
|
|
362
381
|
protected _insert(node: NODE): CRUD {
|
|
363
382
|
let current = this.root;
|
|
@@ -432,8 +451,8 @@ export class RedBlackTree<
|
|
|
432
451
|
* Space Complexity: O(1)
|
|
433
452
|
*
|
|
434
453
|
* The `_insertFixup` function is used to fix the Red-Black Tree after inserting a new node.
|
|
435
|
-
* @param {NODE | undefined} z - The parameter `z` represents a node in the Red-Black Tree
|
|
436
|
-
* either be a valid node
|
|
454
|
+
* @param {NODE | undefined} z - The parameter `z` represents a node in the Red-Black Tree data
|
|
455
|
+
* structure. It can either be a valid node or `undefined`.
|
|
437
456
|
*/
|
|
438
457
|
protected _insertFixup(z: NODE | undefined): void {
|
|
439
458
|
// Continue fixing the tree as long as the parent of z is red
|
|
@@ -504,9 +523,10 @@ export class RedBlackTree<
|
|
|
504
523
|
*
|
|
505
524
|
* The `_deleteFixup` function is used to fix the red-black tree after a node deletion by adjusting
|
|
506
525
|
* the colors and performing rotations.
|
|
507
|
-
* @param {NODE | undefined} node - The `node` parameter represents a node in a
|
|
508
|
-
*
|
|
509
|
-
* @returns The function does not return any value. It has a return type of `void
|
|
526
|
+
* @param {NODE | undefined} node - The `node` parameter represents a node in a binary tree. It can
|
|
527
|
+
* be either a valid node object or `undefined`.
|
|
528
|
+
* @returns The function does not return any value. It has a return type of `void`, which means it
|
|
529
|
+
* does not return anything.
|
|
510
530
|
*/
|
|
511
531
|
protected _deleteFixup(node: NODE | undefined): void {
|
|
512
532
|
// Early exit condition
|