data-structure-typed 2.2.7 → 2.2.8
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 +14 -3
- package/README_CN.md +119 -275
- package/benchmark/report.html +1 -1
- package/benchmark/report.json +20 -324
- package/dist/cjs/index.cjs +106 -107
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +106 -107
- package/dist/cjs-legacy/index.cjs.map +1 -1
- package/dist/esm/index.mjs +106 -107
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +106 -107
- package/dist/esm-legacy/index.mjs.map +1 -1
- package/dist/leetcode/avl-tree-counter.mjs +2957 -0
- package/dist/leetcode/avl-tree-multi-map.mjs +2889 -0
- package/dist/leetcode/avl-tree.mjs +2720 -0
- package/dist/leetcode/binary-tree.mjs +1594 -0
- package/dist/leetcode/bst.mjs +2398 -0
- package/dist/leetcode/deque.mjs +683 -0
- package/dist/leetcode/directed-graph.mjs +1733 -0
- package/dist/leetcode/doubly-linked-list.mjs +709 -0
- package/dist/leetcode/hash-map.mjs +493 -0
- package/dist/leetcode/heap.mjs +542 -0
- package/dist/leetcode/max-heap.mjs +375 -0
- package/dist/leetcode/max-priority-queue.mjs +383 -0
- package/dist/leetcode/min-heap.mjs +363 -0
- package/dist/leetcode/min-priority-queue.mjs +371 -0
- package/dist/leetcode/priority-queue.mjs +363 -0
- package/dist/leetcode/queue.mjs +943 -0
- package/dist/leetcode/red-black-tree.mjs +2765 -0
- package/dist/leetcode/singly-linked-list.mjs +754 -0
- package/dist/leetcode/stack.mjs +217 -0
- package/dist/leetcode/tree-counter.mjs +3039 -0
- package/dist/leetcode/tree-multi-map.mjs +2913 -0
- package/dist/leetcode/trie.mjs +413 -0
- package/dist/leetcode/undirected-graph.mjs +1650 -0
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +10 -10
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +22 -23
- package/dist/types/data-structures/binary-tree/bst.d.ts +11 -11
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -2
- package/dist/umd/data-structure-typed.js +102 -103
- package/dist/umd/data-structure-typed.js.map +1 -1
- package/dist/umd/data-structure-typed.min.js +2 -2
- package/dist/umd/data-structure-typed.min.js.map +1 -1
- package/package.json +48 -171
- package/src/data-structures/binary-tree/avl-tree-counter.ts +6 -6
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +13 -13
- package/src/data-structures/binary-tree/avl-tree.ts +15 -15
- package/src/data-structures/binary-tree/binary-tree.ts +53 -55
- package/src/data-structures/binary-tree/bst.ts +21 -22
- package/src/data-structures/binary-tree/red-black-tree.ts +3 -3
- package/src/data-structures/binary-tree/tree-counter.ts +4 -4
- package/src/data-structures/binary-tree/tree-multi-map.ts +13 -13
- package/test/performance/data-structures/binary-tree/red-black-tree.test.ts +1 -2
- package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +30 -30
- package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +46 -46
- package/test/unit/data-structures/binary-tree/avl-tree.test.ts +43 -43
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +151 -151
- package/test/unit/data-structures/binary-tree/bst.test.ts +99 -99
- package/test/unit/data-structures/binary-tree/overall.test.ts +20 -20
- package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +141 -141
- package/test/unit/data-structures/binary-tree/tree-counter.test.ts +37 -37
- package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +145 -145
- package/tsup.config.js +50 -21
- package/tsup.umd.config.js +29 -0
- package/tsup.node.config.js +0 -83
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "data-structure-typed",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.8",
|
|
4
4
|
"description": "Standard data structure",
|
|
5
5
|
"browser": "dist/umd/data-structure-typed.min.js",
|
|
6
6
|
"umd:main": "dist/umd/data-structure-typed.min.js",
|
|
@@ -38,21 +38,21 @@
|
|
|
38
38
|
"npm": ">=6.14.0"
|
|
39
39
|
},
|
|
40
40
|
"scripts": {
|
|
41
|
-
"build": "npm run build:
|
|
42
|
-
"build:node": "tsup
|
|
43
|
-
"build:
|
|
41
|
+
"build": "npm run build:ecut && npm run build:docs-class",
|
|
42
|
+
"build:node": "tsup",
|
|
43
|
+
"build:umd": "tsup --config tsup.umd.config.js",
|
|
44
44
|
"build:types": "rm -rf dist/types && tsc -p tsconfig.types.json",
|
|
45
|
-
"build:
|
|
46
|
-
"build:
|
|
45
|
+
"build:ecut": "npm run build:node && npm run build:types && npm run build:umd",
|
|
46
|
+
"build:leetcode": "tsup --config tsup.leetcode.config.js",
|
|
47
47
|
"build:docs": "npm run gen:examples && typedoc --out docs ./src",
|
|
48
48
|
"build:docs-class": "npm run gen:examples && typedoc --out docs ./src/data-structures",
|
|
49
49
|
"gen:examples": "ts-node scripts/test-to-example.ts",
|
|
50
50
|
"test:in-band": "jest --runInBand",
|
|
51
51
|
"test": "npm run test:in-band",
|
|
52
52
|
"test:integration": "npm run update:subs && jest --config jest.integration.config.js && tsc test/integration/compile.test.ts && node test/integration/compile.mjs",
|
|
53
|
-
"test:perf": "npm run build:
|
|
53
|
+
"test:perf": "npm run build:ecut && ts-node test/performance/benchmark-runner.ts --isolate --gc --cooldown-ms=80",
|
|
54
54
|
"check": "npm run check:src && npm run check:test",
|
|
55
|
-
"check:src": "tsc
|
|
55
|
+
"check:src": "tsc --noEmit",
|
|
56
56
|
"check:test": "tsc -p tsconfig.test.json --noEmit",
|
|
57
57
|
"check:circular-refs": "dependency-cruiser src",
|
|
58
58
|
"lint:src": "eslint --fix 'src/**/*.{js,ts}'",
|
|
@@ -102,7 +102,7 @@
|
|
|
102
102
|
"benchmark": "^2.1.4",
|
|
103
103
|
"binary-tree-typed": "^1.54.3",
|
|
104
104
|
"bst-typed": "^1.54.3",
|
|
105
|
-
"data-structure-typed": "^2.2.
|
|
105
|
+
"data-structure-typed": "^2.2.7",
|
|
106
106
|
"dependency-cruiser": "^16.5.0",
|
|
107
107
|
"doctoc": "^2.2.1",
|
|
108
108
|
"eslint": "^9.13.0",
|
|
@@ -124,178 +124,55 @@
|
|
|
124
124
|
"typescript": "^5.6.3"
|
|
125
125
|
},
|
|
126
126
|
"keywords": [
|
|
127
|
-
"data",
|
|
128
|
-
"
|
|
129
|
-
"structures",
|
|
130
|
-
"data
|
|
131
|
-
"
|
|
132
|
-
"
|
|
133
|
-
"data structures",
|
|
134
|
-
"datastructures",
|
|
135
|
-
"data-structures",
|
|
136
|
-
"in data structures",
|
|
137
|
-
"in data structure",
|
|
138
|
-
"binary",
|
|
139
|
-
"depth",
|
|
140
|
-
"breadth",
|
|
141
|
-
"first",
|
|
142
|
-
"search",
|
|
143
|
-
"index",
|
|
144
|
-
"avl",
|
|
145
|
-
"red",
|
|
146
|
-
"black",
|
|
147
|
-
"redblack",
|
|
148
|
-
"RB",
|
|
149
|
-
"segment",
|
|
150
|
-
"prefix",
|
|
151
|
-
"tree",
|
|
152
|
-
"multi",
|
|
153
|
-
"map",
|
|
154
|
-
"set",
|
|
155
|
-
"multiset",
|
|
156
|
-
"multimap",
|
|
157
|
-
"directed",
|
|
158
|
-
"undirected",
|
|
159
|
-
"graph",
|
|
160
|
-
"min",
|
|
161
|
-
"max",
|
|
162
|
-
"heap",
|
|
163
|
-
"priority",
|
|
164
|
-
"queue",
|
|
165
|
-
"singly",
|
|
166
|
-
"doubly",
|
|
167
|
-
"linked",
|
|
168
|
-
"list",
|
|
169
|
-
"js",
|
|
170
|
-
"ts",
|
|
171
|
-
"javascript",
|
|
172
|
-
"typscript",
|
|
173
|
-
"Node.js",
|
|
174
|
-
"NodeJS",
|
|
175
|
-
"c++",
|
|
176
|
-
"std",
|
|
177
|
-
"C++ STL",
|
|
178
|
-
"c++stl",
|
|
179
|
-
"C++ std",
|
|
180
|
-
"stl",
|
|
181
|
-
"std::",
|
|
182
|
-
"Python",
|
|
183
|
-
"collections",
|
|
184
|
-
"Python Collections",
|
|
185
|
-
"pythoncollections",
|
|
186
|
-
"python-collections",
|
|
187
|
-
"C#",
|
|
188
|
-
"System.Collections.Generic",
|
|
189
|
-
"collection",
|
|
190
|
-
"java.util",
|
|
191
|
-
"Java",
|
|
192
|
-
"util",
|
|
127
|
+
"data structures typescript",
|
|
128
|
+
"data structures javascript",
|
|
129
|
+
"typescript data structures",
|
|
130
|
+
"javascript data structures",
|
|
131
|
+
"data-structure library",
|
|
132
|
+
"algorithm library",
|
|
193
133
|
"binary search tree",
|
|
194
|
-
"
|
|
195
|
-
"binary-search-tree",
|
|
196
|
-
"BST",
|
|
197
|
-
"binary tree",
|
|
198
|
-
"binarytree",
|
|
199
|
-
"binary-tree",
|
|
134
|
+
"AVL tree",
|
|
200
135
|
"red black tree",
|
|
201
|
-
"
|
|
202
|
-
"
|
|
203
|
-
"
|
|
204
|
-
"
|
|
205
|
-
"trie",
|
|
206
|
-
"prefix tree",
|
|
207
|
-
"prefixtree",
|
|
208
|
-
"prefix-tree",
|
|
209
|
-
"avl tree",
|
|
210
|
-
"avltree",
|
|
211
|
-
"avl-tree",
|
|
212
|
-
"tree set",
|
|
213
|
-
"treeset",
|
|
214
|
-
"tree-set",
|
|
215
|
-
"tree multiset",
|
|
216
|
-
"treemultiset",
|
|
217
|
-
"tree-multiset",
|
|
218
|
-
"tree map",
|
|
219
|
-
"treemap",
|
|
220
|
-
"tree-map",
|
|
221
|
-
"tree multimap",
|
|
222
|
-
"treemultimap",
|
|
223
|
-
"tree-multimap",
|
|
224
|
-
"binary indexed tree",
|
|
225
|
-
"binaryindexedtree",
|
|
226
|
-
"binary-indexed-tree",
|
|
227
|
-
"segment tree",
|
|
228
|
-
"segmenttree",
|
|
229
|
-
"segment-tree",
|
|
230
|
-
"sort",
|
|
231
|
-
"sorted",
|
|
232
|
-
"order",
|
|
233
|
-
"ordered",
|
|
234
|
-
"algorithm",
|
|
235
|
-
"morris",
|
|
236
|
-
"Morris",
|
|
237
|
-
"bellman ford",
|
|
238
|
-
"bellmanford",
|
|
239
|
-
"bellman-ford",
|
|
240
|
-
"dijkstra",
|
|
241
|
-
"Dijkstra",
|
|
242
|
-
"floyd warshall",
|
|
243
|
-
"floydwarshall",
|
|
244
|
-
"floyd-warshall",
|
|
245
|
-
"tarjan",
|
|
246
|
-
"tarjan's",
|
|
247
|
-
"dfs",
|
|
248
|
-
"depth first Search",
|
|
249
|
-
"depthfirstSearch",
|
|
250
|
-
"depth-first-Search",
|
|
251
|
-
"bfs",
|
|
252
|
-
"breadth first search",
|
|
253
|
-
"breadthfirstsearch",
|
|
254
|
-
"dfs iterative",
|
|
255
|
-
"recursive",
|
|
256
|
-
"iterative",
|
|
136
|
+
"balanced tree",
|
|
137
|
+
"binary tree",
|
|
138
|
+
"BST",
|
|
139
|
+
"graph algorithm",
|
|
257
140
|
"directed graph",
|
|
258
|
-
"directedgraph",
|
|
259
|
-
"directed-graph",
|
|
260
141
|
"undirected graph",
|
|
261
|
-
"
|
|
262
|
-
"
|
|
142
|
+
"heap",
|
|
143
|
+
"priority queue",
|
|
263
144
|
"min heap",
|
|
264
|
-
"minheap",
|
|
265
|
-
"min-heap",
|
|
266
145
|
"max heap",
|
|
267
|
-
"
|
|
268
|
-
"max-heap",
|
|
269
|
-
"priority queue",
|
|
270
|
-
"priorityqueue",
|
|
271
|
-
"priority-queue",
|
|
272
|
-
"max priority queue",
|
|
273
|
-
"maxpriorityqueue",
|
|
274
|
-
"max-priority-queue",
|
|
275
|
-
"min priority queue",
|
|
276
|
-
"minpriorityqueue",
|
|
277
|
-
"min-priority-queue",
|
|
278
|
-
"hash",
|
|
279
|
-
"map",
|
|
280
|
-
"hash map",
|
|
281
|
-
"hashmap",
|
|
282
|
-
"hash-map",
|
|
146
|
+
"queue",
|
|
283
147
|
"deque",
|
|
148
|
+
"stack",
|
|
284
149
|
"linked list",
|
|
285
|
-
"linkedlist",
|
|
286
|
-
"linked-list",
|
|
287
150
|
"singly linked list",
|
|
288
|
-
"singlylinkedlist",
|
|
289
|
-
"singly-linked-list",
|
|
290
151
|
"doubly linked list",
|
|
291
|
-
"
|
|
292
|
-
"
|
|
293
|
-
"
|
|
152
|
+
"trie",
|
|
153
|
+
"hash map",
|
|
154
|
+
"hash table",
|
|
155
|
+
"tree multimap",
|
|
156
|
+
"tree multiset",
|
|
157
|
+
"tree map",
|
|
158
|
+
"tree set",
|
|
159
|
+
"ordered set",
|
|
160
|
+
"ordered map",
|
|
161
|
+
"depth first search",
|
|
162
|
+
"breadth first search",
|
|
163
|
+
"DFS",
|
|
164
|
+
"BFS",
|
|
165
|
+
"dijkstra algorithm",
|
|
166
|
+
"bellman ford",
|
|
167
|
+
"floyd warshall",
|
|
168
|
+
"tarjan algorithm",
|
|
169
|
+
"topological sort",
|
|
170
|
+
"leetcode",
|
|
171
|
+
"coding interview",
|
|
172
|
+
"algorithm practice",
|
|
173
|
+
"collection",
|
|
174
|
+
"ES6 modules",
|
|
294
175
|
"CommonJS",
|
|
295
|
-
"
|
|
296
|
-
"UMD",
|
|
297
|
-
"ES Modules",
|
|
298
|
-
"ESModules",
|
|
299
|
-
"ESModule"
|
|
176
|
+
"type-safe"
|
|
300
177
|
]
|
|
301
178
|
}
|
|
@@ -200,7 +200,7 @@ export class AVLTreeCounter<K = any, V = any, R = any> extends AVLTree<K, V, R>
|
|
|
200
200
|
options?: AVLTreeCounterOptions<K, V, R>
|
|
201
201
|
) {
|
|
202
202
|
super([], options);
|
|
203
|
-
if (keysNodesEntriesOrRaws) this.
|
|
203
|
+
if (keysNodesEntriesOrRaws) this.setMany(keysNodesEntriesOrRaws);
|
|
204
204
|
}
|
|
205
205
|
|
|
206
206
|
protected _count = 0;
|
|
@@ -243,7 +243,7 @@ export class AVLTreeCounter<K = any, V = any, R = any> extends AVLTree<K, V, R>
|
|
|
243
243
|
* @param [count] - How much to increase the node's count (default 1).
|
|
244
244
|
* @returns True if inserted/updated; false if ignored.
|
|
245
245
|
*/
|
|
246
|
-
override
|
|
246
|
+
override set(
|
|
247
247
|
keyNodeOrEntry: K | AVLTreeCounterNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
|
|
248
248
|
value?: V,
|
|
249
249
|
count = 1
|
|
@@ -252,7 +252,7 @@ export class AVLTreeCounter<K = any, V = any, R = any> extends AVLTree<K, V, R>
|
|
|
252
252
|
if (newNode === undefined) return false;
|
|
253
253
|
|
|
254
254
|
const orgNodeCount = newNode?.count || 0;
|
|
255
|
-
const inserted = super.
|
|
255
|
+
const inserted = super.set(newNode, newValue);
|
|
256
256
|
if (inserted) {
|
|
257
257
|
this._count += orgNodeCount;
|
|
258
258
|
}
|
|
@@ -380,9 +380,9 @@ export class AVLTreeCounter<K = any, V = any, R = any> extends AVLTree<K, V, R>
|
|
|
380
380
|
const out = this._createInstance();
|
|
381
381
|
|
|
382
382
|
if (this._isMapMode) {
|
|
383
|
-
this.bfs(node => out.
|
|
383
|
+
this.bfs(node => out.set(node.key, undefined, node.count));
|
|
384
384
|
} else {
|
|
385
|
-
this.bfs(node => out.
|
|
385
|
+
this.bfs(node => out.set(node.key, node.value, node.count));
|
|
386
386
|
}
|
|
387
387
|
|
|
388
388
|
if (this._isMapMode) out._store = this._store;
|
|
@@ -410,7 +410,7 @@ export class AVLTreeCounter<K = any, V = any, R = any> extends AVLTree<K, V, R>
|
|
|
410
410
|
|
|
411
411
|
let index = 0;
|
|
412
412
|
for (const [key, value] of this) {
|
|
413
|
-
out.
|
|
413
|
+
out.set(callback.call(thisArg, value, key, index++, this));
|
|
414
414
|
}
|
|
415
415
|
return out;
|
|
416
416
|
}
|
|
@@ -199,7 +199,7 @@ export class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<K, V[],
|
|
|
199
199
|
) {
|
|
200
200
|
super([], { ...options, isMapMode: true });
|
|
201
201
|
if (keysNodesEntriesOrRaws) {
|
|
202
|
-
this.
|
|
202
|
+
this.setMany(keysNodesEntriesOrRaws);
|
|
203
203
|
}
|
|
204
204
|
}
|
|
205
205
|
|
|
@@ -220,29 +220,29 @@ export class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<K, V[],
|
|
|
220
220
|
return keyNodeOrEntry instanceof AVLTreeMultiMapNode;
|
|
221
221
|
}
|
|
222
222
|
|
|
223
|
-
override
|
|
223
|
+
override set(
|
|
224
224
|
keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined
|
|
225
225
|
): boolean;
|
|
226
226
|
|
|
227
|
-
override
|
|
227
|
+
override set(key: K, value: V): boolean;
|
|
228
228
|
|
|
229
229
|
/**
|
|
230
230
|
* Insert a value or a list of values into the multimap. If the key exists, values are appended.
|
|
231
231
|
* @remarks Time O(log N + M), Space O(1)
|
|
232
232
|
* @param keyNodeOrEntry - Key, node, or [key, values] entry.
|
|
233
|
-
* @param [value] - Single value to
|
|
233
|
+
* @param [value] - Single value to set when a bare key is provided.
|
|
234
234
|
* @returns True if inserted or appended; false if ignored.
|
|
235
235
|
*/
|
|
236
|
-
override
|
|
236
|
+
override set(
|
|
237
237
|
keyNodeOrEntry: K | AVLTreeMultiMapNode<K, V> | [K | null | undefined, V[] | undefined] | null | undefined,
|
|
238
238
|
value?: V
|
|
239
239
|
): boolean {
|
|
240
|
-
if (this.isRealNode(keyNodeOrEntry)) return super.
|
|
240
|
+
if (this.isRealNode(keyNodeOrEntry)) return super.set(keyNodeOrEntry);
|
|
241
241
|
|
|
242
242
|
const _commonAdd = (key?: BTNOptKeyOrNull<K>, values?: V[]) => {
|
|
243
243
|
if (key === undefined || key === null) return false;
|
|
244
244
|
|
|
245
|
-
const
|
|
245
|
+
const _setToValues = () => {
|
|
246
246
|
const existingValues = this.get(key);
|
|
247
247
|
if (existingValues !== undefined && values !== undefined) {
|
|
248
248
|
for (const value of values) existingValues.push(value);
|
|
@@ -251,12 +251,12 @@ export class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<K, V[],
|
|
|
251
251
|
return false;
|
|
252
252
|
};
|
|
253
253
|
|
|
254
|
-
const
|
|
254
|
+
const _setByNode = () => {
|
|
255
255
|
const existingNode = this.getNode(key);
|
|
256
256
|
if (this.isRealNode(existingNode)) {
|
|
257
257
|
const existingValues = this.get(existingNode);
|
|
258
258
|
if (existingValues === undefined) {
|
|
259
|
-
super.
|
|
259
|
+
super.set(key, values);
|
|
260
260
|
return true;
|
|
261
261
|
}
|
|
262
262
|
if (values !== undefined) {
|
|
@@ -266,14 +266,14 @@ export class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<K, V[],
|
|
|
266
266
|
return false;
|
|
267
267
|
}
|
|
268
268
|
} else {
|
|
269
|
-
return super.
|
|
269
|
+
return super.set(key, values);
|
|
270
270
|
}
|
|
271
271
|
};
|
|
272
272
|
|
|
273
273
|
if (this._isMapMode) {
|
|
274
|
-
return
|
|
274
|
+
return _setByNode() || _setToValues();
|
|
275
275
|
}
|
|
276
|
-
return
|
|
276
|
+
return _setToValues() || _setByNode();
|
|
277
277
|
};
|
|
278
278
|
|
|
279
279
|
if (this.isEntry(keyNodeOrEntry)) {
|
|
@@ -392,7 +392,7 @@ export class AVLTreeMultiMap<K = any, V = any, R = any> extends AVLTree<K, V[],
|
|
|
392
392
|
): AVLTree<MK, MV, MR> {
|
|
393
393
|
const out = this._createLike<MK, MV, MR>([], options);
|
|
394
394
|
let i = 0;
|
|
395
|
-
for (const [k, v] of this) out.
|
|
395
|
+
for (const [k, v] of this) out.set(callback.call(thisArg, v, k, i++, this));
|
|
396
396
|
return out;
|
|
397
397
|
}
|
|
398
398
|
|
|
@@ -182,7 +182,7 @@ export class AVLTreeNode<K = any, V = any> {
|
|
|
182
182
|
|
|
183
183
|
/**
|
|
184
184
|
* Represents a self-balancing AVL (Adelson-Velsky and Landis) Tree.
|
|
185
|
-
* This tree extends BST and performs rotations on
|
|
185
|
+
* This tree extends BST and performs rotations on set/delete to maintain balance.
|
|
186
186
|
*
|
|
187
187
|
* @template K - The type of the key.
|
|
188
188
|
* @template V - The type of the value.
|
|
@@ -215,7 +215,7 @@ export class AVLTreeNode<K = any, V = any> {
|
|
|
215
215
|
* console.log(tree.size); // 5;
|
|
216
216
|
*
|
|
217
217
|
* // Add a new element
|
|
218
|
-
* tree.
|
|
218
|
+
* tree.set(3);
|
|
219
219
|
* console.log(tree.size); // 6;
|
|
220
220
|
* console.log([...tree.keys()]); // [1, 2, 3, 5, 8, 9];
|
|
221
221
|
* @example
|
|
@@ -278,7 +278,7 @@ export class AVLTreeNode<K = any, V = any> {
|
|
|
278
278
|
* console.log(universityTree.isAVLBalanced()); // true;
|
|
279
279
|
*
|
|
280
280
|
* // Add more universities
|
|
281
|
-
* universityTree.
|
|
281
|
+
* universityTree.set(6, { name: 'Oxford', rank: 6, students: 2000 });
|
|
282
282
|
* console.log(universityTree.isAVLBalanced()); // true;
|
|
283
283
|
*
|
|
284
284
|
* // Delete and verify balance is maintained
|
|
@@ -358,9 +358,9 @@ export class AVLTreeNode<K = any, V = any> {
|
|
|
358
358
|
export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements IBinaryTree<K, V, R> {
|
|
359
359
|
/**
|
|
360
360
|
* Creates an instance of AVLTree.
|
|
361
|
-
* @remarks Time O(N log N) (from `
|
|
361
|
+
* @remarks Time O(N log N) (from `setMany` with balanced set). Space O(N).
|
|
362
362
|
*
|
|
363
|
-
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to
|
|
363
|
+
* @param [keysNodesEntriesOrRaws=[]] - An iterable of items to set.
|
|
364
364
|
* @param [options] - Configuration options for the AVL tree.
|
|
365
365
|
*/
|
|
366
366
|
constructor(
|
|
@@ -370,8 +370,8 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
370
370
|
options?: AVLTreeOptions<K, V, R>
|
|
371
371
|
) {
|
|
372
372
|
super([], options);
|
|
373
|
-
// Note: super.
|
|
374
|
-
if (keysNodesEntriesOrRaws) super.
|
|
373
|
+
// Note: super.setMany is called, which in BST defaults to balanced set.
|
|
374
|
+
if (keysNodesEntriesOrRaws) super.setMany(keysNodesEntriesOrRaws);
|
|
375
375
|
}
|
|
376
376
|
|
|
377
377
|
/**
|
|
@@ -400,19 +400,19 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
400
400
|
}
|
|
401
401
|
|
|
402
402
|
/**
|
|
403
|
-
*
|
|
404
|
-
* @remarks Time O(log N) (O(H) for BST
|
|
403
|
+
* Sets a new node to the AVL tree and balances the tree path.
|
|
404
|
+
* @remarks Time O(log N) (O(H) for BST set + O(H) for `_balancePath`). Space O(H) for path/recursion.
|
|
405
405
|
*
|
|
406
|
-
* @param keyNodeOrEntry - The key, node, or entry to
|
|
406
|
+
* @param keyNodeOrEntry - The key, node, or entry to set.
|
|
407
407
|
* @param [value] - The value, if providing just a key.
|
|
408
408
|
* @returns True if the addition was successful, false otherwise.
|
|
409
409
|
*/
|
|
410
|
-
override
|
|
410
|
+
override set(
|
|
411
411
|
keyNodeOrEntry: K | AVLTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
|
|
412
412
|
value?: V
|
|
413
413
|
): boolean {
|
|
414
414
|
if (keyNodeOrEntry === null) return false;
|
|
415
|
-
const inserted = super.
|
|
415
|
+
const inserted = super.set(keyNodeOrEntry, value);
|
|
416
416
|
// If insertion was successful, balance the path from the new node up to the root.
|
|
417
417
|
if (inserted) this._balancePath(keyNodeOrEntry);
|
|
418
418
|
return inserted;
|
|
@@ -477,7 +477,7 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
477
477
|
|
|
478
478
|
/**
|
|
479
479
|
* Creates a new AVLTree by mapping each [key, value] pair.
|
|
480
|
-
* @remarks Time O(N log N) (O(N) iteration + O(log M) `
|
|
480
|
+
* @remarks Time O(N log N) (O(N) iteration + O(log M) `set` for each item into the new tree). Space O(N) for the new tree.
|
|
481
481
|
*
|
|
482
482
|
* @template MK - New key type.
|
|
483
483
|
* @template MV - New value type.
|
|
@@ -497,8 +497,8 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
497
497
|
let index = 0;
|
|
498
498
|
// Iterates in-order
|
|
499
499
|
for (const [key, value] of this) {
|
|
500
|
-
// `
|
|
501
|
-
out.
|
|
500
|
+
// `set` on the new tree will be O(log N) and will self-balance.
|
|
501
|
+
out.set(callback.call(thisArg, value, key, index++, this));
|
|
502
502
|
}
|
|
503
503
|
return out;
|
|
504
504
|
}
|