mirror of
https://github.com/zrwusa/data-structure-typed.git
synced 2024-11-23 12:54:04 +00:00
[binary-tree] The binary tree's BFS and level-order traversal results should be the same. However, for convenience, we provide an additional method called listLevels to display traversal results in levels.
This commit is contained in:
parent
edad774263
commit
f90afe0b57
|
@ -1,15 +1,18 @@
|
|||
# Editor configuration, see http://editorconfig.org
|
||||
root = true
|
||||
[*]
|
||||
charset = utf-8
|
||||
end_of_line = lf
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
insert_final_newline = true
|
||||
trim_trailing_whitespace = true
|
||||
[*.md]
|
||||
|
||||
root=true # represents the top-level EditorConfig configuration file
|
||||
|
||||
[*] # means applicable to all files
|
||||
charset = utf-8 #Set the file character set to utf-8
|
||||
indent_style = space #Indent style (tab | space)
|
||||
indent_size = 2 # indent size
|
||||
end_of_line = lf # Control line break type (lf | cr | crlf)
|
||||
trim_trailing_whitespace = true # Remove any whitespace characters at the beginning of the line
|
||||
insert_final_newline = true #Always insert a new line at the end of the file
|
||||
|
||||
[*.md] # Indicates that only md files apply the following rules
|
||||
max_line_length = off
|
||||
trim_trailing_whitespace = false
|
||||
[*.yml]
|
||||
|
||||
[*.{yml,yaml}]
|
||||
indent_size = 2
|
||||
|
|
|
@ -8,7 +8,7 @@ All notable changes to this project will be documented in this file.
|
|||
- [Semantic Versioning](https://semver.org/spec/v2.0.0.html)
|
||||
- [`auto-changelog`](https://github.com/CookPete/auto-changelog)
|
||||
|
||||
## [v1.38.6](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...main) (upcoming)
|
||||
## [v1.38.8](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...main) (upcoming)
|
||||
|
||||
### Changes
|
||||
|
||||
|
|
|
@ -56,7 +56,7 @@ import {
|
|||
|
||||
```html
|
||||
|
||||
<script src='https://cdn.jsdelivr.net/npm/data-structure-typed/dist/umd/index.global.js'></script>
|
||||
<script src='https://cdn.jsdelivr.net/npm/data-structure-typed/dist/umd/data-structure-typed.min.js'></script>
|
||||
```
|
||||
|
||||
```js
|
||||
|
|
50
package-lock.json
generated
50
package-lock.json
generated
|
@ -1,12 +1,12 @@
|
|||
{
|
||||
"name": "data-structure-typed",
|
||||
"version": "1.38.6",
|
||||
"version": "1.38.8",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "data-structure-typed",
|
||||
"version": "1.38.6",
|
||||
"version": "1.38.8",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@types/benchmark": "^2.1.3",
|
||||
|
@ -15,17 +15,17 @@
|
|||
"@typescript-eslint/eslint-plugin": "^6.7.4",
|
||||
"@typescript-eslint/parser": "^6.7.4",
|
||||
"auto-changelog": "^2.4.0",
|
||||
"avl-tree-typed": "^1.38.5",
|
||||
"avl-tree-typed": "^1.38.7",
|
||||
"benchmark": "^2.1.4",
|
||||
"binary-tree-typed": "^1.38.5",
|
||||
"bst-typed": "^1.38.5",
|
||||
"binary-tree-typed": "^1.38.7",
|
||||
"bst-typed": "^1.38.7",
|
||||
"dependency-cruiser": "^14.1.0",
|
||||
"eslint": "^8.50.0",
|
||||
"eslint-config-prettier": "^9.0.0",
|
||||
"eslint-import-resolver-alias": "^1.1.2",
|
||||
"eslint-import-resolver-typescript": "^3.6.1",
|
||||
"eslint-plugin-import": "^2.28.1",
|
||||
"heap-typed": "^1.38.5",
|
||||
"heap-typed": "^1.38.7",
|
||||
"istanbul-badges-readme": "^1.8.5",
|
||||
"jest": "^29.7.0",
|
||||
"prettier": "^3.0.3",
|
||||
|
@ -2728,12 +2728,12 @@
|
|||
}
|
||||
},
|
||||
"node_modules/avl-tree-typed": {
|
||||
"version": "1.38.5",
|
||||
"resolved": "https://registry.npmjs.org/avl-tree-typed/-/avl-tree-typed-1.38.5.tgz",
|
||||
"integrity": "sha512-rwVkzj8+N57YPCurFn+kmtxEd9xvE8QqYlOAkibRsEScN8E2z7cHkVlT0mxc26Q8HfLUvkqJjvEcwoLaHqYxzw==",
|
||||
"version": "1.38.7",
|
||||
"resolved": "https://registry.npmjs.org/avl-tree-typed/-/avl-tree-typed-1.38.7.tgz",
|
||||
"integrity": "sha512-B5lOJoYI/8Y+WUI+atnN8FtKwjOZk6X91O55Qu8p6nauduOZlJvx0HtN3mmTKk0bYNqEHDNK5iyHD60Rwqx5Hw==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"data-structure-typed": "^1.38.4"
|
||||
"data-structure-typed": "^1.38.7"
|
||||
}
|
||||
},
|
||||
"node_modules/babel-jest": {
|
||||
|
@ -2927,12 +2927,12 @@
|
|||
}
|
||||
},
|
||||
"node_modules/binary-tree-typed": {
|
||||
"version": "1.38.5",
|
||||
"resolved": "https://registry.npmjs.org/binary-tree-typed/-/binary-tree-typed-1.38.5.tgz",
|
||||
"integrity": "sha512-ksluya1YOjzQWMJzCNWnkjw/iinYvYMQhZYLwcdhwuiSy7nlDJpSxil3dh3sc3ffZDjCDi+FtTe2TOlOKBNzHQ==",
|
||||
"version": "1.38.7",
|
||||
"resolved": "https://registry.npmjs.org/binary-tree-typed/-/binary-tree-typed-1.38.7.tgz",
|
||||
"integrity": "sha512-Z/xh9iEg0r7WNUzRAPa/yPHJJpc3s0jgC8bioiKgoIU67wxV8Mbn5WbuF+m2oXXpYyaM4qhC+AGU43OkHss7rQ==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"data-structure-typed": "^1.38.4"
|
||||
"data-structure-typed": "^1.38.7"
|
||||
}
|
||||
},
|
||||
"node_modules/brace-expansion": {
|
||||
|
@ -3011,12 +3011,12 @@
|
|||
}
|
||||
},
|
||||
"node_modules/bst-typed": {
|
||||
"version": "1.38.5",
|
||||
"resolved": "https://registry.npmjs.org/bst-typed/-/bst-typed-1.38.5.tgz",
|
||||
"integrity": "sha512-JrfBhfqx9VFFQ/EMjG+CHCnEoJdrR3BZ0AGCpT2y6NbBWOdjuLghorVnvy85kiEpU06xSQewhqf3cWNz0pSrPA==",
|
||||
"version": "1.38.7",
|
||||
"resolved": "https://registry.npmjs.org/bst-typed/-/bst-typed-1.38.7.tgz",
|
||||
"integrity": "sha512-jo8owKRnKPOAbdqR6SmvWjF2yceqN/3j736xujDum4Uh8devBLguV7m49HUv7pfkKAx8uwuuE+6b0dPQaxR4vA==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"data-structure-typed": "^1.38.4"
|
||||
"data-structure-typed": "^1.38.7"
|
||||
}
|
||||
},
|
||||
"node_modules/buffer-from": {
|
||||
|
@ -3413,9 +3413,9 @@
|
|||
}
|
||||
},
|
||||
"node_modules/data-structure-typed": {
|
||||
"version": "1.38.4",
|
||||
"resolved": "https://registry.npmjs.org/data-structure-typed/-/data-structure-typed-1.38.4.tgz",
|
||||
"integrity": "sha512-Yt9yjrx3Cm030z3o/6hr+AG0n+ZpWd+YpG2YiwxNwOT+hTZ6PYWt9MI+UVpiJSW/1Mrr0gchLlipU1r7l+UI/w==",
|
||||
"version": "1.38.7",
|
||||
"resolved": "https://registry.npmjs.org/data-structure-typed/-/data-structure-typed-1.38.7.tgz",
|
||||
"integrity": "sha512-YPV3hlUFTAG93+LoW8eaMv+KS4s9PF5MfpvvUkZybuR7WxqLtamCXWE1o3TRKkYFa9MEguxcq5ZaniCdeLg8Mw==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/debug": {
|
||||
|
@ -4771,12 +4771,12 @@
|
|||
}
|
||||
},
|
||||
"node_modules/heap-typed": {
|
||||
"version": "1.38.5",
|
||||
"resolved": "https://registry.npmjs.org/heap-typed/-/heap-typed-1.38.5.tgz",
|
||||
"integrity": "sha512-C9LQZScfnHGbhxL6wnUJEGwfg2oqdY10a0An3t/1pSVGEN91tmZzQS10BfYf5plQgF2fYNhZJbSkiuanUA0TgQ==",
|
||||
"version": "1.38.7",
|
||||
"resolved": "https://registry.npmjs.org/heap-typed/-/heap-typed-1.38.7.tgz",
|
||||
"integrity": "sha512-9ASCFfeKkN8iPFqgRUskah+a3RAxG4f+sbTVE8xRv4xwHpPLfwGo7gbJLkiJ5fkV+0PRN/ZXUkQPT5cI0XcbZQ==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"data-structure-typed": "^1.38.4"
|
||||
"data-structure-typed": "^1.38.7"
|
||||
}
|
||||
},
|
||||
"node_modules/html-escaper": {
|
||||
|
|
13
package.json
13
package.json
|
@ -1,11 +1,11 @@
|
|||
{
|
||||
"name": "data-structure-typed",
|
||||
"version": "1.38.6",
|
||||
"version": "1.38.8",
|
||||
"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.",
|
||||
"main": "dist/cjs/index.js",
|
||||
"module": "dist/mjs/index.js",
|
||||
"types": "dist/mjs/index.d.ts",
|
||||
"umd:main": "dist/umd/index.global.js",
|
||||
"umd:main": "dist/umd/data-structure-typed.min.js",
|
||||
"exports": {
|
||||
".": {
|
||||
"import": "./dist/mjs/index.js",
|
||||
|
@ -24,7 +24,6 @@
|
|||
"lint:test": "eslint --fix 'test/**/*.{js,ts}'",
|
||||
"lint": "npm run lint:src && npm run lint:test",
|
||||
"format:src": "prettier --write 'src/**/*.{js,ts}'",
|
||||
"format:specific": "prettier --write 'src/data-structures/binary-tree/bst.ts'",
|
||||
"format:test": "prettier --write 'test/**/*.{js,ts}'",
|
||||
"format": "npm run format:src && npm run format:test",
|
||||
"fix:src": "npm run lint:src && npm run format:src",
|
||||
|
@ -62,17 +61,17 @@
|
|||
"@typescript-eslint/eslint-plugin": "^6.7.4",
|
||||
"@typescript-eslint/parser": "^6.7.4",
|
||||
"auto-changelog": "^2.4.0",
|
||||
"avl-tree-typed": "^1.38.5",
|
||||
"avl-tree-typed": "^1.38.7",
|
||||
"benchmark": "^2.1.4",
|
||||
"binary-tree-typed": "^1.38.5",
|
||||
"bst-typed": "^1.38.5",
|
||||
"binary-tree-typed": "^1.38.7",
|
||||
"bst-typed": "^1.38.7",
|
||||
"dependency-cruiser": "^14.1.0",
|
||||
"eslint": "^8.50.0",
|
||||
"eslint-config-prettier": "^9.0.0",
|
||||
"eslint-import-resolver-alias": "^1.1.2",
|
||||
"eslint-import-resolver-typescript": "^3.6.1",
|
||||
"eslint-plugin-import": "^2.28.1",
|
||||
"heap-typed": "^1.38.5",
|
||||
"heap-typed": "^1.38.7",
|
||||
"istanbul-badges-readme": "^1.8.5",
|
||||
"jest": "^29.7.0",
|
||||
"prettier": "^3.0.3",
|
||||
|
|
|
@ -19,7 +19,9 @@ export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNeste
|
|||
}
|
||||
}
|
||||
|
||||
export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode> extends BST<V, N> implements IBinaryTree<V, N> {
|
||||
export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTreeNodeNested<V>>>
|
||||
extends BST<V, N>
|
||||
implements IBinaryTree<V, N> {
|
||||
/**
|
||||
* This is a constructor function for an AVL tree data structure in TypeScript.
|
||||
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
|
||||
|
|
|
@ -115,7 +115,7 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
|
|||
* Represents a binary tree data structure.
|
||||
* @template N - The type of the binary tree's nodes.
|
||||
*/
|
||||
export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode> implements IBinaryTree<V, N> {
|
||||
export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>> implements IBinaryTree<V, N> {
|
||||
/**
|
||||
* Creates a new instance of BinaryTree.
|
||||
* @param {BinaryTreeOptions} [options] - The options for the binary tree.
|
||||
|
@ -930,10 +930,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|||
* @param callback - The `callback` parameter is a function that will be called for each node in the
|
||||
* breadth-first search. It takes a node of type `N` as its argument and returns a value of type
|
||||
* `BFSCallbackReturn<N>`. The default value for this parameter is `this._defaultCallbackByKey
|
||||
* @param {boolean} [withLevel=false] - The `withLevel` parameter is a boolean flag that determines
|
||||
* whether to include the level of each node in the callback function. If `withLevel` is set
|
||||
* to `true`, the level of each node will be passed as an argument to the callback function. If
|
||||
* `withLevel` is
|
||||
* @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
|
||||
* search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
|
||||
* will not be performed and an empty array will be returned.
|
||||
|
@ -943,7 +939,6 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|||
*/
|
||||
bfs<C extends BFSCallback<N> = BFSCallback<N, BinaryTreeNodeKey>>(
|
||||
callback: C = this._defaultCallbackByKey as C,
|
||||
withLevel: boolean = false,
|
||||
beginRoot: N | null = this.root,
|
||||
iterationType = this.iterationType
|
||||
): ReturnType<C>[] {
|
||||
|
@ -951,9 +946,67 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|||
|
||||
const ans: BFSCallbackReturn<N>[] = [];
|
||||
|
||||
if (iterationType === IterationType.RECURSIVE) {
|
||||
const queue = new Queue<N>([beginRoot]);
|
||||
|
||||
function traverse(level: number) {
|
||||
if (queue.size === 0) return;
|
||||
|
||||
const current = queue.shift()!;
|
||||
ans.push(callback(current));
|
||||
|
||||
if (current.left) queue.push(current.left);
|
||||
if (current.right) queue.push(current.right);
|
||||
|
||||
traverse(level + 1);
|
||||
}
|
||||
|
||||
traverse(0);
|
||||
} else {
|
||||
const queue = new Queue<N>([beginRoot]);
|
||||
while (queue.size > 0) {
|
||||
const levelSize = queue.size;
|
||||
|
||||
for (let i = 0; i < levelSize; i++) {
|
||||
const current = queue.shift()!;
|
||||
ans.push(callback(current));
|
||||
|
||||
if (current.left) queue.push(current.left);
|
||||
if (current.right) queue.push(current.right);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
/**
|
||||
* The `listLevels` function takes a binary tree node and a callback function, and returns an array
|
||||
* of arrays representing the levels of the tree.
|
||||
* @param {C} callback - The `callback` parameter is a function that will be called on each node in
|
||||
* the tree. It takes a node as input and returns a value. The return type of the callback function
|
||||
* is determined by the generic type `C`.
|
||||
* @param {N | null} beginRoot - The `beginRoot` parameter represents the starting node of the binary tree
|
||||
* traversal. It can be any node in the binary tree. If no node is provided, the traversal will start
|
||||
* from the root node of the binary tree.
|
||||
* @param iterationType - The `iterationType` parameter determines whether the tree traversal is done
|
||||
* recursively or iteratively. It can have two possible values:
|
||||
* @returns The function `listLevels` returns an array of arrays, where each inner array represents a
|
||||
* level in a binary tree. Each inner array contains the return type of the provided callback
|
||||
* function `C` applied to the nodes at that level.
|
||||
*/
|
||||
listLevels<C extends BFSCallback<N> = BFSCallback<N, BinaryTreeNodeKey>>(
|
||||
callback: C = this._defaultCallbackByKey as C,
|
||||
beginRoot: N | null = this.root,
|
||||
iterationType = this.iterationType
|
||||
): ReturnType<C>[][] {
|
||||
if (!beginRoot) return [];
|
||||
const levelsNodes: ReturnType<C>[][] = [];
|
||||
|
||||
if (iterationType === IterationType.RECURSIVE) {
|
||||
const _recursive = (node: N, level: number) => {
|
||||
callback && ans.push(callback(node, withLevel ? level : undefined));
|
||||
if (!levelsNodes[level]) levelsNodes[level] = [];
|
||||
levelsNodes[level].push(callback(node));
|
||||
if (node.left) _recursive(node.left, level + 1);
|
||||
if (node.right) _recursive(node.right, level + 1);
|
||||
};
|
||||
|
@ -966,12 +1019,14 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
|
|||
const head = stack.pop()!;
|
||||
const [node, level] = head;
|
||||
|
||||
callback && ans.push(callback(node, withLevel ? level : undefined));
|
||||
if (!levelsNodes[level]) levelsNodes[level] = [];
|
||||
levelsNodes[level].push(callback(node));
|
||||
if (node.right) stack.push([node.right, level + 1]);
|
||||
if (node.left) stack.push([node.left, level + 1]);
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
|
||||
return levelsNodes;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -24,7 +24,7 @@ export class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extend
|
|||
}
|
||||
}
|
||||
|
||||
export class BST<V = any, N extends BSTNode<V, N> = BSTNode> extends BinaryTree<V, N> implements IBinaryTree<V, N> {
|
||||
export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>> extends BinaryTree<V, N> implements IBinaryTree<V, N> {
|
||||
/**
|
||||
* The constructor function initializes a binary search tree object with an optional comparator
|
||||
* function.
|
||||
|
|
|
@ -2,10 +2,7 @@ import {BinaryTreeNodeKey, RBColor, RBTreeNodeNested, RBTreeOptions} from '../..
|
|||
import {IBinaryTree} from '../../interfaces';
|
||||
import {BST, BSTNode} from './bst';
|
||||
|
||||
export class RBTreeNode<V = any, N extends RBTreeNode<V, N> = RBTreeNodeNested<V>> extends BSTNode<
|
||||
V,
|
||||
N
|
||||
> {
|
||||
export class RBTreeNode<V = any, N extends RBTreeNode<V, N> = RBTreeNodeNested<V>> extends BSTNode<V, N> {
|
||||
constructor(key: BinaryTreeNodeKey, val?: V) {
|
||||
super(key, val);
|
||||
this._color = RBColor.RED;
|
||||
|
@ -22,7 +19,7 @@ export class RBTreeNode<V = any, N extends RBTreeNode<V, N> = RBTreeNodeNested<V
|
|||
}
|
||||
}
|
||||
|
||||
export class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode> extends BST<V, N> implements IBinaryTree<V, N> {
|
||||
export class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNested<V>>> extends BST<V, N> implements IBinaryTree<V, N> {
|
||||
constructor(options?: RBTreeOptions) {
|
||||
super(options);
|
||||
}
|
||||
|
|
|
@ -35,7 +35,7 @@ export class TreeMultisetNode<
|
|||
/**
|
||||
* The only distinction between a TreeMultiset and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
||||
*/
|
||||
export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultisetNode>
|
||||
export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultisetNode<V, TreeMultisetNodeNested<V>>>
|
||||
extends AVLTree<V, N>
|
||||
implements IBinaryTree<V, N> {
|
||||
/**
|
||||
|
@ -191,10 +191,7 @@ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultis
|
|||
* each key or node.
|
||||
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
||||
*/
|
||||
override addMany(
|
||||
keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[],
|
||||
data?: V[]
|
||||
): (N | null | undefined)[] {
|
||||
override addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: V[]): (N | null | undefined)[] {
|
||||
const inserted: (N | null | undefined)[] = [];
|
||||
|
||||
for (let i = 0; i < keysOrNodes.length; i++) {
|
||||
|
|
|
@ -183,7 +183,7 @@ describe('Individual package BST operations test', () => {
|
|||
});
|
||||
|
||||
it('should perform various operations on a Binary Search Tree with object values', () => {
|
||||
const objBST = new BST<BSTNode<{key: number; keyA: number}>>();
|
||||
const objBST = new BST<{ key: number; keyA: number }>();
|
||||
expect(objBST).toBeInstanceOf(BST);
|
||||
objBST.add(11, {key: 11, keyA: 11});
|
||||
objBST.add(3, {key: 3, keyA: 3});
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
<head>
|
||||
<meta charset='UTF-8'>
|
||||
<title>CDN Test</title>
|
||||
<script src='https://cdn.jsdelivr.net/npm/data-structure-typed/dist/umd/index.global.js'></script>
|
||||
<script src='https://cdn.jsdelivr.net/npm/data-structure-typed/dist/umd/data-structure-typed.min.js'></script>
|
||||
</head>
|
||||
<body>
|
||||
<div id='app'>
|
||||
|
|
|
@ -110,7 +110,7 @@ describe('AVL Tree Test', () => {
|
|||
});
|
||||
|
||||
describe('AVLTree APIs test', () => {
|
||||
const avl = new AVLTree<{id: number; text: string}>();
|
||||
const avl = new AVLTree<{ id: number; text: string }>();
|
||||
beforeEach(() => {
|
||||
avl.clear();
|
||||
});
|
||||
|
@ -123,7 +123,7 @@ describe('AVLTree APIs test', () => {
|
|||
avl.add(node3);
|
||||
avl.add(node3, {id: 3, text: 'text33'});
|
||||
|
||||
const bfsRes = avl.bfs(node => node.key, false);
|
||||
const bfsRes = avl.bfs(node => node.key);
|
||||
expect(bfsRes[0]).toBe(2);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,4 +1,8 @@
|
|||
import {AVLTree, AVLTreeNode, BinaryTree, BinaryTreeNode} from '../../../../src';
|
||||
import {AVLTree, AVLTreeNode, BinaryTree, BinaryTreeNode, IterationType} from '../../../../src';
|
||||
import {isDebugTest} from "../../../config";
|
||||
|
||||
const isDebug = isDebugTest;
|
||||
// const isDebug = true;
|
||||
|
||||
describe('BinaryTreeNode', () => {
|
||||
it('should create an instance of BinaryTreeNode', () => {
|
||||
|
@ -196,7 +200,7 @@ describe('BinaryTree Morris Traversal', () => {
|
|||
});
|
||||
|
||||
describe('BinaryTree APIs test', () => {
|
||||
const avl = new AVLTree<{id: number; text: string}>();
|
||||
const avl = new AVLTree<{ id: number; text: string }>();
|
||||
beforeEach(() => {
|
||||
avl.clear();
|
||||
});
|
||||
|
@ -213,3 +217,20 @@ describe('BinaryTree APIs test', () => {
|
|||
expect(bfsRes[0]?.key).toBe(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('BinaryTree traversals', () => {
|
||||
const tree = new BinaryTree<number>();
|
||||
|
||||
const arr = [35, 20, 40, 15, 29, null, 50, null, 16, 28, 30, 45, 55];
|
||||
tree.refill(arr);
|
||||
expect(tree.dfs(node => node.key, 'pre')).toEqual([35, 20, 15, 16, 29, 28, 30, 40, 50, 45, 55]);
|
||||
expect(tree.dfs(node => node.key, 'in')).toEqual([15, 16, 20, 28, 29, 30, 35, 40, 45, 50, 55]);
|
||||
expect(tree.dfs(node => node.key, 'post')).toEqual([16, 15, 28, 30, 29, 20, 45, 55, 50, 40, 35]);
|
||||
expect(tree.bfs(node => node.key, tree.root, IterationType.RECURSIVE)).toEqual([35, 20, 40, 15, 29, 50, 16, 28, 30, 45, 55]);
|
||||
expect(tree.bfs(node => node.key, tree.root, IterationType.ITERATIVE)).toEqual([35, 20, 40, 15, 29, 50, 16, 28, 30, 45, 55]);
|
||||
|
||||
const levels = tree.listLevels(node => node.key);
|
||||
expect(levels).toEqual([[35], [20, 40], [15, 29, 50], [16, 28, 30, 45, 55]]);
|
||||
isDebug && console.log(levels);
|
||||
|
||||
})
|
||||
|
|
|
@ -189,7 +189,7 @@ describe('BST operations test', () => {
|
|||
});
|
||||
|
||||
it('should perform various operations on a Binary Search Tree with object values', () => {
|
||||
const objBST = new BST<{key: number; keyA: number}>();
|
||||
const objBST = new BST<{ key: number; keyA: number }>();
|
||||
expect(objBST).toBeInstanceOf(BST);
|
||||
objBST.add(11, {key: 11, keyA: 11});
|
||||
objBST.add(3, {key: 3, keyA: 3});
|
||||
|
@ -260,7 +260,7 @@ describe('BST operations test', () => {
|
|||
objBST.perfectlyBalance();
|
||||
expect(objBST.isPerfectlyBalanced()).toBe(true);
|
||||
|
||||
const bfsNodesAfterBalanced: BSTNode<{key: number; keyA: number}>[] = [];
|
||||
const bfsNodesAfterBalanced: BSTNode<{ key: number; keyA: number }>[] = [];
|
||||
objBST.bfs(node => bfsNodesAfterBalanced.push(node));
|
||||
expect(bfsNodesAfterBalanced[0].key).toBe(8);
|
||||
expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].key).toBe(16);
|
||||
|
@ -385,7 +385,7 @@ describe('BST operations test', () => {
|
|||
expect(bfsIDs[1]).toBe(12);
|
||||
expect(bfsIDs[2]).toBe(16);
|
||||
|
||||
const bfsNodes: BSTNode<{key: number; keyA: number}>[] = [];
|
||||
const bfsNodes: BSTNode<{ key: number; keyA: number }>[] = [];
|
||||
objBST.bfs(node => bfsNodes.push(node));
|
||||
expect(bfsNodes[0].key).toBe(2);
|
||||
expect(bfsNodes[1].key).toBe(12);
|
||||
|
@ -435,13 +435,7 @@ describe('BST Performance test', function () {
|
|||
bst.addMany(nodes);
|
||||
isDebug && console.log('---add', performance.now() - start);
|
||||
const startL = performance.now();
|
||||
const arr: number[][] = [];
|
||||
bst.bfs((node, level) => {
|
||||
if (level !== undefined) {
|
||||
if (!arr[level]) arr[level] = [];
|
||||
arr[level].push(node.key);
|
||||
}
|
||||
}, true);
|
||||
const arr: number[][] = bst.listLevels((node) => node.key);
|
||||
isDebug && console.log('---listLevels', arr);
|
||||
isDebug && console.log('---listLevels', performance.now() - startL);
|
||||
});
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import {AVLTree, BST, BSTNode} from '../../../../src';
|
||||
import {AVLTree, BST} from '../../../../src';
|
||||
|
||||
describe('Overall BinaryTree Test', () => {
|
||||
it('should perform various operations on BinaryTree', () => {
|
||||
|
@ -6,30 +6,30 @@ describe('Overall BinaryTree Test', () => {
|
|||
bst.add(11);
|
||||
bst.add(3);
|
||||
bst.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5], undefined, false);
|
||||
bst.size === 16; // true
|
||||
expect(bst.size).toBe(16); // true
|
||||
bst.has(6); // true
|
||||
expect(bst.has(6)).toBe(true); // true
|
||||
bst.getHeight(6) === 2; // true
|
||||
bst.getHeight() === 5; // true
|
||||
bst.getDepth(6) === 3; // true
|
||||
expect(bst.getHeight(6)).toBe(2); // true
|
||||
expect(bst.getHeight()).toBe(5); // true
|
||||
expect(bst.getDepth(6)).toBe(3); // true
|
||||
bst.size === 16; // true
|
||||
expect(bst.size).toBe(16); // true
|
||||
bst.has(6); // true
|
||||
expect(bst.has(6)).toBe(true); // true
|
||||
bst.getHeight(6) === 2; // true
|
||||
bst.getHeight() === 5; // true
|
||||
bst.getDepth(6) === 3; // true
|
||||
expect(bst.getHeight(6)).toBe(2); // true
|
||||
expect(bst.getHeight()).toBe(5); // true
|
||||
expect(bst.getDepth(6)).toBe(3); // true
|
||||
const leftMost = bst.getLeftMost();
|
||||
leftMost?.key === 1; // true
|
||||
leftMost?.key === 1; // true
|
||||
expect(leftMost?.key).toBe(1);
|
||||
bst.delete(6);
|
||||
bst.get(6); // null
|
||||
bst.get(6); // null
|
||||
expect(bst.get(6)).toBeNull();
|
||||
bst.isAVLBalanced(); // true or false
|
||||
bst.isAVLBalanced(); // true or false
|
||||
expect(bst.isAVLBalanced()).toBe(true);
|
||||
const bfsIDs: number[] = [];
|
||||
bst.bfs(node => bfsIDs.push(node.key));
|
||||
bfsIDs[0] === 11; // true
|
||||
bfsIDs[0] === 11; // true
|
||||
expect(bfsIDs[0]).toBe(11);
|
||||
|
||||
const objBST = new BST<{key: number; keyA: number}>();
|
||||
const objBST = new BST<{ key: number; keyA: number }>();
|
||||
objBST.add(11, {key: 11, keyA: 11});
|
||||
objBST.add(3, {key: 3, keyA: 3});
|
||||
|
||||
|
@ -57,10 +57,10 @@ describe('Overall BinaryTree Test', () => {
|
|||
|
||||
const avlTree = new AVLTree();
|
||||
avlTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
|
||||
avlTree.isAVLBalanced(); // true
|
||||
expect(avlTree.isAVLBalanced()).toBe(true); // true
|
||||
avlTree.isAVLBalanced(); // true
|
||||
expect(avlTree.isAVLBalanced()).toBe(true); // true
|
||||
avlTree.delete(10);
|
||||
avlTree.isAVLBalanced(); // true
|
||||
expect(avlTree.isAVLBalanced()).toBe(true); // true
|
||||
avlTree.isAVLBalanced(); // true
|
||||
expect(avlTree.isAVLBalanced()).toBe(true); // true
|
||||
});
|
||||
});
|
||||
|
|
|
@ -206,7 +206,7 @@ describe('TreeMultiset operations test', () => {
|
|||
});
|
||||
|
||||
it('should perform various operations on a Binary Search Tree with object values', () => {
|
||||
const objTreeMultiset = new TreeMultiset<{key: number; keyA: number}>();
|
||||
const objTreeMultiset = new TreeMultiset<{ key: number; keyA: number }>();
|
||||
expect(objTreeMultiset).toBeInstanceOf(TreeMultiset);
|
||||
objTreeMultiset.add(11, {key: 11, keyA: 11});
|
||||
objTreeMultiset.add(3, {key: 3, keyA: 3});
|
||||
|
@ -481,7 +481,7 @@ describe('TreeMultiset Performance test', function () {
|
|||
}
|
||||
isDebug && console.log('---add', performance.now() - start);
|
||||
const startL = performance.now();
|
||||
treeMS.lesserOrGreaterTraverse((node: TreeMultisetNode<number>) => (node.count += 1), CP.lt, inputSize / 2);
|
||||
treeMS.lesserOrGreaterTraverse((node) => (node.count += 1), CP.lt, inputSize / 2);
|
||||
isDebug && console.log('---lesserOrGreaterTraverse', performance.now() - startL);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -22,7 +22,7 @@ describe('Heap Operation Test', () => {
|
|||
});
|
||||
|
||||
it('should object heap work well', function () {
|
||||
const minHeap = new MinHeap<{a: string; key: number}>((a, b) => a.key - b.key);
|
||||
const minHeap = new MinHeap<{ a: string; key: number }>((a, b) => a.key - b.key);
|
||||
minHeap.add({key: 1, a: 'a1'});
|
||||
minHeap.add({key: 6, a: 'a6'});
|
||||
minHeap.add({key: 2, a: 'a2'});
|
||||
|
@ -37,7 +37,7 @@ describe('Heap Operation Test', () => {
|
|||
i++;
|
||||
}
|
||||
|
||||
const maxHeap = new MaxHeap<{key: number; a: string}>((a, b) => b.key - a.key);
|
||||
const maxHeap = new MaxHeap<{ key: number; a: string }>((a, b) => b.key - a.key);
|
||||
maxHeap.add({key: 1, a: 'a1'});
|
||||
maxHeap.add({key: 6, a: 'a6'});
|
||||
maxHeap.add({key: 5, a: 'a5'});
|
||||
|
|
|
@ -3,7 +3,7 @@ import {bigO, magnitude} from '../../../utils';
|
|||
|
||||
describe('DoublyLinkedList Operation Test', () => {
|
||||
let list: DoublyLinkedList<number>;
|
||||
let objectList: DoublyLinkedList<{keyA: number}>;
|
||||
let objectList: DoublyLinkedList<{ keyA: number }>;
|
||||
|
||||
beforeEach(() => {
|
||||
list = new DoublyLinkedList();
|
||||
|
|
|
@ -3,10 +3,10 @@ import {bigO, magnitude} from '../../../utils';
|
|||
|
||||
describe('SinglyLinkedList Operation Test', () => {
|
||||
let list: SinglyLinkedList<number>;
|
||||
let objectList: SinglyLinkedList<{keyA: number}>;
|
||||
let objectList: SinglyLinkedList<{ keyA: number }>;
|
||||
beforeEach(() => {
|
||||
list = new SinglyLinkedList<number>();
|
||||
objectList = new SinglyLinkedList<{keyA: number}>();
|
||||
objectList = new SinglyLinkedList<{ keyA: number }>();
|
||||
});
|
||||
|
||||
describe('push', () => {
|
||||
|
|
|
@ -17,7 +17,7 @@ describe('MaxPriorityQueue Operation Test', () => {
|
|||
});
|
||||
|
||||
it('should add elements and maintain heap property in a object MaxPriorityQueue', () => {
|
||||
const priorityQueue = new MaxPriorityQueue<{keyA: number}>((a, b) => b.keyA - a.keyA);
|
||||
const priorityQueue = new MaxPriorityQueue<{ keyA: number }>((a, b) => b.keyA - a.keyA);
|
||||
priorityQueue.refill([{keyA: 5}, {keyA: 3}, {keyA: 1}]);
|
||||
priorityQueue.add({keyA: 7});
|
||||
|
||||
|
@ -64,7 +64,7 @@ describe('MaxPriorityQueue Operation Test', () => {
|
|||
|
||||
it('should correctly heapify an object array', () => {
|
||||
const nodes = [{keyA: 5}, {keyA: 3}, {keyA: 7}, {keyA: 1}];
|
||||
const maxPQ = MaxPriorityQueue.heapify<{keyA: number}>(nodes, (a, b) => b.keyA - a.keyA);
|
||||
const maxPQ = MaxPriorityQueue.heapify<{ keyA: number }>(nodes, (a, b) => b.keyA - a.keyA);
|
||||
|
||||
expect(maxPQ.poll()?.keyA).toBe(7);
|
||||
expect(maxPQ.poll()?.keyA).toBe(5);
|
||||
|
|
|
@ -26,7 +26,7 @@ export const bigO = {
|
|||
|
||||
function findPotentialN(input: any): number {
|
||||
let longestArray: any[] = [];
|
||||
let mostProperties: {[key: string]: any} = {};
|
||||
let mostProperties: { [key: string]: any } = {};
|
||||
|
||||
function recurse(obj: any) {
|
||||
if (Array.isArray(obj)) {
|
||||
|
|
|
@ -1,11 +1,18 @@
|
|||
export default [{
|
||||
entry: ['src/index.ts'],
|
||||
entryPoints: {
|
||||
"data-structure-typed": "src/index.ts"
|
||||
},
|
||||
format: ["iife"],
|
||||
clean: true,
|
||||
sourcemap: true,
|
||||
minify: true,
|
||||
outDir: 'dist/umd',
|
||||
globalName: 'dataStructureTyped',
|
||||
outDir: "dist/umd",
|
||||
globalName: "dataStructureTyped",
|
||||
platform: "browser",
|
||||
bundle: true
|
||||
bundle: true,
|
||||
outExtension() {
|
||||
return {
|
||||
js: `.min.js`,
|
||||
}
|
||||
},
|
||||
}];
|
||||
|
|
Loading…
Reference in a new issue