data-structure-typed 1.51.1 → 1.51.3
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/CHANGELOG.md +1 -1
- package/README.md +208 -185
- package/benchmark/report.html +13 -13
- package/benchmark/report.json +158 -146
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +16 -10
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +31 -25
- package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +27 -1
- package/dist/cjs/data-structures/binary-tree/bst.js +29 -0
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +1 -47
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +6 -61
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +16 -10
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +31 -25
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +27 -1
- package/dist/mjs/data-structures/binary-tree/bst.js +28 -0
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +1 -47
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +6 -60
- package/dist/umd/data-structure-typed.js +66 -86
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +92 -69
- package/src/data-structures/binary-tree/binary-tree.ts +33 -28
- package/src/data-structures/binary-tree/bst.ts +36 -1
- package/src/data-structures/binary-tree/rb-tree.ts +6 -72
- package/test/performance/data-structures/binary-tree/avl-tree.test.ts +4 -0
- package/test/performance/data-structures/binary-tree/rb-tree.test.ts +4 -0
- package/test/unit/data-structures/binary-tree/overall.test.ts +2 -2
- package/test/unit/data-structures/binary-tree/rb-tree.test.ts +40 -40
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "data-structure-typed",
|
|
3
|
-
"version": "1.51.
|
|
3
|
+
"version": "1.51.3",
|
|
4
4
|
"description": "Javascript Data Structure. Heap, Binary Tree, Red Black Tree, Linked List, Deque, Trie, HashMap, Directed Graph, Undirected Graph, Binary Search Tree(BST), AVL Tree, Priority Queue, Graph, Queue, Tree Multiset, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue, Stack. Benchmark compared with C++ STL. API aligned with ES6 and Java.util. Usability is comparable to Python",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/mjs/index.js",
|
|
@@ -66,11 +66,11 @@
|
|
|
66
66
|
"@typescript-eslint/eslint-plugin": "^6.7.4",
|
|
67
67
|
"@typescript-eslint/parser": "^6.7.4",
|
|
68
68
|
"auto-changelog": "^2.4.0",
|
|
69
|
-
"avl-tree-typed": "^1.51.
|
|
69
|
+
"avl-tree-typed": "^1.51.2",
|
|
70
70
|
"benchmark": "^2.1.4",
|
|
71
|
-
"binary-tree-typed": "^1.51.
|
|
72
|
-
"bst-typed": "^1.51.
|
|
73
|
-
"data-structure-typed": "^1.51.
|
|
71
|
+
"binary-tree-typed": "^1.51.2",
|
|
72
|
+
"bst-typed": "^1.51.2",
|
|
73
|
+
"data-structure-typed": "^1.51.2",
|
|
74
74
|
"dependency-cruiser": "^14.1.0",
|
|
75
75
|
"doctoc": "^2.2.1",
|
|
76
76
|
"eslint": "^8.50.0",
|
|
@@ -79,7 +79,7 @@
|
|
|
79
79
|
"eslint-import-resolver-typescript": "^3.6.1",
|
|
80
80
|
"eslint-plugin-import": "^2.28.1",
|
|
81
81
|
"fast-glob": "^3.3.1",
|
|
82
|
-
"heap-typed": "^1.51.
|
|
82
|
+
"heap-typed": "^1.51.2",
|
|
83
83
|
"istanbul-badges-readme": "^1.8.5",
|
|
84
84
|
"jest": "^29.7.0",
|
|
85
85
|
"js-sdsl": "^4.4.2",
|
|
@@ -92,54 +92,17 @@
|
|
|
92
92
|
"typescript": "^5.3.2"
|
|
93
93
|
},
|
|
94
94
|
"keywords": [
|
|
95
|
-
"data structure",
|
|
96
|
-
"data structures",
|
|
97
|
-
"datastructure",
|
|
98
|
-
"datastructures",
|
|
99
95
|
"data",
|
|
100
96
|
"structure",
|
|
101
97
|
"structures",
|
|
102
|
-
"
|
|
103
|
-
"
|
|
104
|
-
"
|
|
105
|
-
"
|
|
106
|
-
"
|
|
107
|
-
"
|
|
108
|
-
"
|
|
109
|
-
"
|
|
110
|
-
"max-heap",
|
|
111
|
-
"priority queue",
|
|
112
|
-
"priority-queue",
|
|
113
|
-
"max priority queue",
|
|
114
|
-
"max-priority-queue",
|
|
115
|
-
"min priority queue",
|
|
116
|
-
"min-priority-queue",
|
|
117
|
-
"binary search tree",
|
|
118
|
-
"binary-search-tree",
|
|
119
|
-
"BST",
|
|
120
|
-
"binary tree",
|
|
121
|
-
"binary-tree",
|
|
122
|
-
"red black tree",
|
|
123
|
-
"red black tree",
|
|
124
|
-
"red-black-tree",
|
|
125
|
-
"rb tree",
|
|
126
|
-
"trie",
|
|
127
|
-
"prefix tree",
|
|
128
|
-
"prefix-tree",
|
|
129
|
-
"avl tree",
|
|
130
|
-
"avl-tree",
|
|
131
|
-
"tree set",
|
|
132
|
-
"tree-set",
|
|
133
|
-
"tree multiset",
|
|
134
|
-
"tree-multiset",
|
|
135
|
-
"tree map",
|
|
136
|
-
"tree-map",
|
|
137
|
-
"tree multimap",
|
|
138
|
-
"tree-multimap",
|
|
139
|
-
"binary indexed tree",
|
|
140
|
-
"binary-indexed-tree",
|
|
141
|
-
"segment tree",
|
|
142
|
-
"segment-tree",
|
|
98
|
+
"data structure",
|
|
99
|
+
"datastructure",
|
|
100
|
+
"data-structure",
|
|
101
|
+
"data structures",
|
|
102
|
+
"datastructures",
|
|
103
|
+
"data-structures",
|
|
104
|
+
"in data structures",
|
|
105
|
+
"in data structure",
|
|
143
106
|
"binary",
|
|
144
107
|
"depth",
|
|
145
108
|
"breadth",
|
|
@@ -149,7 +112,7 @@
|
|
|
149
112
|
"avl",
|
|
150
113
|
"red",
|
|
151
114
|
"black",
|
|
152
|
-
"
|
|
115
|
+
"redblack",
|
|
153
116
|
"RB",
|
|
154
117
|
"segment",
|
|
155
118
|
"prefix",
|
|
@@ -159,26 +122,18 @@
|
|
|
159
122
|
"set",
|
|
160
123
|
"multiset",
|
|
161
124
|
"multimap",
|
|
125
|
+
"directed",
|
|
126
|
+
"undirected",
|
|
127
|
+
"graph",
|
|
128
|
+
"min",
|
|
129
|
+
"max",
|
|
130
|
+
"heap",
|
|
131
|
+
"priority",
|
|
132
|
+
"queue",
|
|
162
133
|
"singly",
|
|
163
134
|
"doubly",
|
|
164
135
|
"linked",
|
|
165
136
|
"list",
|
|
166
|
-
"linked list",
|
|
167
|
-
"linked-list",
|
|
168
|
-
"singly linked list",
|
|
169
|
-
"singly-linked-list",
|
|
170
|
-
"doubly linked list",
|
|
171
|
-
"doubly-linked-list",
|
|
172
|
-
"javascript data structure",
|
|
173
|
-
"javascript data structures",
|
|
174
|
-
"typescript data structures",
|
|
175
|
-
"js data structure",
|
|
176
|
-
"js data structures",
|
|
177
|
-
"data-structure",
|
|
178
|
-
"data-structures",
|
|
179
|
-
"directed",
|
|
180
|
-
"undirected",
|
|
181
|
-
"graph",
|
|
182
137
|
"js",
|
|
183
138
|
"ts",
|
|
184
139
|
"javascript",
|
|
@@ -195,6 +150,7 @@
|
|
|
195
150
|
"Python",
|
|
196
151
|
"collections",
|
|
197
152
|
"Python Collections",
|
|
153
|
+
"pythoncollections",
|
|
198
154
|
"python-collections",
|
|
199
155
|
"C#",
|
|
200
156
|
"System.Collections.Generic",
|
|
@@ -202,6 +158,43 @@
|
|
|
202
158
|
"java.util",
|
|
203
159
|
"Java",
|
|
204
160
|
"util",
|
|
161
|
+
"binary search tree",
|
|
162
|
+
"binarysearchtree",
|
|
163
|
+
"binary-search-tree",
|
|
164
|
+
"BST",
|
|
165
|
+
"binary tree",
|
|
166
|
+
"binarytree",
|
|
167
|
+
"binary-tree",
|
|
168
|
+
"red black tree",
|
|
169
|
+
"redblacktree",
|
|
170
|
+
"redblack tree",
|
|
171
|
+
"red-black-tree",
|
|
172
|
+
"redblack-tree",
|
|
173
|
+
"trie",
|
|
174
|
+
"prefix tree",
|
|
175
|
+
"prefixtree",
|
|
176
|
+
"prefix-tree",
|
|
177
|
+
"avl tree",
|
|
178
|
+
"avltree",
|
|
179
|
+
"avl-tree",
|
|
180
|
+
"tree set",
|
|
181
|
+
"treeset",
|
|
182
|
+
"tree-set",
|
|
183
|
+
"tree multiset",
|
|
184
|
+
"treemultiset",
|
|
185
|
+
"tree-multiset",
|
|
186
|
+
"tree map",
|
|
187
|
+
"treemap",
|
|
188
|
+
"tree-map",
|
|
189
|
+
"tree multimap",
|
|
190
|
+
"treemultimap",
|
|
191
|
+
"tree-multimap",
|
|
192
|
+
"binary indexed tree",
|
|
193
|
+
"binaryindexedtree",
|
|
194
|
+
"binary-indexed-tree",
|
|
195
|
+
"segment tree",
|
|
196
|
+
"segmenttree",
|
|
197
|
+
"segment-tree",
|
|
205
198
|
"sort",
|
|
206
199
|
"sorted",
|
|
207
200
|
"order",
|
|
@@ -210,31 +203,61 @@
|
|
|
210
203
|
"morris",
|
|
211
204
|
"Morris",
|
|
212
205
|
"bellman ford",
|
|
206
|
+
"bellmanford",
|
|
213
207
|
"bellman-ford",
|
|
214
208
|
"dijkstra",
|
|
215
209
|
"Dijkstra",
|
|
216
210
|
"floyd warshall",
|
|
211
|
+
"floydwarshall",
|
|
217
212
|
"floyd-warshall",
|
|
218
213
|
"tarjan",
|
|
219
214
|
"tarjan's",
|
|
220
215
|
"dfs",
|
|
221
216
|
"depth first Search",
|
|
217
|
+
"depthfirstSearch",
|
|
222
218
|
"depth-first-Search",
|
|
223
219
|
"bfs",
|
|
224
220
|
"breadth first search",
|
|
221
|
+
"breadthfirstsearch",
|
|
225
222
|
"dfs iterative",
|
|
226
223
|
"recursive",
|
|
227
224
|
"iterative",
|
|
228
225
|
"directed graph",
|
|
226
|
+
"directedgraph",
|
|
229
227
|
"directed-graph",
|
|
230
228
|
"undirected graph",
|
|
229
|
+
"undirectedgraph",
|
|
231
230
|
"undirected-graph",
|
|
231
|
+
"min heap",
|
|
232
|
+
"minheap",
|
|
233
|
+
"min-heap",
|
|
234
|
+
"max heap",
|
|
235
|
+
"maxheap",
|
|
236
|
+
"max-heap",
|
|
237
|
+
"priority queue",
|
|
238
|
+
"priorityqueue",
|
|
239
|
+
"priority-queue",
|
|
240
|
+
"max priority queue",
|
|
241
|
+
"maxpriorityqueue",
|
|
242
|
+
"max-priority-queue",
|
|
243
|
+
"min priority queue",
|
|
244
|
+
"minpriorityqueue",
|
|
245
|
+
"min-priority-queue",
|
|
232
246
|
"hash",
|
|
233
247
|
"map",
|
|
234
|
-
"hashmap",
|
|
235
248
|
"hash map",
|
|
249
|
+
"hashmap",
|
|
236
250
|
"hash-map",
|
|
237
251
|
"deque",
|
|
252
|
+
"linked list",
|
|
253
|
+
"linkedlist",
|
|
254
|
+
"linked-list",
|
|
255
|
+
"singly linked list",
|
|
256
|
+
"singlylinkedlist",
|
|
257
|
+
"singly-linked-list",
|
|
258
|
+
"doubly linked list",
|
|
259
|
+
"doublylinkedlist",
|
|
260
|
+
"doubly-linked-list",
|
|
238
261
|
"stack",
|
|
239
262
|
"CommonJS",
|
|
240
263
|
"ES6",
|
|
@@ -191,6 +191,16 @@ export class BinaryTree<
|
|
|
191
191
|
return this._size;
|
|
192
192
|
}
|
|
193
193
|
|
|
194
|
+
protected _NIL: NODE = new BinaryTreeNode<K, V>(NaN as K) as unknown as NODE;
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* The function returns the value of the _NIL property.
|
|
198
|
+
* @returns The method is returning the value of the `_NIL` property.
|
|
199
|
+
*/
|
|
200
|
+
get NIL(): NODE {
|
|
201
|
+
return this._NIL;
|
|
202
|
+
}
|
|
203
|
+
|
|
194
204
|
/**
|
|
195
205
|
* Creates a new instance of BinaryTreeNode with the given key and value.
|
|
196
206
|
* @param {K} key - The key for the new node.
|
|
@@ -281,6 +291,15 @@ export class BinaryTree<
|
|
|
281
291
|
}
|
|
282
292
|
}
|
|
283
293
|
|
|
294
|
+
/**
|
|
295
|
+
* The function checks if a given node is a real node or null.
|
|
296
|
+
* @param {any} node - The parameter `node` is of type `any`, which means it can be any data type.
|
|
297
|
+
* @returns a boolean value.
|
|
298
|
+
*/
|
|
299
|
+
isNodeOrNull(node: KeyOrNodeOrEntry<K, V, NODE>): node is NODE | null {
|
|
300
|
+
return this.isRealNode(node) || node === null;
|
|
301
|
+
}
|
|
302
|
+
|
|
284
303
|
/**
|
|
285
304
|
* The function "isNode" checks if an keyOrNodeOrEntry is an instance of the BinaryTreeNode class.
|
|
286
305
|
* @param keyOrNodeOrEntry - The `keyOrNodeOrEntry` parameter is a variable of type `KeyOrNodeOrEntry<K, V,NODE>`.
|
|
@@ -290,16 +309,6 @@ export class BinaryTree<
|
|
|
290
309
|
return keyOrNodeOrEntry instanceof BinaryTreeNode;
|
|
291
310
|
}
|
|
292
311
|
|
|
293
|
-
/**
|
|
294
|
-
* The function checks if a given value is an entry in a binary tree node.
|
|
295
|
-
* @param keyOrNodeOrEntry - KeyOrNodeOrEntry<K, V,NODE> - A generic type representing a node in a binary tree. It has
|
|
296
|
-
* two type parameters V and NODE, representing the value and node type respectively.
|
|
297
|
-
* @returns a boolean value.
|
|
298
|
-
*/
|
|
299
|
-
isEntry(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is BTNEntry<K, V> {
|
|
300
|
-
return Array.isArray(keyOrNodeOrEntry) && keyOrNodeOrEntry.length === 2;
|
|
301
|
-
}
|
|
302
|
-
|
|
303
312
|
/**
|
|
304
313
|
* The function checks if a given node is a real node by verifying if it is an instance of
|
|
305
314
|
* BinaryTreeNode and its key is not NaN.
|
|
@@ -307,7 +316,8 @@ export class BinaryTree<
|
|
|
307
316
|
* @returns a boolean value.
|
|
308
317
|
*/
|
|
309
318
|
isRealNode(node: KeyOrNodeOrEntry<K, V, NODE>): node is NODE {
|
|
310
|
-
|
|
319
|
+
if (!this.isNode(node)) return false;
|
|
320
|
+
return node !== this.NIL;
|
|
311
321
|
}
|
|
312
322
|
|
|
313
323
|
/**
|
|
@@ -316,16 +326,17 @@ export class BinaryTree<
|
|
|
316
326
|
* @returns a boolean value.
|
|
317
327
|
*/
|
|
318
328
|
isNIL(node: KeyOrNodeOrEntry<K, V, NODE>) {
|
|
319
|
-
return node
|
|
329
|
+
return node === this.NIL;
|
|
320
330
|
}
|
|
321
331
|
|
|
322
332
|
/**
|
|
323
|
-
* The function checks if a given
|
|
324
|
-
* @param
|
|
333
|
+
* The function checks if a given value is an entry in a binary tree node.
|
|
334
|
+
* @param keyOrNodeOrEntry - KeyOrNodeOrEntry<K, V,NODE> - A generic type representing a node in a binary tree. It has
|
|
335
|
+
* two type parameters V and NODE, representing the value and node type respectively.
|
|
325
336
|
* @returns a boolean value.
|
|
326
337
|
*/
|
|
327
|
-
|
|
328
|
-
return
|
|
338
|
+
isEntry(keyOrNodeOrEntry: KeyOrNodeOrEntry<K, V, NODE>): keyOrNodeOrEntry is BTNEntry<K, V> {
|
|
339
|
+
return Array.isArray(keyOrNodeOrEntry) && keyOrNodeOrEntry.length === 2;
|
|
329
340
|
}
|
|
330
341
|
|
|
331
342
|
/**
|
|
@@ -612,9 +623,9 @@ export class BinaryTree<
|
|
|
612
623
|
ans.push(cur);
|
|
613
624
|
if (onlyOne) return;
|
|
614
625
|
}
|
|
615
|
-
if (!cur.left && !cur.right) return;
|
|
616
|
-
cur.left && dfs(cur.left);
|
|
617
|
-
cur.right && dfs(cur.right);
|
|
626
|
+
if (!this.isRealNode(cur.left) && !this.isRealNode(cur.right)) return;
|
|
627
|
+
this.isRealNode(cur.left) && dfs(cur.left);
|
|
628
|
+
this.isRealNode(cur.right) && dfs(cur.right);
|
|
618
629
|
};
|
|
619
630
|
|
|
620
631
|
dfs(beginRoot);
|
|
@@ -622,13 +633,13 @@ export class BinaryTree<
|
|
|
622
633
|
const stack = [beginRoot];
|
|
623
634
|
while (stack.length > 0) {
|
|
624
635
|
const cur = stack.pop();
|
|
625
|
-
if (cur) {
|
|
636
|
+
if (this.isRealNode(cur)) {
|
|
626
637
|
if (callback(cur) === identifier) {
|
|
627
638
|
ans.push(cur);
|
|
628
639
|
if (onlyOne) return ans;
|
|
629
640
|
}
|
|
630
|
-
cur.left && stack.push(cur.left);
|
|
631
|
-
cur.right && stack.push(cur.right);
|
|
641
|
+
this.isRealNode(cur.left) && stack.push(cur.left);
|
|
642
|
+
this.isRealNode(cur.right) && stack.push(cur.right);
|
|
632
643
|
}
|
|
633
644
|
}
|
|
634
645
|
}
|
|
@@ -689,9 +700,6 @@ export class BinaryTree<
|
|
|
689
700
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
690
701
|
iterationType: IterationType = this.iterationType
|
|
691
702
|
): NODE | null | undefined {
|
|
692
|
-
if ((!callback || callback === this._DEFAULT_CALLBACK) && (identifier as any) instanceof BinaryTreeNode)
|
|
693
|
-
callback = (node => node) as C;
|
|
694
|
-
|
|
695
703
|
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
|
|
696
704
|
}
|
|
697
705
|
|
|
@@ -793,9 +801,6 @@ export class BinaryTree<
|
|
|
793
801
|
beginRoot: KeyOrNodeOrEntry<K, V, NODE> = this.root,
|
|
794
802
|
iterationType: IterationType = this.iterationType
|
|
795
803
|
): V | undefined {
|
|
796
|
-
if ((!callback || callback === this._DEFAULT_CALLBACK) && (identifier as any) instanceof BinaryTreeNode)
|
|
797
|
-
callback = (node => node) as C;
|
|
798
|
-
|
|
799
804
|
return this.getNode(identifier, callback, beginRoot, iterationType)?.value ?? undefined;
|
|
800
805
|
}
|
|
801
806
|
|
|
@@ -13,7 +13,7 @@ import type {
|
|
|
13
13
|
BTNodePureExemplar,
|
|
14
14
|
KeyOrNodeOrEntry
|
|
15
15
|
} from '../../types';
|
|
16
|
-
import { BSTVariant, CP, DFSOrderPattern, IterationType } from '../../types';
|
|
16
|
+
import { BSTNKeyOrNode, BSTVariant, CP, DFSOrderPattern, IterationType } from '../../types';
|
|
17
17
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
18
18
|
import { IBinaryTree } from '../../interfaces';
|
|
19
19
|
import { Queue } from '../queue';
|
|
@@ -543,6 +543,41 @@ export class BST<
|
|
|
543
543
|
return ans;
|
|
544
544
|
}
|
|
545
545
|
|
|
546
|
+
/**
|
|
547
|
+
* Time Complexity: O(log n)
|
|
548
|
+
* Space Complexity: O(1)
|
|
549
|
+
*/
|
|
550
|
+
|
|
551
|
+
/**
|
|
552
|
+
* Time Complexity: O(log n)
|
|
553
|
+
* Space Complexity: O(1)
|
|
554
|
+
*
|
|
555
|
+
* The `getNode` function retrieves a node from a Red-Black Tree based on the provided identifier and
|
|
556
|
+
* callback function.
|
|
557
|
+
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value or key
|
|
558
|
+
* that you want to search for in the binary search tree. It can be of any type that is compatible
|
|
559
|
+
* with the type of nodes in the tree.
|
|
560
|
+
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
561
|
+
* the tree. It is used to determine whether a node matches the given identifier. The `callback`
|
|
562
|
+
* function should take a node as its parameter and return a value that can be compared to the
|
|
563
|
+
* `identifier` parameter.
|
|
564
|
+
* @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
|
|
565
|
+
* search tree. It can be either a key or a node. If it is a key, it will be converted to a node
|
|
566
|
+
* using the `ensureNode` method. If it is not provided, the `root`
|
|
567
|
+
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
568
|
+
* be performed when searching for nodes in the binary search tree. It is an optional parameter and
|
|
569
|
+
* its default value is taken from the `iterationType` property of the class.
|
|
570
|
+
* @returns The method is returning a value of type `NODE | null | undefined`.
|
|
571
|
+
*/
|
|
572
|
+
override getNode<C extends BTNCallback<NODE>>(
|
|
573
|
+
identifier: ReturnType<C> | undefined,
|
|
574
|
+
callback: C = this._DEFAULT_CALLBACK as C,
|
|
575
|
+
beginRoot: BSTNKeyOrNode<K, NODE> = this.root,
|
|
576
|
+
iterationType: IterationType = this.iterationType
|
|
577
|
+
): NODE | undefined {
|
|
578
|
+
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
|
|
579
|
+
}
|
|
580
|
+
|
|
546
581
|
/**
|
|
547
582
|
* Time complexity: O(n)
|
|
548
583
|
* Space complexity: O(n)
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
BinaryTreeDeleteResult,
|
|
3
|
-
BSTNKeyOrNode,
|
|
4
3
|
BTNCallback,
|
|
5
|
-
IterationType,
|
|
6
4
|
KeyOrNodeOrEntry,
|
|
7
5
|
RBTreeOptions,
|
|
8
6
|
RedBlackTreeNested,
|
|
@@ -73,23 +71,13 @@ export class RedBlackTree<
|
|
|
73
71
|
constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: RBTreeOptions<K>) {
|
|
74
72
|
super([], options);
|
|
75
73
|
|
|
76
|
-
this._root = this.
|
|
74
|
+
this._root = this.NIL;
|
|
77
75
|
|
|
78
76
|
if (keysOrNodesOrEntries) {
|
|
79
77
|
this.addMany(keysOrNodesOrEntries);
|
|
80
78
|
}
|
|
81
79
|
}
|
|
82
80
|
|
|
83
|
-
protected _SENTINEL: NODE = new RedBlackTreeNode<K, V>(NaN as K) as unknown as NODE;
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* The function returns the value of the _SENTINEL property.
|
|
87
|
-
* @returns The method is returning the value of the `_SENTINEL` property.
|
|
88
|
-
*/
|
|
89
|
-
get SENTINEL(): NODE {
|
|
90
|
-
return this._SENTINEL;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
81
|
protected override _root: NODE | undefined;
|
|
94
82
|
|
|
95
83
|
/**
|
|
@@ -184,60 +172,6 @@ export class RedBlackTree<
|
|
|
184
172
|
return keyOrNodeOrEntry instanceof RedBlackTreeNode;
|
|
185
173
|
}
|
|
186
174
|
|
|
187
|
-
/**
|
|
188
|
-
* Time Complexity: O(1)
|
|
189
|
-
* Space Complexity: O(1)
|
|
190
|
-
*/
|
|
191
|
-
|
|
192
|
-
/**
|
|
193
|
-
* Time Complexity: O(1)
|
|
194
|
-
* Space Complexity: O(1)
|
|
195
|
-
*
|
|
196
|
-
* The function checks if a given node is a real node in a Red-Black Tree.
|
|
197
|
-
* @param {NODE | undefined} node - The `node` parameter is of type `NODE | undefined`, which means
|
|
198
|
-
* it can either be of type `NODE` or `undefined`.
|
|
199
|
-
* @returns a boolean value.
|
|
200
|
-
*/
|
|
201
|
-
override isRealNode(node: NODE | undefined): node is NODE {
|
|
202
|
-
if (node === this.SENTINEL || node === undefined) return false;
|
|
203
|
-
return node instanceof RedBlackTreeNode;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* Time Complexity: O(log n)
|
|
208
|
-
* Space Complexity: O(1)
|
|
209
|
-
*/
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* Time Complexity: O(log n)
|
|
213
|
-
* Space Complexity: O(1)
|
|
214
|
-
*
|
|
215
|
-
* The `getNode` function retrieves a node from a Red-Black Tree based on the provided identifier and
|
|
216
|
-
* callback function.
|
|
217
|
-
* @param {ReturnType<C> | undefined} identifier - The `identifier` parameter is the value or key
|
|
218
|
-
* that you want to search for in the binary search tree. It can be of any type that is compatible
|
|
219
|
-
* with the type of nodes in the tree.
|
|
220
|
-
* @param {C} callback - The `callback` parameter is a function that will be called for each node in
|
|
221
|
-
* the tree. It is used to determine whether a node matches the given identifier. The `callback`
|
|
222
|
-
* function should take a node as its parameter and return a value that can be compared to the
|
|
223
|
-
* `identifier` parameter.
|
|
224
|
-
* @param beginRoot - The `beginRoot` parameter is the starting point for the search in the binary
|
|
225
|
-
* search tree. It can be either a key or a node. If it is a key, it will be converted to a node
|
|
226
|
-
* using the `ensureNode` method. If it is not provided, the `root`
|
|
227
|
-
* @param iterationType - The `iterationType` parameter is used to specify the type of iteration to
|
|
228
|
-
* be performed when searching for nodes in the binary search tree. It is an optional parameter and
|
|
229
|
-
* its default value is taken from the `iterationType` property of the class.
|
|
230
|
-
* @returns The method is returning a value of type `NODE | null | undefined`.
|
|
231
|
-
*/
|
|
232
|
-
override getNode<C extends BTNCallback<NODE>>(
|
|
233
|
-
identifier: ReturnType<C> | undefined,
|
|
234
|
-
callback: C = this._DEFAULT_CALLBACK as C,
|
|
235
|
-
beginRoot: BSTNKeyOrNode<K, NODE> = this.root,
|
|
236
|
-
iterationType: IterationType = this.iterationType
|
|
237
|
-
): NODE | null | undefined {
|
|
238
|
-
return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? undefined;
|
|
239
|
-
}
|
|
240
|
-
|
|
241
175
|
/**
|
|
242
176
|
* Time Complexity: O(1)
|
|
243
177
|
* Space Complexity: O(1)
|
|
@@ -252,7 +186,7 @@ export class RedBlackTree<
|
|
|
252
186
|
*/
|
|
253
187
|
override clear() {
|
|
254
188
|
super.clear();
|
|
255
|
-
this._root = this.
|
|
189
|
+
this._root = this.NIL;
|
|
256
190
|
}
|
|
257
191
|
|
|
258
192
|
/**
|
|
@@ -430,9 +364,9 @@ export class RedBlackTree<
|
|
|
430
364
|
while (this.isRealNode(current)) {
|
|
431
365
|
parent = current;
|
|
432
366
|
if (node.key < current.key) {
|
|
433
|
-
current = current.left ?? this.
|
|
367
|
+
current = current.left ?? this.NIL;
|
|
434
368
|
} else if (node.key > current.key) {
|
|
435
|
-
current = current.right ?? this.
|
|
369
|
+
current = current.right ?? this.NIL;
|
|
436
370
|
} else {
|
|
437
371
|
this._replaceNode(current, node);
|
|
438
372
|
return 'UPDATED';
|
|
@@ -449,8 +383,8 @@ export class RedBlackTree<
|
|
|
449
383
|
parent.right = node;
|
|
450
384
|
}
|
|
451
385
|
|
|
452
|
-
node.left = this.
|
|
453
|
-
node.right = this.
|
|
386
|
+
node.left = this.NIL;
|
|
387
|
+
node.right = this.NIL;
|
|
454
388
|
node.color = 'RED';
|
|
455
389
|
|
|
456
390
|
this._insertFixup(node);
|
|
@@ -12,6 +12,10 @@ suite
|
|
|
12
12
|
avlTree.clear();
|
|
13
13
|
for (let i = 0; i < randomArray.length; i++) avlTree.add(i);
|
|
14
14
|
})
|
|
15
|
+
.add(`${HUNDRED_THOUSAND.toLocaleString()} add randomly`, () => {
|
|
16
|
+
avlTree.clear();
|
|
17
|
+
for (let i = 0; i < randomArray.length; i++) avlTree.add(randomArray[i]);
|
|
18
|
+
})
|
|
15
19
|
.add(`${HUNDRED_THOUSAND.toLocaleString()} get`, () => {
|
|
16
20
|
for (let i = 0; i < randomArray.length; i++) avlTree.get(randomArray[i]);
|
|
17
21
|
})
|
|
@@ -15,6 +15,10 @@ suite
|
|
|
15
15
|
rbTree.clear();
|
|
16
16
|
for (let i = 0; i < randomArray.length; i++) rbTree.add(i);
|
|
17
17
|
})
|
|
18
|
+
.add(`${HUNDRED_THOUSAND.toLocaleString()} add randomly`, () => {
|
|
19
|
+
rbTree.clear();
|
|
20
|
+
for (let i = 0; i < randomArray.length; i++) rbTree.add(randomArray[i]);
|
|
21
|
+
})
|
|
18
22
|
.add(`${HUNDRED_THOUSAND.toLocaleString()} get`, () => {
|
|
19
23
|
for (let i = 0; i < randomArray.length; i++) rbTree.get(randomArray[i]);
|
|
20
24
|
})
|
|
@@ -20,8 +20,8 @@ describe('Overall BinaryTree Test', () => {
|
|
|
20
20
|
leftMost?.key === 1; // true
|
|
21
21
|
expect(leftMost?.key).toBe(1);
|
|
22
22
|
bst.delete(6);
|
|
23
|
-
bst.getNode(6); //
|
|
24
|
-
expect(bst.getNode(6)).
|
|
23
|
+
bst.getNode(6); // undefined
|
|
24
|
+
expect(bst.getNode(6)).toBe(undefined);
|
|
25
25
|
bst.isAVLBalanced(); // true or false
|
|
26
26
|
expect(bst.isAVLBalanced()).toBe(true);
|
|
27
27
|
const bfsIDs: number[] = [];
|