data-structure-typed 1.37.1 → 1.37.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/binary-tree.d.ts +36 -200
- package/dist/data-structures/binary-tree/binary-tree.js +79 -231
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.d.ts +9 -8
- package/dist/data-structures/binary-tree/bst.js +37 -25
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +1 -35
- package/dist/data-structures/binary-tree/tree-multiset.js +1 -79
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/data-structures/heap/heap.d.ts +1 -1
- package/dist/data-structures/heap/heap.js +1 -1
- package/dist/types/data-structures/binary-tree.d.ts +2 -0
- package/dist/types/data-structures/index.d.ts +2 -0
- package/lib/data-structures/binary-tree/binary-tree.d.ts +36 -200
- package/lib/data-structures/binary-tree/binary-tree.js +79 -231
- package/lib/data-structures/binary-tree/bst.d.ts +9 -8
- package/lib/data-structures/binary-tree/bst.js +37 -25
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +1 -35
- package/lib/data-structures/binary-tree/tree-multiset.js +1 -79
- package/lib/data-structures/heap/heap.d.ts +1 -1
- package/lib/data-structures/heap/heap.js +1 -1
- package/lib/types/data-structures/binary-tree.d.ts +2 -0
- package/lib/types/data-structures/index.d.ts +2 -0
- package/package.json +5 -5
- package/test/integration/avl-tree.test.ts +19 -16
- package/test/integration/bst.test.ts +37 -33
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.map +1 -1
|
@@ -199,13 +199,13 @@ export class BST extends BinaryTree {
|
|
|
199
199
|
* The function returns the first node in a binary tree that matches the given property name and value.
|
|
200
200
|
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a
|
|
201
201
|
* generic type `N`. It represents the property of the binary tree node that you want to search for.
|
|
202
|
-
* @param
|
|
202
|
+
* @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
|
|
203
203
|
* specifies the property name to use for searching the binary tree nodes. If not provided, it defaults to `'key'`.
|
|
204
204
|
* @returns The method is returning either a BinaryTreeNodeKey or N (generic type) or null.
|
|
205
205
|
*/
|
|
206
|
-
get(nodeProperty,
|
|
206
|
+
get(nodeProperty, callback = this._defaultCallbackByKey) {
|
|
207
207
|
var _a;
|
|
208
|
-
return (_a = this.getNodes(nodeProperty,
|
|
208
|
+
return (_a = this.getNodes(nodeProperty, callback, true)[0]) !== null && _a !== void 0 ? _a : null;
|
|
209
209
|
}
|
|
210
210
|
/**
|
|
211
211
|
* The function returns the key of the rightmost node if the comparison between two values is less than, the key of the
|
|
@@ -227,24 +227,30 @@ export class BST extends BinaryTree {
|
|
|
227
227
|
* The function `getNodes` returns an array of nodes in a binary tree that match a given property value.
|
|
228
228
|
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or an
|
|
229
229
|
* `N` type. It represents the property of the binary tree node that you want to compare with.
|
|
230
|
-
* @param
|
|
230
|
+
* @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
|
|
231
231
|
* specifies the property name to use for comparison. If not provided, it defaults to `'key'`.
|
|
232
232
|
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
233
233
|
* return only one node that matches the given `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne`
|
|
234
234
|
* is set to `true`, the function will return an array with only one node (if
|
|
235
|
+
* @param beginRoot - The `beginRoot` parameter is an optional parameter that specifies the root node from which to
|
|
235
236
|
* @returns an array of nodes (type N).
|
|
236
237
|
*/
|
|
237
|
-
getNodes(nodeProperty,
|
|
238
|
-
if (!
|
|
238
|
+
getNodes(nodeProperty, callback = this._defaultCallbackByKey, onlyOne = false, beginRoot = this.root) {
|
|
239
|
+
if (!beginRoot)
|
|
239
240
|
return [];
|
|
240
|
-
const
|
|
241
|
+
const ans = [];
|
|
241
242
|
if (this.loopType === LoopType.RECURSIVE) {
|
|
242
243
|
const _traverse = (cur) => {
|
|
243
|
-
|
|
244
|
-
|
|
244
|
+
const callbackResult = callback(cur);
|
|
245
|
+
if (callbackResult === nodeProperty) {
|
|
246
|
+
ans.push(cur);
|
|
247
|
+
if (onlyOne)
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
245
250
|
if (!cur.left && !cur.right)
|
|
246
251
|
return;
|
|
247
|
-
|
|
252
|
+
// TODO potential bug
|
|
253
|
+
if (callback === this._defaultCallbackByKey) {
|
|
248
254
|
if (this._compare(cur.key, nodeProperty) === CP.gt)
|
|
249
255
|
cur.left && _traverse(cur.left);
|
|
250
256
|
if (this._compare(cur.key, nodeProperty) === CP.lt)
|
|
@@ -255,16 +261,21 @@ export class BST extends BinaryTree {
|
|
|
255
261
|
cur.right && _traverse(cur.right);
|
|
256
262
|
}
|
|
257
263
|
};
|
|
258
|
-
_traverse(
|
|
264
|
+
_traverse(beginRoot);
|
|
259
265
|
}
|
|
260
266
|
else {
|
|
261
|
-
const queue = new Queue([
|
|
267
|
+
const queue = new Queue([beginRoot]);
|
|
262
268
|
while (queue.size > 0) {
|
|
263
269
|
const cur = queue.shift();
|
|
264
270
|
if (cur) {
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
271
|
+
const callbackResult = callback(cur);
|
|
272
|
+
if (callbackResult === nodeProperty) {
|
|
273
|
+
ans.push(cur);
|
|
274
|
+
if (onlyOne)
|
|
275
|
+
return ans;
|
|
276
|
+
}
|
|
277
|
+
// TODO potential bug
|
|
278
|
+
if (callback === this._defaultCallbackByKey) {
|
|
268
279
|
if (this._compare(cur.key, nodeProperty) === CP.gt)
|
|
269
280
|
cur.left && queue.push(cur.left);
|
|
270
281
|
if (this._compare(cur.key, nodeProperty) === CP.lt)
|
|
@@ -277,22 +288,23 @@ export class BST extends BinaryTree {
|
|
|
277
288
|
}
|
|
278
289
|
}
|
|
279
290
|
}
|
|
280
|
-
return
|
|
291
|
+
return ans;
|
|
281
292
|
}
|
|
282
293
|
// --- start additional functions
|
|
283
294
|
/**
|
|
284
|
-
* The `
|
|
295
|
+
* The `lesserOrGreaterTraverse` function adds a delta value to the specified property of all nodes in a binary tree that
|
|
285
296
|
* have a greater value than a given node.
|
|
297
|
+
* @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
|
|
286
298
|
* @param {N | BinaryTreeNodeKey | null} node - The `node` parameter can be either of type `N` (a generic type), `BinaryTreeNodeKey`, or `null`. It
|
|
287
299
|
* represents the node in the binary tree to which the delta value will be added.
|
|
288
300
|
* @param lesserOrGreater - The `lesserOrGreater` parameter is an optional parameter that specifies whether the delta
|
|
289
|
-
* @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a boolean
|
|
290
301
|
*/
|
|
291
|
-
|
|
302
|
+
lesserOrGreaterTraverse(callback = this._defaultCallbackByKey, lesserOrGreater = CP.lt, node) {
|
|
292
303
|
if (typeof node === 'number')
|
|
293
|
-
node = this.get(node
|
|
304
|
+
node = this.get(node);
|
|
305
|
+
const ans = [];
|
|
294
306
|
if (!node)
|
|
295
|
-
return
|
|
307
|
+
return [];
|
|
296
308
|
const key = node.key;
|
|
297
309
|
if (!this.root)
|
|
298
310
|
return false;
|
|
@@ -300,7 +312,7 @@ export class BST extends BinaryTree {
|
|
|
300
312
|
const _traverse = (cur) => {
|
|
301
313
|
const compared = this._compare(cur.key, key);
|
|
302
314
|
if (compared === lesserOrGreater)
|
|
303
|
-
callback(cur);
|
|
315
|
+
ans.push(callback(cur));
|
|
304
316
|
if (!cur.left && !cur.right)
|
|
305
317
|
return;
|
|
306
318
|
if (cur.left && this._compare(cur.left.key, key) === lesserOrGreater)
|
|
@@ -318,14 +330,14 @@ export class BST extends BinaryTree {
|
|
|
318
330
|
if (cur) {
|
|
319
331
|
const compared = this._compare(cur.key, key);
|
|
320
332
|
if (compared === lesserOrGreater)
|
|
321
|
-
callback(cur);
|
|
333
|
+
ans.push(callback(cur));
|
|
322
334
|
if (cur.left && this._compare(cur.left.key, key) === lesserOrGreater)
|
|
323
335
|
queue.push(cur.left);
|
|
324
336
|
if (cur.right && this._compare(cur.right.key, key) === lesserOrGreater)
|
|
325
337
|
queue.push(cur.right);
|
|
326
338
|
}
|
|
327
339
|
}
|
|
328
|
-
return
|
|
340
|
+
return ans;
|
|
329
341
|
}
|
|
330
342
|
}
|
|
331
343
|
/**
|
|
@@ -343,7 +355,7 @@ export class BST extends BinaryTree {
|
|
|
343
355
|
* @returns The function `perfectlyBalance()` returns a boolean value.
|
|
344
356
|
*/
|
|
345
357
|
perfectlyBalance() {
|
|
346
|
-
const sorted = this.dfs(
|
|
358
|
+
const sorted = this.dfs(node => node, 'in'), n = sorted.length;
|
|
347
359
|
this.clear();
|
|
348
360
|
if (sorted.length < 1)
|
|
349
361
|
return false;
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { BinaryTreeNodeKey, TreeMultisetNodeNested, TreeMultisetOptions } from '../../types';
|
|
9
|
-
import { BinaryTreeDeletedResult
|
|
9
|
+
import { BinaryTreeDeletedResult } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
12
12
|
export declare class TreeMultisetNode<V = any, FAMILY extends TreeMultisetNode<V, FAMILY> = TreeMultisetNodeNested<V>> extends AVLTreeNode<V, FAMILY> {
|
|
@@ -102,40 +102,6 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
|
|
|
102
102
|
* @returns The function `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
103
103
|
*/
|
|
104
104
|
delete(nodeOrKey: N | BinaryTreeNodeKey, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[];
|
|
105
|
-
/**
|
|
106
|
-
* The function `getNodesByCount` returns an array of nodes that have a specific count property, either recursively or
|
|
107
|
-
* using a queue.
|
|
108
|
-
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a
|
|
109
|
-
* `N`. It represents the property of the nodes that you want to search for.
|
|
110
|
-
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
111
|
-
* return only one node that matches the `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne` is set
|
|
112
|
-
* to `true`, the function will return only one node. If `onlyOne`
|
|
113
|
-
* @returns an array of nodes that match the given nodeProperty.
|
|
114
|
-
*/
|
|
115
|
-
getNodesByCount(nodeProperty: BinaryTreeNodeKey | N, onlyOne?: boolean): N[];
|
|
116
|
-
/**
|
|
117
|
-
* The BFSCount function returns an array of counts from a breadth-first search of nodes.
|
|
118
|
-
* @returns The BFSCount() function returns an array of numbers, specifically the count property of each node in the
|
|
119
|
-
* bfs traversal.
|
|
120
|
-
*/
|
|
121
|
-
bfsCount(): number[];
|
|
122
|
-
/**
|
|
123
|
-
* The function "listLevelsCount" takes a node and returns an array of arrays, where each inner array contains the
|
|
124
|
-
* count property of each node at that level.
|
|
125
|
-
* @param {N | null} node - The parameter `node` is of type `N | null`. This means that it can either be an instance of
|
|
126
|
-
* the class `N` or `null`.
|
|
127
|
-
* @returns a 2D array of numbers. Each inner array represents a level in the binary tree, and each number in the inner
|
|
128
|
-
* array represents the count property of a node in that level.
|
|
129
|
-
*/
|
|
130
|
-
listLevelsCount(node: N | null): number[][];
|
|
131
|
-
/**
|
|
132
|
-
* The `morrisCount` function returns an array of counts for each node in a binary tree, based on a specified traversal
|
|
133
|
-
* pattern.
|
|
134
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The `pattern` parameter is an optional parameter that specifies the
|
|
135
|
-
* traversal pattern for the Morris traversal algorithm. It can have one of three values: 'in', 'pre', or 'post'.
|
|
136
|
-
* @returns The function `morrisCount` returns an array of numbers.
|
|
137
|
-
*/
|
|
138
|
-
morrisCount(pattern?: DFSOrderPattern): number[];
|
|
139
105
|
/**
|
|
140
106
|
* The clear() function clears the data and sets the count to 0.
|
|
141
107
|
*/
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { CP, FamilyPosition, LoopType } from '../../types';
|
|
2
2
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
3
|
-
import { Queue } from '../queue';
|
|
4
3
|
export class TreeMultisetNode extends AVLTreeNode {
|
|
5
4
|
/**
|
|
6
5
|
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
|
@@ -222,7 +221,7 @@ export class TreeMultiset extends AVLTree {
|
|
|
222
221
|
* @returns The function `perfectlyBalance()` returns a boolean value.
|
|
223
222
|
*/
|
|
224
223
|
perfectlyBalance() {
|
|
225
|
-
const sorted = this.dfs(
|
|
224
|
+
const sorted = this.dfs(node => node, 'in'), n = sorted.length;
|
|
226
225
|
if (sorted.length < 1)
|
|
227
226
|
return false;
|
|
228
227
|
this.clear();
|
|
@@ -322,83 +321,6 @@ export class TreeMultiset extends AVLTree {
|
|
|
322
321
|
}
|
|
323
322
|
return bstDeletedResult;
|
|
324
323
|
}
|
|
325
|
-
/**
|
|
326
|
-
* The function `getNodesByCount` returns an array of nodes that have a specific count property, either recursively or
|
|
327
|
-
* using a queue.
|
|
328
|
-
* @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeKey` or a
|
|
329
|
-
* `N`. It represents the property of the nodes that you want to search for.
|
|
330
|
-
* @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
|
|
331
|
-
* return only one node that matches the `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne` is set
|
|
332
|
-
* to `true`, the function will return only one node. If `onlyOne`
|
|
333
|
-
* @returns an array of nodes that match the given nodeProperty.
|
|
334
|
-
*/
|
|
335
|
-
getNodesByCount(nodeProperty, onlyOne = false) {
|
|
336
|
-
if (!this.root)
|
|
337
|
-
return [];
|
|
338
|
-
const result = [];
|
|
339
|
-
if (this.loopType === LoopType.RECURSIVE) {
|
|
340
|
-
const _traverse = (cur) => {
|
|
341
|
-
if (cur.count === nodeProperty) {
|
|
342
|
-
result.push(cur);
|
|
343
|
-
if (onlyOne)
|
|
344
|
-
return;
|
|
345
|
-
}
|
|
346
|
-
if (!cur.left && !cur.right)
|
|
347
|
-
return;
|
|
348
|
-
cur.left && _traverse(cur.left);
|
|
349
|
-
cur.right && _traverse(cur.right);
|
|
350
|
-
};
|
|
351
|
-
_traverse(this.root);
|
|
352
|
-
}
|
|
353
|
-
else {
|
|
354
|
-
const queue = new Queue([this.root]);
|
|
355
|
-
while (queue.size > 0) {
|
|
356
|
-
const cur = queue.shift();
|
|
357
|
-
if (cur) {
|
|
358
|
-
if (cur.count === nodeProperty) {
|
|
359
|
-
result.push(cur);
|
|
360
|
-
if (onlyOne)
|
|
361
|
-
return result;
|
|
362
|
-
}
|
|
363
|
-
cur.left && queue.push(cur.left);
|
|
364
|
-
cur.right && queue.push(cur.right);
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
return result;
|
|
369
|
-
}
|
|
370
|
-
/**
|
|
371
|
-
* The BFSCount function returns an array of counts from a breadth-first search of nodes.
|
|
372
|
-
* @returns The BFSCount() function returns an array of numbers, specifically the count property of each node in the
|
|
373
|
-
* bfs traversal.
|
|
374
|
-
*/
|
|
375
|
-
bfsCount() {
|
|
376
|
-
const nodes = super.bfs('node');
|
|
377
|
-
return nodes.map(node => node.count);
|
|
378
|
-
}
|
|
379
|
-
/**
|
|
380
|
-
* The function "listLevelsCount" takes a node and returns an array of arrays, where each inner array contains the
|
|
381
|
-
* count property of each node at that level.
|
|
382
|
-
* @param {N | null} node - The parameter `node` is of type `N | null`. This means that it can either be an instance of
|
|
383
|
-
* the class `N` or `null`.
|
|
384
|
-
* @returns a 2D array of numbers. Each inner array represents a level in the binary tree, and each number in the inner
|
|
385
|
-
* array represents the count property of a node in that level.
|
|
386
|
-
*/
|
|
387
|
-
listLevelsCount(node) {
|
|
388
|
-
const levels = super.listLevels(node, 'node');
|
|
389
|
-
return levels.map(level => level.map(node => node.count));
|
|
390
|
-
}
|
|
391
|
-
/**
|
|
392
|
-
* The `morrisCount` function returns an array of counts for each node in a binary tree, based on a specified traversal
|
|
393
|
-
* pattern.
|
|
394
|
-
* @param {'in' | 'pre' | 'post'} [pattern] - The `pattern` parameter is an optional parameter that specifies the
|
|
395
|
-
* traversal pattern for the Morris traversal algorithm. It can have one of three values: 'in', 'pre', or 'post'.
|
|
396
|
-
* @returns The function `morrisCount` returns an array of numbers.
|
|
397
|
-
*/
|
|
398
|
-
morrisCount(pattern = 'in') {
|
|
399
|
-
const nodes = super.morris(pattern, 'node');
|
|
400
|
-
return nodes.map(node => node.count);
|
|
401
|
-
}
|
|
402
324
|
/**
|
|
403
325
|
* The clear() function clears the data and sets the count to 0.
|
|
404
326
|
*/
|
|
@@ -80,7 +80,7 @@ export declare class Heap<E> {
|
|
|
80
80
|
has(element: E): boolean;
|
|
81
81
|
/**
|
|
82
82
|
* Depth-first search (DFS) method, different traversal orders can be selected。
|
|
83
|
-
* @param order -
|
|
83
|
+
* @param order - Traverse order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
|
|
84
84
|
* @returns An array containing elements traversed in the specified order.
|
|
85
85
|
*/
|
|
86
86
|
dfs(order: DFSOrderPattern): E[];
|
|
@@ -151,7 +151,7 @@ export class Heap {
|
|
|
151
151
|
}
|
|
152
152
|
/**
|
|
153
153
|
* Depth-first search (DFS) method, different traversal orders can be selected。
|
|
154
|
-
* @param order -
|
|
154
|
+
* @param order - Traverse order parameter: 'in' (in-order), 'pre' (pre-order) or 'post' (post-order).
|
|
155
155
|
* @returns An array containing elements traversed in the specified order.
|
|
156
156
|
*/
|
|
157
157
|
dfs(order) {
|
|
@@ -21,6 +21,8 @@ export declare enum FamilyPosition {
|
|
|
21
21
|
export type BinaryTreeNodePropertyName = 'key' | 'val';
|
|
22
22
|
export type NodeOrPropertyName = 'node' | BinaryTreeNodePropertyName;
|
|
23
23
|
export type BinaryTreeNodeKey = number;
|
|
24
|
+
export type BFSCallback<N> = (node: N, level?: number) => any;
|
|
25
|
+
export type BFSCallbackReturn<N> = ReturnType<BFSCallback<N>>;
|
|
24
26
|
export type BinaryTreeNodeProperty<N extends BinaryTreeNode<N['val'], N>> = N['val'] | N | number | BinaryTreeNodeKey;
|
|
25
27
|
export type BinaryTreeDeletedResult<N> = {
|
|
26
28
|
deleted: N | null | undefined;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "data-structure-typed",
|
|
3
|
-
"version": "1.37.
|
|
3
|
+
"version": "1.37.2",
|
|
4
4
|
"description": "Data Structures of Javascript & TypeScript. Binary Tree, BST, Graph, Heap, Priority Queue, Linked List, Queue, Deque, Stack, AVL Tree, Tree Multiset, Trie, Directed Graph, Undirected Graph, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "lib/index.js",
|
|
@@ -58,17 +58,17 @@
|
|
|
58
58
|
"@typescript-eslint/eslint-plugin": "^6.7.4",
|
|
59
59
|
"@typescript-eslint/parser": "^6.7.4",
|
|
60
60
|
"auto-changelog": "^2.4.0",
|
|
61
|
-
"avl-tree-typed": "^1.
|
|
61
|
+
"avl-tree-typed": "^1.37.0",
|
|
62
62
|
"benchmark": "^2.1.4",
|
|
63
|
-
"binary-tree-typed": "^1.
|
|
64
|
-
"bst-typed": "^1.
|
|
63
|
+
"binary-tree-typed": "^1.37.0",
|
|
64
|
+
"bst-typed": "^1.37.0",
|
|
65
65
|
"dependency-cruiser": "^14.1.0",
|
|
66
66
|
"eslint": "^8.50.0",
|
|
67
67
|
"eslint-config-prettier": "^9.0.0",
|
|
68
68
|
"eslint-import-resolver-alias": "^1.1.2",
|
|
69
69
|
"eslint-import-resolver-typescript": "^3.6.1",
|
|
70
70
|
"eslint-plugin-import": "^2.28.1",
|
|
71
|
-
"heap-typed": "^1.
|
|
71
|
+
"heap-typed": "^1.37.0",
|
|
72
72
|
"istanbul-badges-readme": "^1.8.5",
|
|
73
73
|
"jest": "^29.7.0",
|
|
74
74
|
"prettier": "^3.0.3",
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {AVLTree} from 'avl-tree-typed';
|
|
2
|
+
import {CP} from "data-structure-typed";
|
|
2
3
|
|
|
3
4
|
describe('AVL Tree Test', () => {
|
|
4
5
|
it('should perform various operations on a AVL Tree', () => {
|
|
@@ -22,10 +23,12 @@ describe('AVL Tree Test', () => {
|
|
|
22
23
|
const getMinNodeBySpecificNode = node15 && tree.getLeftMost(node15);
|
|
23
24
|
expect(getMinNodeBySpecificNode?.key).toBe(12);
|
|
24
25
|
|
|
25
|
-
|
|
26
|
+
let subTreeSum = 0;
|
|
27
|
+
node15 && tree.subTreeForeach(15, node => subTreeSum += node.key);
|
|
26
28
|
expect(subTreeSum).toBe(70);
|
|
27
29
|
|
|
28
|
-
|
|
30
|
+
let lesserSum = 0;
|
|
31
|
+
tree.lesserOrGreaterForeach(10, CP.lt, node => lesserSum += node.key);
|
|
29
32
|
expect(lesserSum).toBe(45);
|
|
30
33
|
|
|
31
34
|
// node15 has type problem. After the uniform design, the generics of containers (DirectedGraph, BST) are based on the type of value. However, this design has a drawback: when I attempt to inherit from the Vertex or BSTNode classes, the types of the results obtained by all methods are those of the parent class.
|
|
@@ -41,56 +44,56 @@ describe('AVL Tree Test', () => {
|
|
|
41
44
|
expect(bfs[0].key).toBe(8);
|
|
42
45
|
expect(bfs[bfs.length - 1].key).toBe(16);
|
|
43
46
|
|
|
44
|
-
expect(tree.
|
|
47
|
+
expect(tree.delete(11)[0].deleted?.key).toBe(11);
|
|
45
48
|
expect(tree.isAVLBalanced()).toBe(true);
|
|
46
49
|
expect(node15 && tree.getHeight(node15)).toBe(2);
|
|
47
50
|
|
|
48
|
-
expect(tree.
|
|
51
|
+
expect(tree.delete(1)[0].deleted?.key).toBe(1);
|
|
49
52
|
expect(tree.isAVLBalanced()).toBe(true);
|
|
50
53
|
expect(tree.getHeight()).toBe(4);
|
|
51
54
|
|
|
52
|
-
expect(tree.
|
|
55
|
+
expect(tree.delete(4)[0].deleted?.key).toBe(4);
|
|
53
56
|
expect(tree.isAVLBalanced()).toBe(true);
|
|
54
57
|
expect(tree.getHeight()).toBe(4);
|
|
55
58
|
|
|
56
|
-
expect(tree.
|
|
59
|
+
expect(tree.delete(10)[0].deleted?.key).toBe(10);
|
|
57
60
|
expect(tree.isAVLBalanced()).toBe(true);
|
|
58
61
|
expect(tree.getHeight()).toBe(3);
|
|
59
62
|
|
|
60
|
-
expect(tree.
|
|
63
|
+
expect(tree.delete(15)[0].deleted?.key).toBe(15);
|
|
61
64
|
expect(tree.isAVLBalanced()).toBe(true);
|
|
62
65
|
|
|
63
66
|
expect(tree.getHeight()).toBe(3);
|
|
64
67
|
|
|
65
|
-
expect(tree.
|
|
68
|
+
expect(tree.delete(5)[0].deleted?.key).toBe(5);
|
|
66
69
|
expect(tree.isAVLBalanced()).toBe(true);
|
|
67
70
|
expect(tree.getHeight()).toBe(3);
|
|
68
71
|
|
|
69
|
-
expect(tree.
|
|
72
|
+
expect(tree.delete(13)[0].deleted?.key).toBe(13);
|
|
70
73
|
expect(tree.isAVLBalanced()).toBe(true);
|
|
71
74
|
expect(tree.getHeight()).toBe(3);
|
|
72
75
|
|
|
73
|
-
expect(tree.
|
|
76
|
+
expect(tree.delete(3)[0].deleted?.key).toBe(3);
|
|
74
77
|
expect(tree.isAVLBalanced()).toBe(true);
|
|
75
78
|
expect(tree.getHeight()).toBe(3);
|
|
76
79
|
|
|
77
|
-
expect(tree.
|
|
80
|
+
expect(tree.delete(8)[0].deleted?.key).toBe(8);
|
|
78
81
|
expect(tree.isAVLBalanced()).toBe(true);
|
|
79
82
|
expect(tree.getHeight()).toBe(3);
|
|
80
83
|
|
|
81
|
-
expect(tree.
|
|
82
|
-
expect(tree.
|
|
84
|
+
expect(tree.delete(6)[0].deleted?.key).toBe(6);
|
|
85
|
+
expect(tree.delete(6).length).toBe(0);
|
|
83
86
|
expect(tree.isAVLBalanced()).toBe(true);
|
|
84
87
|
expect(tree.getHeight()).toBe(2);
|
|
85
88
|
|
|
86
|
-
expect(tree.
|
|
89
|
+
expect(tree.delete(7)[0].deleted?.key).toBe(7);
|
|
87
90
|
expect(tree.isAVLBalanced()).toBe(true);
|
|
88
91
|
expect(tree.getHeight()).toBe(2);
|
|
89
92
|
|
|
90
|
-
expect(tree.
|
|
93
|
+
expect(tree.delete(9)[0].deleted?.key).toBe(9);
|
|
91
94
|
expect(tree.isAVLBalanced()).toBe(true);
|
|
92
95
|
expect(tree.getHeight()).toBe(2);
|
|
93
|
-
expect(tree.
|
|
96
|
+
expect(tree.delete(14)[0].deleted?.key).toBe(14);
|
|
94
97
|
expect(tree.isAVLBalanced()).toBe(true);
|
|
95
98
|
expect(tree.getHeight()).toBe(1);
|
|
96
99
|
|