├── LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code ├── src │ ├── ts │ │ ├── algorithms │ │ │ ├── string │ │ │ │ ├── huffman.ts │ │ │ │ ├── longest-common-substring.ts │ │ │ │ ├── brute-force.ts │ │ │ │ ├── knuth-morris-pratt.ts │ │ │ │ └── rabin-karp.ts │ │ │ ├── shuffle │ │ │ │ └── fisher–yates.ts │ │ │ ├── greedy │ │ │ │ ├── min-coin-change.ts │ │ │ │ ├── longest-common-subsequence.ts │ │ │ │ ├── matrix-chain-multiplication.ts │ │ │ │ └── knapsack.ts │ │ │ ├── search │ │ │ │ ├── sequential-search.ts │ │ │ │ ├── min-max-search.ts │ │ │ │ ├── binary-search.ts │ │ │ │ ├── interpolation-search.ts │ │ │ │ └── binary-search-recursive.ts │ │ │ ├── math │ │ │ │ ├── find-divisors.ts │ │ │ │ ├── sieve-eratosthenes.ts │ │ │ │ ├── lcm.ts │ │ │ │ ├── greatest-difference.ts │ │ │ │ ├── gcd.ts │ │ │ │ └── primality-test.ts │ │ │ ├── dynamic-programing │ │ │ │ ├── knapsack-recursive.ts │ │ │ │ ├── longest-common-subsequence.ts │ │ │ │ └── min-coin-change.ts │ │ │ ├── sorting │ │ │ │ ├── bubble-sort.ts │ │ │ │ ├── insertion-sort.ts │ │ │ │ ├── bubble-sort-improved.ts │ │ │ │ ├── counting-sort.ts │ │ │ │ ├── shell-sort.ts │ │ │ │ ├── selection-sort.ts │ │ │ │ ├── merge-sort.ts │ │ │ │ └── heap-sort.ts │ │ │ └── graph │ │ │ │ ├── floyd-warshall.ts │ │ │ │ ├── dijkstra.ts │ │ │ │ └── prim.ts │ │ ├── data-structures │ │ │ ├── models │ │ │ │ ├── value-pair.ts │ │ │ │ ├── node.ts │ │ │ │ ├── value-pair-lazy.ts │ │ │ │ ├── linked-list-models.ts │ │ │ │ └── red-black-node.ts │ │ │ ├── stack-array.ts │ │ │ ├── stack-linked-list.ts │ │ │ └── stack.ts │ │ └── others │ │ │ ├── factorial.ts │ │ │ ├── hot-potato.ts │ │ │ ├── palindrome-checker.ts │ │ │ ├── balanced-symbols.ts │ │ │ ├── fibonacci.ts │ │ │ └── base-converter.ts │ └── js │ │ ├── data-structures │ │ ├── models │ │ │ ├── node.js │ │ │ ├── value-pair.js │ │ │ ├── value-pair-lazy.js │ │ │ └── linked-list-models.js │ │ ├── stack-array.js │ │ ├── stack-linked-list.js │ │ ├── stack.js │ │ └── sorted-linked-list.js │ │ ├── algorithms │ │ ├── search │ │ │ ├── sequential-search.js │ │ │ ├── min-max-search.js │ │ │ ├── binary-search.js │ │ │ ├── binary-search-recursive.js │ │ │ └── interpolation-search.js │ │ ├── greedy │ │ │ ├── min-coin-change.js │ │ │ ├── longest-common-subsequence.js │ │ │ ├── matrix-chain-multiplication.js │ │ │ └── knapsack.js │ │ ├── shuffle │ │ │ └── fisher–yates.js │ │ ├── dynamic-programing │ │ │ ├── knapsack-recursive.js │ │ │ ├── longest-common-subsequence.js │ │ │ └── min-coin-change.js │ │ ├── sorting │ │ │ ├── bubble-sort.js │ │ │ ├── insertion-sort.js │ │ │ ├── bubble-sort-improved.js │ │ │ ├── counting-sort.js │ │ │ ├── shell-sort.js │ │ │ ├── selection-sort.js │ │ │ ├── merge-sort.js │ │ │ ├── heap-sort.js │ │ │ ├── quicksort.js │ │ │ └── bucket-sort.js │ │ ├── graph │ │ │ ├── floyd-warshall.js │ │ │ ├── dijkstra.js │ │ │ └── prim.js │ │ └── backtracking │ │ │ └── rat-in-maze.js │ │ └── others │ │ ├── factorial.js │ │ ├── hot-potato.js │ │ ├── balanced-symbols.js │ │ ├── palindrome-checker.js │ │ ├── fibonacci.js │ │ └── base-converter.js ├── examples │ ├── chapter14 │ │ ├── 01-DC-BinarySearch.js │ │ ├── 09-MatrixChainMultiplicationDP.js │ │ ├── 10-MatrixChainMultiplicationRecursive.js │ │ ├── 02-MinCoinChangeDP.js │ │ ├── 08-LongestCommonSubsequenceRecursive.js │ │ ├── 03-MinCoinChangeGreedy.js │ │ ├── 11-RatInMaze.js │ │ ├── 06-KnapSackProblemGreedy.js │ │ ├── 04-KnapsackProblemDP.js │ │ ├── 05-KnapSackProblemRecursive.js │ │ ├── 11-RatInMaze.html │ │ ├── 07-LongestCommonSubsequenceDP.js │ │ ├── 12-SudokuSolver.html │ │ ├── 01-DC-BinarySearch.html │ │ ├── 02-MinCoinChangeDP.html │ │ ├── 04-KnapsackProblemDP.html │ │ ├── 03-MinCoinChangeGreedy.html │ │ ├── 06-KnapSackProblemGreedy.html │ │ ├── 05-KnapSackProblemRecursive.html │ │ ├── 07-LongestCommonSubsequenceDP.html │ │ ├── 09-MatrixChainMultiplicationDP.html │ │ ├── 13-IntroFunctionalProgramming.html │ │ ├── 08-LongestCommonSubsequenceRecursive.html │ │ ├── 10-MatrixChainMultiplicationRecursive.html │ │ └── 12-SudokuSolver.js │ ├── chapter01_02 │ │ ├── 17-ES2015-Modules-node │ │ │ ├── 17-Book.mjs │ │ │ ├── 17-CalcArea.mjs │ │ │ └── 17-ES2015-ES6-Modules.mjs │ │ ├── 07-Loops.html │ │ ├── 03-Operators.html │ │ ├── 08-Functions.html │ │ ├── 17-Book.js │ │ ├── 02-Variables.html │ │ ├── 04-TruthyFalsy.html │ │ ├── 01-HelloWorld.js │ │ ├── 05-EqualsOperators.html │ │ ├── 09-ObjectOrientedJS.html │ │ ├── 06-ConditionalStatements.html │ │ ├── 16-ES2015-ES6-Classes.html │ │ ├── 10-ES2015-ES6-letconst.html │ │ ├── 17-CalcArea.js │ │ ├── 13-ES2015-ES6-ArrowFunctions.html │ │ ├── 14-ES2015-ES6-ParameterHandling.html │ │ ├── 11-ES2015-ES6-variableScope.html │ │ ├── 12-ES2015-ES6-StringTemplates.html │ │ ├── 18-ES2016-ES7-ExponentiationOperator.html │ │ ├── 15-ES2015-ES6-EnhancedObjectProperties.html │ │ ├── 17-ES2015-ES6-Modules-node.js │ │ ├── 17-ES2015-ES6-Modules.html │ │ ├── 01-HelloWorld.html │ │ ├── 18-ES2016-ES7-ExponentiationOperator.js │ │ ├── 12-ES2015-ES6-StringTemplates.js │ │ ├── 07-Loops.js │ │ ├── 08-Functions.js │ │ ├── 13-ES2015-ES6-ArrowFunctions.js │ │ ├── 17-ES2015-ES6-Modules.js │ │ ├── 10-ES2015-ES6-letconst.js │ │ ├── 04-TruthyFalsy.js │ │ ├── lib │ │ │ └── 17-CalcArea.js │ │ ├── 09-ObjectOrientedJS.js │ │ ├── typescript │ │ │ ├── hello-world.js │ │ │ └── hello-world.ts │ │ ├── 15-ES2015-ES6-EnhancedObjectProperties.js │ │ ├── 02-Variables.js │ │ └── 05-EqualsOperators.js │ ├── chapter09 │ │ ├── 02-Factorial.html │ │ ├── 03-JSCallStack.js │ │ ├── 04-Fibonacci.html │ │ ├── 03-JSCallStack.html │ │ ├── 01-IntroRecursion.html │ │ ├── 02-Factorial.js │ │ └── 01-IntroRecursion.js │ ├── chapter04 │ │ ├── 04-TowerOfHanoi.js │ │ ├── 04-TowerOfHanoi.html │ │ ├── 02-BalancedSymbols.js │ │ ├── 03-DecimalToBinary.html │ │ ├── 02-BalancedSymbols.html │ │ ├── 01-Stack.html │ │ ├── 03-DecimalToBinary.js │ │ └── 01-StackSymbol.js │ ├── examples-screenshot.png │ ├── chapter08 │ │ ├── 05-ES2015Map.html │ │ ├── 06-ES2015WeakMap.html │ │ ├── 07-ES2015WeakSet.html │ │ ├── 02-HashTable.html │ │ ├── 01-Dictionaries.html │ │ ├── 04-HashCollisionLinearProbing.html │ │ ├── 03-HashCollisionSeparateChaining.html │ │ ├── 07-ES2015WeakSet.js │ │ ├── 06-ES2015WeakMap.js │ │ └── 05-ES2015Map.js │ ├── chapter03 │ │ ├── 01-Introduction.html │ │ ├── 05-ArrayMethods.html │ │ ├── 07-Sorting.html │ │ ├── 08-Searching.html │ │ ├── 09-TypedArrays.html │ │ ├── 03-AddingRemovingElements.html │ │ ├── 06-ES2015Methods.html │ │ ├── 02-CreatingAndInitialingArrays.html │ │ ├── 04-TwoDimensionalMultiDimensional.html │ │ ├── 09-TypedArrays.js │ │ ├── 08-Searching.js │ │ ├── 10-ArraysAndTypeScript.ts │ │ ├── 01-Introduction.js │ │ └── 02-CreatingAndInitialingArrays.js │ ├── chapter15 │ │ ├── 01-BigONotation.html │ │ └── bigOChart │ │ │ └── index.html │ ├── chapter11 │ │ ├── 03-HeapSort.js │ │ ├── 03-HeapSort.html │ │ ├── 01-UsingMinHeap.html │ │ ├── 02-UsingMaxHeap.html │ │ ├── 01-UsingMinHeap.js │ │ └── 02-UsingMaxHeap.js │ ├── chapter07 │ │ ├── 01-Set.html │ │ ├── 03-ES2015Set.html │ │ ├── 02-SetOperations.html │ │ └── 01-Set.js │ ├── chapter12 │ │ ├── 02-BFS.html │ │ ├── 03-DFS.html │ │ ├── 06-Prim.html │ │ ├── 04-Dijkstra.html │ │ ├── 07-Kruskal.html │ │ ├── 01-UsingGraphs.html │ │ ├── 05-Floyd-Warshall.html │ │ ├── 04-Dijkstra.js │ │ ├── 06-Prim.js │ │ ├── 07-Kruskal.js │ │ ├── 01-UsingGraphs.js │ │ └── 05-Floyd-Warshall.js │ ├── chapter05 │ │ ├── 01-Queue.html │ │ ├── 02-Deque.html │ │ ├── 03-HotPotato.html │ │ ├── 04-PalindromeChecker.html │ │ ├── 04-PalindromeChecker.js │ │ ├── 01-Queue.js │ │ ├── 03-HotPotato.js │ │ └── 02-Deque.js │ ├── chapter06 │ │ ├── 01-LinkedList.html │ │ ├── 02-DoublyLinkedList.html │ │ ├── 04-SortedLinkedList.html │ │ ├── 05-StackLinkedList.html │ │ ├── 03-CircularLinkedList.html │ │ └── 05-StackLinkedList.js │ └── chapter13 │ │ ├── 01-BubbleSort.html │ │ ├── 01-BucketSort.html │ │ ├── 01-CountingSort.html │ │ ├── 01-InsertionSort.html │ │ ├── 01-MergeSort.html │ │ ├── 01-QuickSort.html │ │ ├── 01-RadixSort.html │ │ ├── 01-SelectionSort.html │ │ ├── 01-BubbleSort.js │ │ ├── 01-BucketSort.js │ │ ├── 01-CountingSort.js │ │ ├── 01-InsertionSort.js │ │ ├── 01-MergeSort.js │ │ ├── 01-QuickSort.js │ │ ├── 01-RadixSort.js │ │ └── 01-SelectionSort.js ├── .vscode │ ├── settings.json │ └── tasks.json ├── .babelrc ├── .gitignore ├── test │ ├── js │ │ ├── algorithms │ │ │ ├── sorting │ │ │ │ ├── heap-sort.spec.js │ │ │ │ ├── merge-sort.spec.js │ │ │ │ ├── quicksort.spec.js │ │ │ │ ├── shell-sort.spec.js │ │ │ │ ├── bubble-sort.spec.js │ │ │ │ ├── insertion-sort.spec.js │ │ │ │ ├── selection-sort.spec.js │ │ │ │ ├── bucket-sort.spec.js │ │ │ │ ├── radix-sort.spec.js │ │ │ │ ├── bubble-sort-improved.spec.js │ │ │ │ └── counting-sort.spec.js │ │ │ ├── search │ │ │ │ ├── binary-search.spec.js │ │ │ │ ├── sequential-search.spec.js │ │ │ │ ├── binary-search-recursive.spec.js │ │ │ │ ├── interpolation-search.spec.js │ │ │ │ └── min-max-search.spec.js │ │ │ ├── greedy │ │ │ │ ├── longest-common-subsequence.spec.js │ │ │ │ ├── matrix-chain-multiplication.spec.js │ │ │ │ ├── knapsack.spec.js │ │ │ │ └── min-coin-change.spec.js │ │ │ ├── dynamic-programming │ │ │ │ ├── longest-common-subsequence.spec.js │ │ │ │ ├── matrix-chain-multiplication.spec.js │ │ │ │ ├── longest-common-subsequence-print.spec.js │ │ │ │ ├── knapsack.spec.js │ │ │ │ ├── knapsack-recursive.spec.js │ │ │ │ └── min-coin-change.spec.js │ │ │ ├── backtracking │ │ │ │ └── rat-in-maze.spec.js │ │ │ └── shuffle │ │ │ │ └── fisher–yates.spec.js │ │ ├── data-structures │ │ │ ├── my-obj.js │ │ │ └── avl-tree.spec.js │ │ └── others │ │ │ ├── hanoi.spec.js │ │ │ ├── hot-potato.spec.js │ │ │ ├── palindrome-checker.spec.js │ │ │ ├── factorial.spec.js │ │ │ └── balanced-symbols.spec.js │ └── ts │ │ ├── algorithms │ │ ├── sorting │ │ │ ├── heap-sort.spec.ts │ │ │ ├── merge-sort.spec.ts │ │ │ ├── quicksort.spec.ts │ │ │ ├── shell-sort.spec.ts │ │ │ ├── bubble-sort.spec.ts │ │ │ ├── insertion-sort.spec.ts │ │ │ ├── selection-sort.spec.ts │ │ │ ├── bucket-sort.spec.ts │ │ │ ├── radix-sort.spec.ts │ │ │ ├── counting-sort.spec.ts │ │ │ └── bubble-sort-improved.spec.ts │ │ ├── search │ │ │ ├── binary-search.spec.ts │ │ │ ├── sequential-search.spec.ts │ │ │ ├── binary-search-recursive.spec.ts │ │ │ ├── interpolation-search.spec.ts │ │ │ └── min-max-search.spec.ts │ │ ├── greedy │ │ │ ├── longest-common-subsequence.spec.ts │ │ │ ├── matrix-chain-multiplication.spec.ts │ │ │ ├── knapsack.spec.ts │ │ │ └── min-coin-change.spec.ts │ │ ├── dynamic-programming │ │ │ ├── longest-common-subsequence.spec.ts │ │ │ ├── matrix-chain-multiplication.spec.ts │ │ │ ├── longest-common-subsequence-print.spec.ts │ │ │ ├── knapsack.spec.ts │ │ │ ├── knapsack-recursive.spec.ts │ │ │ └── min-coin-change.spec.ts │ │ ├── backtracking │ │ │ └── rat-in-maze.spec.ts │ │ └── shuffle │ │ │ └── fisher–yates.spec.ts │ │ ├── data-structures │ │ ├── my-obj.ts │ │ └── avl-tree.spec.ts │ │ └── others │ │ ├── hanoi.spec.ts │ │ ├── hot-potato.spec.ts │ │ ├── palindrome-checker.spec.ts │ │ ├── factorial.spec.ts │ │ └── balanced-symbols.spec.ts ├── .editorconfig ├── CHANGELOG.md ├── jsconfig.json └── .eslintrc.json └── LICENSE /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/algorithms/string/huffman.ts: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter14/01-DC-BinarySearch.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/algorithms/string/longest-common-substring.ts: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "typescript.tsdk": "node_modules/typescript/lib" 3 | } -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["env"], 3 | "plugins": [ 4 | "add-module-exports", 5 | "transform-es2015-modules-umd" 6 | ] 7 | } 8 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/.gitignore: -------------------------------------------------------------------------------- 1 | .idea/* 2 | *.log 3 | node_modules 4 | coverage 5 | .nyc_output 6 | coverage.lcov 7 | mochawesome-report/* 8 | dist/js/* 9 | dist-/* 10 | dist/ts/* 11 | snippet.js 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter14/09-MatrixChainMultiplicationDP.js: -------------------------------------------------------------------------------- 1 | const { matrixChainOrder } = PacktDataStructuresAlgorithms; 2 | 3 | const p = [10, 100, 5, 50, 1]; 4 | console.log(matrixChainOrder(p)); 5 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter14/10-MatrixChainMultiplicationRecursive.js: -------------------------------------------------------------------------------- 1 | const { matrixChainOrderGreedy } = PacktDataStructuresAlgorithms; 2 | 3 | const p = [10, 100, 5, 50, 1]; 4 | console.log(matrixChainOrderGreedy(p)); 5 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/17-ES2015-Modules-node/17-Book.mjs: -------------------------------------------------------------------------------- 1 | export default class Book { 2 | constructor(title) { 3 | this.title = title; 4 | } 5 | printTitle() { 6 | console.log(this.title); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/data-structures/models/value-pair.ts: -------------------------------------------------------------------------------- 1 | export class ValuePair { 2 | constructor(public key: K, public value: V) {} 3 | 4 | toString() { 5 | return `[#${this.key}: ${this.value}]`; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/js/algorithms/sorting/heap-sort.spec.js: -------------------------------------------------------------------------------- 1 | import { heapSort } from '../../../../src/js/index'; 2 | import { testSortAlgorithm } from './sort-algorithm-tests'; 3 | 4 | testSortAlgorithm(heapSort, 'Heap Sort'); 5 | 6 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/js/algorithms/sorting/merge-sort.spec.js: -------------------------------------------------------------------------------- 1 | import { mergeSort } from '../../../../src/js/index'; 2 | import { testSortAlgorithm } from './sort-algorithm-tests'; 3 | 4 | testSortAlgorithm(mergeSort, 'Merge Sort'); 5 | 6 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/js/algorithms/sorting/quicksort.spec.js: -------------------------------------------------------------------------------- 1 | import { quickSort } from '../../../../src/js/index'; 2 | import { testSortAlgorithm } from './sort-algorithm-tests'; 3 | 4 | testSortAlgorithm(quickSort, 'Quick Sort'); 5 | 6 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/js/algorithms/sorting/shell-sort.spec.js: -------------------------------------------------------------------------------- 1 | import { shellSort } from '../../../../src/js/index'; 2 | import { testSortAlgorithm } from './sort-algorithm-tests'; 3 | 4 | testSortAlgorithm(shellSort, 'Shell Sort'); 5 | 6 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/ts/algorithms/sorting/heap-sort.spec.ts: -------------------------------------------------------------------------------- 1 | import { heapSort } from '../../../../src/ts/index'; 2 | import { testSortAlgorithm } from './sort-algorithm-tests'; 3 | 4 | testSortAlgorithm(heapSort, 'Heap Sort'); 5 | 6 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/ts/algorithms/sorting/merge-sort.spec.ts: -------------------------------------------------------------------------------- 1 | import { mergeSort } from '../../../../src/ts/index'; 2 | import { testSortAlgorithm } from './sort-algorithm-tests'; 3 | 4 | testSortAlgorithm(mergeSort, 'Merge Sort'); 5 | 6 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/ts/algorithms/sorting/quicksort.spec.ts: -------------------------------------------------------------------------------- 1 | import { quickSort } from '../../../../src/ts/index'; 2 | import { testSortAlgorithm } from './sort-algorithm-tests'; 3 | 4 | testSortAlgorithm(quickSort, 'Quick Sort'); 5 | 6 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/ts/algorithms/sorting/shell-sort.spec.ts: -------------------------------------------------------------------------------- 1 | import { shellSort } from '../../../../src/ts/index'; 2 | import { testSortAlgorithm } from './sort-algorithm-tests'; 3 | 4 | testSortAlgorithm(shellSort, 'Shell Sort'); 5 | 6 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/ts/data-structures/my-obj.ts: -------------------------------------------------------------------------------- 1 | export default class MyObj { 2 | constructor(public el1: any, public el2: any) { } 3 | toString() { 4 | return `${this.el1.toString()}|${this.el2.toString()}`; 5 | } 6 | } 7 | 8 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter14/02-MinCoinChangeDP.js: -------------------------------------------------------------------------------- 1 | const { minCoinChange } = PacktDataStructuresAlgorithms; 2 | 3 | console.log(minCoinChange([1, 5, 10], 15)); // [5, 10] 4 | console.log(minCoinChange([1, 3, 4], 6)); // [3, 3] 5 | 6 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/data-structures/models/node.ts: -------------------------------------------------------------------------------- 1 | export class Node { 2 | left: Node; 3 | right: Node; 4 | 5 | constructor(public key: K) {} 6 | 7 | toString() { 8 | return `${this.key}`; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/js/algorithms/sorting/bubble-sort.spec.js: -------------------------------------------------------------------------------- 1 | import { bubbleSort } from '../../../../src/js/index'; 2 | import { testSortAlgorithm } from './sort-algorithm-tests'; 3 | 4 | testSortAlgorithm(bubbleSort, 'Bubble Sort'); 5 | 6 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/ts/algorithms/sorting/bubble-sort.spec.ts: -------------------------------------------------------------------------------- 1 | import { bubbleSort } from '../../../../src/ts/index'; 2 | import { testSortAlgorithm } from './sort-algorithm-tests'; 3 | 4 | testSortAlgorithm(bubbleSort, 'Bubble Sort'); 5 | 6 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter09/02-Factorial.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter09/03-JSCallStack.js: -------------------------------------------------------------------------------- 1 | let i = 0; 2 | function recursiveFn() { 3 | i++; 4 | recursiveFn(); 5 | } 6 | 7 | try { 8 | recursiveFn(); 9 | } catch (ex) { 10 | console.log('i = ' + i + ' error: ' + ex); 11 | } 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter09/04-Fibonacci.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/js/algorithms/search/binary-search.spec.js: -------------------------------------------------------------------------------- 1 | import { binarySearch } from '../../../../src/js/index'; 2 | import { testSearchAlgorithm } from './search-algorithms-tests'; 3 | 4 | testSearchAlgorithm(binarySearch, 'Binary Search'); 5 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter04/04-TowerOfHanoi.js: -------------------------------------------------------------------------------- 1 | const { hanoiStack } = PacktDataStructuresAlgorithms; 2 | const { hanoi } = PacktDataStructuresAlgorithms; 3 | 4 | console.log(hanoiStack(3)); 5 | 6 | console.log(hanoi(3, 'source', 'helper', 'dest')); 7 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter09/03-JSCallStack.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter14/08-LongestCommonSubsequenceRecursive.js: -------------------------------------------------------------------------------- 1 | const { lcsRecursive } = PacktDataStructuresAlgorithms; 2 | 3 | const wordX = 'acbaed'; 4 | const wordY = 'abcadf'; 5 | 6 | console.log('lcsRecursive', lcsRecursive(wordX, wordY)); 7 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/examples-screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learning-JavaScript-Data-Structures-and-Algorithms-Third-Edition/HEAD/LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/examples-screenshot.png -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/js/algorithms/sorting/insertion-sort.spec.js: -------------------------------------------------------------------------------- 1 | import { insertionSort } from '../../../../src/js/index'; 2 | import { testSortAlgorithm } from './sort-algorithm-tests'; 3 | 4 | testSortAlgorithm(insertionSort, 'Insertion Sort'); 5 | 6 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/js/algorithms/sorting/selection-sort.spec.js: -------------------------------------------------------------------------------- 1 | import { selectionSort } from '../../../../src/js/index'; 2 | import { testSortAlgorithm } from './sort-algorithm-tests'; 3 | 4 | testSortAlgorithm(selectionSort, 'Selection Sort'); 5 | 6 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/ts/algorithms/search/binary-search.spec.ts: -------------------------------------------------------------------------------- 1 | import { binarySearch } from '../../../../src/ts/index'; 2 | import { testSearchAlgorithm } from './search-algorithms-tests'; 3 | 4 | testSearchAlgorithm(binarySearch, 'Binary Search'); 5 | 6 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/ts/algorithms/sorting/insertion-sort.spec.ts: -------------------------------------------------------------------------------- 1 | import { insertionSort } from '../../../../src/ts/index'; 2 | import { testSortAlgorithm } from './sort-algorithm-tests'; 3 | 4 | testSortAlgorithm(insertionSort, 'Insertion Sort'); 5 | 6 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/ts/algorithms/sorting/selection-sort.spec.ts: -------------------------------------------------------------------------------- 1 | import { selectionSort } from '../../../../src/ts/index'; 2 | import { testSortAlgorithm } from './sort-algorithm-tests'; 3 | 4 | testSortAlgorithm(selectionSort, 'Selection Sort'); 5 | 6 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/07-Loops.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter08/05-ES2015Map.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter09/01-IntroRecursion.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/ts/algorithms/sorting/bucket-sort.spec.ts: -------------------------------------------------------------------------------- 1 | import { bucketSort } from '../../../../src/ts/index'; 2 | import { testSortAlgorithm } from './sort-algorithm-tests'; 3 | 4 | testSortAlgorithm(bucketSort, 'Bucket Sort', {reverseCompare: false}); 5 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/ts/algorithms/sorting/radix-sort.spec.ts: -------------------------------------------------------------------------------- 1 | import { radixSort } from '../../../../src/ts/index'; 2 | import { testSortAlgorithm } from './sort-algorithm-tests'; 3 | 4 | testSortAlgorithm(radixSort, 'Radix Sort', {reverseCompare: false}); 5 | 6 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/03-Operators.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/08-Functions.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/17-Book.js: -------------------------------------------------------------------------------- 1 | // @ts-check 2 | /* eslint-disable */ 3 | 4 | export default class Book { 5 | constructor(title) { 6 | this.title = title; 7 | } 8 | printTitle() { 9 | console.log(this.title); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter08/06-ES2015WeakMap.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter08/07-ES2015WeakSet.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter14/03-MinCoinChangeGreedy.js: -------------------------------------------------------------------------------- 1 | const { minCoinChangeGreedy } = PacktDataStructuresAlgorithms; 2 | 3 | console.log(minCoinChangeGreedy([1, 5, 10], 15)); // [5, 10] 4 | console.log(minCoinChangeGreedy([1, 3, 4], 6)); // [4, 1, 1] 5 | 6 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter14/11-RatInMaze.js: -------------------------------------------------------------------------------- 1 | const { ratInAMaze } = PacktDataStructuresAlgorithms; 2 | 3 | const maze = [ 4 | [1, 0, 0, 0], 5 | [1, 1, 1, 1], 6 | [0, 0, 1, 0], 7 | [0, 1, 1, 1] 8 | ]; 9 | 10 | console.log(ratInAMaze(maze)); 11 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/data-structures/models/node.js: -------------------------------------------------------------------------------- 1 | export class Node { 2 | constructor(key) { 3 | this.key = key; 4 | this.left = undefined; 5 | this.right = undefined; 6 | } 7 | toString() { 8 | return `${this.key}`; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/data-structures/models/value-pair.js: -------------------------------------------------------------------------------- 1 | export class ValuePair { 2 | constructor(key, value) { 3 | this.key = key; 4 | this.value = value; 5 | } 6 | toString() { 7 | return `[#${this.key}: ${this.value}]`; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/js/algorithms/search/sequential-search.spec.js: -------------------------------------------------------------------------------- 1 | import { sequentialSearch } from '../../../../src/js/index'; 2 | import { testSearchAlgorithm } from './search-algorithms-tests'; 3 | 4 | testSearchAlgorithm(sequentialSearch, 'Sequential Search'); 5 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/js/algorithms/sorting/bucket-sort.spec.js: -------------------------------------------------------------------------------- 1 | import { bucketSort } from '../../../../src/js/index'; 2 | import { testSortAlgorithm } from './sort-algorithm-tests'; 3 | 4 | testSortAlgorithm(bucketSort, 'Bucket Sort', { reverseCompare: false }); 5 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/js/algorithms/sorting/radix-sort.spec.js: -------------------------------------------------------------------------------- 1 | import { radixSort } from '../../../../src/js/index'; 2 | import { testSortAlgorithm } from './sort-algorithm-tests'; 3 | 4 | testSortAlgorithm(radixSort, 'Radix Sort', { reverseCompare: false }); 5 | 6 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 2 6 | end_of_line = LF 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [*.md] 12 | trim_trailing_whitespace = false -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/02-Variables.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/04-TruthyFalsy.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter03/01-Introduction.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter03/05-ArrayMethods.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter14/06-KnapSackProblemGreedy.js: -------------------------------------------------------------------------------- 1 | const { knapSackGreedy } = PacktDataStructuresAlgorithms; 2 | 3 | const values = [3,4,5]; 4 | const weights = [2,3,4]; 5 | const capacity = 5; 6 | 7 | console.log(knapSackGreedy(capacity, weights, values)); 8 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/js/algorithms/sorting/bubble-sort-improved.spec.js: -------------------------------------------------------------------------------- 1 | import { modifiedBubbleSort } from '../../../../src/js/index'; 2 | import { testSortAlgorithm } from './sort-algorithm-tests'; 3 | 4 | testSortAlgorithm(modifiedBubbleSort, 'Bubble Sort - Improved'); 5 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/js/algorithms/sorting/counting-sort.spec.js: -------------------------------------------------------------------------------- 1 | import { countingSort } from '../../../../src/js/index'; 2 | import { testSortAlgorithm } from './sort-algorithm-tests'; 3 | 4 | testSortAlgorithm(countingSort, 'Counting Sort', { reverseCompare: false }); 5 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/js/data-structures/my-obj.js: -------------------------------------------------------------------------------- 1 | export default class MyObj { 2 | constructor(el1, el2) { 3 | this.el1 = el1; 4 | this.el2 = el2; 5 | } 6 | toString() { 7 | return `${this.el1.toString()}|${this.el2.toString()}`; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/ts/algorithms/search/sequential-search.spec.ts: -------------------------------------------------------------------------------- 1 | import { sequentialSearch } from '../../../../src/ts/index'; 2 | import { testSearchAlgorithm } from './search-algorithms-tests'; 3 | 4 | testSearchAlgorithm(sequentialSearch, 'Sequential Search'); 5 | 6 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/ts/algorithms/sorting/counting-sort.spec.ts: -------------------------------------------------------------------------------- 1 | import { countingSort } from '../../../../src/ts/index'; 2 | import { testSortAlgorithm } from './sort-algorithm-tests'; 3 | 4 | testSortAlgorithm(countingSort, 'Counting Sort', {reverseCompare: false}); 5 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/01-HelloWorld.js: -------------------------------------------------------------------------------- 1 | // @ts-check 2 | /* eslint-disable */ 3 | 4 | function output(t) { 5 | document.write('

' + t + '

'); 6 | } 7 | 8 | alert('Hello, World!'); 9 | console.log('Hello, World!'); 10 | output('Hello, World!'); 11 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter03/07-Sorting.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter15/01-BigONotation.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/ts/algorithms/sorting/bubble-sort-improved.spec.ts: -------------------------------------------------------------------------------- 1 | import { modifiedBubbleSort } from '../../../../src/ts/index'; 2 | import { testSortAlgorithm } from './sort-algorithm-tests'; 3 | 4 | testSortAlgorithm(modifiedBubbleSort, 'Bubble Sort - Improved'); 5 | 6 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/05-EqualsOperators.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter03/08-Searching.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/09-ObjectOrientedJS.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/17-ES2015-Modules-node/17-CalcArea.mjs: -------------------------------------------------------------------------------- 1 | export const circleArea = r => 3.14 * (r ** 2); 2 | 3 | export const squareArea = s => s * s; 4 | 5 | // export { circleArea, squareArea }; // {1} 6 | export { circleArea as circle, squareArea as square }; 7 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter03/09-TypedArrays.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter14/04-KnapsackProblemDP.js: -------------------------------------------------------------------------------- 1 | const { knapSack } = PacktDataStructuresAlgorithms; 2 | 3 | const values = [3,4,5]; 4 | const weights = [2,3,4]; 5 | const capacity = 5; 6 | const n = values.length; 7 | 8 | console.log(knapSack(capacity, weights, values, n)); 9 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | All notable changes to the "javascript-datastructures-algorithms" third edition source code bundle will be documented in this file. 3 | 4 | Based on [Keep a Changelog](http://keepachangelog.com/). 5 | 6 | ## [Unreleased] 7 | - Initial release 8 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/06-ConditionalStatements.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter03/03-AddingRemovingElements.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter03/06-ES2015Methods.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/data-structures/models/value-pair-lazy.ts: -------------------------------------------------------------------------------- 1 | import { ValuePair } from './value-pair'; 2 | 3 | export class ValuePairLazy extends ValuePair { 4 | constructor(public key: K, public value: V, public isDeleted = false) { 5 | super(key, value); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/js/algorithms/search/binary-search-recursive.spec.js: -------------------------------------------------------------------------------- 1 | import { binarySearchRecursive } from '../../../../src/js/index'; 2 | import { testSearchAlgorithm } from './search-algorithms-tests'; 3 | 4 | testSearchAlgorithm(binarySearchRecursive, 'Binary Search Recursive'); 5 | 6 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/ts/algorithms/search/binary-search-recursive.spec.ts: -------------------------------------------------------------------------------- 1 | import { binarySearchRecursive } from '../../../../src/ts/index'; 2 | import { testSearchAlgorithm } from './search-algorithms-tests'; 3 | 4 | testSearchAlgorithm(binarySearchRecursive, 'Binary Search Recursive'); 5 | 6 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/16-ES2015-ES6-Classes.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter03/02-CreatingAndInitialingArrays.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter11/03-HeapSort.js: -------------------------------------------------------------------------------- 1 | const { heapSort } = PacktDataStructuresAlgorithms; 2 | 3 | console.log('********** Heap Sort **********'); 4 | const array = [7, 6, 3, 5, 4, 1, 2]; 5 | 6 | console.log('Before sorting: ', array); 7 | console.log('After sorting: ', heapSort(array)); 8 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter03/04-TwoDimensionalMultiDimensional.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/js/algorithms/search/interpolation-search.spec.js: -------------------------------------------------------------------------------- 1 | import { interpolationSearch } from '../../../../src/js/index'; 2 | import { testSearchAlgorithm } from './search-algorithms-tests'; 3 | 4 | testSearchAlgorithm(interpolationSearch, 'Interpolation Search', { customEquals: false }); 5 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/10-ES2015-ES6-letconst.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/17-ES2015-Modules-node/17-ES2015-ES6-Modules.mjs: -------------------------------------------------------------------------------- 1 | import * as area from './17-CalcArea.mjs'; 2 | import Book from './17-Book.mjs'; 3 | 4 | console.log(area.circle(2)); 5 | console.log(area.square(2)); 6 | 7 | const myBook = new Book('some title'); 8 | myBook.printTitle(); 9 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter07/01-Set.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter12/02-BFS.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter12/03-DFS.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter12/06-Prim.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter14/05-KnapSackProblemRecursive.js: -------------------------------------------------------------------------------- 1 | const { knapSackRecursive } = PacktDataStructuresAlgorithms; 2 | 3 | const values = [3,4,5]; 4 | const weights = [2,3,4]; 5 | const capacity = 5; 6 | const n = values.length; 7 | 8 | console.log(knapSackRecursive(capacity, weights, values, n)); 9 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/ts/algorithms/search/interpolation-search.spec.ts: -------------------------------------------------------------------------------- 1 | import { interpolationSearch } from '../../../../src/ts/index'; 2 | import { testSearchAlgorithm } from './search-algorithms-tests'; 3 | 4 | testSearchAlgorithm(interpolationSearch, 'Interpolation Search', { customEquals: false }); 5 | 6 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/17-CalcArea.js: -------------------------------------------------------------------------------- 1 | // @ts-check 2 | /* eslint-disable */ 3 | 4 | export const circleArea = r => 3.14 * (r ** 2); 5 | 6 | export const squareArea = s => s * s; 7 | 8 | // export { circleArea, squareArea }; // {1} 9 | export { circleArea as circle, squareArea as square }; 10 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter05/01-Queue.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter05/02-Deque.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/jsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "ES5", 4 | "module": "commonjs" , 5 | "outDir": "./dist/js", 6 | "removeComments": true, 7 | "strict": true 8 | }, 9 | "exclude": ["node_modules", "dist"], 10 | "include": ["src/js/**/*","html/**"] 11 | } 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/13-ES2015-ES6-ArrowFunctions.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/14-ES2015-ES6-ParameterHandling.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter05/03-HotPotato.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter07/03-ES2015Set.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter08/02-HashTable.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter11/03-HeapSort.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter12/04-Dijkstra.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter12/07-Kruskal.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/11-ES2015-ES6-variableScope.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/12-ES2015-ES6-StringTemplates.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter04/04-TowerOfHanoi.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter06/01-LinkedList.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter12/01-UsingGraphs.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter13/01-BubbleSort.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter13/01-BucketSort.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter13/01-CountingSort.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter13/01-InsertionSort.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter13/01-MergeSort.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter13/01-QuickSort.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter13/01-RadixSort.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter13/01-SelectionSort.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter14/11-RatInMaze.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/18-ES2016-ES7-ExponentiationOperator.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter04/02-BalancedSymbols.js: -------------------------------------------------------------------------------- 1 | const { parenthesesChecker } = PacktDataStructuresAlgorithms; 2 | 3 | console.log('{([])}', parenthesesChecker('{([])}')); // true 4 | console.log('{{([][])}()}', parenthesesChecker('{{([][])}()}')); // true 5 | console.log('[{()]', parenthesesChecker('[{()]')); // false 6 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter07/02-SetOperations.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter08/01-Dictionaries.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter11/01-UsingMinHeap.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter11/02-UsingMaxHeap.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter12/05-Floyd-Warshall.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter14/07-LongestCommonSubsequenceDP.js: -------------------------------------------------------------------------------- 1 | const { lcs } = PacktDataStructuresAlgorithms; 2 | const { lcsPrint } = PacktDataStructuresAlgorithms; 3 | 4 | const wordX = 'acbaed'; 5 | const wordY = 'abcadf'; 6 | 7 | console.log('lcs', lcs(wordX, wordY)); 8 | console.log('lcsPrint', lcsPrint(wordX, wordY)); 9 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter14/12-SudokuSolver.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter06/02-DoublyLinkedList.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter06/04-SortedLinkedList.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter06/05-StackLinkedList.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter14/01-DC-BinarySearch.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter14/02-MinCoinChangeDP.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/15-ES2015-ES6-EnhancedObjectProperties.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter04/03-DecimalToBinary.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter05/04-PalindromeChecker.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter06/03-CircularLinkedList.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter14/04-KnapsackProblemDP.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter14/03-MinCoinChangeGreedy.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/algorithms/shuffle/fisher–yates.ts: -------------------------------------------------------------------------------- 1 | import { swap } from '../../util'; 2 | 3 | export function shuffle(array: T[]) { 4 | for (let i = array.length - 1; i > 0; i--) { 5 | const randomIndex = Math.floor(Math.random() * (i + 1)); 6 | swap(array, i, randomIndex); 7 | } 8 | 9 | return array; 10 | } 11 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter04/02-BalancedSymbols.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter14/06-KnapSackProblemGreedy.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/17-ES2015-ES6-Modules-node.js: -------------------------------------------------------------------------------- 1 | // @ts-check 2 | /* eslint-disable */ 3 | 4 | const area = require('./lib/17-CalcArea'); 5 | const Book = require('./lib/17-Book'); 6 | 7 | console.log(area.circle(2)); 8 | console.log(area.square(2)); 9 | 10 | const myBook = new Book('some title'); 11 | myBook.printTitle(); 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter08/04-HashCollisionLinearProbing.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter14/05-KnapSackProblemRecursive.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/data-structures/models/value-pair-lazy.js: -------------------------------------------------------------------------------- 1 | import { ValuePair } from './value-pair'; 2 | 3 | export class ValuePairLazy extends ValuePair { 4 | constructor(key, value, isDeleted = false) { 5 | super(key, value); 6 | this.key = key; 7 | this.value = value; 8 | this.isDeleted = isDeleted; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter08/03-HashCollisionSeparateChaining.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter13/01-BubbleSort.js: -------------------------------------------------------------------------------- 1 | const { bubbleSort } = PacktDataStructuresAlgorithms; 2 | 3 | function createNonSortedArray(){ 4 | var array = []; 5 | for (let i = 5; i > 0; i--){ 6 | array.push(i); 7 | } 8 | return array; 9 | } 10 | 11 | const array = bubbleSort(createNonSortedArray()); 12 | console.log(array); 13 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter13/01-BucketSort.js: -------------------------------------------------------------------------------- 1 | const { bubbleSort } = PacktDataStructuresAlgorithms; 2 | 3 | function createNonSortedArray(){ 4 | var array = []; 5 | for (let i = 5; i > 0; i--){ 6 | array.push(i); 7 | } 8 | return array; 9 | } 10 | 11 | const array = bubbleSort(createNonSortedArray()); 12 | console.log(array); 13 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter13/01-CountingSort.js: -------------------------------------------------------------------------------- 1 | const { bubbleSort } = PacktDataStructuresAlgorithms; 2 | 3 | function createNonSortedArray(){ 4 | var array = []; 5 | for (let i = 5; i > 0; i--){ 6 | array.push(i); 7 | } 8 | return array; 9 | } 10 | 11 | const array = bubbleSort(createNonSortedArray()); 12 | console.log(array); 13 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter13/01-InsertionSort.js: -------------------------------------------------------------------------------- 1 | const { bubbleSort } = PacktDataStructuresAlgorithms; 2 | 3 | function createNonSortedArray(){ 4 | var array = []; 5 | for (let i = 5; i > 0; i--){ 6 | array.push(i); 7 | } 8 | return array; 9 | } 10 | 11 | const array = bubbleSort(createNonSortedArray()); 12 | console.log(array); 13 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter13/01-MergeSort.js: -------------------------------------------------------------------------------- 1 | const { bubbleSort } = PacktDataStructuresAlgorithms; 2 | 3 | function createNonSortedArray(){ 4 | var array = []; 5 | for (let i = 5; i > 0; i--){ 6 | array.push(i); 7 | } 8 | return array; 9 | } 10 | 11 | const array = bubbleSort(createNonSortedArray()); 12 | console.log(array); 13 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter13/01-QuickSort.js: -------------------------------------------------------------------------------- 1 | const { bubbleSort } = PacktDataStructuresAlgorithms; 2 | 3 | function createNonSortedArray(){ 4 | var array = []; 5 | for (let i = 5; i > 0; i--){ 6 | array.push(i); 7 | } 8 | return array; 9 | } 10 | 11 | const array = bubbleSort(createNonSortedArray()); 12 | console.log(array); 13 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter13/01-RadixSort.js: -------------------------------------------------------------------------------- 1 | const { bubbleSort } = PacktDataStructuresAlgorithms; 2 | 3 | function createNonSortedArray(){ 4 | var array = []; 5 | for (let i = 5; i > 0; i--){ 6 | array.push(i); 7 | } 8 | return array; 9 | } 10 | 11 | const array = bubbleSort(createNonSortedArray()); 12 | console.log(array); 13 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter13/01-SelectionSort.js: -------------------------------------------------------------------------------- 1 | const { bubbleSort } = PacktDataStructuresAlgorithms; 2 | 3 | function createNonSortedArray(){ 4 | var array = []; 5 | for (let i = 5; i > 0; i--){ 6 | array.push(i); 7 | } 8 | return array; 9 | } 10 | 11 | const array = bubbleSort(createNonSortedArray()); 12 | console.log(array); 13 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter14/07-LongestCommonSubsequenceDP.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter14/09-MatrixChainMultiplicationDP.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter14/13-IntroFunctionalProgramming.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/data-structures/models/linked-list-models.js: -------------------------------------------------------------------------------- 1 | export class Node { 2 | constructor(element, next) { 3 | this.element = element; 4 | this.next = next; 5 | } 6 | } 7 | export class DoublyNode extends Node { 8 | constructor(element, next, prev) { 9 | super(element, next); 10 | this.prev = prev; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/17-ES2015-ES6-Modules.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter14/08-LongestCommonSubsequenceRecursive.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter14/10-MatrixChainMultiplicationRecursive.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/algorithms/search/sequential-search.js: -------------------------------------------------------------------------------- 1 | import { defaultEquals, DOES_NOT_EXIST } from '../../util'; 2 | 3 | export function sequentialSearch(array, value, equalsFn = defaultEquals) { 4 | for (let i = 0; i < array.length; i++) { 5 | if (equalsFn(value, array[i])) { 6 | return i; 7 | } 8 | } 9 | return DOES_NOT_EXIST; 10 | } 11 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/algorithms/greedy/min-coin-change.js: -------------------------------------------------------------------------------- 1 | export function minCoinChange(coins, amount) { 2 | const change = []; 3 | let total = 0; 4 | for (let i = coins.length; i >= 0; i--) { 5 | const coin = coins[i]; 6 | while (total + coin <= amount) { 7 | change.push(coin); 8 | total += coin; 9 | } 10 | } 11 | return change; 12 | } 13 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/algorithms/shuffle/fisher–yates.js: -------------------------------------------------------------------------------- 1 | import { swap } from '../../util'; 2 | 3 | export function shuffle(array) { 4 | let currentIndex = array.length; 5 | while (currentIndex !== 0) { 6 | const randomIndex = Math.floor(Math.random() * currentIndex); 7 | currentIndex--; 8 | swap(array, currentIndex, randomIndex); 9 | } 10 | return array; 11 | } 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/data-structures/models/linked-list-models.ts: -------------------------------------------------------------------------------- 1 | export class Node { 2 | constructor(public element: T, public next?: Node) {} 3 | } 4 | 5 | export class DoublyNode extends Node { 6 | constructor( 7 | public element: T, 8 | public next?: DoublyNode, 9 | public prev?: DoublyNode 10 | ) { 11 | super(element, next); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/algorithms/greedy/min-coin-change.ts: -------------------------------------------------------------------------------- 1 | export function minCoinChange(coins: number[], amount: number) { 2 | const change: number[] = []; 3 | let total = 0; 4 | for (let i = coins.length; i >= 0; i--) { 5 | const coin = coins[i]; 6 | while (total + coin <= amount) { 7 | change.push(coin); 8 | total += coin; 9 | } 10 | } 11 | return change; 12 | } 13 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/01-HelloWorld.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 13 | 14 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/18-ES2016-ES7-ExponentiationOperator.js: -------------------------------------------------------------------------------- 1 | // @ts-check 2 | /* eslint-disable */ 3 | 4 | //* ****** EcmaScript 2016 (ES7): Exponentiation operator (https://goo.gl/Z6dCFB) 5 | let r = 2; 6 | const area = 3.14 * r * r; 7 | const area2 = 3.14 * Math.pow(r, 2); 8 | const area3 = 3.14 * r ** 2; 9 | 10 | console.log(area); 11 | console.log(area2); 12 | console.log(area3); 13 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/algorithms/search/sequential-search.ts: -------------------------------------------------------------------------------- 1 | import { defaultEquals, DOES_NOT_EXIST, IEqualsFunction } from '../../util'; 2 | 3 | export function sequentialSearch(array: T[], value: T, equalsFn: IEqualsFunction = defaultEquals) { 4 | for (let i = 0; i < array.length; i++) { 5 | if (equalsFn(value, array[i])) { 6 | return i; 7 | } 8 | } 9 | return DOES_NOT_EXIST; 10 | } 11 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/js/algorithms/greedy/longest-common-subsequence.spec.js: -------------------------------------------------------------------------------- 1 | import 'mocha'; 2 | import { expect } from 'chai'; 3 | import { lcsRecursive } from '../../../../src/js/index'; 4 | 5 | describe('LCS Greedy', () => { 6 | 7 | it('works with Greedy approach', () => { 8 | const wordX = 'acbaed'; 9 | const wordY = 'abcadf'; 10 | 11 | expect(lcsRecursive(wordX, wordY)).to.equal(4); 12 | }); 13 | }); 14 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/js/algorithms/greedy/matrix-chain-multiplication.spec.js: -------------------------------------------------------------------------------- 1 | import 'mocha'; 2 | import { expect } from 'chai'; 3 | import { matrixChainOrderGreedy } from '../../../../src/js/index'; 4 | 5 | describe('Matrix Chain Multiplication', () => { 6 | 7 | it('works with DP approach', () => { 8 | const p = [10, 100, 5, 50, 1]; 9 | 10 | expect(matrixChainOrderGreedy(p)).to.equal(1750); 11 | }); 12 | }); 13 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/ts/algorithms/greedy/longest-common-subsequence.spec.ts: -------------------------------------------------------------------------------- 1 | import 'mocha'; 2 | import { expect } from 'chai'; 3 | import { lcsRecursive } from '../../../../src/ts/index'; 4 | 5 | describe('LCS Greedy', () => { 6 | 7 | it('works with Greedy approach', () => { 8 | const wordX = 'acbaed'; 9 | const wordY = 'abcadf'; 10 | 11 | expect(lcsRecursive(wordX, wordY)).to.equal(4); 12 | }); 13 | }); 14 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/ts/algorithms/greedy/matrix-chain-multiplication.spec.ts: -------------------------------------------------------------------------------- 1 | import 'mocha'; 2 | import { expect } from 'chai'; 3 | import { matrixChainOrderGreedy } from '../../../../src/ts/index'; 4 | 5 | describe('Matrix Chain Multiplication', () => { 6 | 7 | it('works with DP approach', () => { 8 | const p = [10, 100, 5, 50, 1]; 9 | 10 | expect(matrixChainOrderGreedy(p)).to.equal(1750); 11 | }); 12 | }); 13 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/algorithms/greedy/longest-common-subsequence.js: -------------------------------------------------------------------------------- 1 | export function lcs(wordX, wordY, m = wordX.length, n = wordY.length) { 2 | if (m === 0 || n === 0) { 3 | return 0; 4 | } 5 | if (wordX[m - 1] === wordY[n - 1]) { 6 | return 1 + lcs(wordX, wordY, m - 1, n - 1); 7 | } 8 | const a = lcs(wordX, wordY, m, n - 1); 9 | const b = lcs(wordX, wordY, m - 1, n); 10 | return a > b ? a : b; 11 | } 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/js/algorithms/dynamic-programming/longest-common-subsequence.spec.js: -------------------------------------------------------------------------------- 1 | import 'mocha'; 2 | import { expect } from 'chai'; 3 | import { lcs } from '../../../../src/js/index'; 4 | 5 | describe('LCS Dynamic Programming', () => { 6 | 7 | it('works with DP approach', () => { 8 | const wordX = 'acbaed'; 9 | const wordY = 'abcadf'; 10 | 11 | expect(lcs(wordX, wordY)).to.equal(4); 12 | }); 13 | }); 14 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/js/algorithms/dynamic-programming/matrix-chain-multiplication.spec.js: -------------------------------------------------------------------------------- 1 | import 'mocha'; 2 | import { expect } from 'chai'; 3 | import { matrixChainOrder } from '../../../../src/js/index'; 4 | 5 | describe('Matrix Chain Multiplication', () => { 6 | 7 | it('works with DP approach', () => { 8 | const p = [10, 100, 5, 50, 1]; 9 | 10 | expect(matrixChainOrder(p)).to.equal(1750); 11 | }); 12 | }); 13 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/ts/algorithms/dynamic-programming/longest-common-subsequence.spec.ts: -------------------------------------------------------------------------------- 1 | import 'mocha'; 2 | import { expect } from 'chai'; 3 | import { lcs } from '../../../../src/ts/index'; 4 | 5 | describe('LCS Dynamic Programming', () => { 6 | 7 | it('works with DP approach', () => { 8 | const wordX = 'acbaed'; 9 | const wordY = 'abcadf'; 10 | 11 | expect(lcs(wordX, wordY)).to.equal(4); 12 | }); 13 | }); 14 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/ts/algorithms/dynamic-programming/matrix-chain-multiplication.spec.ts: -------------------------------------------------------------------------------- 1 | import 'mocha'; 2 | import { expect } from 'chai'; 3 | import { matrixChainOrder } from '../../../../src/ts/index'; 4 | 5 | describe('Matrix Chain Multiplication', () => { 6 | 7 | it('works with DP approach', () => { 8 | const p = [10, 100, 5, 50, 1]; 9 | 10 | expect(matrixChainOrder(p)).to.equal(1750); 11 | }); 12 | }); 13 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter08/07-ES2015WeakSet.js: -------------------------------------------------------------------------------- 1 | var set = new WeakSet(); 2 | 3 | const ob1 = { name: 'Gandalf' }; 4 | const ob2 = { name: 'John' }; 5 | const ob3 = { name: 'Tyrion' }; 6 | 7 | set.add(ob1); 8 | set.add(ob2); 9 | set.add(ob3); 10 | 11 | console.log(set.has(ob1)); // true 12 | console.log(set.has(ob2)); // true 13 | console.log(set.has(ob3)); // true 14 | 15 | set.delete(ob2); 16 | console.log(set.has(ob2)); // false 17 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter15/bigOChart/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/others/factorial.js: -------------------------------------------------------------------------------- 1 | export function factorialIterative(number) { 2 | if (number < 0) { 3 | return undefined; 4 | } 5 | let total = 1; 6 | for (let n = number; n > 1; n--) { 7 | total *= n; 8 | } 9 | return total; 10 | } 11 | 12 | export function factorial(n) { 13 | if (n < 0) { 14 | return undefined; 15 | } 16 | if (n === 1 || n === 0) { 17 | return 1; 18 | } 19 | return n * factorial(n - 1); 20 | } 21 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/js/algorithms/greedy/knapsack.spec.js: -------------------------------------------------------------------------------- 1 | import 'mocha'; 2 | import { expect } from 'chai'; 3 | import { knapSackGreedy } from '../../../../src/js/index'; 4 | 5 | describe('KnapSack Greedy', () => { 6 | 7 | it('works with greedy approach', () => { 8 | const values = [3, 4, 5]; 9 | const weights = [2, 3, 4]; 10 | const capacity = 5; 11 | 12 | expect(knapSackGreedy(capacity, weights, values)).to.equal(7); 13 | }); 14 | }); 15 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/js/algorithms/greedy/min-coin-change.spec.js: -------------------------------------------------------------------------------- 1 | import 'mocha'; 2 | import { expect } from 'chai'; 3 | import { minCoinChangeGreedy } from '../../../../src/js/index'; 4 | 5 | describe('Min Coin Change Greedy', () => { 6 | 7 | it('works with greedy approach', () => { 8 | expect(minCoinChangeGreedy([1, 5, 10], 15)).to.deep.equal([10, 5]); 9 | expect(minCoinChangeGreedy([1, 3, 4], 6)).to.deep.equal([4, 1, 1]); 10 | }); 11 | 12 | }); 13 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/ts/algorithms/greedy/knapsack.spec.ts: -------------------------------------------------------------------------------- 1 | import 'mocha'; 2 | import { expect } from 'chai'; 3 | import { knapSackGreedy } from '../../../../src/ts/index'; 4 | 5 | describe('KnapSack Greedy', () => { 6 | 7 | it('works with greedy approach', () => { 8 | const values = [3, 4, 5]; 9 | const weights = [2, 3, 4]; 10 | const capacity = 5; 11 | 12 | expect(knapSackGreedy(capacity, weights, values)).to.equal(7); 13 | }); 14 | }); 15 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/ts/algorithms/greedy/min-coin-change.spec.ts: -------------------------------------------------------------------------------- 1 | import 'mocha'; 2 | import { expect } from 'chai'; 3 | import { minCoinChangeGreedy } from '../../../../src/ts/index'; 4 | 5 | describe('Min Coin Change Greedy', () => { 6 | 7 | it('works with greedy approach', () => { 8 | expect(minCoinChangeGreedy([1, 5, 10], 15)).to.deep.equal([10, 5]); 9 | expect(minCoinChangeGreedy([1, 3, 4], 6)).to.deep.equal([4, 1, 1]); 10 | }); 11 | 12 | }); 13 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter04/01-Stack.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/algorithms/greedy/matrix-chain-multiplication.js: -------------------------------------------------------------------------------- 1 | export function matrixChainOrder(p, i = 1, j = p.length - 1) { 2 | if (i === j) { 3 | return 0; 4 | } 5 | let min = Number.MAX_SAFE_INTEGER; 6 | for (let k = i; k < j; k++) { 7 | const count = 8 | matrixChainOrder(p, i, k) + matrixChainOrder(p, k + 1, j) + ((p[i - 1] * p[k]) * p[j]); 9 | if (count < min) { 10 | min = count; 11 | } 12 | } 13 | return min; 14 | } 15 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/algorithms/math/find-divisors.ts: -------------------------------------------------------------------------------- 1 | export const findDivisors = (num: number) => { 2 | const divisors = []; 3 | 4 | const sqrt = Math.floor(Math.sqrt(num)); 5 | 6 | for (let i = 1; i <= sqrt; i++) { 7 | if (num % i === 0) { 8 | divisors.push(i); 9 | if (i !== sqrt) { 10 | divisors.push(Math.floor(num / i)); 11 | } 12 | } 13 | } 14 | 15 | divisors.sort((a, b) => a - b); 16 | 17 | return divisors; 18 | }; 19 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/algorithms/math/sieve-eratosthenes.ts: -------------------------------------------------------------------------------- 1 | export const sieveOfEratosthenes = (n: number) => { 2 | 3 | const prime: boolean[] = []; 4 | 5 | for (let i = 0; i < n; i++) { 6 | prime[i] = true; 7 | } 8 | 9 | for (let p = 2; p * p <= n; p++) { 10 | if (prime[p]) { 11 | for (let i = p * 2; i <= n; i += p) { 12 | prime[i] = false; 13 | } 14 | } 15 | } 16 | 17 | return prime.filter(num => num === true); 18 | }; 19 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/algorithms/greedy/longest-common-subsequence.ts: -------------------------------------------------------------------------------- 1 | export function lcs(wordX: string, wordY: string, m = wordX.length, n = wordY.length): number { 2 | if (m === 0 || n === 0) { 3 | return 0; 4 | } 5 | 6 | if (wordX[m - 1] === wordY[n - 1]) { 7 | return 1 + lcs(wordX, wordY, m - 1, n - 1); 8 | } else { 9 | const a = lcs(wordX, wordY, m, n - 1); 10 | const b = lcs(wordX, wordY, m - 1, n); 11 | return a > b ? a : b; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter05/04-PalindromeChecker.js: -------------------------------------------------------------------------------- 1 | const { palindromeChecker } = PacktDataStructuresAlgorithms; 2 | 3 | console.log('a', palindromeChecker('a')); 4 | console.log('aa', palindromeChecker('aa')); 5 | console.log('kayak', palindromeChecker('kayak')); 6 | console.log('level', palindromeChecker('level')); 7 | console.log('Was it a car or a cat I saw', palindromeChecker('Was it a car or a cat I saw')); 8 | console.log('Step on no pets', palindromeChecker('Step on no pets')); 9 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/algorithms/dynamic-programing/knapsack-recursive.js: -------------------------------------------------------------------------------- 1 | export function knapSack(capacity, weights, values, n) { 2 | if (n === 0 || capacity === 0) { 3 | return 0; 4 | } 5 | if (weights[n - 1] > capacity) { 6 | return knapSack(capacity, weights, values, n - 1); 7 | } 8 | const a = values[n - 1] + knapSack(capacity - weights[n - 1], weights, values, n - 1); 9 | const b = knapSack(capacity, weights, values, n - 1); 10 | return a > b ? a : b; 11 | } 12 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/others/factorial.ts: -------------------------------------------------------------------------------- 1 | export function factorialIterative(number: number) { 2 | if (number < 0) { 3 | return undefined; 4 | } 5 | let total = 1; 6 | for (let n = number; n > 1; n--) { 7 | total *= n; 8 | } 9 | return total; 10 | } 11 | 12 | export function factorial(n: number): number { 13 | if (n < 0) { 14 | return undefined; 15 | } 16 | if (n === 1 || n === 0) { 17 | return 1; 18 | } 19 | return n * factorial(n - 1); 20 | } 21 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/js/algorithms/dynamic-programming/longest-common-subsequence-print.spec.js: -------------------------------------------------------------------------------- 1 | import 'mocha'; 2 | import { expect } from 'chai'; 3 | import { lcsPrint } from '../../../../src/js/index'; 4 | 5 | describe('LCS Dynamic Programming with print solution', () => { 6 | 7 | it('works with DP approach with print solution', () => { 8 | const wordX = 'acbaed'; 9 | const wordY = 'abcadf'; 10 | 11 | expect(lcsPrint(wordX, wordY)).to.equal('acad'); 12 | }); 13 | }); 14 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/ts/algorithms/dynamic-programming/longest-common-subsequence-print.spec.ts: -------------------------------------------------------------------------------- 1 | import 'mocha'; 2 | import { expect } from 'chai'; 3 | import { lcsPrint } from '../../../../src/ts/index'; 4 | 5 | describe('LCS Dynamic Programming with print solution', () => { 6 | 7 | it('works with DP approach with print solution', () => { 8 | const wordX = 'acbaed'; 9 | const wordY = 'abcadf'; 10 | 11 | expect(lcsPrint(wordX, wordY)).to.equal('acad'); 12 | }); 13 | }); 14 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/12-ES2015-ES6-StringTemplates.js: -------------------------------------------------------------------------------- 1 | // @ts-check 2 | /* eslint-disable */ 3 | 4 | //* ****** EcmaScript 2015 (ES6): Template literals (https://goo.gl/4N36CS) 5 | const book = { 6 | name: 'Learning JavaScript DataStructures and Algorithms' 7 | }; 8 | 9 | console.log('You are reading ' + book.name + '.,\n and this is a new line\n and so is this.'); 10 | 11 | console.log(`You are reading ${book.name}., 12 | and this is a new line 13 | and so is this.`); 14 | 15 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/js/algorithms/dynamic-programming/knapsack.spec.js: -------------------------------------------------------------------------------- 1 | import 'mocha'; 2 | import { expect } from 'chai'; 3 | import { knapSack } from '../../../../src/js/index'; 4 | 5 | describe('KnapSack Dynamic Programming', () => { 6 | 7 | it('works with DP approach', () => { 8 | const values = [3, 4, 5]; 9 | const weights = [2, 3, 4]; 10 | const capacity = 5; 11 | const n = values.length; 12 | 13 | expect(knapSack(capacity, weights, values, n)).to.equal(7); 14 | }); 15 | }); 16 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/ts/algorithms/dynamic-programming/knapsack.spec.ts: -------------------------------------------------------------------------------- 1 | import 'mocha'; 2 | import { expect } from 'chai'; 3 | import { knapSack } from '../../../../src/ts/index'; 4 | 5 | describe('KnapSack Dynamic Programming', () => { 6 | 7 | it('works with DP approach', () => { 8 | const values = [3, 4, 5]; 9 | const weights = [2, 3, 4]; 10 | const capacity = 5; 11 | const n = values.length; 12 | 13 | expect(knapSack(capacity, weights, values, n)).to.equal(7); 14 | }); 15 | }); 16 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter12/04-Dijkstra.js: -------------------------------------------------------------------------------- 1 | const { dijkstra } = PacktDataStructuresAlgorithms; 2 | 3 | const graph = [ 4 | [0, 2, 4, 0, 0, 0], 5 | [0, 0, 2, 4, 2, 0], 6 | [0, 0, 0, 0, 3, 0], 7 | [0, 0, 0, 0, 0, 2], 8 | [0, 0, 0, 3, 0, 2], 9 | [0, 0, 0, 0, 0, 0] 10 | ]; 11 | 12 | console.log("********* Dijkstra's Algorithm - Shortest Path ***********"); 13 | 14 | var dist = dijkstra(graph, 0); 15 | 16 | for (i = 0; i < dist.length; i++){ 17 | console.log(i + '\t\t' + dist[i]); 18 | } 19 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter14/12-SudokuSolver.js: -------------------------------------------------------------------------------- 1 | const { sudokuSolver } = PacktDataStructuresAlgorithms; 2 | 3 | const sudokuGrid = [ 4 | [5, 3, 0, 0, 7, 0, 0, 0, 0], 5 | [6, 0, 0, 1, 9, 5, 0, 0, 0], 6 | [0, 9, 8, 0, 0, 0, 0, 6, 0], 7 | [8, 0, 0, 0, 6, 0, 0, 0, 3], 8 | [4, 0, 0, 8, 0, 3, 0, 0, 1], 9 | [7, 0, 0, 0, 2, 0, 0, 0, 6], 10 | [0, 6, 0, 0, 0, 0, 2, 8, 0], 11 | [0, 0, 0, 4, 1, 9, 0, 0, 5], 12 | [0, 0, 0, 0, 8, 0, 0, 7, 9] 13 | ]; 14 | 15 | console.log(sudokuSolver(sudokuGrid)); 16 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/algorithms/greedy/matrix-chain-multiplication.ts: -------------------------------------------------------------------------------- 1 | export function matrixChainOrder(p: number[], i = 1, j = p.length - 1): number { 2 | if (i === j) { 3 | return 0; 4 | } 5 | 6 | let min = Number.MAX_SAFE_INTEGER; 7 | 8 | for (let k = i; k < j; k++) { 9 | const count = 10 | matrixChainOrder(p, i, k) + matrixChainOrder(p, k + 1, j) + p[i - 1] * p[k] * p[j]; 11 | 12 | if (count < min) { 13 | min = count; 14 | } 15 | } 16 | 17 | return min; 18 | } 19 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/algorithms/string/brute-force.ts: -------------------------------------------------------------------------------- 1 | const stringSearch = (text: string, pattern: string) => { 2 | const n = text.length; 3 | const m = pattern.length; 4 | 5 | if (m > n) { 6 | return -1; 7 | } 8 | 9 | for (let i = 0; i < n; i++) { 10 | let j = 0; 11 | for (j = 0; j < m && (i + j) < n; j++) { 12 | if (text.charAt(i + j) !== pattern.charAt(j)) { 13 | break; 14 | } 15 | } 16 | if (j === m) { 17 | return i; 18 | } 19 | } 20 | 21 | return -1; 22 | }; 23 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/07-Loops.js: -------------------------------------------------------------------------------- 1 | // @ts-check 2 | /* eslint-disable */ 3 | 4 | console.log('**** for example ****'); 5 | /* for - example */ 6 | for (var i = 0; i < 10; i++) { 7 | console.log(i); 8 | } 9 | 10 | console.log('**** while example ****'); 11 | /* while - example */ 12 | var i = 0; 13 | while (i < 10) { 14 | console.log(i); 15 | i++; 16 | } 17 | 18 | console.log('**** do-while example ****'); 19 | /* do-while - example */ 20 | var i = 0; 21 | do { 22 | console.log(i); 23 | i++; 24 | } while (i < 10); 25 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/08-Functions.js: -------------------------------------------------------------------------------- 1 | // @ts-check 2 | /* eslint-disable */ 3 | 4 | function sayHello() { 5 | console.log('Hello!'); 6 | } 7 | 8 | sayHello(); 9 | 10 | /* function with parameter */ 11 | function output(text) { 12 | console.log(text); 13 | } 14 | 15 | output('Hello!'); 16 | 17 | output('Hello!', 'Other text'); 18 | 19 | output(); 20 | 21 | /* function using the return statement */ 22 | function sum(num1, num2) { 23 | return num1 + num2; 24 | } 25 | 26 | var result = sum(1, 2); 27 | output(result); 28 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/algorithms/math/lcm.ts: -------------------------------------------------------------------------------- 1 | import { gcd } from './gcd'; 2 | 3 | export const lcm = (num1: number, num2: number) => { 4 | if (num1 === 0 || num2 === 0) { 5 | return 0; 6 | } 7 | num1 = Math.abs(num1); 8 | num2 = Math.abs(num2); 9 | return (num1 * num2) / gcd(num1, num2); 10 | }; 11 | 12 | export const lcmArray = (num: number[]) => { 13 | let result = num[0]; 14 | 15 | for (let i = 1; i < num.length; i++) { 16 | result = num[i] * result / gcd(num[i], result); 17 | } 18 | 19 | return result; 20 | }; 21 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/algorithms/math/greatest-difference.ts: -------------------------------------------------------------------------------- 1 | export const greatestDifference = (numbers: number[]) => { 2 | let index = 0; 3 | let largest = numbers[0]; 4 | const length = numbers.length; 5 | let number; 6 | let smallest = numbers[0]; 7 | 8 | for (index; index < length; index++) { 9 | number = numbers[index]; 10 | 11 | if (number > largest) { 12 | largest = number; 13 | } 14 | if (number < smallest) { 15 | smallest = number; 16 | } 17 | } 18 | 19 | return largest - smallest; 20 | }; 21 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/js/algorithms/dynamic-programming/knapsack-recursive.spec.js: -------------------------------------------------------------------------------- 1 | import 'mocha'; 2 | import { expect } from 'chai'; 3 | import { knapSackRecursive } from '../../../../src/js/index'; 4 | 5 | describe('KnapSack Dynamic Programming - Recursive', () => { 6 | 7 | it('works with recursive approach', () => { 8 | const values = [3, 4, 5]; 9 | const weights = [2, 3, 4]; 10 | const capacity = 5; 11 | const n = values.length; 12 | 13 | expect(knapSackRecursive(capacity, weights, values, n)).to.equal(7); 14 | }); 15 | }); 16 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/ts/algorithms/dynamic-programming/knapsack-recursive.spec.ts: -------------------------------------------------------------------------------- 1 | import 'mocha'; 2 | import { expect } from 'chai'; 3 | import { knapSackRecursive } from '../../../../src/ts/index'; 4 | 5 | describe('KnapSack Dynamic Programming - Recursive', () => { 6 | 7 | it('works with recursive approach', () => { 8 | const values = [3, 4, 5]; 9 | const weights = [2, 3, 4]; 10 | const capacity = 5; 11 | const n = values.length; 12 | 13 | expect(knapSackRecursive(capacity, weights, values, n)).to.equal(7); 14 | }); 15 | }); 16 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter07/01-Set.js: -------------------------------------------------------------------------------- 1 | const { Set } = PacktDataStructuresAlgorithms; 2 | 3 | const set = new Set(); 4 | 5 | set.add(1); 6 | console.log(set.values()); // outputs [1] 7 | console.log(set.has(1)); // outputs true 8 | console.log(set.size()); // outputs 1 9 | 10 | set.add(2); 11 | console.log(set.values()); // outputs [1, 2] 12 | console.log(set.has(2)); // true 13 | console.log(set.size()); // 2 14 | 15 | set.delete(1); 16 | console.log(set.values()); // outputs [2] 17 | 18 | set.delete(2); 19 | console.log(set.values()); // outputs [] 20 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/algorithms/math/gcd.ts: -------------------------------------------------------------------------------- 1 | export const gcd = (num1: number, num2: number): number => { 2 | if (num1 === 0 || num2 === 0) { 3 | return 0; 4 | } 5 | if (num1 === num2) { 6 | return num1; 7 | } 8 | if (num1 > num2) { 9 | return gcd(num1 - num2, num2); 10 | } 11 | return gcd(num1, num2 - num1); 12 | }; 13 | 14 | export const gcdArray = (num: number[]) => { 15 | let result = num[0]; 16 | 17 | for (let i = 1; i < num.length; i++) { 18 | result = gcd(num[i], result); 19 | } 20 | 21 | return result; 22 | }; 23 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter08/06-ES2015WeakMap.js: -------------------------------------------------------------------------------- 1 | const map = new WeakMap(); 2 | 3 | const ob1 = { name: 'Gandalf' }; 4 | const ob2 = { name: 'John' }; 5 | const ob3 = { name: 'Tyrion' }; 6 | 7 | map.set(ob1, 'gandalf@email.com'); 8 | map.set(ob2, 'johnsnow@email.com'); 9 | map.set(ob3, 'tyrion@email.com'); 10 | 11 | console.log(map.has(ob1)); // true 12 | console.log(map.has(ob2)); // true 13 | console.log(map.has(ob3)); // true 14 | 15 | console.log(map.get(ob3)); // tyrion@email.com 16 | 17 | map.delete(ob2); 18 | console.log(map.has(ob2)); // false 19 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter12/06-Prim.js: -------------------------------------------------------------------------------- 1 | const { prim } = PacktDataStructuresAlgorithms; 2 | 3 | const graph = [ 4 | [0, 2, 4, 0, 0, 0], 5 | [2, 0, 2, 4, 2, 0], 6 | [4, 2, 0, 0, 3, 0], 7 | [0, 4, 0, 0, 3, 2], 8 | [0, 2, 3, 3, 0, 2], 9 | [0, 0, 0, 2, 2, 0] 10 | ]; 11 | 12 | console.log("********* Prim's Algorithm - Minimum Spanning Tree ***********"); 13 | 14 | const parent = prim(graph); 15 | 16 | console.log('Edge Weight'); 17 | for (let i = 1; i < graph.length; i++) { 18 | console.log(parent[i] + ' - ' + i + ' ' + graph[i][parent[i]]); 19 | } 20 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/algorithms/dynamic-programing/knapsack-recursive.ts: -------------------------------------------------------------------------------- 1 | export function knapSack(capacity: number, weights: number[], values: number[], n: number): number { 2 | if (n === 0 || capacity === 0) { 3 | return 0; 4 | } 5 | 6 | if (weights[n - 1] > capacity) { 7 | return knapSack(capacity, weights, values, n - 1); 8 | } else { 9 | const a: number = values[n - 1] + knapSack(capacity - weights[n - 1], weights, values, n - 1); 10 | const b: number = knapSack(capacity, weights, values, n - 1); 11 | return a > b ? a : b; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter05/01-Queue.js: -------------------------------------------------------------------------------- 1 | const { Queue } = PacktDataStructuresAlgorithms; 2 | 3 | const queue = new Queue(); 4 | console.log(queue.isEmpty()); // outputs true 5 | queue.enqueue('John'); 6 | queue.enqueue('Jack'); 7 | console.log(queue.toString()); // John,Jack 8 | queue.enqueue('Camila'); 9 | console.log(queue.toString()); // John,Jack,Camila 10 | console.log(queue.size()); // outputs 3 11 | console.log(queue.isEmpty()); // outputs false 12 | queue.dequeue(); // remove John 13 | queue.dequeue(); // remove Jack 14 | console.log(queue.toString()); // Camila 15 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter12/07-Kruskal.js: -------------------------------------------------------------------------------- 1 | const { kruskal } = PacktDataStructuresAlgorithms; 2 | 3 | const graph = [ 4 | [0, 2, 4, 0, 0, 0], 5 | [2, 0, 2, 4, 2, 0], 6 | [4, 2, 0, 0, 3, 0], 7 | [0, 4, 0, 0, 3, 2], 8 | [0, 2, 3, 3, 0, 2], 9 | [0, 0, 0, 2, 2, 0] 10 | ]; 11 | 12 | console.log('********* Kruskal Algorithm - Minimum Spanning Tree ***********'); 13 | 14 | const parent = kruskal(graph); 15 | 16 | console.log('Edge Weight'); 17 | for (i = 1; i < graph.length; i++) { 18 | console.log(parent[i] + ' - ' + i + ' ' + graph[i][parent[i]]); 19 | } 20 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/js/algorithms/backtracking/rat-in-maze.spec.js: -------------------------------------------------------------------------------- 1 | import 'mocha'; 2 | import { expect } from 'chai'; 3 | import { ratInAMaze } from '../../../../src/js/index'; 4 | 5 | describe('Rat in a maze', () => { 6 | it('rat in a maze solver', () => { 7 | const maze = [ 8 | [1, 0, 0, 0], 9 | [1, 1, 1, 1], 10 | [0, 0, 1, 0], 11 | [0, 1, 1, 1] 12 | ]; 13 | const solution = [ 14 | [1, 0, 0, 0], 15 | [1, 1, 1, 0], 16 | [0, 0, 1, 0], 17 | [0, 0, 1, 1] 18 | ]; 19 | expect(ratInAMaze(maze)).to.deep.equal(solution); 20 | }); 21 | }); 22 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/ts/algorithms/backtracking/rat-in-maze.spec.ts: -------------------------------------------------------------------------------- 1 | import 'mocha'; 2 | import { expect } from 'chai'; 3 | import { ratInAMaze } from '../../../../src/ts/index'; 4 | 5 | describe('Rat in a maze', () => { 6 | it('rat in a maze solver', () => { 7 | const maze = [ 8 | [1, 0, 0, 0], 9 | [1, 1, 1, 1], 10 | [0, 0, 1, 0], 11 | [0, 1, 1, 1] 12 | ]; 13 | const solution = [ 14 | [1, 0, 0, 0], 15 | [1, 1, 1, 0], 16 | [0, 0, 1, 0], 17 | [0, 0, 1, 1] 18 | ]; 19 | expect(ratInAMaze(maze)).to.deep.equal(solution); 20 | }); 21 | }); 22 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/js/others/hanoi.spec.js: -------------------------------------------------------------------------------- 1 | import 'mocha'; 2 | import { expect } from 'chai'; 3 | import { hanoi, hanoiStack } from '../../../src/js/others/hanoi'; 4 | 5 | describe('Tower of Hanoi', () => { 6 | it('Hanoi', () => { 7 | for (let i = 0; i < 10; i++) { 8 | const result = hanoi(i, 'a', 'b', 'c'); 9 | expect(result.length).to.equal((2 ** i) - 1); 10 | } 11 | }); 12 | 13 | it('Hanoi with Stack', () => { 14 | for (let i = 0; i < 10; i++) { 15 | const result = hanoiStack(i); 16 | expect(result.length).to.equal((2 ** i) - 1); 17 | } 18 | }); 19 | }); 20 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/ts/others/hanoi.spec.ts: -------------------------------------------------------------------------------- 1 | import 'mocha'; 2 | import { expect } from 'chai'; 3 | import { hanoi, hanoiStack } from '../../../src/ts/others/hanoi'; 4 | 5 | describe('Tower of Hanoi', () => { 6 | 7 | it('Hanoi', () => { 8 | for (let i = 0; i < 10; i++) { 9 | const result = hanoi(i, 'a', 'b', 'c'); 10 | expect(result.length).to.equal(2 ** i - 1); 11 | } 12 | }); 13 | 14 | it('Hanoi with Stack', () => { 15 | for (let i = 0; i < 10; i++) { 16 | const result = hanoiStack(i); 17 | expect(result.length).to.equal(2 ** i - 1); 18 | } 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter04/03-DecimalToBinary.js: -------------------------------------------------------------------------------- 1 | const { decimalToBinary } = PacktDataStructuresAlgorithms; 2 | const { baseConverter } = PacktDataStructuresAlgorithms; 3 | 4 | // 233 == 11101001 5 | // 2x(10x10) + 3x(10) + 3x(1) 6 | console.log(decimalToBinary(233)); // 11101001 7 | console.log(decimalToBinary(10)); // 1010 8 | console.log(decimalToBinary(1000)); // 1111101000 9 | 10 | console.log(baseConverter(100345, 2)); // 11000011111111001 11 | console.log(baseConverter(100345, 8)); // 303771 12 | console.log(baseConverter(100345, 16)); // 187F9 13 | console.log(baseConverter(100345, 35)); // 2BW0 14 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/others/hot-potato.js: -------------------------------------------------------------------------------- 1 | import Queue from '../data-structures/queue'; 2 | 3 | export function hotPotato(elementsList, num) { 4 | const queue = new Queue(); 5 | const eliminatedList = []; 6 | 7 | for (let i = 0; i < elementsList.length; i++) { 8 | queue.enqueue(elementsList[i]); 9 | } 10 | 11 | while (queue.size() > 1) { 12 | for (let i = 0; i < num; i++) { 13 | queue.enqueue(queue.dequeue()); 14 | } 15 | eliminatedList.push(queue.dequeue()); 16 | } 17 | 18 | return { 19 | eliminated: eliminatedList, 20 | winner: queue.dequeue() 21 | }; 22 | } 23 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/others/hot-potato.ts: -------------------------------------------------------------------------------- 1 | import Queue from '../data-structures/queue'; 2 | 3 | export function hotPotato(elementsList: any[], num: number) { 4 | const queue = new Queue(); 5 | const eliminatedList = []; 6 | 7 | for (let i = 0; i < elementsList.length; i++) { 8 | queue.enqueue(elementsList[i]); 9 | } 10 | 11 | while (queue.size() > 1) { 12 | for (let i = 0; i < num; i++) { 13 | queue.enqueue(queue.dequeue()); 14 | } 15 | eliminatedList.push(queue.dequeue()); 16 | } 17 | 18 | return { 19 | eliminated: eliminatedList, 20 | winner: queue.dequeue() 21 | }; 22 | } 23 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | // See https://go.microsoft.com/fwlink/?LinkId=733558 3 | // for the documentation about the tasks.json format 4 | "version": "2.0.0", 5 | "tasks": [ 6 | { 7 | "type": "npm", 8 | "script": "go", 9 | "group": { 10 | "kind": "build", 11 | "isDefault": true 12 | } 13 | }, 14 | { 15 | "type": "npm", 16 | "script": "dev", 17 | "group": { 18 | "kind": "test", 19 | "isDefault": true 20 | } 21 | } 22 | ] 23 | } 24 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter05/03-HotPotato.js: -------------------------------------------------------------------------------- 1 | const { hotPotato } = PacktDataStructuresAlgorithms; 2 | 3 | const names = ['John', 'Jack', 'Camila', 'Ingrid', 'Carl']; 4 | const result = hotPotato(names, 7); 5 | 6 | result.eliminated.forEach(name => { 7 | console.log(`${name} was eliminated from the Hot Potato game.`); 8 | }); 9 | 10 | console.log(`The winner is: ${result.winner}`); 11 | 12 | // Camila was eliminated from the Hot Potato game. 13 | // Jack was eliminated from the Hot Potato game. 14 | // Carl was eliminated from the Hot Potato game. 15 | // Ingrid was eliminated from the Hot Potato game. 16 | // The winner is: John 17 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/js/others/hot-potato.spec.js: -------------------------------------------------------------------------------- 1 | import 'mocha'; 2 | import { expect } from 'chai'; 3 | import { hotPotato } from '../../../src/js/others/hot-potato'; 4 | 5 | describe('Hot Potato with Queue', () => { 6 | it('Hot potato game', () => { 7 | const names = ['John', 'Jack', 'Camila', 'Ingrid', 'Carl']; 8 | expect(hotPotato(names, 6).winner).to.equal('Ingrid'); 9 | expect(hotPotato(names, 7).winner).to.equal('John'); 10 | expect(hotPotato(names, 8).winner).to.equal('Jack'); 11 | expect(hotPotato(names, 9).winner).to.equal('Ingrid'); 12 | expect(hotPotato(names, 10).winner).to.equal('Carl'); 13 | }); 14 | }); 15 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/13-ES2015-ES6-ArrowFunctions.js: -------------------------------------------------------------------------------- 1 | // @ts-check 2 | /* eslint-disable */ 3 | 4 | //* ****** EcmaScript 2015 (ES6): arrow functions (https://goo.gl/nM414v) 5 | var circleAreaES5 = function circleArea(r) { 6 | var PI = 3.14; 7 | var area = PI * r * r; 8 | return area; 9 | }; 10 | console.log(circleAreaES5(2)); 11 | 12 | const circleArea = r => { // {1} 13 | const PI = 3.14; 14 | const area = PI * r * r; 15 | return area; 16 | }; 17 | console.log(circleArea(2)); 18 | 19 | const circleArea2 = r => 3.14 * r * r; 20 | console.log(circleArea2(2)); 21 | 22 | const hello = () => console.log('hello!'); 23 | hello(); 24 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/algorithms/sorting/bubble-sort.js: -------------------------------------------------------------------------------- 1 | import { Compare, defaultCompare, swap } from '../../util'; 2 | 3 | export function bubbleSort(array, compareFn = defaultCompare) { 4 | const { length } = array; 5 | for (let i = 0; i < length; i++) { 6 | // console.log('--- '); 7 | for (let j = 0; j < length - 1; j++) { 8 | // console.log('compare ' + array[j] + ' with ' + array[j + 1]); 9 | if (compareFn(array[j], array[j + 1]) === Compare.BIGGER_THAN) { 10 | // console.log('swap ' + array[j] + ' with ' + array[j + 1]); 11 | swap(array, j, j + 1); 12 | } 13 | } 14 | } 15 | return array; 16 | } 17 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/ts/others/hot-potato.spec.ts: -------------------------------------------------------------------------------- 1 | import { hotPotato } from '../../../src/ts/others/hot-potato'; 2 | import 'mocha'; 3 | import { expect } from 'chai'; 4 | 5 | describe('Hot Potato with Queue', () => { 6 | 7 | it('Hot potato game', () => { 8 | const names = ['John', 'Jack', 'Camila', 'Ingrid', 'Carl']; 9 | expect(hotPotato(names, 6).winner).to.equal('Ingrid'); 10 | expect(hotPotato(names, 7).winner).to.equal('John'); 11 | expect(hotPotato(names, 8).winner).to.equal('Jack'); 12 | expect(hotPotato(names, 9).winner).to.equal('Ingrid'); 13 | expect(hotPotato(names, 10).winner).to.equal('Carl'); 14 | }); 15 | 16 | }); 17 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter03/09-TypedArrays.js: -------------------------------------------------------------------------------- 1 | const length = 5; 2 | const int16 = new Int16Array(length); 3 | 4 | const array16 = []; 5 | array16.length = length; 6 | 7 | for (let i = 0; i < length; i++) { 8 | int16[i] = i + 1; 9 | } 10 | 11 | console.log(int16); 12 | 13 | // Int8Array(); 14 | // Uint8Array(); 15 | // Uint8ClampedArray(); 16 | // Int16Array(); 17 | // Uint16Array(); 18 | // Int32Array(); 19 | // Uint32Array(); 20 | // Float32Array(); 21 | // Float64Array(); 22 | 23 | // http://www.html5rocks.com/en/tutorials/webgl/typed_arrays/ 24 | // http://www.i-programmer.info/programming/javascript/6135-javascript-data-structures-typed-arrays.html 25 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/algorithms/sorting/insertion-sort.js: -------------------------------------------------------------------------------- 1 | import { Compare, defaultCompare } from '../../util'; 2 | 3 | export const insertionSort = (array, compareFn = defaultCompare) => { 4 | const { length } = array; 5 | let temp; 6 | for (let i = 1; i < length; i++) { 7 | let j = i; 8 | temp = array[i]; 9 | // console.log('to be inserted ' + temp); 10 | while (j > 0 && compareFn(array[j - 1], temp) === Compare.BIGGER_THAN) { 11 | // console.log('shift ' + array[j - 1]); 12 | array[j] = array[j - 1]; 13 | j--; 14 | } 15 | // console.log('insert ' + temp); 16 | array[j] = temp; 17 | } 18 | return array; 19 | }; 20 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter03/08-Searching.js: -------------------------------------------------------------------------------- 1 | // @ts-check 2 | 3 | const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; 4 | console.log('numbers', numbers); 5 | 6 | //* ** toString 7 | console.log('numbers.toString()', numbers.toString()); 8 | 9 | console.log('numbers.indexOf(10)', numbers.indexOf(10)); 10 | console.log('numbers.indexOf(100)', numbers.indexOf(100)); 11 | 12 | numbers.push(10); 13 | console.log('push 10: numbers.lastIndexOf(10)', numbers.lastIndexOf(10)); 14 | console.log('push 10: numbers.lastIndexOf(100)', numbers.lastIndexOf(100)); 15 | 16 | const numbersString = numbers.join('-'); 17 | console.log('numbers.join("-")', numbersString); 18 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/algorithms/greedy/knapsack.js: -------------------------------------------------------------------------------- 1 | export function knapSack(capacity, weights, values) { 2 | const n = values.length; 3 | let load = 0; 4 | let val = 0; 5 | for (let i = 0; i < n && load < capacity; i++) { 6 | if (weights[i] <= capacity - load) { 7 | val += values[i]; 8 | load += weights[i]; 9 | // console.log('using item ' + (i + 1) + ' for the solution'); 10 | } else { 11 | const r = (capacity - load) / weights[i]; 12 | val += r * values[i]; 13 | load += weights[i]; 14 | // console.log('using ratio of ' + r + ' for item ' + (i + 1) + ' for the solution'); 15 | } 16 | } 17 | return val; 18 | } 19 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/algorithms/sorting/bubble-sort-improved.js: -------------------------------------------------------------------------------- 1 | import { Compare, defaultCompare, swap } from '../../util'; 2 | 3 | export function modifiedBubbleSort(array, compareFn = defaultCompare) { 4 | const { length } = array; 5 | for (let i = 0; i < length; i++) { 6 | // console.log('--- '); 7 | for (let j = 0; j < length - 1 - i; j++) { 8 | // console.log('compare ' + array[j] + ' with ' + array[j + 1]); 9 | if (compareFn(array[j], array[j + 1]) === Compare.BIGGER_THAN) { 10 | // console.log('swap ' + array[j] + ' with ' + array[j + 1]); 11 | swap(array, j, j + 1); 12 | } 13 | } 14 | } 15 | return array; 16 | } 17 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/algorithms/sorting/bubble-sort.ts: -------------------------------------------------------------------------------- 1 | import { Compare, defaultCompare, swap } from '../../util'; 2 | 3 | export function bubbleSort(array: T[], compareFn = defaultCompare) { 4 | const { length } = array; 5 | 6 | for (let i = 0; i < length; i++) { 7 | // console.log('--- '); 8 | for (let j = 0; j < length - 1; j++) { 9 | // console.log('compare ' + array[j] + ' with ' + array[j + 1]); 10 | if (compareFn(array[j], array[j + 1]) === Compare.BIGGER_THAN) { 11 | // console.log('swap ' + array[j] + ' with ' + array[j + 1]); 12 | swap(array, j, j + 1); 13 | } 14 | } 15 | } 16 | 17 | return array; 18 | } 19 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/algorithms/sorting/insertion-sort.ts: -------------------------------------------------------------------------------- 1 | import { Compare, defaultCompare } from '../../util'; 2 | 3 | export const insertionSort = (array: any[], compareFn = defaultCompare) => { 4 | const { length } = array; 5 | let temp; 6 | for (let i = 1; i < length; i++) { 7 | let j = i; 8 | temp = array[i]; 9 | // console.log('to be inserted ' + temp); 10 | while (j > 0 && compareFn(array[j - 1], temp) === Compare.BIGGER_THAN) { 11 | // console.log('shift ' + array[j - 1]); 12 | array[j] = array[j - 1]; 13 | j--; 14 | } 15 | // console.log('insert ' + temp); 16 | array[j] = temp; 17 | } 18 | 19 | return array; 20 | }; 21 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/data-structures/models/red-black-node.ts: -------------------------------------------------------------------------------- 1 | import { Node } from './node'; 2 | 3 | export enum Colors { 4 | RED = 0, 5 | BLACK = 1 6 | } 7 | 8 | export class RedBlackNode extends Node { 9 | left: RedBlackNode; 10 | right: RedBlackNode; 11 | parent: RedBlackNode; 12 | color: Colors; 13 | 14 | constructor(public key: K) { 15 | super(key); 16 | this.color = Colors.RED; 17 | } 18 | 19 | isRed() { 20 | return this.color === Colors.RED; 21 | } 22 | 23 | flipColor() { 24 | if (this.color === Colors.RED) { 25 | this.color = Colors.BLACK; 26 | } else { 27 | this.color = Colors.RED; 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/algorithms/sorting/bubble-sort-improved.ts: -------------------------------------------------------------------------------- 1 | import { Compare, defaultCompare, swap } from '../../util'; 2 | 3 | export function modifiedBubbleSort(array: T[], compareFn = defaultCompare) { 4 | const { length } = array; 5 | 6 | for (let i = 0; i < length; i++) { 7 | // console.log('--- '); 8 | for (let j = 0; j < length - 1 - i; j++) { 9 | // console.log('compare ' + array[j] + ' with ' + array[j + 1]); 10 | if (compareFn(array[j], array[j + 1]) === Compare.BIGGER_THAN) { 11 | // console.log('swap ' + array[j] + ' with ' + array[j + 1]); 12 | swap(array, j, j + 1); 13 | } 14 | } 15 | } 16 | 17 | return array; 18 | } 19 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter09/02-Factorial.js: -------------------------------------------------------------------------------- 1 | // @ts-check 2 | 3 | function factorialIterative(number) { 4 | if (number < 0) { 5 | return undefined; 6 | } 7 | let total = 1; 8 | for (let n = number; n > 1; n--) { 9 | total = total * n; 10 | } 11 | return total; 12 | } 13 | 14 | console.log('factorialIterative(5): ', factorialIterative(5)); 15 | console.log('factorialIterative(3): ', factorialIterative(3)); 16 | 17 | function factorial(n) { 18 | // console.trace(); 19 | if (n === 1 || n === 0) { 20 | return 1; 21 | } 22 | return n * factorial(n - 1); 23 | } 24 | 25 | console.log('factorial(5): ', factorial(5)); 26 | console.log('factorial(3): ', factorial(3)); 27 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter12/01-UsingGraphs.js: -------------------------------------------------------------------------------- 1 | const { Graph } = PacktDataStructuresAlgorithms; 2 | 3 | const graph = new Graph(); 4 | 5 | const myVertices = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I']; 6 | 7 | for (let i = 0; i < myVertices.length; i++) { 8 | graph.addVertex(myVertices[i]); 9 | } 10 | graph.addEdge('A', 'B'); 11 | graph.addEdge('A', 'C'); 12 | graph.addEdge('A', 'D'); 13 | graph.addEdge('C', 'D'); 14 | graph.addEdge('C', 'G'); 15 | graph.addEdge('D', 'G'); 16 | graph.addEdge('D', 'H'); 17 | graph.addEdge('B', 'E'); 18 | graph.addEdge('B', 'F'); 19 | graph.addEdge('E', 'I'); 20 | 21 | console.log('********* printing graph ***********'); 22 | 23 | console.log(graph.toString()); 24 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/data-structures/stack-array.js: -------------------------------------------------------------------------------- 1 | // @ts-check 2 | 3 | export default class StackArray { 4 | constructor() { 5 | this.items = []; 6 | } 7 | push(element) { 8 | this.items.push(element); 9 | } 10 | 11 | pop() { 12 | return this.items.pop(); 13 | } 14 | 15 | peek() { 16 | return this.items[this.items.length - 1]; 17 | } 18 | 19 | isEmpty() { 20 | return this.items.length === 0; 21 | } 22 | 23 | size() { 24 | return this.items.length; 25 | } 26 | 27 | clear() { 28 | this.items = []; 29 | } 30 | 31 | toArray() { 32 | return this.items; 33 | } 34 | 35 | toString() { 36 | return this.items.toString(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/algorithms/greedy/knapsack.ts: -------------------------------------------------------------------------------- 1 | export function knapSack(capacity: number, weights: number[], values: number[]) { 2 | const n = values.length; 3 | let load = 0; 4 | let val = 0; 5 | 6 | for (let i = 0; i < n && load < capacity; i++) { 7 | if (weights[i] <= capacity - load) { 8 | val += values[i]; 9 | load += weights[i]; 10 | // console.log('using item ' + (i + 1) + ' for the solution'); 11 | } else { 12 | const r = (capacity - load) / weights[i]; 13 | val += r * values[i]; 14 | load += weights[i]; 15 | // console.log('using ratio of ' + r + ' for item ' + (i + 1) + ' for the solution'); 16 | } 17 | } 18 | return val; 19 | } 20 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/algorithms/sorting/counting-sort.js: -------------------------------------------------------------------------------- 1 | import { findMaxValue } from '../search/min-max-search'; 2 | 3 | export function countingSort(array) { 4 | if (array.length < 2) { 5 | return array; 6 | } 7 | const maxValue = findMaxValue(array); 8 | let sortedIndex = 0; 9 | const counts = new Array(maxValue + 1); 10 | array.forEach(element => { 11 | if (!counts[element]) { 12 | counts[element] = 0; 13 | } 14 | counts[element]++; 15 | }); 16 | // console.log('Frequencies: ' + counts.join()); 17 | counts.forEach((element, i) => { 18 | while (element > 0) { 19 | array[sortedIndex++] = i; 20 | element--; 21 | } 22 | }); 23 | return array; 24 | } 25 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter08/05-ES2015Map.js: -------------------------------------------------------------------------------- 1 | const map = new Map(); 2 | 3 | map.set('Gandalf', 'gandalf@email.com'); 4 | map.set('John', 'johnsnow@email.com'); 5 | map.set('Tyrion', 'tyrion@email.com'); 6 | 7 | console.log(map.has('Gandalf')); // true 8 | console.log(map.size); // 3 9 | 10 | console.log(map.keys()); // MapIterator {"Gandalf", "John", "Tyrion"} 11 | console.log(map.values()); // MapIterator {"gandalf@email.com", "johnsnow@email.com", "tyrion@email.com"} 12 | console.log(map.get('Tyrion')); // tyrion@email.com 13 | 14 | map.delete('John'); 15 | 16 | console.log(map.keys()); // MapIterator {"Gandalf", "Tyrion"} 17 | console.log(map.values()); // MapIterator {"gandalf@email.com", "tyrion@email.com"} 18 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/17-ES2015-ES6-Modules.js: -------------------------------------------------------------------------------- 1 | // @ts-check 2 | /* eslint-disable */ 3 | // import { circleArea, squareArea } from './17-CalcArea'; // {2} 4 | // import { circleArea as circle } from './17-CalcArea'; 5 | 6 | // console.log(circleArea(2)); 7 | // console.log(squareArea(2)); 8 | 9 | /* Different way of importing the module */ 10 | // import * as area from './17-CalcArea'; 11 | // import Book from './17-Book'; 12 | 13 | import * as area from './17-CalcArea.js'; // we need the .js to run this code in the browser 14 | import Book from './17-Book.js'; 15 | 16 | console.log(area.circle(2)); 17 | console.log(area.square(2)); 18 | 19 | const myBook = new Book('some title'); 20 | myBook.printTitle(); 21 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/data-structures/stack-array.ts: -------------------------------------------------------------------------------- 1 | export default class StackArray { 2 | private items: T[]; 3 | 4 | constructor() { 5 | this.items = []; 6 | } 7 | 8 | push(element: T) { 9 | this.items.push(element); 10 | } 11 | 12 | pop() { 13 | return this.items.pop(); 14 | } 15 | 16 | peek() { 17 | return this.items[this.items.length - 1]; 18 | } 19 | 20 | isEmpty() { 21 | return this.items.length === 0; 22 | } 23 | 24 | size() { 25 | return this.items.length; 26 | } 27 | 28 | clear() { 29 | this.items = []; 30 | } 31 | 32 | toArray() { 33 | return this.items; 34 | } 35 | 36 | toString() { 37 | return this.items.toString(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter11/01-UsingMinHeap.js: -------------------------------------------------------------------------------- 1 | const { MinHeap } = PacktDataStructuresAlgorithms; 2 | 3 | let heap = new MinHeap(); 4 | 5 | heap.insert(2); 6 | heap.insert(3); 7 | heap.insert(4); 8 | heap.insert(5); 9 | 10 | heap.insert(2); 11 | 12 | console.log(heap.getAsArray()); 13 | 14 | console.log('Heap size: ', heap.size()); // 5 15 | console.log('Heap is empty: ', heap.isEmpty()); // false 16 | console.log('Heap min value: ', heap.findMinimum()); // 1 17 | 18 | heap = new MinHeap(); 19 | for (let i = 1; i < 10; i++) { 20 | heap.insert(i); 21 | } 22 | 23 | console.log(heap.getAsArray()); 24 | 25 | console.log('Extract minimum: ', heap.extract()); // 1 26 | console.log(heap.getAsArray()); // [2, 4, 3, 8, 5, 6, 7, 9] 27 | 28 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter05/02-Deque.js: -------------------------------------------------------------------------------- 1 | const { Deque } = PacktDataStructuresAlgorithms; 2 | 3 | const deque = new Deque(); 4 | console.log(deque.isEmpty()); // outputs true 5 | deque.addBack('John'); 6 | deque.addBack('Jack'); 7 | console.log(deque.toString()); // John,Jack 8 | deque.addBack('Camila'); 9 | console.log(deque.toString()); // John,Jack,Camila 10 | console.log(deque.size()); // outputs 3 11 | console.log(deque.isEmpty()); // outputs false 12 | deque.removeFront(); // remove John 13 | console.log(deque.toString()); // Jack,Camila 14 | deque.removeBack(); // Camila decides to leave 15 | console.log(deque.toString()); // Jack 16 | deque.addFront('John'); // John comes back for information 17 | console.log(deque.toString()); // John,Jack 18 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter09/01-IntroRecursion.js: -------------------------------------------------------------------------------- 1 | 2 | // functions commented below - inifite loop 3 | 4 | /* 5 | function recursiveFunction(someParam) { 6 | recursiveFunction(someParam); 7 | } 8 | 9 | function recursiveFunction1(someParam) { 10 | recursiveFunction2(someParam); 11 | } 12 | 13 | function recursiveFunction2(someParam) { 14 | recursiveFunction1(someParam); 15 | } 16 | */ 17 | 18 | function understandRecursion(doIunderstandRecursion) { 19 | const recursionAnswer = confirm('Do you understand recursion?'); // function logic 20 | if (recursionAnswer === true) { // base case or stop point 21 | return true; 22 | } 23 | understandRecursion(recursionAnswer); // recursive call 24 | } 25 | 26 | understandRecursion(false); 27 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter11/02-UsingMaxHeap.js: -------------------------------------------------------------------------------- 1 | const { MaxHeap } = PacktDataStructuresAlgorithms; 2 | 3 | const maxHeap = new MaxHeap(); 4 | 5 | maxHeap.insert(2); 6 | maxHeap.insert(3); 7 | maxHeap.insert(4); 8 | maxHeap.insert(5); 9 | 10 | maxHeap.insert(1); 11 | 12 | console.log(maxHeap.getAsArray()); 13 | 14 | console.log('Heap size: ', maxHeap.size()); // 5 15 | console.log('Heap is empty: ', maxHeap.isEmpty()); // false 16 | console.log('Heap min value: ', maxHeap.findMinimum()); // 5 17 | 18 | maxHeap.insert(6); 19 | maxHeap.insert(9); 20 | maxHeap.insert(10); 21 | maxHeap.insert(14); 22 | 23 | console.log(maxHeap.getAsArray()); 24 | 25 | console.log('Extract minimum: ', maxHeap.extract()); 26 | console.log(maxHeap.getAsArray()); 27 | 28 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/algorithms/sorting/shell-sort.js: -------------------------------------------------------------------------------- 1 | import { Compare, defaultCompare } from '../../util'; 2 | 3 | export function shellSort(array, compareFn = defaultCompare) { 4 | let increment = array.length / 2; 5 | while (increment > 0) { 6 | for (let i = increment; i < array.length; i++) { 7 | let j = i; 8 | const temp = array[i]; 9 | while (j >= increment && compareFn(array[j - increment], temp) === Compare.BIGGER_THAN) { 10 | array[j] = array[j - increment]; 11 | j -= increment; 12 | } 13 | array[j] = temp; 14 | } 15 | if (increment === 2) { 16 | increment = 1; 17 | } else { 18 | increment = Math.floor((increment * 5) / 11); 19 | } 20 | } 21 | return array; 22 | } 23 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/algorithms/sorting/counting-sort.ts: -------------------------------------------------------------------------------- 1 | import { findMaxValue } from '../search/min-max-search'; 2 | 3 | export function countingSort(array: number[]) { 4 | 5 | if (array.length < 2) { 6 | return array; 7 | } 8 | 9 | const maxValue = findMaxValue(array); 10 | let sortedIndex = 0; 11 | const counts = new Array(maxValue + 1); 12 | 13 | array.forEach(element => { 14 | if (!counts[element]) { 15 | counts[element] = 0; 16 | } 17 | counts[element]++; 18 | }); 19 | 20 | // console.log('Frequencies: ' + counts.join()); 21 | 22 | counts.forEach((element, i) => { 23 | while (element > 0) { 24 | array[sortedIndex++] = i; 25 | element--; 26 | } 27 | }); 28 | 29 | return array; 30 | } 31 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter12/05-Floyd-Warshall.js: -------------------------------------------------------------------------------- 1 | const { floydWarshall } = PacktDataStructuresAlgorithms; 2 | 3 | const INF = Infinity; 4 | const graph = [ 5 | [INF, 2, 4, INF, INF, INF], 6 | [INF, INF, 2, 4, 2, INF], 7 | [INF, INF, INF, INF, 3, INF], 8 | [INF, INF, INF, INF, INF, 2], 9 | [INF, INF, INF, 3, INF, 2], 10 | [INF, INF, INF, INF, INF, INF] 11 | ]; 12 | 13 | console.log('********* Floyd-Warshall Algorithm - All-Pairs Shortest Path ***********'); 14 | 15 | dist = floydWarshall(graph); 16 | 17 | let s = ''; 18 | for (let i = 0; i < dist.length; ++i) { 19 | s = ''; 20 | for (var j = 0; j < dist.length; ++j) { 21 | if (dist[i][j] === INF) s += 'INF '; 22 | else s += dist[i][j] + ' '; 23 | } 24 | console.log(s); 25 | } 26 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter06/05-StackLinkedList.js: -------------------------------------------------------------------------------- 1 | const { StackLinkedList } = PacktDataStructuresAlgorithms; 2 | 3 | const stack = new StackLinkedList(); 4 | 5 | console.log('stack.isEmpty() => ', stack.isEmpty()); // outputs true 6 | 7 | stack.push(5); 8 | stack.push(8); 9 | 10 | console.log('stack after push 5 and 8 => ', stack.toString()); 11 | 12 | console.log('stack.peek() => ', stack.peek()); // outputs 8 13 | 14 | stack.push(11); 15 | 16 | console.log('stack.size() after push 11 => ', stack.size()); // outputs 3 17 | console.log('stack.isEmpty() => ', stack.isEmpty()); // outputs false 18 | 19 | stack.push(15); 20 | 21 | stack.pop(); 22 | stack.pop(); 23 | 24 | console.log('stack.size() after push 15 and pop twice => ', stack.size()); // outputs 2 25 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/algorithms/sorting/selection-sort.js: -------------------------------------------------------------------------------- 1 | import { Compare, defaultCompare, swap } from '../../util'; 2 | 3 | export const selectionSort = (array, compareFn = defaultCompare) => { 4 | const { length } = array; 5 | let indexMin; 6 | for (let i = 0; i < length - 1; i++) { 7 | indexMin = i; 8 | // console.log('index ' + array[i]); 9 | for (let j = i; j < length; j++) { 10 | if (compareFn(array[indexMin], array[j]) === Compare.BIGGER_THAN) { 11 | // console.log('new index min ' + array[j]); 12 | indexMin = j; 13 | } 14 | } 15 | if (i !== indexMin) { 16 | // console.log('swap ' + array[i] + ' with ' + array[indexMin]); 17 | swap(array, i, indexMin); 18 | } 19 | } 20 | return array; 21 | }; 22 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/algorithms/sorting/shell-sort.ts: -------------------------------------------------------------------------------- 1 | import { Compare, defaultCompare } from '../../util'; 2 | 3 | export function shellSort(array: T[], compareFn = defaultCompare) { 4 | let increment = array.length / 2; 5 | while (increment > 0) { 6 | for (let i = increment; i < array.length; i++) { 7 | let j = i; 8 | const temp = array[i]; 9 | 10 | while (j >= increment && compareFn(array[j - increment], temp) === Compare.BIGGER_THAN) { 11 | array[j] = array[j - increment]; 12 | j = j - increment; 13 | } 14 | 15 | array[j] = temp; 16 | } 17 | 18 | if (increment === 2) { 19 | increment = 1; 20 | } else { 21 | increment = Math.floor(increment * 5 / 11); 22 | } 23 | } 24 | return array; 25 | } 26 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/algorithms/graph/floyd-warshall.js: -------------------------------------------------------------------------------- 1 | export const floydWarshall = graph => { 2 | const dist = []; 3 | const { length } = graph; 4 | for (let i = 0; i < length; i++) { 5 | dist[i] = []; 6 | for (let j = 0; j < length; j++) { 7 | if (i === j) { 8 | dist[i][j] = 0; 9 | } else if (!isFinite(graph[i][j])) { 10 | dist[i][j] = Infinity; 11 | } else { 12 | dist[i][j] = graph[i][j]; 13 | } 14 | } 15 | } 16 | for (let k = 0; k < length; k++) { 17 | for (let i = 0; i < length; i++) { 18 | for (let j = 0; j < length; j++) { 19 | if (dist[i][k] + dist[k][j] < dist[i][j]) { 20 | dist[i][j] = dist[i][k] + dist[k][j]; 21 | } 22 | } 23 | } 24 | } 25 | return dist; 26 | }; 27 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/algorithms/sorting/selection-sort.ts: -------------------------------------------------------------------------------- 1 | import { Compare, defaultCompare, swap } from '../../util'; 2 | 3 | export const selectionSort = (array: any[], compareFn = defaultCompare) => { 4 | const { length } = array; 5 | let indexMin; 6 | 7 | for (let i = 0; i < length - 1; i++) { 8 | indexMin = i; 9 | // console.log('index ' + array[i]); 10 | for (let j = i; j < length; j++) { 11 | if (compareFn(array[indexMin], array[j]) === Compare.BIGGER_THAN) { 12 | // console.log('new index min ' + array[j]); 13 | indexMin = j; 14 | } 15 | } 16 | if (i !== indexMin) { 17 | // console.log('swap ' + array[i] + ' with ' + array[indexMin]); 18 | swap(array, i, indexMin); 19 | } 20 | } 21 | 22 | return array; 23 | }; 24 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/algorithms/dynamic-programing/longest-common-subsequence.js: -------------------------------------------------------------------------------- 1 | export function lcs(wordX, wordY) { 2 | const m = wordX.length; 3 | const n = wordY.length; 4 | const l = []; 5 | for (let i = 0; i <= m; i++) { 6 | l[i] = []; 7 | for (let j = 0; j <= n; j++) { 8 | l[i][j] = 0; 9 | } 10 | } 11 | for (let i = 0; i <= m; i++) { 12 | for (let j = 0; j <= n; j++) { 13 | if (i === 0 || j === 0) { 14 | l[i][j] = 0; 15 | } else if (wordX[i - 1] === wordY[j - 1]) { 16 | l[i][j] = l[i - 1][j - 1] + 1; 17 | } else { 18 | const a = l[i - 1][j]; 19 | const b = l[i][j - 1]; 20 | l[i][j] = a > b ? a : b; // max(a,b) 21 | } 22 | } 23 | // console.log(l[i].join()); 24 | } 25 | return l[m][n]; 26 | } 27 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/others/balanced-symbols.js: -------------------------------------------------------------------------------- 1 | // @ts-check 2 | import Stack from '../data-structures/stack'; 3 | 4 | export function parenthesesChecker(symbols) { 5 | const stack = new Stack(); 6 | const opens = '([{'; 7 | const closers = ')]}'; 8 | let balanced = true; 9 | let index = 0; 10 | let symbol; 11 | let top; 12 | 13 | while (index < symbols.length && balanced) { 14 | symbol = symbols[index]; 15 | if (opens.indexOf(symbol) >= 0) { 16 | stack.push(symbol); 17 | } else if (stack.isEmpty()) { 18 | balanced = false; 19 | } else { 20 | top = stack.pop(); 21 | if (!(opens.indexOf(top) === closers.indexOf(symbol))) { 22 | balanced = false; 23 | } 24 | } 25 | index++; 26 | } 27 | return balanced && stack.isEmpty(); 28 | } 29 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/js/data-structures/avl-tree.spec.js: -------------------------------------------------------------------------------- 1 | import 'mocha'; 2 | import { expect } from 'chai'; 3 | import { AVLTree } from '../../../src/js/index'; 4 | 5 | describe('AVLTree', () => { 6 | let tree; 7 | 8 | beforeEach(() => { 9 | tree = new AVLTree(); 10 | }); 11 | 12 | it('starts empty', () => { 13 | expect(tree.getRoot()).to.equal(null); 14 | }); 15 | 16 | it('inserts elements in the AVLTree', () => { 17 | expect(tree.getRoot()).to.equal(null); 18 | 19 | tree.insert(1); 20 | tree.insert(2); 21 | tree.insert(3); 22 | tree.insert(4); 23 | tree.insert(5); 24 | tree.insert(6); 25 | tree.insert(7); 26 | tree.insert(14); 27 | tree.insert(15); 28 | tree.insert(13); 29 | tree.insert(12); 30 | tree.insert(11); 31 | }); 32 | }); 33 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/others/palindrome-checker.js: -------------------------------------------------------------------------------- 1 | import Deque from '../data-structures/deque'; 2 | 3 | export function palindromeChecker(aString) { 4 | if ( 5 | aString === undefined || 6 | aString === null || 7 | (aString !== null && aString.length === 0) 8 | ) { 9 | return false; 10 | } 11 | const deque = new Deque(); 12 | const lowerString = aString.toLocaleLowerCase().split(' ').join(''); 13 | let firstChar; 14 | let lastChar; 15 | 16 | for (let i = 0; i < lowerString.length; i++) { 17 | deque.addBack(lowerString.charAt(i)); 18 | } 19 | 20 | while (deque.size() > 1) { 21 | firstChar = deque.removeFront(); 22 | lastChar = deque.removeBack(); 23 | if (firstChar !== lastChar) { 24 | return false; 25 | } 26 | } 27 | 28 | return true; 29 | } 30 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/others/palindrome-checker.ts: -------------------------------------------------------------------------------- 1 | import Deque from '../data-structures/deque'; 2 | 3 | export function palindromeChecker(aString: string) { 4 | 5 | if (aString === undefined || aString === null || 6 | (aString !== null && aString.length === 0)) { 7 | return false; 8 | } 9 | 10 | const deque = new Deque(); 11 | const lowerString = aString.toLocaleLowerCase().split(' ').join(''); 12 | let firstChar: string, lastChar: string; 13 | 14 | for (let i = 0; i < lowerString.length; i++) { 15 | deque.addBack(lowerString.charAt(i)); 16 | } 17 | 18 | while (deque.size() > 1) { 19 | firstChar = deque.removeFront(); 20 | lastChar = deque.removeBack(); 21 | if (firstChar !== lastChar) { 22 | return false; 23 | } 24 | } 25 | 26 | return true; 27 | } 28 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/10-ES2015-ES6-letconst.js: -------------------------------------------------------------------------------- 1 | // @ts-check 2 | /* eslint-disable */ 3 | 4 | //* ****** EcmaScript 2015 (ES6): let and const keywords 5 | 6 | //* ****** EcmaScript 2015 (ES6): let is the new var (https://goo.gl/he0udZ) 7 | var framework = 'Angular'; 8 | var framework = 'React'; 9 | console.log(framework); 10 | 11 | let language = 'JavaScript!'; // {1} 12 | // let language = 'Ruby!'; // {2} - throws error 13 | console.log(language); 14 | 15 | 16 | //* ****** EcmaScript 2015 (ES6): const (https://goo.gl/YUQj3r) 17 | const PI = 3.141593; 18 | // PI = 3.0; //throws error 19 | console.log(PI); 20 | 21 | const jsFramework = { 22 | name: 'Angular' 23 | }; 24 | jsFramework.name = 'React'; 25 | 26 | // error, cannot reassign object reference 27 | /* 28 | jsFramework = { 29 | name: 'Vue' 30 | }; 31 | */ 32 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/algorithms/search/min-max-search.js: -------------------------------------------------------------------------------- 1 | import { defaultCompare, Compare } from '../../util'; 2 | 3 | export function findMaxValue(array, compareFn = defaultCompare) { 4 | if (array && array.length > 0) { 5 | let max = array[0]; 6 | for (let i = 1; i < array.length; i++) { 7 | if (compareFn(max, array[i]) === Compare.LESS_THAN) { 8 | max = array[i]; 9 | } 10 | } 11 | return max; 12 | } 13 | return undefined; 14 | } 15 | export function findMinValue(array, compareFn = defaultCompare) { 16 | if (array && array.length > 0) { 17 | let min = array[0]; 18 | for (let i = 1; i < array.length; i++) { 19 | if (compareFn(min, array[i]) === Compare.BIGGER_THAN) { 20 | min = array[i]; 21 | } 22 | } 23 | return min; 24 | } 25 | return undefined; 26 | } 27 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/algorithms/graph/floyd-warshall.ts: -------------------------------------------------------------------------------- 1 | export const floydWarshall = (graph: number[][]) => { 2 | const dist: number[][] = []; 3 | const length = graph.length; 4 | 5 | for (let i = 0; i < length; i++) { 6 | dist[i] = []; 7 | for (let j = 0; j < length; j++) { 8 | if (i === j) { 9 | dist[i][j] = 0; 10 | } else if (!isFinite(graph[i][j])) { 11 | dist[i][j] = Infinity; 12 | } else { 13 | dist[i][j] = graph[i][j]; 14 | } 15 | } 16 | } 17 | 18 | for (let k = 0; k < length; k++) { 19 | for (let i = 0; i < length; i++) { 20 | for (let j = 0; j < length; j++) { 21 | if (dist[i][k] + dist[k][j] < dist[i][j]) { 22 | dist[i][j] = dist[i][k] + dist[k][j]; 23 | } 24 | } 25 | } 26 | } 27 | 28 | return dist; 29 | }; 30 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/ts/data-structures/avl-tree.spec.ts: -------------------------------------------------------------------------------- 1 | import 'mocha'; 2 | import { expect } from 'chai'; 3 | import { AVLTree } from '../../../src/ts/index'; 4 | 5 | describe('AVLTree', () => { 6 | let tree: AVLTree; 7 | 8 | beforeEach(() => { 9 | tree = new AVLTree(); 10 | }); 11 | 12 | it('starts empty', () => { 13 | expect(tree.getRoot()).to.equal(undefined); 14 | }); 15 | 16 | it('inserts elements in the AVLTree', () => { 17 | expect(tree.getRoot()).to.equal(undefined); 18 | 19 | tree.insert(1); 20 | tree.insert(2); 21 | tree.insert(3); 22 | tree.insert(4); 23 | tree.insert(5); 24 | tree.insert(6); 25 | tree.insert(7); 26 | tree.insert(14); 27 | tree.insert(15); 28 | tree.insert(13); 29 | tree.insert(12); 30 | tree.insert(11); 31 | }); 32 | }); 33 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/algorithms/sorting/merge-sort.js: -------------------------------------------------------------------------------- 1 | import { Compare, defaultCompare } from '../../util'; 2 | 3 | function merge(left, right, compareFn) { 4 | let i = 0; 5 | let j = 0; 6 | const result = []; 7 | while (i < left.length && j < right.length) { 8 | result.push(compareFn(left[i], right[j]) === Compare.LESS_THAN ? left[i++] : right[j++]); 9 | } 10 | return result.concat(i < left.length ? left.slice(i) : right.slice(j)); 11 | } 12 | export function mergeSort(array, compareFn = defaultCompare) { 13 | if (array.length > 1) { 14 | const { length } = array; 15 | const middle = Math.floor(length / 2); 16 | const left = mergeSort(array.slice(0, middle), compareFn); 17 | const right = mergeSort(array.slice(middle, length), compareFn); 18 | array = merge(left, right, compareFn); 19 | } 20 | return array; 21 | } 22 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/algorithms/dynamic-programing/longest-common-subsequence.ts: -------------------------------------------------------------------------------- 1 | export function lcs(wordX: string, wordY: string) { 2 | const m = wordX.length; 3 | const n = wordY.length; 4 | const l: Array> = []; 5 | 6 | for (let i = 0; i <= m; i++) { 7 | l[i] = []; 8 | for (let j = 0; j <= n; j++) { 9 | l[i][j] = 0; 10 | } 11 | } 12 | 13 | for (let i = 0; i <= m; i++) { 14 | for (let j = 0; j <= n; j++) { 15 | if (i === 0 || j === 0) { 16 | l[i][j] = 0; 17 | } else if (wordX[i - 1] === wordY[j - 1]) { 18 | l[i][j] = l[i - 1][j - 1] + 1; 19 | } else { 20 | const a = l[i - 1][j]; 21 | const b = l[i][j - 1]; 22 | l[i][j] = a > b ? a : b; // max(a,b) 23 | } 24 | } 25 | // console.log(l[i].join()); 26 | } 27 | 28 | return l[m][n]; 29 | } 30 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/algorithms/search/min-max-search.ts: -------------------------------------------------------------------------------- 1 | import { defaultCompare, Compare } from '../../util'; 2 | 3 | export function findMaxValue(array: T[], compareFn = defaultCompare) { 4 | if (array && array.length > 0) { 5 | let max = array[0]; 6 | for (let i = 1; i < array.length; i++) { 7 | if (compareFn(max, array[i]) === Compare.LESS_THAN) { 8 | max = array[i]; 9 | } 10 | } 11 | return max; 12 | } 13 | return undefined; 14 | } 15 | 16 | export function findMinValue(array: T[], compareFn = defaultCompare) { 17 | if (array && array.length > 0) { 18 | let min = array[0]; 19 | for (let i = 1; i < array.length; i++) { 20 | if (compareFn(min, array[i]) === Compare.BIGGER_THAN) { 21 | min = array[i]; 22 | } 23 | } 24 | return min; 25 | } 26 | return undefined; 27 | } 28 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/others/balanced-symbols.ts: -------------------------------------------------------------------------------- 1 | import Stack from '../data-structures/stack'; 2 | 3 | export function parenthesesChecker(symbols: string) { 4 | const stack = new Stack(); 5 | const opens = '([{'; 6 | const closers = ')]}'; 7 | let balanced = true; 8 | let index = 0; 9 | let symbol: string; 10 | let top: string; 11 | 12 | while (index < symbols.length && balanced) { 13 | symbol = symbols[index]; 14 | if (opens.indexOf(symbol) >= 0) { 15 | stack.push(symbol); 16 | } else { 17 | if (stack.isEmpty()) { 18 | balanced = false; 19 | } else { 20 | top = stack.pop(); 21 | if (!(opens.indexOf(top) === closers.indexOf(symbol))) { 22 | balanced = false; 23 | } 24 | } 25 | } 26 | index++; 27 | } 28 | return balanced && stack.isEmpty(); 29 | } 30 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/04-TruthyFalsy.js: -------------------------------------------------------------------------------- 1 | // @ts-check 2 | /* eslint-disable */ 3 | 4 | function testTruthy(val) { 5 | return val ? console.log('truthy') : console.log('falsy'); 6 | } 7 | 8 | testTruthy(true); // true 9 | testTruthy(false); // false 10 | testTruthy(new Boolean(false)); // true (object is always true) 11 | 12 | testTruthy(''); // false 13 | testTruthy('a'); // true 14 | testTruthy('Packt'); // true 15 | testTruthy(new String('')); // true (object is always true) 16 | 17 | testTruthy(1); // true 18 | testTruthy(-1); // true 19 | testTruthy(NaN); // false 20 | testTruthy(new Number(NaN)); // true (object is always true) 21 | 22 | testTruthy({}); // true (object is always true) 23 | 24 | var obj = { name: 'John' }; 25 | testTruthy(obj); // true 26 | testTruthy(obj.name); // true 27 | testTruthy(obj.age); // false (property is undefined) 28 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/others/fibonacci.js: -------------------------------------------------------------------------------- 1 | export function fibonacci(n) { 2 | if (n < 1) { 3 | return 0; 4 | } 5 | if (n <= 2) { 6 | return 1; 7 | } 8 | return fibonacci(n - 1) + fibonacci(n - 2); 9 | } 10 | 11 | export function fibonacciIterative(n) { 12 | if (n < 1) { return 0; } 13 | let fibNMinus2 = 0; 14 | let fibNMinus1 = 1; 15 | let fibN = n; 16 | for (let i = 2; i <= n; i++) { 17 | fibN = fibNMinus1 + fibNMinus2; 18 | fibNMinus2 = fibNMinus1; 19 | fibNMinus1 = fibN; 20 | } 21 | return fibN; 22 | } 23 | 24 | export function fibonacciMemoization(n) { 25 | if (n < 1) { return 0; } 26 | const memo = [0, 1]; 27 | const fibonacciMem = num => { 28 | if (memo[num] != null) { return memo[num]; } 29 | return (memo[num] = fibonacciMem(num - 1) + fibonacciMem(num - 2)); 30 | }; 31 | return fibonacciMem(n); 32 | } 33 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/data-structures/stack-linked-list.js: -------------------------------------------------------------------------------- 1 | import DoublyLinkedList from './doubly-linked-list'; 2 | 3 | export default class StackLinkedList { 4 | constructor() { 5 | this.items = new DoublyLinkedList(); 6 | } 7 | push(element) { 8 | this.items.push(element); 9 | } 10 | pop() { 11 | if (this.isEmpty()) { 12 | return undefined; 13 | } 14 | const result = this.items.removeAt(this.size() - 1); 15 | return result; 16 | } 17 | peek() { 18 | if (this.isEmpty()) { 19 | return undefined; 20 | } 21 | return this.items.getElementAt(this.size() - 1).element; 22 | } 23 | isEmpty() { 24 | return this.items.isEmpty(); 25 | } 26 | size() { 27 | return this.items.size(); 28 | } 29 | clear() { 30 | this.items.clear(); 31 | } 32 | toString() { 33 | return this.items.toString(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/lib/17-CalcArea.js: -------------------------------------------------------------------------------- 1 | (function (global, factory) { 2 | if (typeof define === "function" && define.amd) { 3 | define(["exports"], factory); 4 | } else if (typeof exports !== "undefined") { 5 | factory(exports); 6 | } else { 7 | var mod = { 8 | exports: {} 9 | }; 10 | factory(mod.exports); 11 | global.CalcArea = mod.exports; 12 | } 13 | })(this, function (exports) { 14 | "use strict"; 15 | 16 | Object.defineProperty(exports, "__esModule", { 17 | value: true 18 | }); 19 | var circleArea = exports.circleArea = function circleArea(r) { 20 | return 3.14 * Math.pow(r, 2); 21 | }; 22 | 23 | var squareArea = exports.squareArea = function squareArea(s) { 24 | return s * s; 25 | }; 26 | 27 | // export { circleArea, squareArea }; // {1} 28 | exports.circle = circleArea; 29 | exports.square = squareArea; 30 | }); -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter03/10-ArraysAndTypeScript.ts: -------------------------------------------------------------------------------- 1 | // const numbers: number[]; 2 | const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 3 | console.log('numbers', numbers); 4 | 5 | // let names: string[]; 6 | let names = ['Ana', 'ana', 'john', 'John']; 7 | console.log('names', names); 8 | console.log('names.sort()', names.sort()); 9 | 10 | interface Person { 11 | name: string; 12 | age: number; 13 | } 14 | 15 | // const friends: {name: string, age: number}[]; 16 | const friends = [ 17 | { name: 'John', age: 30 }, 18 | { name: 'Ana', age: 20 }, 19 | { name: 'Chris', age: 25 }, // trailing comma ES2017 20 | ]; 21 | 22 | function comparePerson(a: Person, b: Person) { 23 | if (a.age < b.age) { 24 | return -1; 25 | } 26 | if (a.age > b.age) { 27 | return 1; 28 | } 29 | return 0; 30 | } 31 | 32 | console.log('friends.sort(comparePerson)', friends.sort(comparePerson)); 33 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter03/01-Introduction.js: -------------------------------------------------------------------------------- 1 | // @ts-check 2 | 3 | const averageTempJan = 31.9; 4 | const averageTempFeb = 35.3; 5 | const averageTempMar = 42.4; 6 | const averageTempApr = 52; 7 | const averageTempMay = 60.8; 8 | 9 | const averageTemp = []; 10 | averageTemp[0] = 31.9; 11 | averageTemp[1] = 35.3; 12 | averageTemp[2] = 42.4; 13 | averageTemp[3] = 52; 14 | averageTemp[4] = 60.8; 15 | 16 | console.log('averageTempJan', averageTempJan); 17 | console.log('averageTempFeb', averageTempFeb); 18 | console.log('averageTempMar', averageTempMar); 19 | console.log('averageTempApr', averageTempApr); 20 | console.log('averageTempMay', averageTempMay); 21 | 22 | console.log('averageTemp[0]', averageTemp[0]); 23 | console.log('averageTemp[1]', averageTemp[1]); 24 | console.log('averageTemp[2]', averageTemp[2]); 25 | console.log('averageTemp[3]', averageTemp[3]); 26 | console.log('averageTemp[4]', averageTemp[4]); 27 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/js/algorithms/search/min-max-search.spec.js: -------------------------------------------------------------------------------- 1 | import 'mocha'; 2 | import { expect } from 'chai'; 3 | import { findMinValue, findMaxValue } from '../../../../src/js/index'; 4 | 5 | describe('Min and Max Values Search', () => { 6 | const SIZE = 10; 7 | 8 | function createSortedArray() { 9 | const array = []; 10 | for (let i = 1; i <= SIZE; i++) { 11 | array.push(i); 12 | } 13 | return array; 14 | } 15 | 16 | it('min value - works with empty arrays', () => { 17 | expect(findMinValue([])).to.equal(undefined); 18 | }); 19 | 20 | it('max value - works with empty arrays', () => { 21 | expect(findMaxValue([])).to.equal(undefined); 22 | }); 23 | 24 | it('min value', () => { 25 | expect(findMinValue(createSortedArray())).to.equal(1); 26 | }); 27 | 28 | it('max value', () => { 29 | expect(findMaxValue(createSortedArray())).to.equal(SIZE); 30 | }); 31 | }); 32 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/js/algorithms/shuffle/fisher–yates.spec.js: -------------------------------------------------------------------------------- 1 | import 'mocha'; 2 | import { expect } from 'chai'; 3 | import { shuffle } from '../../../../src/js/index'; 4 | 5 | describe('Fisher-Yates Suffle', () => { 6 | const SIZE = 100; 7 | 8 | function createSortedArray() { 9 | const array = []; 10 | for (let i = 1; i <= SIZE; i++) { 11 | array.push(i); 12 | } 13 | return array; 14 | } 15 | 16 | it('works with empty arrays', () => { 17 | expect(shuffle([])).to.deep.equal([]); 18 | }); 19 | 20 | it('works with arrays with a single value', () => { 21 | const array = [1]; 22 | expect(shuffle(array)).to.deep.equal(array); 23 | }); 24 | 25 | it('works with sorted arrays', () => { 26 | let array = createSortedArray(); 27 | const sortedArray = createSortedArray(); 28 | array = shuffle(array); 29 | expect(array).to.not.deep.equal(sortedArray); 30 | }); 31 | }); 32 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "browser": true, 4 | "commonjs": true, 5 | "es6": true, 6 | "node": true 7 | }, 8 | "globals": { 9 | "expect": true, 10 | "it": true, 11 | "describe": true, 12 | "beforeEach": true, 13 | "afterEach": true, 14 | "document": false, 15 | "navigator": false, 16 | "window": false 17 | }, 18 | "parser": "babel-eslint", 19 | "extends": "airbnb-base", 20 | "rules": { 21 | "class-methods-use-this": 0, 22 | "no-plusplus": 0, 23 | "arrow-parens": 0, 24 | "no-console": 0, 25 | "import/prefer-default-export": 0, 26 | "comma-dangle": 0, 27 | "no-underscore-dangle": 0, 28 | "no-param-reassign": 0, 29 | "no-return-assign": 0, 30 | "no-restricted-globals": 0, 31 | "no-multi-assign": 0, 32 | "prefer-destructuring": ["error", {"object": true, "array": false}], 33 | "padded-blocks": 0 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/algorithms/search/binary-search.js: -------------------------------------------------------------------------------- 1 | import { Compare, defaultCompare, DOES_NOT_EXIST } from '../../util'; 2 | import { quickSort } from '../sorting/quicksort'; 3 | 4 | export function binarySearch(array, value, compareFn = defaultCompare) { 5 | const sortedArray = quickSort(array); 6 | let low = 0; 7 | let high = sortedArray.length - 1; 8 | while (low <= high) { 9 | const mid = Math.floor((low + high) / 2); 10 | const element = sortedArray[mid]; 11 | // console.log('mid element is ' + element); 12 | if (compareFn(element, value) === Compare.LESS_THAN) { 13 | low = mid + 1; 14 | // console.log('low is ' + low); 15 | } else if (compareFn(element, value) === Compare.BIGGER_THAN) { 16 | high = mid - 1; 17 | // console.log('high is ' + high); 18 | } else { 19 | // console.log('found it'); 20 | return mid; 21 | } 22 | } 23 | return DOES_NOT_EXIST; 24 | } 25 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/algorithms/sorting/merge-sort.ts: -------------------------------------------------------------------------------- 1 | import { Compare, defaultCompare, ICompareFunction } from '../../util'; 2 | 3 | function merge(left: T[], right: T[], compareFn: ICompareFunction) { 4 | let i = 0; 5 | let j = 0; 6 | const result = []; 7 | 8 | while (i < left.length && j < right.length) { 9 | result.push(compareFn(left[i], right[j]) === Compare.LESS_THAN ? left[i++] : right[j++]); 10 | } 11 | 12 | return result.concat(i < left.length ? left.slice(i) : right.slice(j)); 13 | } 14 | 15 | export function mergeSort(array: T[], compareFn = defaultCompare): T[] { 16 | if (array.length > 1) { 17 | const { length } = array; 18 | const middle = Math.floor(length / 2); 19 | const left = mergeSort(array.slice(0, middle), compareFn); 20 | const right = mergeSort(array.slice(middle, length), compareFn); 21 | array = merge(left, right, compareFn); 22 | } 23 | 24 | return array; 25 | } 26 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/others/fibonacci.ts: -------------------------------------------------------------------------------- 1 | export function fibonacci(n: number): number { 2 | if (n < 1) { return 0; } // {1} 3 | if (n <= 2) { return 1; } // {2} 4 | return fibonacci(n - 1) + fibonacci(n - 2); // {3} 5 | } 6 | 7 | export function fibonacciIterative(n: number) { 8 | if (n < 1) { return 0; } 9 | let fibNMinus2 = 0; 10 | let fibNMinus1 = 1; 11 | let fibN = n; 12 | for (let i = 2; i <= n; i++) { 13 | // n >= 2 14 | fibN = fibNMinus1 + fibNMinus2; // f(n-1) + f(n-2) 15 | fibNMinus2 = fibNMinus1; 16 | fibNMinus1 = fibN; 17 | } 18 | return fibN; 19 | } 20 | 21 | export function fibonacciMemoization(n: number) { 22 | if (n < 1) { return 0; } 23 | const memo = [0, 1]; 24 | const fibonacciMem = (num: number): number => { 25 | if (memo[num] != null) { return memo[num]; } 26 | return (memo[num] = fibonacciMem(num - 1) + fibonacciMem(num - 2)); 27 | }; 28 | return fibonacciMem(n); 29 | } 30 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/ts/algorithms/search/min-max-search.spec.ts: -------------------------------------------------------------------------------- 1 | import 'mocha'; 2 | import { expect } from 'chai'; 3 | import { findMinValue, findMaxValue } from '../../../../src/ts/index'; 4 | 5 | describe('Min and Max Values Search', () => { 6 | 7 | const SIZE = 10; 8 | 9 | function createSortedArray() { 10 | const array: number[] = []; 11 | for (let i = 1; i <= SIZE; i++) { 12 | array.push(i); 13 | } 14 | return array; 15 | } 16 | 17 | it('min value - works with empty arrays', () => { 18 | expect(findMinValue([])).to.equal(undefined); 19 | }); 20 | 21 | it('max value - works with empty arrays', () => { 22 | expect(findMaxValue([])).to.equal(undefined); 23 | }); 24 | 25 | it('min value', () => { 26 | expect(findMinValue(createSortedArray())).to.equal(1); 27 | }); 28 | 29 | it('max value', () => { 30 | expect(findMaxValue(createSortedArray())).to.equal(SIZE); 31 | }); 32 | }); 33 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/algorithms/math/primality-test.ts: -------------------------------------------------------------------------------- 1 | export const isPrime = (n: number) => { 2 | if (n <= 1) { 3 | return false; 4 | } 5 | 6 | const sqrt = Math.floor(Math.sqrt(n)); 7 | for (let i = 2; i < sqrt; i++) { 8 | if (n % i === 0) { 9 | return false; 10 | } 11 | } 12 | 13 | return true; 14 | }; 15 | 16 | export const testPrime = (n: number) => { 17 | if (n <= 1) { 18 | return false; 19 | } else { 20 | if (n === 2 || n === 3) { 21 | return true; 22 | } else if (n % 2 === 0) { 23 | return false; 24 | } else { 25 | const sqrt = Math.floor(Math.sqrt(n)); 26 | for (let i = 3; i <= sqrt; i += 2) { 27 | if (n % i === 0) { 28 | return false; 29 | } 30 | } 31 | } 32 | } 33 | return true; 34 | }; 35 | 36 | export const isPrime2 = (n: number) => ![...Array(n).keys()].slice(2).map(i => !(n % i)).includes(true) && ![0, 1].includes(n); 37 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/ts/algorithms/shuffle/fisher–yates.spec.ts: -------------------------------------------------------------------------------- 1 | import 'mocha'; 2 | import { expect } from 'chai'; 3 | import { shuffle } from '../../../../src/ts/index'; 4 | 5 | describe('Fisher-Yates Suffle', () => { 6 | 7 | const SIZE = 100; 8 | 9 | function createSortedArray() { 10 | const array: number[] = []; 11 | for (let i = 1; i <= SIZE; i++) { 12 | array.push(i); 13 | } 14 | return array; 15 | } 16 | 17 | it('works with empty arrays', () => { 18 | expect(shuffle([])).to.deep.equal([]); 19 | }); 20 | 21 | it('works with arrays with a single value', () => { 22 | const array = [1]; 23 | expect(shuffle(array)).to.deep.equal(array); 24 | }); 25 | 26 | it('works with sorted arrays', () => { 27 | let array = createSortedArray(); 28 | const sortedArray = createSortedArray(); 29 | array = shuffle(array); 30 | expect(array).to.not.deep.equal(sortedArray); 31 | }); 32 | 33 | }); 34 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/algorithms/search/binary-search.ts: -------------------------------------------------------------------------------- 1 | import { Compare, defaultCompare, DOES_NOT_EXIST } from '../../util'; 2 | import { quickSort } from '../sorting/quicksort'; 3 | 4 | export function binarySearch (array: T[], value: T, compareFn = defaultCompare) { 5 | const sortedArray = quickSort(array); 6 | let low = 0; 7 | let high = sortedArray.length - 1; 8 | 9 | while (low <= high) { 10 | const mid = Math.floor((low + high) / 2); 11 | const element = sortedArray[mid]; 12 | // console.log('mid element is ' + element); 13 | if (compareFn(element, value) === Compare.LESS_THAN) { 14 | low = mid + 1; 15 | // console.log('low is ' + low); 16 | } else if (compareFn(element, value) === Compare.BIGGER_THAN) { 17 | high = mid - 1; 18 | // console.log('high is ' + high); 19 | } else { 20 | // console.log('found it'); 21 | return mid; 22 | } 23 | } 24 | return DOES_NOT_EXIST; 25 | } 26 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/algorithms/dynamic-programing/min-coin-change.js: -------------------------------------------------------------------------------- 1 | export function minCoinChange(coins, amount) { 2 | const cache = []; 3 | 4 | const makeChange = (value) => { 5 | if (!value) { 6 | return []; 7 | } 8 | if (cache[value]) { 9 | return cache[value]; 10 | } 11 | let min = []; 12 | let newMin; 13 | let newAmount; 14 | for (let i = 0; i < coins.length; i++) { 15 | const coin = coins[i]; 16 | newAmount = value - coin; 17 | if (newAmount >= 0) { 18 | newMin = makeChange(newAmount); 19 | } 20 | if ( 21 | newAmount >= 0 && 22 | (newMin.length < min.length - 1 || !min.length) && 23 | (newMin.length || !newAmount) 24 | ) { 25 | min = [coin].concat(newMin); 26 | // console.log('new Min ' + min + ' for ' + amount); 27 | } 28 | } 29 | return (cache[value] = min); 30 | }; 31 | return makeChange(amount); 32 | } 33 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/js/others/palindrome-checker.spec.js: -------------------------------------------------------------------------------- 1 | import 'mocha'; 2 | import { expect } from 'chai'; 3 | import { palindromeChecker } from '../../../src/js/others/palindrome-checker'; 4 | 5 | describe('Palindrome', () => { 6 | it('Palindrome Checker', () => { 7 | expect(palindromeChecker('')).to.equal(false); 8 | expect(palindromeChecker('a')).to.equal(true); 9 | expect(palindromeChecker('aa')).to.equal(true); 10 | expect(palindromeChecker('aba')).to.equal(true); 11 | expect(palindromeChecker('ab')).to.equal(false); 12 | expect(palindromeChecker('kayak')).to.equal(true); 13 | expect(palindromeChecker('radar')).to.equal(true); 14 | expect(palindromeChecker('level')).to.equal(true); 15 | expect(palindromeChecker('Was it a car or a cat I saw')).to.equal(true); 16 | expect(palindromeChecker('Step on no pets')).to.equal(true); 17 | expect(palindromeChecker('Able was I ere I saw Elba')).to.equal(true); 18 | }); 19 | }); 20 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/ts/others/palindrome-checker.spec.ts: -------------------------------------------------------------------------------- 1 | import 'mocha'; 2 | import { expect } from 'chai'; 3 | import { palindromeChecker } from '../../../src/ts/others/palindrome-checker'; 4 | 5 | describe('Palindrome', () => { 6 | 7 | it('Palindrome Checker', () => { 8 | expect(palindromeChecker('')).to.equal(false); 9 | expect(palindromeChecker('a')).to.equal(true); 10 | expect(palindromeChecker('aa')).to.equal(true); 11 | expect(palindromeChecker('aba')).to.equal(true); 12 | expect(palindromeChecker('ab')).to.equal(false); 13 | expect(palindromeChecker('kayak')).to.equal(true); 14 | expect(palindromeChecker('radar')).to.equal(true); 15 | expect(palindromeChecker('level')).to.equal(true); 16 | expect(palindromeChecker('Was it a car or a cat I saw')).to.equal(true); 17 | expect(palindromeChecker('Step on no pets')).to.equal(true); 18 | expect(palindromeChecker('Able was I ere I saw Elba')).to.equal(true); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/09-ObjectOrientedJS.js: -------------------------------------------------------------------------------- 1 | // @ts-check 2 | /* eslint-disable */ 3 | 4 | /* Object example 1 */ 5 | var obj = new Object(); 6 | 7 | /* Object example 2 */ 8 | var obj = {}; 9 | 10 | obj = { 11 | name: { 12 | first: 'Gandalf', 13 | last: 'the Grey' 14 | }, 15 | address: 'Middle Earth' 16 | }; 17 | 18 | /* Object example 3 */ 19 | function Book(title, pages, isbn) { 20 | this.title = title; 21 | this.pages = pages; 22 | this.isbn = isbn; 23 | this.printIsbn = function() { 24 | console.log(this.isbn); 25 | }; 26 | } 27 | 28 | var book = new Book('title', 'pag', 'isbn'); 29 | 30 | console.log(book.title); // outputs the book title 31 | 32 | book.title = 'new title'; // update the value of the book title 33 | 34 | console.log(book.title); // outputs the updated value 35 | 36 | Book.prototype.printTitle = function() { 37 | console.log(this.title); 38 | }; 39 | 40 | book.printTitle(); 41 | 42 | book.printIsbn(); 43 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/typescript/hello-world.js: -------------------------------------------------------------------------------- 1 | var myName = 'Packt'; 2 | // myName = 10; 3 | var age = 20; // number 4 | var existsFlag = true; // boolean 5 | var language = 'JavaScript'; // string 6 | var favoriteLanguage; 7 | var langs = ['JavaScript', 'Ruby', 'Python']; 8 | favoriteLanguage = langs[0]; 9 | function printName(person) { 10 | console.log(person.name); 11 | } 12 | var john = { name: 'John', age: 21 }; 13 | var mary = { name: 'Mary', age: 21, phone: '123-45678' }; 14 | printName(john); 15 | printName(mary); 16 | var MyObject = /** @class */ (function () { 17 | function MyObject() { 18 | } 19 | MyObject.prototype.compareTo = function (b) { 20 | if (this.age === b.age) { 21 | return 0; 22 | } 23 | return this.age > b.age ? 1 : -1; 24 | }; 25 | return MyObject; 26 | }()); 27 | function compareTwoObjects(a, b) { 28 | console.log(a.compareTo(b)); 29 | console.log(b.compareTo(a)); 30 | } 31 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/data-structures/stack-linked-list.ts: -------------------------------------------------------------------------------- 1 | import DoublyLinkedList from './doubly-linked-list'; 2 | 3 | export default class StackLinkedList { 4 | private items: DoublyLinkedList; 5 | 6 | constructor() { 7 | this.items = new DoublyLinkedList(); 8 | } 9 | 10 | push(element: T) { 11 | this.items.push(element); 12 | } 13 | 14 | pop() { 15 | if (this.isEmpty()) { 16 | return undefined; 17 | } 18 | const result = this.items.removeAt(this.size() - 1); 19 | return result; 20 | } 21 | 22 | peek() { 23 | if (this.isEmpty()) { 24 | return undefined; 25 | } 26 | return this.items.getElementAt(this.size() - 1).element; 27 | } 28 | 29 | isEmpty() { 30 | return this.items.isEmpty(); 31 | } 32 | 33 | size() { 34 | return this.items.size(); 35 | } 36 | 37 | clear() { 38 | this.items.clear(); 39 | } 40 | 41 | toString() { 42 | return this.items.toString(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/js/algorithms/dynamic-programming/min-coin-change.spec.js: -------------------------------------------------------------------------------- 1 | import 'mocha'; 2 | import { expect } from 'chai'; 3 | import { minCoinChange } from '../../../../src/js/index'; 4 | 5 | describe('Dynamic Programming: Min Coin Change', () => { 6 | 7 | it('works with amount 0', () => { 8 | expect(minCoinChange([1, 2, 3], 0)).to.deep.equal([]); 9 | }); 10 | 11 | it('works with amount 1', () => { 12 | expect(minCoinChange([1, 2, 3], 1)).to.deep.equal([1]); 13 | }); 14 | 15 | it('works with amount 2', () => { 16 | expect(minCoinChange([1, 2, 3], 2)).to.deep.equal([2]); 17 | }); 18 | 19 | it('works with amount 3', () => { 20 | expect(minCoinChange([1, 2, 3], 3)).to.deep.equal([3]); 21 | }); 22 | 23 | it('works with amount 4', () => { 24 | expect(minCoinChange([1, 2, 3], 4)).to.deep.equal([1, 3]); 25 | }); 26 | 27 | it('works with amount 6', () => { 28 | expect(minCoinChange([1, 2, 3], 6)).to.deep.equal([3, 3]); 29 | }); 30 | }); 31 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/ts/algorithms/dynamic-programming/min-coin-change.spec.ts: -------------------------------------------------------------------------------- 1 | import 'mocha'; 2 | import { expect } from 'chai'; 3 | import { minCoinChange } from '../../../../src/ts/index'; 4 | 5 | describe('Dynamic Programming: Min Coin Change', () => { 6 | 7 | it('works with amount 0', () => { 8 | expect(minCoinChange([1, 2, 3], 0)).to.deep.equal([]); 9 | }); 10 | 11 | it('works with amount 1', () => { 12 | expect(minCoinChange([1, 2, 3], 1)).to.deep.equal([1]); 13 | }); 14 | 15 | it('works with amount 2', () => { 16 | expect(minCoinChange([1, 2, 3], 2)).to.deep.equal([2]); 17 | }); 18 | 19 | it('works with amount 3', () => { 20 | expect(minCoinChange([1, 2, 3], 3)).to.deep.equal([3]); 21 | }); 22 | 23 | it('works with amount 4', () => { 24 | expect(minCoinChange([1, 2, 3], 4)).to.deep.equal([1, 3]); 25 | }); 26 | 27 | it('works with amount 6', () => { 28 | expect(minCoinChange([1, 2, 3], 6)).to.deep.equal([3, 3]); 29 | }); 30 | }); 31 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/algorithms/graph/dijkstra.js: -------------------------------------------------------------------------------- 1 | const INF = Number.MAX_SAFE_INTEGER; 2 | const minDistance = (dist, visited) => { 3 | let min = INF; 4 | let minIndex = -1; 5 | for (let v = 0; v < dist.length; v++) { 6 | if (visited[v] === false && dist[v] <= min) { 7 | min = dist[v]; 8 | minIndex = v; 9 | } 10 | } 11 | return minIndex; 12 | }; 13 | export const dijkstra = (graph, src) => { 14 | const dist = []; 15 | const visited = []; 16 | const { length } = graph; 17 | for (let i = 0; i < length; i++) { 18 | dist[i] = INF; 19 | visited[i] = false; 20 | } 21 | dist[src] = 0; 22 | for (let i = 0; i < length - 1; i++) { 23 | const u = minDistance(dist, visited); 24 | visited[u] = true; 25 | for (let v = 0; v < length; v++) { 26 | if (!visited[v] && graph[u][v] !== 0 && dist[u] !== INF && dist[u] + graph[u][v] < dist[v]) { 27 | dist[v] = dist[u] + graph[u][v]; 28 | } 29 | } 30 | } 31 | return dist; 32 | }; 33 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/15-ES2015-ES6-EnhancedObjectProperties.js: -------------------------------------------------------------------------------- 1 | // @ts-check 2 | /* eslint-disable */ 3 | 4 | //* ****** EcmaScript 2015 (ES6): Destructuring Assignment + Property Shorthand (https://goo.gl/VsLecp ) 5 | let [x, y] = ['a', 'b']; 6 | let obj = { x, y }; 7 | console.log(obj); // { x: "a", y: "b" } 8 | 9 | // swap (https://goo.gl/EyFAII) 10 | [x, y] = [y, x]; 11 | var temp = x; 12 | x = y; 13 | y = temp; 14 | 15 | // code above is the same as 16 | var x2 = 'a'; 17 | var y2 = 'b'; 18 | var obj2 = { x2: x2, y2: y2 }; 19 | console.log(obj2); // { x: "a", y: "b" } 20 | 21 | // Method Properties (https://goo.gl/DKU2PN) 22 | const hello = { 23 | name: 'abcdef', 24 | printHello() { 25 | console.log('Hello'); 26 | } 27 | }; 28 | console.log(hello.printHello()); 29 | 30 | // code above is the same as: 31 | var hello2 = { 32 | name: 'abcdef', 33 | printHello: function printHello() { 34 | console.log('Hello'); 35 | } 36 | }; 37 | console.log(hello2.printHello()); 38 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/ts/others/factorial.spec.ts: -------------------------------------------------------------------------------- 1 | import 'mocha'; 2 | import { expect } from 'chai'; 3 | import { factorialIterative, factorial } from '../../../src/ts/index'; 4 | 5 | describe('Factorial', () => { 6 | 7 | it('Iterative Factorial', () => { 8 | expect(factorialIterative(-1)).to.equal(undefined); 9 | expect(factorialIterative(0)).to.equal(1); 10 | expect(factorialIterative(1)).to.equal(1); 11 | expect(factorialIterative(2)).to.equal(2); 12 | expect(factorialIterative(3)).to.equal(6); 13 | expect(factorialIterative(4)).to.equal(24); 14 | expect(factorialIterative(5)).to.equal(120); 15 | }); 16 | 17 | it('Recursive Factorial', () => { 18 | expect(factorial(-1)).to.equal(undefined); 19 | expect(factorial(0)).to.equal(1); 20 | expect(factorial(1)).to.equal(1); 21 | expect(factorial(2)).to.equal(2); 22 | expect(factorial(3)).to.equal(6); 23 | expect(factorial(4)).to.equal(24); 24 | expect(factorial(5)).to.equal(120); 25 | }); 26 | }); 27 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/js/others/factorial.spec.js: -------------------------------------------------------------------------------- 1 | import 'mocha'; 2 | import { expect } from 'chai'; 3 | import { factorialIterative, factorial } from './../../../src/js/others/factorial'; 4 | 5 | describe('Factorial', () => { 6 | it('Iterative Factorial', () => { 7 | expect(factorialIterative(-1)).to.equal(undefined); 8 | expect(factorialIterative(0)).to.equal(1); 9 | expect(factorialIterative(1)).to.equal(1); 10 | expect(factorialIterative(2)).to.equal(2); 11 | expect(factorialIterative(3)).to.equal(6); 12 | expect(factorialIterative(4)).to.equal(24); 13 | expect(factorialIterative(5)).to.equal(120); 14 | }); 15 | 16 | it('Recursive Factorial', () => { 17 | expect(factorial(-1)).to.equal(undefined); 18 | expect(factorial(0)).to.equal(1); 19 | expect(factorial(1)).to.equal(1); 20 | expect(factorial(2)).to.equal(2); 21 | expect(factorial(3)).to.equal(6); 22 | expect(factorial(4)).to.equal(24); 23 | expect(factorial(5)).to.equal(120); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/algorithms/search/binary-search-recursive.js: -------------------------------------------------------------------------------- 1 | import { Compare, defaultCompare, DOES_NOT_EXIST } from '../../util'; 2 | import { quickSort } from '../sorting/quicksort'; 3 | 4 | function binarySearchRecursive(array, value, low, high, compareFn = defaultCompare) { 5 | if (low <= high) { 6 | const mid = Math.floor((low + high) / 2); 7 | const element = array[mid]; 8 | if (compareFn(element, value) === Compare.LESS_THAN) { 9 | return binarySearchRecursive(array, value, mid + 1, high, compareFn); 10 | } 11 | if (compareFn(element, value) === Compare.BIGGER_THAN) { 12 | return binarySearchRecursive(array, value, low, mid - 1, compareFn); 13 | } 14 | return mid; 15 | } 16 | return DOES_NOT_EXIST; 17 | } 18 | 19 | export function binarySearch(array, value, compareFn = defaultCompare) { 20 | const sortedArray = quickSort(array); 21 | const low = 0; 22 | const high = sortedArray.length - 1; 23 | return binarySearchRecursive(array, value, low, high, compareFn); 24 | } 25 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/algorithms/graph/prim.js: -------------------------------------------------------------------------------- 1 | const INF = Number.MAX_SAFE_INTEGER; 2 | const minKey = (graph, key, visited) => { 3 | // Initialize min value 4 | let min = INF; 5 | let minIndex = 0; 6 | for (let v = 0; v < graph.length; v++) { 7 | if (visited[v] === false && key[v] < min) { 8 | min = key[v]; 9 | minIndex = v; 10 | } 11 | } 12 | return minIndex; 13 | }; 14 | export const prim = graph => { 15 | const parent = []; 16 | const key = []; 17 | const visited = []; 18 | const { length } = graph; 19 | for (let i = 0; i < length; i++) { 20 | key[i] = INF; 21 | visited[i] = false; 22 | } 23 | key[0] = 0; 24 | parent[0] = -1; 25 | for (let i = 0; i < length - 1; i++) { 26 | const u = minKey(graph, key, visited); 27 | visited[u] = true; 28 | for (let v = 0; v < length; v++) { 29 | if (graph[u][v] && !visited[v] && graph[u][v] < key[v]) { 30 | parent[v] = u; 31 | key[v] = graph[u][v]; 32 | } 33 | } 34 | } 35 | return parent; 36 | }; 37 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/02-Variables.js: -------------------------------------------------------------------------------- 1 | // @ts-check 2 | /* eslint-disable */ 3 | 4 | var num = 1; // {1} 5 | num = 3; // {2} 6 | 7 | var price = 1.5; // {3} 8 | var myName = 'Packt'; // {4} 9 | var trueValue = true; // {5} 10 | var nullVar = null; // {6} 11 | var und; // {7} 12 | 13 | console.log('num: ' + num); 14 | console.log('myName: ' + myName); 15 | console.log('trueValue: ' + trueValue); 16 | console.log('price: ' + price); 17 | console.log('nullVar: ' + nullVar); 18 | console.log('und: ' + und); 19 | 20 | // ******* Variable Scope 21 | 22 | var myVariable = 'global'; 23 | myOtherVariable = 'global'; 24 | 25 | function myFunction() { 26 | var myVariable = 'local'; 27 | return myVariable; 28 | } 29 | 30 | function myOtherFunction() { 31 | myOtherVariable = 'local'; 32 | return myOtherVariable; 33 | } 34 | 35 | console.log(myVariable); //{1} 36 | console.log(myFunction()); //{2} 37 | 38 | console.log(myOtherVariable); //{3} 39 | console.log(myOtherFunction()); //{4} 40 | console.log(myOtherVariable); //{5} 41 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter04/01-StackSymbol.js: -------------------------------------------------------------------------------- 1 | const _items = Symbol('stackItems'); 2 | 3 | class Stack { 4 | constructor() { 5 | this[_items] = []; 6 | } 7 | 8 | push(element) { 9 | this[_items].push(element); 10 | } 11 | 12 | pop() { 13 | return this[_items].pop(); 14 | } 15 | 16 | peek() { 17 | return this[_items][this[_items].length - 1]; 18 | } 19 | 20 | isEmpty() { 21 | return this[_items].length === 0; 22 | } 23 | 24 | size() { 25 | return this[_items].length; 26 | } 27 | 28 | clear() { 29 | this[_items] = []; 30 | } 31 | 32 | print() { 33 | console.log(this.toString()); 34 | } 35 | 36 | toString() { 37 | return this[_items].toString(); 38 | } 39 | } 40 | 41 | const stack = new Stack(); 42 | const objectSymbols = Object.getOwnPropertySymbols(stack); 43 | console.log(objectSymbols.length); // 1 44 | console.log(objectSymbols); // [Symbol()] 45 | console.log(objectSymbols[0]); // Symbol() 46 | stack[objectSymbols[0]].push(1); 47 | stack.print(); // 5, 8, 1 48 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/algorithms/dynamic-programing/min-coin-change.ts: -------------------------------------------------------------------------------- 1 | export function minCoinChange(coins: number[], amount: number) { 2 | const cache: Array> = []; 3 | 4 | // tslint:disable-next-line:no-shadowed-variable 5 | const makeChange = function(amount: number) { 6 | if (!amount) { 7 | return []; 8 | } 9 | if (cache[amount]) { 10 | return cache[amount]; 11 | } 12 | let min: number[] = [], 13 | newMin, 14 | newAmount; 15 | for (let i = 0; i < coins.length; i++) { 16 | const coin = coins[i]; 17 | newAmount = amount - coin; 18 | if (newAmount >= 0) { 19 | newMin = makeChange(newAmount); 20 | } 21 | if ( 22 | newAmount >= 0 && 23 | (newMin.length < min.length - 1 || !min.length) && 24 | (newMin.length || !newAmount) 25 | ) { 26 | min = [coin].concat(newMin); 27 | // console.log('new Min ' + min + ' for ' + amount); 28 | } 29 | } 30 | return (cache[amount] = min); 31 | }; 32 | 33 | return makeChange(amount); 34 | } 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Packt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/05-EqualsOperators.js: -------------------------------------------------------------------------------- 1 | // @ts-check 2 | /* eslint-disable */ 3 | 4 | // Packt == true 5 | 6 | console.log('packt' ? true : false); 7 | // outputs true 8 | 9 | console.log('packt' == true); 10 | // 1 - converts Boolean using toNumber 11 | // 'packt' == 1 12 | // 2 - converts String using toNumber 13 | // NaN == 1 14 | // outputs false 15 | 16 | console.log('packt' == false); 17 | // 1 - converts Boolean using toNumber 18 | // 'packt' == 0 19 | // 2 - converts String using toNumber 20 | // NaN == 0 21 | // outputs false 22 | 23 | console.log([0] == true); 24 | // 1 - converts Boolean using toNumber 25 | // [0] == 1 26 | // 2 - converts Object using toPrimitive 27 | // 2.1 - [0].valueOf() is not primitive 28 | // 2.2 - [0].toString is 0 29 | // 0 == 1 30 | // outputs false 31 | 32 | //* ****************************** === 33 | console.log('packt' === true); // false 34 | 35 | console.log('packt' === 'packt'); // true 36 | 37 | var person1 = { name: 'John' }; 38 | var person2 = { name: 'John' }; 39 | console.log(person1 === person2); // false, different objects 40 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/algorithms/backtracking/rat-in-maze.js: -------------------------------------------------------------------------------- 1 | function isSafe(maze, x, y) { 2 | const n = maze.length; 3 | if (x >= 0 && y >= 0 && x < n && y < n && maze[x][y] !== 0) { 4 | return true; 5 | } 6 | return false; 7 | } 8 | 9 | function findPath(maze, x, y, solution) { 10 | const n = maze.length; 11 | if (x === n - 1 && y === n - 1) { 12 | solution[x][y] = 1; 13 | return true; 14 | } 15 | if (isSafe(maze, x, y) === true) { 16 | solution[x][y] = 1; 17 | if (findPath(maze, x + 1, y, solution)) { 18 | return true; 19 | } 20 | if (findPath(maze, x, y + 1, solution)) { 21 | return true; 22 | } 23 | solution[x][y] = 0; 24 | return false; 25 | } 26 | return false; 27 | } 28 | 29 | export function ratInAMaze(maze) { 30 | const solution = []; 31 | for (let i = 0; i < maze.length; i++) { 32 | solution[i] = []; 33 | for (let j = 0; j < maze[i].length; j++) { 34 | solution[i][j] = 0; 35 | } 36 | } 37 | if (findPath(maze, 0, 0, solution) === true) { 38 | return solution; 39 | } 40 | return 'NO PATH FOUND'; 41 | } 42 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/algorithms/graph/dijkstra.ts: -------------------------------------------------------------------------------- 1 | const INF = Number.MAX_SAFE_INTEGER; 2 | 3 | const minDistance = (dist: number[], visited: boolean[]) => { 4 | let min = INF; 5 | let minIndex = -1; 6 | 7 | for (let v = 0; v < dist.length; v++) { 8 | if (visited[v] === false && dist[v] <= min) { 9 | min = dist[v]; 10 | minIndex = v; 11 | } 12 | } 13 | 14 | return minIndex; 15 | }; 16 | 17 | export const dijkstra = (graph: number[][], src: number) => { 18 | const dist: number[] = []; 19 | const visited: boolean[] = []; 20 | const length = graph.length; 21 | 22 | for (let i = 0; i < length; i++) { 23 | dist[i] = INF; 24 | visited[i] = false; 25 | } 26 | 27 | dist[src] = 0; 28 | 29 | for (let i = 0; i < length - 1; i++) { 30 | const u = minDistance(dist, visited); 31 | 32 | visited[u] = true; 33 | 34 | for (let v = 0; v < length; v++) { 35 | if (!visited[v] && graph[u][v] !== 0 && dist[u] !== INF && dist[u] + graph[u][v] < dist[v]) { 36 | dist[v] = dist[u] + graph[u][v]; 37 | } 38 | } 39 | } 40 | 41 | return dist; 42 | }; 43 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/algorithms/search/interpolation-search.js: -------------------------------------------------------------------------------- 1 | import { 2 | biggerEquals, 3 | Compare, 4 | defaultCompare, 5 | defaultEquals, 6 | defaultDiff, 7 | DOES_NOT_EXIST, 8 | lesserEquals 9 | } from '../../util'; 10 | 11 | export function interpolationSearch( 12 | array, 13 | value, 14 | compareFn = defaultCompare, 15 | equalsFn = defaultEquals, 16 | diffFn = defaultDiff 17 | ) { 18 | const { length } = array; 19 | let low = 0; 20 | let high = length - 1; 21 | let position = -1; 22 | let delta = -1; 23 | while ( 24 | low <= high && 25 | biggerEquals(value, array[low], compareFn) && 26 | lesserEquals(value, array[high], compareFn) 27 | ) { 28 | delta = diffFn(value, array[low]) / diffFn(array[high], array[low]); 29 | position = low + Math.floor((high - low) * delta); 30 | if (equalsFn(array[position], value)) { 31 | return position; 32 | } 33 | if (compareFn(array[position], value) === Compare.LESS_THAN) { 34 | low = position + 1; 35 | } else { 36 | high = position - 1; 37 | } 38 | } 39 | return DOES_NOT_EXIST; 40 | } 41 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/algorithms/sorting/heap-sort.js: -------------------------------------------------------------------------------- 1 | import { defaultCompare, swap } from '../../util'; 2 | 3 | function heapify(array, index, heapSize, compareFn) { 4 | let largest = index; 5 | const left = (2 * index) + 1; 6 | const right = (2 * index) + 2; 7 | if (left < heapSize && compareFn(array[left], array[index]) > 0) { 8 | largest = left; 9 | } 10 | if (right < heapSize && compareFn(array[right], array[largest]) > 0) { 11 | largest = right; 12 | } 13 | if (largest !== index) { 14 | swap(array, index, largest); 15 | heapify(array, largest, heapSize, compareFn); 16 | } 17 | } 18 | 19 | function buildMaxHeap(array, compareFn) { 20 | for (let i = Math.floor(array.length / 2); i >= 0; i -= 1) { 21 | heapify(array, i, array.length, compareFn); 22 | } 23 | return array; 24 | } 25 | 26 | export default function heapSort(array, compareFn = defaultCompare) { 27 | let heapSize = array.length; 28 | buildMaxHeap(array, compareFn); 29 | while (heapSize > 1) { 30 | swap(array, 0, --heapSize); 31 | heapify(array, 0, heapSize, compareFn); 32 | } 33 | return array; 34 | } 35 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/algorithms/sorting/quicksort.js: -------------------------------------------------------------------------------- 1 | import { Compare, defaultCompare, swap } from '../../util'; 2 | 3 | function partition(array, left, right, compareFn) { 4 | const pivot = array[Math.floor((right + left) / 2)]; 5 | let i = left; 6 | let j = right; 7 | 8 | while (i <= j) { 9 | while (compareFn(array[i], pivot) === Compare.LESS_THAN) { 10 | i++; 11 | } 12 | while (compareFn(array[j], pivot) === Compare.BIGGER_THAN) { 13 | j--; 14 | } 15 | if (i <= j) { 16 | swap(array, i, j); 17 | i++; 18 | j--; 19 | } 20 | } 21 | return i; 22 | } 23 | function quick(array, left, right, compareFn) { 24 | let index; 25 | if (array.length > 1) { 26 | index = partition(array, left, right, compareFn); 27 | if (left < index - 1) { 28 | quick(array, left, index - 1, compareFn); 29 | } 30 | if (index < right) { 31 | quick(array, index, right, compareFn); 32 | } 33 | } 34 | return array; 35 | } 36 | export function quickSort(array, compareFn = defaultCompare) { 37 | return quick(array, 0, array.length - 1, compareFn); 38 | } 39 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter01_02/typescript/hello-world.ts: -------------------------------------------------------------------------------- 1 | let myName = 'Packt'; 2 | // myName = 10; 3 | 4 | let age = 20; // number 5 | let existsFlag = true; // boolean 6 | let language = 'JavaScript'; // string 7 | 8 | let favoriteLanguage: string; 9 | let langs = ['JavaScript', 'Ruby', 'Python']; 10 | favoriteLanguage = langs[0]; 11 | 12 | interface Person { 13 | name: string; 14 | age: number; 15 | } 16 | 17 | function printName(person: Person) { 18 | console.log(person.name); 19 | } 20 | 21 | const john = { name: 'John', age: 21 }; 22 | const mary = { name: 'Mary', age: 21, phone: '123-45678' }; 23 | printName(john); 24 | printName(mary); 25 | 26 | interface Comparable { 27 | compareTo(b: T): number; 28 | } 29 | 30 | class MyObject implements Comparable { 31 | age: number; 32 | 33 | compareTo(b: MyObject): number { 34 | if (this.age === b.age) { 35 | return 0; 36 | } 37 | return this.age > b.age ? 1 : -1; 38 | } 39 | } 40 | 41 | function compareTwoObjects(a: MyObject, b: MyObject) { 42 | console.log(a.compareTo(b)); 43 | console.log(b.compareTo(a)); 44 | } 45 | 46 | 47 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/algorithms/search/interpolation-search.ts: -------------------------------------------------------------------------------- 1 | import { 2 | biggerEquals, 3 | Compare, 4 | defaultCompare, 5 | defaultEquals, 6 | defaultDiff, 7 | DOES_NOT_EXIST, 8 | lesserEquals 9 | } from '../../util'; 10 | 11 | export function interpolationSearch( 12 | array: T[], 13 | value: T, 14 | compareFn = defaultCompare, 15 | equalsFn = defaultEquals, 16 | diffFn = defaultDiff 17 | ) { 18 | const { length } = array; 19 | let low = 0; 20 | let high = length - 1; 21 | let position = -1; 22 | let delta = -1; 23 | while ( 24 | low <= high && 25 | biggerEquals(value, array[low], compareFn) && 26 | lesserEquals(value, array[high], compareFn) 27 | ) { 28 | delta = diffFn(value, array[low]) / diffFn(array[high], array[low]); 29 | position = low + Math.floor((high - low) * delta); 30 | if (equalsFn(array[position], value)) { 31 | return position; 32 | } 33 | if (compareFn(array[position], value) === Compare.LESS_THAN) { 34 | low = position + 1; 35 | } else { 36 | high = position - 1; 37 | } 38 | } 39 | 40 | return DOES_NOT_EXIST; 41 | } 42 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/others/base-converter.ts: -------------------------------------------------------------------------------- 1 | import Stack from '../data-structures/stack'; 2 | 3 | export function decimalToBinary(decNumber: number) { 4 | const remStack = new Stack(); 5 | let rem: number; 6 | let binaryString = ''; 7 | 8 | while (decNumber > 0) { 9 | rem = Math.floor(decNumber % 2); 10 | remStack.push(rem); 11 | decNumber = Math.floor(decNumber / 2); 12 | } 13 | 14 | while (!remStack.isEmpty()) { 15 | binaryString += remStack.pop().toString(); 16 | } 17 | 18 | return binaryString; 19 | } 20 | 21 | export function baseConverter(decNumber: number, base: number) { 22 | const remStack = new Stack(); 23 | const digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; 24 | let rem: number; 25 | let baseString = ''; 26 | 27 | if (!(base >= 2 && base <= 36)) { 28 | return ''; 29 | } 30 | 31 | while (decNumber > 0) { 32 | rem = Math.floor(decNumber % base); 33 | remStack.push(rem); 34 | decNumber = Math.floor(decNumber / base); 35 | } 36 | 37 | while (!remStack.isEmpty()) { 38 | baseString += digits[remStack.pop()]; 39 | } 40 | 41 | return baseString; 42 | } 43 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/examples/chapter03/02-CreatingAndInitialingArrays.js: -------------------------------------------------------------------------------- 1 | // @ts-check 2 | /* eslint-disable */ 3 | 4 | let daysOfWeek = new Array(); // {1} 5 | 6 | daysOfWeek = new Array(7); // {2} 7 | 8 | daysOfWeek = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'); // {3} 9 | 10 | // preferred 11 | daysOfWeek = []; 12 | 13 | daysOfWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; // {3} 14 | 15 | console.log('daysOfWeek.length', daysOfWeek.length); 16 | 17 | for (let i = 0; i < daysOfWeek.length; i++) { 18 | console.log(`daysOfWeek[${i}]`, daysOfWeek[i]); 19 | } 20 | 21 | // fibonacci numbers 22 | // 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... 23 | const fibonacci = []; // {1} 24 | fibonacci[1] = 1; // {2} 25 | fibonacci[2] = 1; // {3} 26 | 27 | for (let i = 3; i < 20; i++) { 28 | fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2]; // //{4} 29 | } 30 | 31 | for (let i = 1; i < fibonacci.length; i++) { // {5} 32 | console.log(`fibonacci[${i}]`, fibonacci[i]); // {6} 33 | } 34 | 35 | // instead of {5} and {6} we can simply use 36 | console.log('fibonacci', fibonacci); 37 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/algorithms/search/binary-search-recursive.ts: -------------------------------------------------------------------------------- 1 | import { Compare, defaultCompare, DOES_NOT_EXIST } from '../../util'; 2 | import { quickSort } from '../sorting/quicksort'; 3 | 4 | function binarySearchRecursive( 5 | array: T[], 6 | value: T, 7 | low: number, 8 | high: number, 9 | compareFn = defaultCompare 10 | ): number { 11 | if (low <= high) { 12 | const mid = Math.floor((low + high) / 2); 13 | const element = array[mid]; 14 | 15 | if (compareFn(element, value) === Compare.LESS_THAN) { 16 | return binarySearchRecursive(array, value, mid + 1, high, compareFn); 17 | } else if (compareFn(element, value) === Compare.BIGGER_THAN) { 18 | return binarySearchRecursive(array, value, low, mid - 1, compareFn); 19 | } else { 20 | return mid; 21 | } 22 | } 23 | return DOES_NOT_EXIST; 24 | } 25 | 26 | export function binarySearch(array: T[], value: T, compareFn = defaultCompare) { 27 | const sortedArray = quickSort(array); 28 | const low = 0; 29 | const high = sortedArray.length - 1; 30 | 31 | return binarySearchRecursive(array, value, low, high, compareFn); 32 | } 33 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/algorithms/string/knuth-morris-pratt.ts: -------------------------------------------------------------------------------- 1 | const buildTable = (pattern: string) => { 2 | const length = pattern.length; 3 | const table = []; 4 | let position = 2; 5 | let cnd = 0; 6 | 7 | table[0] = -1; 8 | table[1] = 0; 9 | 10 | while (position < length) { 11 | if (pattern[position - 1] === pattern[cnd]) { 12 | table[position++] = ++cnd; 13 | } else if (cnd > 0) { 14 | cnd = table[cnd]; 15 | } else { 16 | table[position++] = 0; 17 | } 18 | } 19 | 20 | return table; 21 | }; 22 | 23 | const knuthMorrisPratt = (text: string, pattern: string) => { 24 | const textLength = text.length; 25 | const patternLength = pattern.length; 26 | let m = 0; 27 | let i = 0; 28 | const table = buildTable(pattern); 29 | 30 | while (m + i < textLength) { 31 | if (pattern[i] === text[m + i]) { 32 | if (i === patternLength - 1) { 33 | return m; 34 | } 35 | i++; 36 | } else if (table[i] >= 0) { 37 | i = table[i]; 38 | m = m + i - table[i]; 39 | } else { 40 | i = 0; 41 | m++; 42 | } 43 | } 44 | 45 | return textLength; 46 | }; 47 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/others/base-converter.js: -------------------------------------------------------------------------------- 1 | // @ts-check 2 | import Stack from '../data-structures/stack'; 3 | 4 | export function decimalToBinary(decNumber) { 5 | const remStack = new Stack(); 6 | let number = decNumber; 7 | let rem; 8 | let binaryString = ''; 9 | 10 | while (number > 0) { 11 | rem = Math.floor(number % 2); 12 | remStack.push(rem); 13 | number = Math.floor(number / 2); 14 | } 15 | 16 | while (!remStack.isEmpty()) { 17 | binaryString += remStack.pop().toString(); 18 | } 19 | 20 | return binaryString; 21 | } 22 | 23 | export function baseConverter(decNumber, base) { 24 | const remStack = new Stack(); 25 | const digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; 26 | let number = decNumber; 27 | let rem; 28 | let baseString = ''; 29 | 30 | if (!(base >= 2 && base <= 36)) { 31 | return ''; 32 | } 33 | 34 | while (number > 0) { 35 | rem = Math.floor(number % base); 36 | remStack.push(rem); 37 | number = Math.floor(number / base); 38 | } 39 | 40 | while (!remStack.isEmpty()) { 41 | baseString += digits[remStack.pop()]; 42 | } 43 | 44 | return baseString; 45 | } 46 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/data-structures/stack.js: -------------------------------------------------------------------------------- 1 | // @ts-check 2 | 3 | export default class Stack { 4 | constructor() { 5 | this.count = 0; 6 | this.items = {}; 7 | } 8 | push(element) { 9 | this.items[this.count] = element; 10 | this.count++; 11 | } 12 | pop() { 13 | if (this.isEmpty()) { 14 | return undefined; 15 | } 16 | this.count--; 17 | const result = this.items[this.count]; 18 | delete this.items[this.count]; 19 | return result; 20 | } 21 | peek() { 22 | if (this.isEmpty()) { 23 | return undefined; 24 | } 25 | return this.items[this.count - 1]; 26 | } 27 | isEmpty() { 28 | return this.count === 0; 29 | } 30 | size() { 31 | return this.count; 32 | } 33 | clear() { 34 | /* while (!this.isEmpty()) { 35 | this.pop(); 36 | } */ 37 | this.items = {}; 38 | this.count = 0; 39 | } 40 | toString() { 41 | if (this.isEmpty()) { 42 | return ''; 43 | } 44 | let objString = `${this.items[0]}`; 45 | for (let i = 1; i < this.count; i++) { 46 | objString = `${objString},${this.items[i]}`; 47 | } 48 | return objString; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/algorithms/graph/prim.ts: -------------------------------------------------------------------------------- 1 | const INF = Number.MAX_SAFE_INTEGER; 2 | 3 | const minKey = (graph: number[][], key: number[], visited: boolean[]) => { 4 | // Initialize min value 5 | let min = INF; 6 | let minIndex = 0; 7 | 8 | for (let v = 0; v < graph.length; v++) { 9 | if (visited[v] === false && key[v] < min) { 10 | min = key[v]; 11 | minIndex = v; 12 | } 13 | } 14 | 15 | return minIndex; 16 | }; 17 | 18 | export const prim = (graph: number[][]) => { 19 | const parent: number[] = []; 20 | const key: number[] = []; 21 | const visited: boolean[] = []; 22 | const length = graph.length; 23 | 24 | for (let i = 0; i < length; i++) { 25 | key[i] = INF; 26 | visited[i] = false; 27 | } 28 | 29 | key[0] = 0; 30 | parent[0] = -1; 31 | 32 | for (let i = 0; i < length - 1; i++) { 33 | const u = minKey(graph, key, visited); 34 | visited[u] = true; 35 | 36 | for (let v = 0; v < length; v++) { 37 | if (graph[u][v] && visited[v] === false && graph[u][v] < key[v]) { 38 | parent[v] = u; 39 | key[v] = graph[u][v]; 40 | } 41 | } 42 | } 43 | 44 | return parent; 45 | }; 46 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/algorithms/sorting/heap-sort.ts: -------------------------------------------------------------------------------- 1 | import { defaultCompare, ICompareFunction, swap } from '../../util'; 2 | 3 | function heapify(array: any[], index: number, heapSize: number, compareFn: ICompareFunction) { 4 | let largest = index; 5 | const left = (2 * index) + 1; 6 | const right = (2 * index) + 2; 7 | 8 | if (left < heapSize && compareFn(array[left], array[index]) > 0) { 9 | largest = left; 10 | } 11 | 12 | if (right < heapSize && compareFn(array[right], array[largest]) > 0) { 13 | largest = right; 14 | } 15 | 16 | if (largest !== index) { 17 | swap(array, index, largest); 18 | heapify(array, largest, heapSize, compareFn); 19 | } 20 | } 21 | 22 | function buildMaxHeap(array: any[], compareFn: ICompareFunction) { 23 | for (let i = Math.floor(array.length / 2); i >= 0; i -= 1) { 24 | heapify(array, i, array.length, compareFn); 25 | } 26 | return array; 27 | } 28 | 29 | export default function heapSort(array: any[], compareFn = defaultCompare) { 30 | let heapSize = array.length; 31 | buildMaxHeap(array, compareFn); 32 | while (heapSize > 1) { 33 | swap(array, 0, --heapSize); 34 | heapify(array, 0, heapSize, compareFn); 35 | } 36 | return array; 37 | } 38 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/data-structures/sorted-linked-list.js: -------------------------------------------------------------------------------- 1 | import { Compare, defaultCompare, defaultEquals } from '../util'; 2 | import LinkedList from './linked-list'; 3 | 4 | export default class SortedLinkedList extends LinkedList { 5 | constructor(equalsFn = defaultEquals, compareFn = defaultCompare) { 6 | super(equalsFn); 7 | this.equalsFn = equalsFn; 8 | this.compareFn = compareFn; 9 | } 10 | push(element) { 11 | if (this.isEmpty()) { 12 | super.push(element); 13 | } else { 14 | const index = this.getIndexNextSortedElement(element); 15 | super.insert(element, index); 16 | } 17 | } 18 | insert(element, index = 0) { 19 | if (this.isEmpty()) { 20 | return super.insert(element, index === 0 ? index : 0); 21 | } 22 | const pos = this.getIndexNextSortedElement(element); 23 | return super.insert(element, pos); 24 | } 25 | getIndexNextSortedElement(element) { 26 | let current = this.head; 27 | let i = 0; 28 | for (; i < this.size() && current; i++) { 29 | const comp = this.compareFn(element, current.element); 30 | if (comp === Compare.LESS_THAN) { 31 | return i; 32 | } 33 | current = current.next; 34 | } 35 | return i; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/algorithms/string/rabin-karp.ts: -------------------------------------------------------------------------------- 1 | const base = 997; 2 | 3 | const hash = (word: string) => { 4 | let h = 0; 5 | 6 | for (let i = 0; i < word.length; i++) { 7 | h += word.charCodeAt(i) * Math.pow(base, word.length - i - 1); 8 | } 9 | 10 | return h; 11 | }; 12 | 13 | const rabinKarp = (text: string, pattern: string) => { 14 | if (pattern == null || pattern.length === 0) { 15 | return 0; 16 | } 17 | 18 | const hashPattern = hash(pattern); 19 | let currentSubstring = text.substring(0, pattern.length); 20 | let hashCurrentSubstring; 21 | 22 | for (let i = pattern.length; i <= text.length; i++) { 23 | if (hashCurrentSubstring === undefined) { 24 | hashCurrentSubstring = hash(currentSubstring); 25 | } else { 26 | hashCurrentSubstring -= currentSubstring.charCodeAt(0) * Math.pow(base, pattern.length - 1); 27 | hashCurrentSubstring *= base; 28 | hashCurrentSubstring += text.charCodeAt(i); 29 | 30 | currentSubstring = currentSubstring.substring(1) + text[i]; 31 | } 32 | 33 | if (hashPattern === hashCurrentSubstring && pattern === currentSubstring) { 34 | return i === pattern.length ? 0 : i - pattern.length + 1; 35 | } 36 | } 37 | 38 | return -1; 39 | }; 40 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/js/others/balanced-symbols.spec.js: -------------------------------------------------------------------------------- 1 | import 'mocha'; 2 | import { expect } from 'chai'; 3 | import { parenthesesChecker } from '../../../src/js/others/balanced-symbols'; 4 | 5 | describe('Balanced Symbols', () => { 6 | it('empty to be falsy', () => { 7 | expect(parenthesesChecker('')).to.equal(true); 8 | }); 9 | 10 | it('{ to be falsy', () => { 11 | expect(parenthesesChecker('{')).to.equal(false); 12 | }); 13 | 14 | it('} to be falsy', () => { 15 | expect(parenthesesChecker('}')).to.equal(false); 16 | }); 17 | 18 | it('11 to be falsy', () => { 19 | expect(parenthesesChecker('11')).to.equal(false); 20 | }); 21 | 22 | it('{11 to be falsy', () => { 23 | expect(parenthesesChecker('{11')).to.equal(false); 24 | }); 25 | 26 | it('{([1])} to be falsy', () => { 27 | expect(parenthesesChecker('{([1])}')).to.equal(false); 28 | }); 29 | 30 | it('{([])} to be truthy', () => { 31 | expect(parenthesesChecker('{([])}')).to.equal(true); 32 | }); 33 | 34 | it('{{([][])}()} to be truthy', () => { 35 | expect(parenthesesChecker('{{([][])}()}')).to.equal(true); 36 | }); 37 | 38 | it('[{()] to be falsy', () => { 39 | expect(parenthesesChecker('[{()]')).to.equal(false); 40 | }); 41 | }); 42 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/ts/data-structures/stack.ts: -------------------------------------------------------------------------------- 1 | export default class Stack { 2 | private count: number; 3 | private items: any; 4 | 5 | constructor() { 6 | this.count = 0; 7 | this.items = {}; 8 | } 9 | 10 | push(element: T) { 11 | this.items[this.count] = element; 12 | this.count++; 13 | } 14 | 15 | pop() { 16 | if (this.isEmpty()) { 17 | return undefined; 18 | } 19 | this.count--; 20 | const result = this.items[this.count]; 21 | delete this.items[this.count]; 22 | return result; 23 | } 24 | 25 | peek() { 26 | if (this.isEmpty()) { 27 | return undefined; 28 | } 29 | return this.items[this.count - 1]; 30 | } 31 | 32 | isEmpty() { 33 | return this.count === 0; 34 | } 35 | 36 | size() { 37 | return this.count; 38 | } 39 | 40 | clear() { 41 | /* while (!this.isEmpty()) { 42 | this.pop(); 43 | } */ 44 | 45 | this.items = {}; 46 | this.count = 0; 47 | } 48 | 49 | toString() { 50 | if (this.isEmpty()) { 51 | return ''; 52 | } 53 | let objString = `${this.items[0]}`; 54 | for (let i = 1; i < this.count; i++) { 55 | objString = `${objString},${this.items[i]}`; 56 | } 57 | return objString; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/test/ts/others/balanced-symbols.spec.ts: -------------------------------------------------------------------------------- 1 | import 'mocha'; 2 | import { expect } from 'chai'; 3 | import { parenthesesChecker } from '../../../src/ts/others/balanced-symbols'; 4 | 5 | describe('Balanced Symbols', () => { 6 | 7 | it('empty to be falsy', () => { 8 | expect(parenthesesChecker('')).to.equal(true); 9 | }); 10 | 11 | it('{ to be falsy', () => { 12 | expect(parenthesesChecker('{')).to.equal(false); 13 | }); 14 | 15 | it('} to be falsy', () => { 16 | expect(parenthesesChecker('}')).to.equal(false); 17 | }); 18 | 19 | it('11 to be falsy', () => { 20 | expect(parenthesesChecker('11')).to.equal(false); 21 | }); 22 | 23 | it('{11 to be falsy', () => { 24 | expect(parenthesesChecker('{11')).to.equal(false); 25 | }); 26 | 27 | it('{([1])} to be falsy', () => { 28 | expect(parenthesesChecker('{([1])}')).to.equal(false); 29 | }); 30 | 31 | it('{([])} to be truthy', () => { 32 | expect(parenthesesChecker('{([])}')).to.equal(true); 33 | }); 34 | 35 | it('{{([][])}()} to be truthy', () => { 36 | expect(parenthesesChecker('{{([][])}()}')).to.equal(true); 37 | }); 38 | 39 | it('[{()] to be falsy', () => { 40 | expect(parenthesesChecker('[{()]')).to.equal(false); 41 | }); 42 | }); 43 | -------------------------------------------------------------------------------- /LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/src/js/algorithms/sorting/bucket-sort.js: -------------------------------------------------------------------------------- 1 | import { insertionSort } from './insertion-sort'; 2 | 3 | function createBuckets(array, bucketSize) { 4 | let minValue = array[0]; 5 | let maxValue = array[0]; 6 | for (let i = 1; i < array.length; i++) { 7 | if (array[i] < minValue) { 8 | minValue = array[i]; 9 | } else if (array[i] > maxValue) { 10 | maxValue = array[i]; 11 | } 12 | } 13 | const bucketCount = Math.floor((maxValue - minValue) / bucketSize) + 1; 14 | const buckets = []; 15 | for (let i = 0; i < bucketCount; i++) { 16 | buckets[i] = []; 17 | } 18 | for (let i = 0; i < array.length; i++) { 19 | buckets[Math.floor((array[i] - minValue) / bucketSize)].push(array[i]); 20 | } 21 | return buckets; 22 | } 23 | function sortBuckets(buckets) { 24 | const sortedArray = []; 25 | for (let i = 0; i < buckets.length; i++) { 26 | if (buckets[i] != null) { 27 | insertionSort(buckets[i]); 28 | sortedArray.push(...buckets[i]); 29 | } 30 | } 31 | return sortedArray; 32 | } 33 | export function bucketSort(array, bucketSize = 5) { 34 | if (array.length < 2) { 35 | return array; 36 | } 37 | const buckets = createBuckets(array, bucketSize); 38 | return sortBuckets(buckets); 39 | } 40 | --------------------------------------------------------------------------------