data-structure-typed 1.50.2 → 1.50.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 +17 -15
- package/README_zh-CN.md +1 -1
- package/SPECIFICATION.md +56 -0
- package/SPONSOR-zh-CN.md +90 -0
- package/SPONSOR.md +54 -0
- package/benchmark/report.html +13 -13
- package/benchmark/report.json +149 -155
- package/dist/cjs/data-structures/base/iterable-base.d.ts +6 -0
- package/dist/cjs/data-structures/base/iterable-base.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +29 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.js +33 -1
- package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js +22 -0
- package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/cjs/data-structures/binary-tree/binary-tree.js +1 -1
- package/dist/cjs/data-structures/binary-tree/bst.d.ts +46 -13
- package/dist/cjs/data-structures/binary-tree/bst.js +46 -13
- package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +54 -2
- package/dist/cjs/data-structures/binary-tree/rb-tree.js +73 -15
- package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/segment-tree.d.ts +99 -6
- package/dist/cjs/data-structures/binary-tree/segment-tree.js +127 -10
- package/dist/cjs/data-structures/binary-tree/segment-tree.js.map +1 -1
- package/dist/cjs/data-structures/binary-tree/tree-multimap.d.ts +35 -2
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js +38 -0
- package/dist/cjs/data-structures/binary-tree/tree-multimap.js.map +1 -1
- package/dist/cjs/data-structures/graph/abstract-graph.d.ts +0 -78
- package/dist/cjs/data-structures/graph/abstract-graph.js +0 -189
- package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/directed-graph.d.ts +59 -0
- package/dist/cjs/data-structures/graph/directed-graph.js +105 -0
- package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
- package/dist/cjs/data-structures/graph/undirected-graph.d.ts +60 -7
- package/dist/cjs/data-structures/graph/undirected-graph.js +126 -18
- package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
- package/dist/cjs/data-structures/hash/hash-map.d.ts +143 -23
- package/dist/cjs/data-structures/hash/hash-map.js +196 -62
- package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
- package/dist/cjs/data-structures/heap/heap.d.ts +29 -19
- package/dist/cjs/data-structures/heap/heap.js +29 -20
- package/dist/cjs/data-structures/heap/heap.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +71 -25
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +83 -25
- package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +26 -3
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js +34 -3
- package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
- package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +2 -2
- package/dist/cjs/data-structures/linked-list/skip-linked-list.js +2 -2
- package/dist/cjs/data-structures/matrix/matrix.d.ts +1 -1
- package/dist/cjs/data-structures/matrix/matrix.js +1 -1
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +10 -0
- package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +11 -0
- package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +8 -0
- package/dist/cjs/data-structures/priority-queue/priority-queue.js +8 -0
- package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
- package/dist/cjs/data-structures/queue/deque.d.ts +95 -21
- package/dist/cjs/data-structures/queue/deque.js +100 -16
- package/dist/cjs/data-structures/queue/deque.js.map +1 -1
- package/dist/cjs/data-structures/queue/queue.d.ts +65 -45
- package/dist/cjs/data-structures/queue/queue.js +65 -45
- package/dist/cjs/data-structures/queue/queue.js.map +1 -1
- package/dist/cjs/data-structures/stack/stack.d.ts +36 -22
- package/dist/cjs/data-structures/stack/stack.js +36 -22
- package/dist/cjs/data-structures/stack/stack.js.map +1 -1
- package/dist/cjs/data-structures/tree/tree.d.ts +57 -3
- package/dist/cjs/data-structures/tree/tree.js +77 -11
- package/dist/cjs/data-structures/tree/tree.js.map +1 -1
- package/dist/cjs/data-structures/trie/trie.d.ts +100 -36
- package/dist/cjs/data-structures/trie/trie.js +115 -36
- package/dist/cjs/data-structures/trie/trie.js.map +1 -1
- package/dist/mjs/data-structures/base/iterable-base.d.ts +6 -0
- package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +29 -1
- package/dist/mjs/data-structures/binary-tree/avl-tree.js +34 -2
- package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.d.ts +22 -0
- package/dist/mjs/data-structures/binary-tree/binary-indexed-tree.js +22 -0
- package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +1 -1
- package/dist/mjs/data-structures/binary-tree/binary-tree.js +1 -1
- package/dist/mjs/data-structures/binary-tree/bst.d.ts +46 -13
- package/dist/mjs/data-structures/binary-tree/bst.js +46 -13
- package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +54 -2
- package/dist/mjs/data-structures/binary-tree/rb-tree.js +74 -16
- package/dist/mjs/data-structures/binary-tree/segment-tree.d.ts +99 -6
- package/dist/mjs/data-structures/binary-tree/segment-tree.js +127 -10
- package/dist/mjs/data-structures/binary-tree/tree-multimap.d.ts +35 -2
- package/dist/mjs/data-structures/binary-tree/tree-multimap.js +38 -1
- package/dist/mjs/data-structures/graph/abstract-graph.d.ts +0 -78
- package/dist/mjs/data-structures/graph/abstract-graph.js +0 -188
- package/dist/mjs/data-structures/graph/directed-graph.d.ts +59 -0
- package/dist/mjs/data-structures/graph/directed-graph.js +105 -0
- package/dist/mjs/data-structures/graph/undirected-graph.d.ts +60 -7
- package/dist/mjs/data-structures/graph/undirected-graph.js +127 -19
- package/dist/mjs/data-structures/hash/hash-map.d.ts +143 -23
- package/dist/mjs/data-structures/hash/hash-map.js +204 -70
- package/dist/mjs/data-structures/heap/heap.d.ts +29 -19
- package/dist/mjs/data-structures/heap/heap.js +29 -20
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +71 -25
- package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +86 -28
- package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +26 -3
- package/dist/mjs/data-structures/linked-list/singly-linked-list.js +36 -5
- package/dist/mjs/data-structures/linked-list/skip-linked-list.d.ts +2 -2
- package/dist/mjs/data-structures/linked-list/skip-linked-list.js +2 -2
- package/dist/mjs/data-structures/matrix/matrix.d.ts +1 -1
- package/dist/mjs/data-structures/matrix/matrix.js +1 -1
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.d.ts +10 -0
- package/dist/mjs/data-structures/priority-queue/max-priority-queue.js +10 -0
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.d.ts +11 -0
- package/dist/mjs/data-structures/priority-queue/min-priority-queue.js +11 -0
- package/dist/mjs/data-structures/priority-queue/priority-queue.d.ts +8 -0
- package/dist/mjs/data-structures/priority-queue/priority-queue.js +8 -0
- package/dist/mjs/data-structures/queue/deque.d.ts +95 -21
- package/dist/mjs/data-structures/queue/deque.js +105 -21
- package/dist/mjs/data-structures/queue/queue.d.ts +65 -45
- package/dist/mjs/data-structures/queue/queue.js +65 -45
- package/dist/mjs/data-structures/stack/stack.d.ts +36 -22
- package/dist/mjs/data-structures/stack/stack.js +36 -22
- package/dist/mjs/data-structures/tree/tree.d.ts +57 -3
- package/dist/mjs/data-structures/tree/tree.js +80 -14
- package/dist/mjs/data-structures/trie/trie.d.ts +100 -36
- package/dist/mjs/data-structures/trie/trie.js +118 -39
- package/dist/umd/data-structure-typed.js +1445 -595
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +59 -58
- package/src/data-structures/base/iterable-base.ts +12 -0
- package/src/data-structures/binary-tree/avl-tree.ts +37 -3
- package/src/data-structures/binary-tree/binary-indexed-tree.ts +22 -0
- package/src/data-structures/binary-tree/binary-tree.ts +1 -1
- package/src/data-structures/binary-tree/bst.ts +46 -13
- package/src/data-structures/binary-tree/rb-tree.ts +79 -18
- package/src/data-structures/binary-tree/segment-tree.ts +145 -11
- package/src/data-structures/binary-tree/tree-multimap.ts +42 -3
- package/src/data-structures/graph/abstract-graph.ts +0 -211
- package/src/data-structures/graph/directed-graph.ts +122 -0
- package/src/data-structures/graph/undirected-graph.ts +143 -19
- package/src/data-structures/hash/hash-map.ts +228 -76
- package/src/data-structures/heap/heap.ts +31 -20
- package/src/data-structures/linked-list/doubly-linked-list.ts +96 -29
- package/src/data-structures/linked-list/singly-linked-list.ts +42 -6
- package/src/data-structures/linked-list/skip-linked-list.ts +2 -2
- package/src/data-structures/matrix/matrix.ts +1 -1
- package/src/data-structures/priority-queue/max-priority-queue.ts +10 -0
- package/src/data-structures/priority-queue/min-priority-queue.ts +11 -0
- package/src/data-structures/priority-queue/priority-queue.ts +8 -0
- package/src/data-structures/queue/deque.ts +118 -22
- package/src/data-structures/queue/queue.ts +68 -45
- package/src/data-structures/stack/stack.ts +39 -23
- package/src/data-structures/tree/tree.ts +89 -15
- package/src/data-structures/trie/trie.ts +131 -40
- package/test/performance/data-structures/graph/directed-graph.test.ts +1 -1
- package/test/unit/data-structures/graph/abstract-graph.test.ts +3 -0
- package/test/unit/data-structures/graph/directed-graph.test.ts +189 -253
- package/test/unit/data-structures/graph/undirected-graph.test.ts +263 -18
- package/test/unit/data-structures/queue/queue.test.ts +2 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "data-structure-typed",
|
|
3
|
-
"version": "1.50.
|
|
3
|
+
"version": "1.50.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",
|
|
@@ -94,17 +94,52 @@
|
|
|
94
94
|
"keywords": [
|
|
95
95
|
"data structure",
|
|
96
96
|
"data structures",
|
|
97
|
-
"javascript data structure",
|
|
98
|
-
"javascript data structures",
|
|
99
|
-
"typescript data structure",
|
|
100
|
-
"typescript data structures",
|
|
101
|
-
"js data structure",
|
|
102
|
-
"js data structures",
|
|
103
|
-
"data-structure",
|
|
104
97
|
"datastructure",
|
|
98
|
+
"datastructures",
|
|
105
99
|
"data",
|
|
106
100
|
"structure",
|
|
107
101
|
"structures",
|
|
102
|
+
"min",
|
|
103
|
+
"max",
|
|
104
|
+
"heap",
|
|
105
|
+
"priority",
|
|
106
|
+
"queue",
|
|
107
|
+
"min heap",
|
|
108
|
+
"min-heap",
|
|
109
|
+
"max heap",
|
|
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",
|
|
108
143
|
"binary",
|
|
109
144
|
"depth",
|
|
110
145
|
"breadth",
|
|
@@ -124,18 +159,26 @@
|
|
|
124
159
|
"set",
|
|
125
160
|
"multiset",
|
|
126
161
|
"multimap",
|
|
127
|
-
"directed",
|
|
128
|
-
"undirected",
|
|
129
|
-
"graph",
|
|
130
|
-
"min",
|
|
131
|
-
"max",
|
|
132
|
-
"heap",
|
|
133
|
-
"priority",
|
|
134
|
-
"queue",
|
|
135
162
|
"singly",
|
|
136
163
|
"doubly",
|
|
137
164
|
"linked",
|
|
138
165
|
"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",
|
|
139
182
|
"js",
|
|
140
183
|
"ts",
|
|
141
184
|
"javascript",
|
|
@@ -159,32 +202,6 @@
|
|
|
159
202
|
"java.util",
|
|
160
203
|
"Java",
|
|
161
204
|
"util",
|
|
162
|
-
"binary search tree",
|
|
163
|
-
"binary-search-tree",
|
|
164
|
-
"BST",
|
|
165
|
-
"binary tree",
|
|
166
|
-
"binary-tree",
|
|
167
|
-
"red black tree",
|
|
168
|
-
"red black tree",
|
|
169
|
-
"red-black-tree",
|
|
170
|
-
"rb tree",
|
|
171
|
-
"trie",
|
|
172
|
-
"prefix tree",
|
|
173
|
-
"prefix-tree",
|
|
174
|
-
"avl tree",
|
|
175
|
-
"avl-tree",
|
|
176
|
-
"tree set",
|
|
177
|
-
"tree-set",
|
|
178
|
-
"tree multiset",
|
|
179
|
-
"tree-multiset",
|
|
180
|
-
"tree map",
|
|
181
|
-
"tree-map",
|
|
182
|
-
"tree multimap",
|
|
183
|
-
"tree-multimap",
|
|
184
|
-
"binary indexed tree",
|
|
185
|
-
"binary-indexed-tree",
|
|
186
|
-
"segment tree",
|
|
187
|
-
"segment-tree",
|
|
188
205
|
"sort",
|
|
189
206
|
"sorted",
|
|
190
207
|
"order",
|
|
@@ -212,28 +229,12 @@
|
|
|
212
229
|
"directed-graph",
|
|
213
230
|
"undirected graph",
|
|
214
231
|
"undirected-graph",
|
|
215
|
-
"min heap",
|
|
216
|
-
"min-heap",
|
|
217
|
-
"max heap",
|
|
218
|
-
"max-heap",
|
|
219
|
-
"priority queue",
|
|
220
|
-
"priority-queue",
|
|
221
|
-
"max priority queue",
|
|
222
|
-
"max-priority-queue",
|
|
223
|
-
"min priority queue",
|
|
224
|
-
"min-priority-queue",
|
|
225
232
|
"hash",
|
|
226
233
|
"map",
|
|
227
234
|
"hashmap",
|
|
228
235
|
"hash map",
|
|
229
236
|
"hash-map",
|
|
230
237
|
"deque",
|
|
231
|
-
"linked list",
|
|
232
|
-
"linked-list",
|
|
233
|
-
"singly linked list",
|
|
234
|
-
"singly-linked-list",
|
|
235
|
-
"doubly linked list",
|
|
236
|
-
"doubly-linked-list",
|
|
237
238
|
"stack",
|
|
238
239
|
"CommonJS",
|
|
239
240
|
"ES6",
|
|
@@ -289,8 +289,14 @@ export abstract class IterableEntryBase<K = any, V = any> {
|
|
|
289
289
|
|
|
290
290
|
abstract isEmpty(): boolean;
|
|
291
291
|
|
|
292
|
+
abstract clear(): void;
|
|
293
|
+
|
|
292
294
|
abstract clone(): any;
|
|
293
295
|
|
|
296
|
+
abstract map(...args: any[]): any;
|
|
297
|
+
|
|
298
|
+
abstract filter(...args: any[]): any;
|
|
299
|
+
|
|
294
300
|
protected abstract _getIterator(...args: any[]): IterableIterator<[K, V]>;
|
|
295
301
|
}
|
|
296
302
|
|
|
@@ -497,7 +503,13 @@ export abstract class IterableElementBase<E = any, C = any> {
|
|
|
497
503
|
|
|
498
504
|
abstract isEmpty(): boolean;
|
|
499
505
|
|
|
506
|
+
abstract clear(): void;
|
|
507
|
+
|
|
500
508
|
abstract clone(): C;
|
|
501
509
|
|
|
510
|
+
abstract map(...args: any[]): any;
|
|
511
|
+
|
|
512
|
+
abstract filter(...args: any[]): any;
|
|
513
|
+
|
|
502
514
|
protected abstract _getIterator(...args: any[]): IterableIterator<E>;
|
|
503
515
|
}
|
|
@@ -22,11 +22,36 @@ export class AVLTreeNode<
|
|
|
22
22
|
V = any,
|
|
23
23
|
NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNodeNested<K, V>
|
|
24
24
|
> extends BSTNode<K, V, NODE> {
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
/**
|
|
26
|
+
* The constructor function initializes a new instance of a class with a key and an optional value,
|
|
27
|
+
* and sets the height property to 0.
|
|
28
|
+
* @param {K} key - The "key" parameter is of type K, which represents the type of the key for the
|
|
29
|
+
* constructor. It is used to initialize the key property of the object being created.
|
|
30
|
+
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
|
|
31
|
+
* value associated with the key in the constructor.
|
|
32
|
+
*/
|
|
27
33
|
constructor(key: K, value?: V) {
|
|
28
34
|
super(key, value);
|
|
29
|
-
this.
|
|
35
|
+
this._height = 0;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
protected _height: number;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The function returns the value of the height property.
|
|
42
|
+
* @returns The height of the object.
|
|
43
|
+
*/
|
|
44
|
+
get height(): number {
|
|
45
|
+
return this._height;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The above function sets the value of the height property.
|
|
50
|
+
* @param {number} value - The value parameter is a number that represents the new height value to be
|
|
51
|
+
* set.
|
|
52
|
+
*/
|
|
53
|
+
set height(value: number) {
|
|
54
|
+
this._height = value;
|
|
30
55
|
}
|
|
31
56
|
}
|
|
32
57
|
|
|
@@ -488,6 +513,15 @@ export class AVLTree<
|
|
|
488
513
|
}
|
|
489
514
|
}
|
|
490
515
|
|
|
516
|
+
/**
|
|
517
|
+
* The function replaces an old node with a new node while preserving the height of the old node.
|
|
518
|
+
* @param {NODE} oldNode - The `oldNode` parameter is the node that you want to replace with the
|
|
519
|
+
* `newNode`.
|
|
520
|
+
* @param {NODE} newNode - The `newNode` parameter is the new node that will replace the `oldNode` in
|
|
521
|
+
* the data structure.
|
|
522
|
+
* @returns the result of calling the `_replaceNode` method on the superclass, passing in the
|
|
523
|
+
* `oldNode` and `newNode` as arguments.
|
|
524
|
+
*/
|
|
491
525
|
protected _replaceNode(oldNode: NODE, newNode: NODE): NODE {
|
|
492
526
|
newNode.height = oldNode.height;
|
|
493
527
|
|
|
@@ -27,26 +27,48 @@ export class BinaryIndexedTree {
|
|
|
27
27
|
|
|
28
28
|
protected _freqMap: Record<number, number>;
|
|
29
29
|
|
|
30
|
+
/**
|
|
31
|
+
* The function returns the frequency map of numbers.
|
|
32
|
+
* @returns The `_freqMap` property, which is a record with number keys and number values, is being
|
|
33
|
+
* returned.
|
|
34
|
+
*/
|
|
30
35
|
get freqMap(): Record<number, number> {
|
|
31
36
|
return this._freqMap;
|
|
32
37
|
}
|
|
33
38
|
|
|
34
39
|
protected _msb: number;
|
|
35
40
|
|
|
41
|
+
/**
|
|
42
|
+
* The function returns the value of the _msb property.
|
|
43
|
+
* @returns The `_msb` property of the object.
|
|
44
|
+
*/
|
|
36
45
|
get msb(): number {
|
|
37
46
|
return this._msb;
|
|
38
47
|
}
|
|
39
48
|
|
|
40
49
|
protected _negativeCount: number;
|
|
41
50
|
|
|
51
|
+
/**
|
|
52
|
+
* The function returns the value of the _negativeCount property.
|
|
53
|
+
* @returns The method is returning the value of the variable `_negativeCount`, which is of type
|
|
54
|
+
* `number`.
|
|
55
|
+
*/
|
|
42
56
|
get negativeCount(): number {
|
|
43
57
|
return this._negativeCount;
|
|
44
58
|
}
|
|
45
59
|
|
|
60
|
+
/**
|
|
61
|
+
* The above function returns the value of the protected variable `_freq`.
|
|
62
|
+
* @returns The frequency value stored in the protected variable `_freq`.
|
|
63
|
+
*/
|
|
46
64
|
get freq(): number {
|
|
47
65
|
return this._freq;
|
|
48
66
|
}
|
|
49
67
|
|
|
68
|
+
/**
|
|
69
|
+
* The above function returns the maximum value.
|
|
70
|
+
* @returns The maximum value stored in the variable `_max`.
|
|
71
|
+
*/
|
|
50
72
|
get max(): number {
|
|
51
73
|
return this._max;
|
|
52
74
|
}
|
|
@@ -44,7 +44,7 @@ export class BinaryTreeNode<
|
|
|
44
44
|
/**
|
|
45
45
|
* The constructor function initializes an object with a key and an optional value.
|
|
46
46
|
* @param {K} key - The "key" parameter is of type K, which represents the type of the key for the
|
|
47
|
-
* constructor. It is used to set the
|
|
47
|
+
* constructor. It is used to set the key property of the object being created.
|
|
48
48
|
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
|
|
49
49
|
* value associated with the key in the constructor.
|
|
50
50
|
*/
|
|
@@ -34,10 +34,19 @@ export class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNod
|
|
|
34
34
|
|
|
35
35
|
protected override _left?: NODE;
|
|
36
36
|
|
|
37
|
+
/**
|
|
38
|
+
* The function returns the value of the `_left` property.
|
|
39
|
+
* @returns The `_left` property of the current object is being returned.
|
|
40
|
+
*/
|
|
37
41
|
override get left(): NODE | undefined {
|
|
38
42
|
return this._left;
|
|
39
43
|
}
|
|
40
44
|
|
|
45
|
+
/**
|
|
46
|
+
* The function sets the left child of a node and updates the parent reference of the child.
|
|
47
|
+
* @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. It can either be an
|
|
48
|
+
* instance of the `NODE` class or `undefined`.
|
|
49
|
+
*/
|
|
41
50
|
override set left(v: NODE | undefined) {
|
|
42
51
|
if (v) {
|
|
43
52
|
v.parent = this as unknown as NODE;
|
|
@@ -47,10 +56,20 @@ export class BSTNode<K = any, V = any, NODE extends BSTNode<K, V, NODE> = BSTNod
|
|
|
47
56
|
|
|
48
57
|
protected override _right?: NODE;
|
|
49
58
|
|
|
59
|
+
/**
|
|
60
|
+
* The function returns the right node of a binary tree or undefined if there is no right node.
|
|
61
|
+
* @returns The method is returning the value of the `_right` property, which is of type `NODE` or
|
|
62
|
+
* `undefined`.
|
|
63
|
+
*/
|
|
50
64
|
override get right(): NODE | undefined {
|
|
51
65
|
return this._right;
|
|
52
66
|
}
|
|
53
67
|
|
|
68
|
+
/**
|
|
69
|
+
* The function sets the right child of a node and updates the parent reference of the child.
|
|
70
|
+
* @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. It can either be a
|
|
71
|
+
* `NODE` object or `undefined`.
|
|
72
|
+
*/
|
|
54
73
|
override set right(v: NODE | undefined) {
|
|
55
74
|
if (v) {
|
|
56
75
|
v.parent = this as unknown as NODE;
|
|
@@ -77,10 +96,10 @@ export class BST<
|
|
|
77
96
|
extends BinaryTree<K, V, NODE, TREE>
|
|
78
97
|
implements IBinaryTree<K, V, NODE, TREE> {
|
|
79
98
|
/**
|
|
80
|
-
* This is the constructor function for a
|
|
81
|
-
*
|
|
82
|
-
* @param
|
|
83
|
-
* binary search tree.
|
|
99
|
+
* This is the constructor function for a TypeScript class that initializes a binary search tree with
|
|
100
|
+
* optional keys or nodes or entries and options.
|
|
101
|
+
* @param keysOrNodesOrEntries - An iterable object that contains keys, nodes, or entries. It is used
|
|
102
|
+
* to initialize the binary search tree with the provided keys, nodes, or entries.
|
|
84
103
|
* @param [options] - The `options` parameter is an optional object that can contain additional
|
|
85
104
|
* configuration options for the binary search tree. It can have the following properties:
|
|
86
105
|
*/
|
|
@@ -99,23 +118,31 @@ export class BST<
|
|
|
99
118
|
|
|
100
119
|
protected override _root?: NODE;
|
|
101
120
|
|
|
121
|
+
/**
|
|
122
|
+
* The function returns the root node of a tree structure.
|
|
123
|
+
* @returns The `_root` property of the object, which is of type `NODE` or `undefined`.
|
|
124
|
+
*/
|
|
102
125
|
override get root(): NODE | undefined {
|
|
103
126
|
return this._root;
|
|
104
127
|
}
|
|
105
128
|
|
|
106
129
|
protected _variant = BSTVariant.STANDARD;
|
|
107
130
|
|
|
131
|
+
/**
|
|
132
|
+
* The function returns the value of the _variant property.
|
|
133
|
+
* @returns The value of the `_variant` property.
|
|
134
|
+
*/
|
|
108
135
|
get variant() {
|
|
109
136
|
return this._variant;
|
|
110
137
|
}
|
|
111
138
|
|
|
112
139
|
/**
|
|
113
|
-
* The function creates a new
|
|
114
|
-
* @param {K} key - The key parameter is the
|
|
115
|
-
*
|
|
116
|
-
* @param [value] - The parameter
|
|
117
|
-
*
|
|
118
|
-
* @returns a new instance of the BSTNode class
|
|
140
|
+
* The function creates a new BSTNode with the given key and value and returns it.
|
|
141
|
+
* @param {K} key - The key parameter is of type K, which represents the type of the key for the node
|
|
142
|
+
* being created.
|
|
143
|
+
* @param {V} [value] - The "value" parameter is an optional parameter of type V. It represents the
|
|
144
|
+
* value associated with the key in the node being created.
|
|
145
|
+
* @returns The method is returning a new instance of the BSTNode class, casted as the NODE type.
|
|
119
146
|
*/
|
|
120
147
|
override createNode(key: K, value?: V): NODE {
|
|
121
148
|
return new BSTNode<K, V, NODE>(key, value) as NODE;
|
|
@@ -124,9 +151,10 @@ export class BST<
|
|
|
124
151
|
/**
|
|
125
152
|
* The function creates a new binary search tree with the specified options.
|
|
126
153
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
127
|
-
* behavior of the `createTree` method. It
|
|
128
|
-
*
|
|
129
|
-
* @returns a new instance of the BST class with the
|
|
154
|
+
* behavior of the `createTree` method. It is of type `Partial<BSTOptions<K>>`, which means it is a
|
|
155
|
+
* partial object of type `BSTOptions<K>`.
|
|
156
|
+
* @returns a new instance of the BST class, with the provided options merged with the default
|
|
157
|
+
* options. The returned value is casted as TREE.
|
|
130
158
|
*/
|
|
131
159
|
override createTree(options?: Partial<BSTOptions<K>>): TREE {
|
|
132
160
|
return new BST<K, V, NODE, TREE>([], {
|
|
@@ -824,6 +852,11 @@ export class BST<
|
|
|
824
852
|
return balanced;
|
|
825
853
|
}
|
|
826
854
|
|
|
855
|
+
/**
|
|
856
|
+
* The function sets the root property of an object and updates the parent property of the new root.
|
|
857
|
+
* @param {NODE | undefined} v - The parameter `v` is of type `NODE | undefined`. This means that it
|
|
858
|
+
* can either be an object of type `NODE` or it can be `undefined`.
|
|
859
|
+
*/
|
|
827
860
|
protected _setRoot(v: NODE | undefined) {
|
|
828
861
|
if (v) {
|
|
829
862
|
v.parent = undefined;
|
|
@@ -24,11 +24,38 @@ export class RedBlackTreeNode<
|
|
|
24
24
|
V = any,
|
|
25
25
|
NODE extends RedBlackTreeNode<K, V, NODE> = RedBlackTreeNodeNested<K, V>
|
|
26
26
|
> extends BSTNode<K, V, NODE> {
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
/**
|
|
28
|
+
* The constructor function initializes a Red-Black Tree Node with a key, an optional value, and a
|
|
29
|
+
* color.
|
|
30
|
+
* @param {K} key - The key parameter is of type K and represents the key of the node in the
|
|
31
|
+
* Red-Black Tree.
|
|
32
|
+
* @param {V} [value] - The `value` parameter is an optional parameter that represents the value
|
|
33
|
+
* associated with the key in the Red-Black Tree Node. It is not required and can be omitted when
|
|
34
|
+
* creating a new instance of the Red-Black Tree Node.
|
|
35
|
+
* @param {RBTNColor} color - The `color` parameter is used to specify the color of the Red-Black
|
|
36
|
+
* Tree Node. It is an optional parameter with a default value of `RBTNColor.BLACK`.
|
|
37
|
+
*/
|
|
29
38
|
constructor(key: K, value?: V, color: RBTNColor = RBTNColor.BLACK) {
|
|
30
39
|
super(key, value);
|
|
31
|
-
this.
|
|
40
|
+
this._color = color;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
protected _color: RBTNColor;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The function returns the color value of a variable.
|
|
47
|
+
* @returns The color value stored in the protected variable `_color`.
|
|
48
|
+
*/
|
|
49
|
+
get color(): RBTNColor {
|
|
50
|
+
return this._color;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The function sets the color property to the specified value.
|
|
55
|
+
* @param {RBTNColor} value - The value parameter is of type RBTNColor.
|
|
56
|
+
*/
|
|
57
|
+
set color(value: RBTNColor) {
|
|
58
|
+
this._color = value;
|
|
32
59
|
}
|
|
33
60
|
}
|
|
34
61
|
|
|
@@ -47,8 +74,6 @@ export class RedBlackTree<
|
|
|
47
74
|
>
|
|
48
75
|
extends BST<K, V, NODE, TREE>
|
|
49
76
|
implements IBinaryTree<K, V, NODE, TREE> {
|
|
50
|
-
Sentinel: NODE = new RedBlackTreeNode<K, V>(NaN as K) as unknown as NODE;
|
|
51
|
-
|
|
52
77
|
/**
|
|
53
78
|
* This is the constructor function for a Red-Black Tree data structure in TypeScript, which
|
|
54
79
|
* initializes the tree with optional nodes and options.
|
|
@@ -63,18 +88,36 @@ export class RedBlackTree<
|
|
|
63
88
|
constructor(keysOrNodesOrEntries: Iterable<KeyOrNodeOrEntry<K, V, NODE>> = [], options?: RBTreeOptions<K>) {
|
|
64
89
|
super([], options);
|
|
65
90
|
|
|
66
|
-
this._root = this.
|
|
91
|
+
this._root = this._Sentinel;
|
|
67
92
|
if (keysOrNodesOrEntries) super.addMany(keysOrNodesOrEntries);
|
|
68
93
|
}
|
|
69
94
|
|
|
95
|
+
protected _Sentinel: NODE = new RedBlackTreeNode<K, V>(NaN as K) as unknown as NODE;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* The function returns the value of the `_Sentinel` property.
|
|
99
|
+
* @returns The method is returning the value of the `_Sentinel` property.
|
|
100
|
+
*/
|
|
101
|
+
get Sentinel(): NODE {
|
|
102
|
+
return this._Sentinel;
|
|
103
|
+
}
|
|
104
|
+
|
|
70
105
|
protected _root: NODE;
|
|
71
106
|
|
|
107
|
+
/**
|
|
108
|
+
* The function returns the root node.
|
|
109
|
+
* @returns The root node of the data structure.
|
|
110
|
+
*/
|
|
72
111
|
get root(): NODE {
|
|
73
112
|
return this._root;
|
|
74
113
|
}
|
|
75
114
|
|
|
76
115
|
protected _size: number = 0;
|
|
77
116
|
|
|
117
|
+
/**
|
|
118
|
+
* The function returns the size of an object.
|
|
119
|
+
* @returns The size of the object, which is a number.
|
|
120
|
+
*/
|
|
78
121
|
get size(): number {
|
|
79
122
|
return this._size;
|
|
80
123
|
}
|
|
@@ -149,8 +192,14 @@ export class RedBlackTree<
|
|
|
149
192
|
return keyOrNodeOrEntry instanceof RedBlackTreeNode;
|
|
150
193
|
}
|
|
151
194
|
|
|
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
|
+
*/
|
|
152
201
|
override isRealNode(node: NODE | undefined): node is NODE {
|
|
153
|
-
if (node === this.
|
|
202
|
+
if (node === this._Sentinel || node === undefined) return false;
|
|
154
203
|
return node instanceof RedBlackTreeNode;
|
|
155
204
|
}
|
|
156
205
|
|
|
@@ -176,13 +225,13 @@ export class RedBlackTree<
|
|
|
176
225
|
const newNode = this.keyValueOrEntryToNode(keyOrNodeOrEntry, value);
|
|
177
226
|
if (newNode === undefined) return false;
|
|
178
227
|
|
|
179
|
-
newNode.left = this.
|
|
180
|
-
newNode.right = this.
|
|
228
|
+
newNode.left = this._Sentinel;
|
|
229
|
+
newNode.right = this._Sentinel;
|
|
181
230
|
|
|
182
231
|
let y: NODE | undefined = undefined;
|
|
183
232
|
let x: NODE | undefined = this.root;
|
|
184
233
|
|
|
185
|
-
while (x !== this.
|
|
234
|
+
while (x !== this._Sentinel) {
|
|
186
235
|
y = x;
|
|
187
236
|
if (x) {
|
|
188
237
|
if (newNode.key < x.key) {
|
|
@@ -250,9 +299,9 @@ export class RedBlackTree<
|
|
|
250
299
|
const ans: BinaryTreeDeleteResult<NODE>[] = [];
|
|
251
300
|
if (identifier === null) return ans;
|
|
252
301
|
const helper = (node: NODE | undefined): void => {
|
|
253
|
-
let z: NODE = this.
|
|
302
|
+
let z: NODE = this._Sentinel;
|
|
254
303
|
let x: NODE | undefined, y: NODE;
|
|
255
|
-
while (node !== this.
|
|
304
|
+
while (node !== this._Sentinel) {
|
|
256
305
|
if (node && callback(node) === identifier) {
|
|
257
306
|
z = node;
|
|
258
307
|
}
|
|
@@ -264,17 +313,17 @@ export class RedBlackTree<
|
|
|
264
313
|
}
|
|
265
314
|
}
|
|
266
315
|
|
|
267
|
-
if (z === this.
|
|
316
|
+
if (z === this._Sentinel) {
|
|
268
317
|
this._size--;
|
|
269
318
|
return;
|
|
270
319
|
}
|
|
271
320
|
|
|
272
321
|
y = z;
|
|
273
322
|
let yOriginalColor: number = y.color;
|
|
274
|
-
if (z.left === this.
|
|
323
|
+
if (z.left === this._Sentinel) {
|
|
275
324
|
x = z.right;
|
|
276
325
|
this._rbTransplant(z, z.right!);
|
|
277
|
-
} else if (z.right === this.
|
|
326
|
+
} else if (z.right === this._Sentinel) {
|
|
278
327
|
x = z.left;
|
|
279
328
|
this._rbTransplant(z, z.left!);
|
|
280
329
|
} else {
|
|
@@ -346,8 +395,14 @@ export class RedBlackTree<
|
|
|
346
395
|
* Space Complexity: O(1)
|
|
347
396
|
*/
|
|
348
397
|
|
|
398
|
+
/**
|
|
399
|
+
* Time Complexity: O(1)
|
|
400
|
+
* Space Complexity: O(1)
|
|
401
|
+
*
|
|
402
|
+
* The "clear" function sets the root node to the sentinel node and resets the size to 0.
|
|
403
|
+
*/
|
|
349
404
|
override clear() {
|
|
350
|
-
this._root = this.
|
|
405
|
+
this._root = this._Sentinel;
|
|
351
406
|
this._size = 0;
|
|
352
407
|
}
|
|
353
408
|
|
|
@@ -379,6 +434,12 @@ export class RedBlackTree<
|
|
|
379
434
|
return y!;
|
|
380
435
|
}
|
|
381
436
|
|
|
437
|
+
/**
|
|
438
|
+
* The function sets the root node of a tree structure and updates the parent property of the new
|
|
439
|
+
* root node.
|
|
440
|
+
* @param {NODE} v - The parameter "v" is of type "NODE", which represents a node in a data
|
|
441
|
+
* structure.
|
|
442
|
+
*/
|
|
382
443
|
protected override _setRoot(v: NODE) {
|
|
383
444
|
if (v) {
|
|
384
445
|
v.parent = undefined;
|
|
@@ -402,7 +463,7 @@ export class RedBlackTree<
|
|
|
402
463
|
if (x.right) {
|
|
403
464
|
const y: NODE = x.right;
|
|
404
465
|
x.right = y.left;
|
|
405
|
-
if (y.left !== this.
|
|
466
|
+
if (y.left !== this._Sentinel) {
|
|
406
467
|
if (y.left) y.left.parent = x;
|
|
407
468
|
}
|
|
408
469
|
y.parent = x.parent;
|
|
@@ -435,7 +496,7 @@ export class RedBlackTree<
|
|
|
435
496
|
if (x.left) {
|
|
436
497
|
const y: NODE = x.left;
|
|
437
498
|
x.left = y.right;
|
|
438
|
-
if (y.right !== this.
|
|
499
|
+
if (y.right !== this._Sentinel) {
|
|
439
500
|
if (y.right) y.right.parent = x;
|
|
440
501
|
}
|
|
441
502
|
y.parent = x.parent;
|