diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..563ba71 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/node_modules +/build \ No newline at end of file diff --git a/common/TreeNode.js b/common/TreeNode.js deleted file mode 100644 index 6887a4d..0000000 --- a/common/TreeNode.js +++ /dev/null @@ -1,4 +0,0 @@ -module.exports = function TreeNode(val) { - this.val = val; - this.left = this.right = null; -} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..d540550 --- /dev/null +++ b/package.json @@ -0,0 +1,28 @@ +{ + "name": "awesome-javascript", + "version": "1.0.0", + "description": "算法学习、实践", + "main": "index.js", + "scripts": { + "start": "npm run build:live", + "build": "./node_modules/.bin/tsc -p ./src", + "build:live": "nodemon --watch src/**/*.ts --exec ./node_modules/.bin/ts-node src/NAryTree/findNode.ts" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/JxSx/awesome-javascript.git" + }, + "keywords": [], + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/JxSx/awesome-javascript/issues" + }, + "homepage": "https://github.com/JxSx/awesome-javascript#readme", + "devDependencies": { + "@types/node": "^14.6.2", + "nodemon": "^2.0.4", + "ts-node": "^9.0.0", + "typescript": "^4.0.2" + } +} diff --git a/BinarySearchTree/index.js b/src/BinarySearchTree/index.js similarity index 100% rename from BinarySearchTree/index.js rename to src/BinarySearchTree/index.js diff --git a/BinaryTree/level_serialization.js b/src/BinaryTree/level_serialization.js similarity index 100% rename from BinaryTree/level_serialization.js rename to src/BinaryTree/level_serialization.js diff --git a/BinaryTree/maxDepth.js b/src/BinaryTree/maxDepth.js similarity index 100% rename from BinaryTree/maxDepth.js rename to src/BinaryTree/maxDepth.js diff --git a/BinaryTree/minDepth.js b/src/BinaryTree/minDepth.js similarity index 100% rename from BinaryTree/minDepth.js rename to src/BinaryTree/minDepth.js diff --git a/BinaryTree/serialization.js b/src/BinaryTree/serialization.js similarity index 88% rename from BinaryTree/serialization.js rename to src/BinaryTree/serialization.js index 2b8afe4..a51a302 100644 --- a/BinaryTree/serialization.js +++ b/src/BinaryTree/serialization.js @@ -1,5 +1,5 @@ -const { binaryTreeDataSource } = require('../common/data'); +const { BINARY_TREE_DATA_SOURCE } = require('../common/data'); const TreeNode = require("../common/TreeNode"); /** @@ -37,7 +37,7 @@ function deserialize(arr) { return root; } if (require.main === module) { - const list = serialize(binaryTreeDataSource); + const list = serialize(BINARY_TREE_DATA_SOURCE); console.log(list) console.log(JSON.stringify(deserialize(list), null, 2)) diff --git a/BinaryTree/traversal.js b/src/BinaryTree/traversal.js similarity index 84% rename from BinaryTree/traversal.js rename to src/BinaryTree/traversal.js index 24c2371..7a21cd2 100644 --- a/BinaryTree/traversal.js +++ b/src/BinaryTree/traversal.js @@ -1,4 +1,4 @@ -const { binaryTreeDataSource } = require('../common/data'); +const { BINARY_TREE_DATA_SOURCE } = require('../common/data'); /* F / \ @@ -21,7 +21,7 @@ const preOrder = function (root, arr = []) { preOrder(root.right, arr); // 遍历右子树 return arr; } -console.log("前序遍历:", preOrder(binaryTreeDataSource)); +console.log("前序遍历:", preOrder(BINARY_TREE_DATA_SOURCE)); @@ -37,7 +37,7 @@ const inOrder = function (root, arr = []) { inOrder(root.right, arr); // 遍历左子树 return arr; } -console.log("中序遍历:", inOrder(binaryTreeDataSource)); +console.log("中序遍历:", inOrder(BINARY_TREE_DATA_SOURCE)); /** @@ -52,7 +52,7 @@ const postOrder = function (root, arr = []) { arr.push(root.val); return arr; } -console.log("后序遍历:", postOrder(binaryTreeDataSource)); +console.log("后序遍历:", postOrder(BINARY_TREE_DATA_SOURCE)); @@ -78,7 +78,7 @@ var BFS = function (root) { return result; }; -console.log("深度优先遍历:", BFS(binaryTreeDataSource)); +console.log("深度优先遍历:", BFS(BINARY_TREE_DATA_SOURCE)); /** @@ -107,4 +107,4 @@ function levelOrder(root) { return result; } -console.log("层序遍历:", levelOrder(binaryTreeDataSource)); \ No newline at end of file +console.log("层序遍历:", levelOrder(BINARY_TREE_DATA_SOURCE)); \ No newline at end of file diff --git a/NAryTree/findNode.js b/src/NAryTree/findNode.ts similarity index 67% rename from NAryTree/findNode.js rename to src/NAryTree/findNode.ts index 07b9563..e2db94f 100644 --- a/NAryTree/findNode.js +++ b/src/NAryTree/findNode.ts @@ -1,10 +1,12 @@ -const { nAryTreeDataSource } = require('../common/data'); +import { Area } from "../common/data"; + +const { N_ARY_TREE_DATA_SOURCE } = require('../common/data'); /** * 树结构查找某个结点算法 * @param {*} root * @param {*} data */ -function findNode(root, data) { +function findNode(root: Area, data: string): any { console.log(root.name) if (!root) return null; if (root.name === data) { @@ -14,7 +16,7 @@ function findNode(root, data) { return null; } for (let item of root.children) { - let node = findNode(item, data); + let node: Area = findNode(item, data); if (node) return node; } } @@ -25,7 +27,7 @@ function findNode(root, data) { * @param {*} data * @param {*} arr */ -function findNodePath(root, data, arr = []) { +function findNodePath(root: Area, data: string, arr: string[] = []): any { if (!root) return null; if (root.name === data) { arr.push(root.name); @@ -42,5 +44,5 @@ function findNodePath(root, data, arr = []) { arr.pop(); // 出栈 } -console.log(findNode(nAryTreeDataSource, "铜川")) -console.log(findNodePath(nAryTreeDataSource, "铜川")) \ No newline at end of file +console.log(findNode(N_ARY_TREE_DATA_SOURCE, "铜川")) +console.log(findNodePath(N_ARY_TREE_DATA_SOURCE, "铜川")) \ No newline at end of file diff --git a/NAryTree/traversal.js b/src/NAryTree/traversal.ts similarity index 55% rename from NAryTree/traversal.js rename to src/NAryTree/traversal.ts index 6165c73..797cb24 100644 --- a/NAryTree/traversal.js +++ b/src/NAryTree/traversal.ts @@ -1,11 +1,10 @@ -const { nAryTreeDataSource } = require('../common/data'); - +import { N_ARY_TREE_DATA_SOURCE, Area } from '../common/data'; /** * 深度优先遍历 - * @param {*} root - * @param {*} arr + * @param {Area} root + * @param {string[]} arr */ -function DFS(root, arr = []) { +function DFS(root: Area, arr: string[] = []) { if (!root) return; arr.push(root.name); if (root.children) { @@ -15,21 +14,21 @@ function DFS(root, arr = []) { } return arr; } -console.log(DFS(nAryTreeDataSource)); +console.log("深度优先遍历:", DFS(N_ARY_TREE_DATA_SOURCE)); /** * 广度优先遍历,返回一维数组 - * @param {*} root + * @param {Area} root */ -var BFS = function (root) { - let queue = []; // 队列,先进先出 - let result = []; +var BFS = function (root: Area) { + let queue: Area[] = []; // 队列,先进先出 + let result: string[] = []; queue.push(root); while (queue.length > 0) { - let node = queue.shift(); + let node: any = queue.shift(); result.push(node.name); if (node.children) { - node.children.forEach(element => { + node.children.forEach((element: Area) => { queue.push(element); }); } @@ -37,10 +36,13 @@ var BFS = function (root) { return result; }; -console.log(BFS(nAryTreeDataSource)); - +console.log("广度优先遍历:", BFS(N_ARY_TREE_DATA_SOURCE)); -var levelOrder = function (root) { +/** + * 层序遍历 + * @param root + */ +var levelOrder = function (root: Area) { let queue = []; // let result = []; queue.push(root); @@ -48,10 +50,10 @@ var levelOrder = function (root) { let size = queue.length; // 当前层级的个数 let levelArr = []; for (let i = 0; i < size; i++) { // 嵌套一层循环,处理每个层级的数据 - const node = queue.shift(); + const node: any = queue.shift(); levelArr.push(node.name); if (node.children) { - node.children.forEach(element => { + node.children.forEach((element: Area) => { queue.push(element); }); } @@ -61,4 +63,4 @@ var levelOrder = function (root) { return result; }; -console.log(levelOrder(nAryTreeDataSource)); +console.log("层序遍历:", levelOrder(N_ARY_TREE_DATA_SOURCE)); diff --git a/src/common/TreeNode.ts b/src/common/TreeNode.ts new file mode 100644 index 0000000..f581883 --- /dev/null +++ b/src/common/TreeNode.ts @@ -0,0 +1,11 @@ +class TreeNode { + val: string | number; + left: TreeNode | null; + right: TreeNode | null; + constructor(val: string | number) { + this.val = val; + this.left = this.right = null; + } +} + +module.exports = TreeNode; \ No newline at end of file diff --git a/common/data.js b/src/common/data.ts similarity index 89% rename from common/data.js rename to src/common/data.ts index 6091cdf..468218a 100644 --- a/common/data.js +++ b/src/common/data.ts @@ -1,4 +1,4 @@ -const binaryTreeDataSource = { +export const BINARY_TREE_DATA_SOURCE = { val: "F", left: { val: "B", @@ -27,7 +27,13 @@ const binaryTreeDataSource = { } }; -const nAryTreeDataSource = { +export interface Area { + code: string; + name: string; + children?: Area[]; +} + +export const N_ARY_TREE_DATA_SOURCE: Area = { code: "china", name: "中国", children: [{ @@ -67,8 +73,3 @@ const nAryTreeDataSource = { name: '四川' }] }; - -module.exports = { - binaryTreeDataSource, - nAryTreeDataSource -} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..81715aa --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,72 @@ +{ + "compilerOptions": { + /* Visit https://aka.ms/tsconfig.json to read more about this file */ + + /* Basic Options */ + // "incremental": true, /* Enable incremental compilation */ + "target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */ + "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ + // "lib": [], /* Specify library files to be included in the compilation. */ + "allowJs": true, /* Allow javascript files to be compiled. */ + // "checkJs": true, /* Report errors in .js files. */ + // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ + // "declaration": true, /* Generates corresponding '.d.ts' file. */ + // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ + // "sourceMap": true, /* Generates corresponding '.map' file. */ + // "outFile": "./", /* Concatenate and emit output to single file. */ + "outDir": "./build", /* Redirect output structure to the directory. */ + "rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ + // "composite": true, /* Enable project compilation */ + // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ + // "removeComments": true, /* Do not emit comments to output. */ + // "noEmit": true, /* Do not emit outputs. */ + // "importHelpers": true, /* Import emit helpers from 'tslib'. */ + // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */ + // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ + + /* Strict Type-Checking Options */ + "strict": true, /* Enable all strict type-checking options. */ + // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ + // "strictNullChecks": true, /* Enable strict null checks. */ + // "strictFunctionTypes": true, /* Enable strict checking of function types. */ + // "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */ + // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */ + // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */ + // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */ + + /* Additional Checks */ + // "noUnusedLocals": true, /* Report errors on unused locals. */ + // "noUnusedParameters": true, /* Report errors on unused parameters. */ + // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ + // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ + + /* Module Resolution Options */ + // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ + // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ + // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ + // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ + // "typeRoots": [], /* List of folders to include type definitions from. */ + // "types": [], /* Type declaration files to be included in compilation. */ + // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ + "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ + // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ + // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ + + /* Source Map Options */ + // "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */ + // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ + // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */ + // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */ + + /* Experimental Options */ + // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ + // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ + + /* Advanced Options */ + "skipLibCheck": true, /* Skip type checking of declaration files. */ + "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */ + }, + "include": [ + "./src/**/*" + ] +}