diff --git a/.bithoundrc b/.bithoundrc
new file mode 100644
index 00000000..46722b1d
--- /dev/null
+++ b/.bithoundrc
@@ -0,0 +1,19 @@
+{
+ "ignore": [
+ "**/.vscode/**",
+ "**/node_modules/**",
+ "**/dist/**",
+ "**/examples/**"
+ ],
+ "test": [
+ "**/test/**"
+ ],
+ "critics": {
+ "lint": {
+ "engine": "eslint"
+ },
+ "wc": {
+ "limit": 1000
+ }
+ }
+}
diff --git a/.eslintrc.json b/.eslintrc.json
index 8c4a28cc..ac117e1a 100644
--- a/.eslintrc.json
+++ b/.eslintrc.json
@@ -24,6 +24,16 @@
"no-console": 0,
"import/prefer-default-export": 0,
"comma-dangle": 0,
- "no-underscore-dangle": 0
+ "no-underscore-dangle": 0,
+ "no-param-reassign": 0,
+ "no-return-assign": 0,
+ "no-restricted-globals": 0,
+ "no-multi-assign": 0,
+ "prefer-destructuring": ["error", {"object": true, "array": false}],
+ "padded-blocks": 0,
+ "no-sparse-arrays": 0,
+ "array-bracket-spacing": 0,
+ "import/no-named-as-default": 0,
+ "implicit-arrow-linebreak": 0
}
}
diff --git a/.firebaserc b/.firebaserc
new file mode 100644
index 00000000..d33152ab
--- /dev/null
+++ b/.firebaserc
@@ -0,0 +1,5 @@
+{
+ "projects": {
+ "default": "javascript-ds-algorithms-book"
+ }
+}
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 00000000..cdaaeff1
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1,40 @@
+# Handle line endings automatically for files detected as text
+# and leave all files detected as binary untouched.
+* text=auto
+
+#
+# The above will handle all files NOT found below
+#
+# These files are text and should be normalized (Convert crlf => lf)
+*.css eol=lf
+*.df eol=lf
+*.htm eol=lf
+*.html eol=lf
+*.java eol=lf
+*.js eol=lf
+*.json eol=lf
+*.jsp eol=lf
+*.jspf eol=lf
+*.jspx eol=lf
+*.properties eol=lf
+*.sh eol=lf
+*.tld eol=lf
+*.txt eol=lf
+*.tag eol=lf
+*.tagx eol=lf
+*.xml eol=lf
+*.yml eol=lf
+
+# These files are binary and should be left untouched
+# (binary is a macro for -text -diff)
+*.class binary
+*.dll binary
+*.ear binary
+*.gif binary
+*.ico binary
+*.jar binary
+*.jpg binary
+*.jpeg binary
+*.png binary
+*.so binary
+*.war binary
diff --git a/.gitignore b/.gitignore
index 25cf67d5..0c2aa12a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,5 +6,6 @@ coverage
coverage.lcov
mochawesome-report/*
dist/js/*
+dist-/*
dist/ts/*
snippet.js
diff --git a/.travis.yml b/.travis.yml
index 2cd47824..7def9b91 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,12 +1,15 @@
language: node_js
node_js:
- node
+before_script:
+ - npm install -g --silent firebase-tools
install:
- npm install
script:
- npm run go
after_success:
- npm run coverage
+ - test $TRAVIS_BRANCH = "master" && test $TRAVIS_PULL_REQUEST = "false" && firebase deploy --token $FIREBASE_TOKEN --non-interactive
notifications:
email:
on_failure: change
diff --git a/.vscode/launch.json b/.vscode/launch.json
index 24399b76..85c3207b 100644
--- a/.vscode/launch.json
+++ b/.vscode/launch.json
@@ -8,7 +8,7 @@
"type": "chrome",
"request": "launch",
"name": "Chrome",
- "url": "http://127.0.0.1:8887/examples",
+ "url": "http://127.0.0.1:8080/examples",
"webRoot": "${workspaceRoot}"
},
{
@@ -41,8 +41,9 @@
"-r",
"ts-node/register",
"--colors",
- "${workspaceRoot}/test/ts/**/*.spec.ts"
+ "${workspaceRoot}/test/ts/**/**/*.spec.ts"
],
+ "protocol": "auto",
"internalConsoleOptions": "openOnSessionStart"
}
]
diff --git a/.vscode/settings.json b/.vscode/settings.json
new file mode 100644
index 00000000..55712c19
--- /dev/null
+++ b/.vscode/settings.json
@@ -0,0 +1,3 @@
+{
+ "typescript.tsdk": "node_modules/typescript/lib"
+}
\ No newline at end of file
diff --git a/CHANGELOG.md b/CHANGELOG.md
new file mode 100644
index 00000000..ade741a9
--- /dev/null
+++ b/CHANGELOG.md
@@ -0,0 +1,7 @@
+# Change Log
+All notable changes to the "javascript-datastructures-algorithms" third edition source code bundle will be documented in this file.
+
+Based on [Keep a Changelog](http://keepachangelog.com/).
+
+## [Unreleased]
+- Initial release
diff --git a/README.md b/README.md
index 0be9ad9b..96845564 100644
--- a/README.md
+++ b/README.md
@@ -1,31 +1,68 @@
Learning JavaScript Data Structures and Algorithms
====================================
-[](https://travis-ci.org/loiane/javascript-datastructures-algorithms)
-[](https://codecov.io/gh/loiane/javascript-datastructures-algorithms)
+[](https://travis-ci.org/loiane/javascript-datastructures-algorithms)
+[](https://codecov.io/gh/loiane/javascript-datastructures-algorithms)
+[](https://david-dm.org/loiane/javascript-datastructures-algorithms?type=dev)
+[](https://david-dm.org/loiane/javascript-datastructures-algorithms)
+[](https://greenkeeper.io/)
-Source code of **Learning JavaScript Data Structures and Algorithms** book, third edition.
-Work in Progress.
+Source code of **Learning JavaScript Data Structures and Algorithms** book, third edition.
## List of available chapters:
-* 01: [JavaScript, ECMAScript and TypeScript: a quick overview](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter01)
-* 02: [Arrays](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter02)
-* 03: [Stacks](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter03)
+* 01: [JavaScript: a quick overview](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter01_02)
+* 02: [ECMAScript and TypeScript Introduction](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter01_02)
+* 03: [Arrays](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter03)
+* 04: [Stacks](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter04)
+* 05: [Queues and Deques](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter05)
+* 06: [LinkedLists](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter06)
+* 07: [Sets](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter07)
+* 08: [Dictionaries and Hashes](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter08)
+* 09: [Recursion](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter09)
+* 10: [Trees](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter10)
+* 11: [Heap](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter11)
+* 12: [Graphs](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter12)
+* 13: [Sorting and Searching Algorithms](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter13)
+* 14: [Algorithm Design and Techniques](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter14)
+* 15: [Algorithm Complexity](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter15)
-## Thrid Edition Updates
+### Third Edition Updates
-* Algorithms using ES2015+
-* Creation of a Data Structure and Algorithms library that can be used in the browser or with Node.js
+* Algorithms using ES2015+ (ES6+)
+* New data structures and algorithms
+* All chapters rewritten and reviewed
+* Three (3) new chapters
+* Creation of a Data Structures and Algorithms library that can be used in the browser or with Node.js
* Algorithms tested with Mocha + Chai (test code available in `test` directory)
-* TypeScript version of the source code included
+* **TypeScript** version of the source code included (library and tests)
+
+## Project Structure
+
+`src/js/index.js` file contains all the data structures and algorithms listed by chapter.
+
+```
+|_examples (how to use each data structure and algorithm, organized by chapter)
+|_src
+|___js (source code: JavaScript version)
+|_____data-structures
+|_______models (classes used by DS: Node, ValuePair, ...)
+|_____others (other algorithms such as palindome checker, hanoi tower)
+|___ts (source code: TypeScript version)
+|_____data-structures
+|_______models
+|_____others
+|_test (unit tests with Mocha and Chai for src)
+|___js (tests for JavaScript code)
+|___ts (tests for TypeScript code)
+```
## Installing and running the book examples With Node
* Install [Node](https://nodejs.org)
-* Open terminal/cmd and change directoty to this project folder: `cd /Users/.../javascript-datastructures-algorithms` (Linux/Max) or `cd C:/.../javascript-datastructures-algorithms`
-* run `npm install` to install all depencies
+* Open terminal/cmd and change directory to this project folder: `cd /Users/.../javascript-datastructures-algorithms` (Linux/Max) or `cd C:/.../javascript-datastructures-algorithms`
+* run `npm install` to install all dependencies
* To see the examples, run `http-server html` or `npm run serve`. Open your browser `http:\\localhost:8080` to see the book examples
* Or `cd html/chapter01` and run each javascript file with node: `node 02-Variables`
@@ -33,4 +70,39 @@ Work in Progress.
* Right click on the html file you would like to see the examples, right click and 'Open with Chrome (or any other browser)'
+* Or open the `examples/index.html` file to easily navigate through all examples:
+
+* Demo: [https://javascript-ds-algorithms-book.firebaseapp.com](https://javascript-ds-algorithms-book.firebaseapp.com)
+
+
+
Happy Coding!
+
+## Other editions
+
+| 1st edition | 2nd edition | 3rd edition |
+| ------------- |:-------------:|:-------------:|
+|  |  |  |
+| [Book link](http://amzn.to/1Y1OWPx)| [Book link](http://amzn.to/1TSkcA1)| [Book link](http://a.co/cbMlYmJ)|
+
+Book link - first edition:
+ - [Packt](https://www.packtpub.com/application-development/learning-javascript-data-structures-and-algorithms)
+ - [Amazon](http://amzn.to/1Y1OWPx)
+ - [Chinese version](http://www.ituring.com.cn/book/1613)
+ - [Korean version](http://www.acornpub.co.kr/book/javascript-data-structure)
+
+Book link - second edition:
+ - [Packt](https://www.packtpub.com/web-development/learning-javascript-data-structures-and-algorithms-second-edition)
+ - [Amazon](http://amzn.to/1TSkcA1)
+ - [Chinese version](http://www.ituring.com.cn/book/2029)
+ - [Brazilian Portuguese version](https://novatec.com.br/livros/estruturas-de-dados-algoritmos-em-javascript/)
+
+ Book link - third edition:
+ - [Packt](https://www.packtpub.com/web-development/learning-javascript-data-structures-and-algorithms-third-edition)
+ - [Amazon](http://a.co/cbMlYmJ)
+ - [Chinese version](http://www.ituring.com.cn/book/2653)
+ - [Brazilian Portuguese version](https://novatec.com.br/livros/estruturas-de-dados-algoritmos-em-javascript-2ed/)
+
+### Found an issue or have a question?
+
+Please create an [Issue](https://github.com/loiane/javascript-datastructures-algorithms/issues) or [Pull Request](https://github.com/loiane/javascript-datastructures-algorithms/pulls)
diff --git a/examples/PacktDataStructuresAlgorithms.min.js b/examples/PacktDataStructuresAlgorithms.min.js
index 2977a273..b7d4c0c6 100644
--- a/examples/PacktDataStructuresAlgorithms.min.js
+++ b/examples/PacktDataStructuresAlgorithms.min.js
@@ -1 +1,2 @@
-!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("PacktDataStructuresAlgorithms",[],e):"object"==typeof exports?exports.PacktDataStructuresAlgorithms=e():t.PacktDataStructuresAlgorithms=e()}(this,function(){return function(t){function e(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,e),o.l=!0,o.exports}var n={};return e.m=t,e.c=n,e.d=function(t,n,r){e.o(t,n)||Object.defineProperty(t,n,{configurable:!1,enumerable:!0,get:r})},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=1)}([function(t,e,n){var r,o,i;!function(n,u){o=[t,e],r=u,void 0!==(i="function"==typeof r?r.apply(e,o):r)&&(t.exports=i)}(0,function(t,e){"use strict";function n(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(e,"__esModule",{value:!0});var r=function(){function t(t,e){for(var n=0;n7&&void 0!==arguments[7]?arguments[7]:[];if(t<=0)return s;if(1===t){o.push(e.pop());var f={};f[i]=e.toString(),f[u]=r.toString(),f[a]=o.toString(),s.push(f)}else{n(t-1,e,o,r,i,a,u,s),o.push(e.pop());var c={};c[i]=e.toString(),c[u]=r.toString(),c[a]=o.toString(),s.push(c),n(t-1,r,e,o,u,i,a,s)}return s}function r(t){for(var e=new i.default,r=new i.default,o=new i.default,u=t;u>0;u--)e.push(u);return n(t,e,o,r,"source","helper","dest")}function o(t,e,n,r){var i=arguments.length>4&&void 0!==arguments[4]?arguments[4]:[];return t<=0?i:(1===t?i.push([e,r]):(o(t-1,e,r,n,i),i.push([e,r]),o(t-1,n,e,r,i)),i)}Object.defineProperty(t,"__esModule",{value:!0}),t.hanoiStack=r,t.hanoi=o;var i=function(t){return t&&t.__esModule?t:{default:t}}(e)})},function(t,e,n){var r,o,i;!function(u,a){o=[e,n(0)],r=a,void 0!==(i="function"==typeof r?r.apply(e,o):r)&&(t.exports=i)}(0,function(t,e){"use strict";function n(t){for(var e=new o.default,n=t,r=void 0,i="";n>0;)r=Math.floor(n%2),e.push(r),n=Math.floor(n/2);for(;!e.isEmpty();)i+=e.pop().toString();return i}function r(t,e){var n=new o.default,r=t,i=void 0,u="";if(!(e>=2&&e<=36))return"";for(;r>0;)i=Math.floor(r%e),n.push(i),r=Math.floor(r/e);for(;!n.isEmpty();)u+="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[n.pop()];return u}Object.defineProperty(t,"__esModule",{value:!0}),t.decimalToBinary=n,t.baseConverter=r;var o=function(t){return t&&t.__esModule?t:{default:t}}(e)})},function(t,e,n){var r,o,i;!function(n,u){o=[t,e],r=u,void 0!==(i="function"==typeof r?r.apply(e,o):r)&&(t.exports=i)}(0,function(t,e){"use strict";function n(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(e,"__esModule",{value:!0});var r=function(){function t(t,e){for(var n=0;n=0?e.push(a):e.isEmpty()?i=!1:(s=e.pop(),n.indexOf(s)!==o.indexOf(a)&&(i=!1)),u++;return!(!i||!e.isEmpty())}Object.defineProperty(t,"__esModule",{value:!0}),t.parenthesesChecker=n;var r=function(t){return t&&t.__esModule?t:{default:t}}(e)})}])});
\ No newline at end of file
+!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("PacktDataStructuresAlgorithms",[],t):"object"==typeof exports?exports.PacktDataStructuresAlgorithms=t():e.PacktDataStructuresAlgorithms=t()}(window,function(){return function(e){var t={};function n(r){if(t[r])return t[r].exports;var i=t[r]={i:r,l:!1,exports:{}};return e[r].call(i.exports,i,i.exports,n),i.l=!0,i.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{configurable:!1,enumerable:!0,get:r})},n.r=function(e){Object.defineProperty(e,"__esModule",{value:!0})},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=66)}([function(e,t,n){var r,i,o;i=[t],void 0===(o="function"==typeof(r=function(e){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.lesserEquals=function(e,n,r){var i=r(e,n);return i===t.LESS_THAN||i===t.EQUALS},e.biggerEquals=function(e,n,r){var i=r(e,n);return i===t.BIGGER_THAN||i===t.EQUALS},e.defaultCompare=function(e,n){return e===n?t.EQUALS:e0&&void 0!==arguments[0]?arguments[0]:n.defaultEquals;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.equalsFn=t,this.count=0,this.head=void 0}return i(e,[{key:"push",value:function(e){var t=new r.Node(e),n=void 0;if(null==this.head)this.head=t;else{for(n=this.head;null!=n.next;)n=n.next;n.next=t}this.count++}},{key:"getElementAt",value:function(e){if(e>=0&&e<=this.count){for(var t=this.head,n=0;n=0&&t<=this.count){var n=new r.Node(e);if(0===t){var i=this.head;n.next=i,this.head=n}else{var o=this.getElementAt(t-1);n.next=o.next,o.next=n}return this.count++,!0}return!1}},{key:"removeAt",value:function(e){if(e>=0&&e1&&(u=function(e,n,r,i){for(var o=e[Math.floor((r+n)/2)],u=n,a=r;u<=a;){for(;i(e[u],o)===t.Compare.LESS_THAN;)u++;for(;i(e[a],o)===t.Compare.BIGGER_THAN;)a--;u<=a&&((0,t.swap)(e,u,a),u++,a--)}return u}(e,r,i,o),r1&&void 0!==arguments[1]?arguments[1]:t.defaultCompare;return n(e,0,e.length-1,r)}})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[t,n(0)],void 0===(o="function"==typeof(r=function(e,t){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.findMaxValue=function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:t.defaultCompare;if(e&&e.length>0){for(var r=e[0],i=1;i1&&void 0!==arguments[1]?arguments[1]:t.defaultCompare;if(e&&e.length>0){for(var r=e[0],i=1;i1&&void 0!==arguments[1]?arguments[1]:t.defaultCompare,r=e.length,i=void 0,o=1;o0&&n(e[u-1],i)===t.Compare.BIGGER_THAN;)e[u]=e[u-1],u--;e[u]=i}return e}})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[t],void 0===(o="function"==typeof(r=function(e){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var t=function(){function e(e,t){for(var n=0;n0&&void 0!==arguments[0]?arguments[0]:n.defaultCompare;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.compareFn=t,this.root=void 0}return i(e,[{key:"insert",value:function(e){null==this.root?this.root=new r.Node(e):this.insertNode(this.root,e)}},{key:"insertNode",value:function(e,t){this.compareFn(t,e.key)===n.Compare.LESS_THAN?null==e.left?e.left=new r.Node(t):this.insertNode(e.left,t):null==e.right?e.right=new r.Node(t):this.insertNode(e.right,t)}},{key:"getRoot",value:function(){return this.root}},{key:"search",value:function(e){return this.searchNode(this.root,e)}},{key:"searchNode",value:function(e,t){return null!=e&&(this.compareFn(t,e.key)===n.Compare.LESS_THAN?this.searchNode(e.left,t):this.compareFn(t,e.key)!==n.Compare.BIGGER_THAN||this.searchNode(e.right,t))}},{key:"inOrderTraverse",value:function(e){this.inOrderTraverseNode(this.root,e)}},{key:"inOrderTraverseNode",value:function(e,t){null!=e&&(this.inOrderTraverseNode(e.left,t),t(e.key),this.inOrderTraverseNode(e.right,t))}},{key:"preOrderTraverse",value:function(e){this.preOrderTraverseNode(this.root,e)}},{key:"preOrderTraverseNode",value:function(e,t){null!=e&&(t(e.key),this.preOrderTraverseNode(e.left,t),this.preOrderTraverseNode(e.right,t))}},{key:"postOrderTraverse",value:function(e){this.postOrderTraverseNode(this.root,e)}},{key:"postOrderTraverseNode",value:function(e,t){null!=e&&(this.postOrderTraverseNode(e.left,t),this.postOrderTraverseNode(e.right,t),t(e.key))}},{key:"min",value:function(){return this.minNode(this.root)}},{key:"minNode",value:function(e){for(var t=e;null!=t&&null!=t.left;)t=t.left;return t}},{key:"max",value:function(){return this.maxNode(this.root)}},{key:"maxNode",value:function(e){for(var t=e;null!=t&&null!=t.right;)t=t.right;return t}},{key:"remove",value:function(e){this.root=this.removeNode(this.root,e)}},{key:"removeNode",value:function(e,t){if(null!=e){if(this.compareFn(t,e.key)===n.Compare.LESS_THAN)return e.left=this.removeNode(e.left,t),e;if(this.compareFn(t,e.key)===n.Compare.BIGGER_THAN)return e.right=this.removeNode(e.right,t),e;if(null==e.left&&null==e.right)return e=void 0;if(null==e.left)return e=e.right;if(null==e.right)return e=e.left;var r=this.minNode(e.right);return e.key=r.key,e.right=this.removeNode(e.right,r.key),e}}}]),e}();t.default=o,e.exports=t.default})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[e,t,n(0),n(1)],void 0===(o="function"==typeof(r=function(e,t,n,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var i=function(){function e(e,t){for(var n=0;n0&&void 0!==arguments[0]?arguments[0]:n.defaultToString;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.toStrFn=t,this.table={}}return i(e,[{key:"set",value:function(e,t){if(null!=e&&null!=t){var n=this.toStrFn(e);return this.table[n]=new r.ValuePair(e,t),!0}return!1}},{key:"get",value:function(e){var t=this.table[this.toStrFn(e)];return null==t?void 0:t.value}},{key:"hasKey",value:function(e){return null!=this.table[this.toStrFn(e)]}},{key:"remove",value:function(e){return!!this.hasKey(e)&&(delete this.table[this.toStrFn(e)],!0)}},{key:"values",value:function(){return this.keyValues().map(function(e){return e.value})}},{key:"keys",value:function(){return this.keyValues().map(function(e){return e.key})}},{key:"keyValues",value:function(){return Object.values(this.table)}},{key:"forEach",value:function(e){for(var t=this.keyValues(),n=0;n0&&void 0!==arguments[0]?arguments[0]:n.defaultEquals;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,t);var r=function(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e));return r.tail=void 0,r}return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}(t,e),a(t,[{key:"push",value:function(e){var t=new i.DoublyNode(e);null==this.head?(this.head=t,this.tail=t):(this.tail.next=t,t.prev=this.tail,this.tail=t),this.count++}},{key:"insert",value:function(e,t){if(t>=0&&t<=this.count){var n=new i.DoublyNode(e),r=this.head;if(0===t)null==this.head?(this.head=n,this.tail=n):(n.next=this.head,this.head.prev=n,this.head=n);else if(t===this.count)(r=this.tail).next=n,n.prev=r,this.tail=n;else{var o=this.getElementAt(t-1);r=o.next,n.next=r,o.next=n,r.prev=n,n.prev=o}return this.count++,!0}return!1}},{key:"removeAt",value:function(e){if(e>=0&&e0)this.lowestCount--,this.items[this.lowestCount]=e;else{for(var t=this.count;t>0;t--)this.items[t]=this.items[t-1];this.count++,this.items[0]=e}}},{key:"addBack",value:function(e){this.items[this.count]=e,this.count++}},{key:"removeFront",value:function(){if(!this.isEmpty()){var e=this.items[this.lowestCount];return delete this.items[this.lowestCount],this.lowestCount++,e}}},{key:"removeBack",value:function(){if(!this.isEmpty()){this.count--;var e=this.items[this.count];return delete this.items[this.count],e}}},{key:"peekFront",value:function(){if(!this.isEmpty())return this.items[this.lowestCount]}},{key:"peekBack",value:function(){if(!this.isEmpty())return this.items[this.count-1]}},{key:"isEmpty",value:function(){return 0===this.size()}},{key:"clear",value:function(){this.items={},this.count=0,this.lowestCount=0}},{key:"size",value:function(){return this.count-this.lowestCount}},{key:"toString",value:function(){if(this.isEmpty())return"";for(var e=""+this.items[this.lowestCount],t=this.lowestCount+1;t=0&&n>=0&&t1&&void 0!==arguments[1]?arguments[1]:1,r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:t.length-1;if(n===r)return 0;for(var i=Number.MAX_SAFE_INTEGER,o=n;o2&&void 0!==arguments[2]?arguments[2]:t.length,i=arguments.length>3&&void 0!==arguments[3]?arguments[3]:n.length;if(0===r||0===i)return 0;if(t[r-1]===n[i-1])return 1+e(t,n,r-1,i-1);var o=e(t,n,r,i-1),u=e(t,n,r-1,i);return o>u?o:u}})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[t],void 0===(o="function"==typeof(r=function(e){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.lcs=function(e,t){for(var n=e.length,r=t.length,i=[],o=[],u=0;u<=n;u++){i[u]=[],o[u]=[];for(var a=0;a<=r;a++)i[u][a]=0,o[u][a]="0"}for(var f=0;f<=n;f++)for(var l=0;l<=r;l++)if(0===f||0===l)i[f][l]=0;else if(e[f-1]===t[l-1])i[f][l]=i[f-1][l-1]+1,o[f][l]="diagonal";else{var s=i[f-1][l],c=i[f][l-1];i[f][l]=s>c?s:c,o[f][l]=i[f][l]===i[f-1][l]?"top":"left"}return function(e,t,n,r){for(var i=n,o=r,u=e[i][o],a="";"0"!==u;)"diagonal"===e[i][o]?(a=t[i-1]+a,i--,o--):"left"===e[i][o]?o--:"top"===e[i][o]&&i--,u=e[i][o];return a}(o,e,n,r)}})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[t],void 0===(o="function"==typeof(r=function(e){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.lcs=function(e,t){for(var n=e.length,r=t.length,i=[],o=0;o<=n;o++){i[o]=[];for(var u=0;u<=r;u++)i[o][u]=0}for(var a=0;a<=n;a++)for(var f=0;f<=r;f++)if(0===a||0===f)i[a][f]=0;else if(e[a-1]===t[f-1])i[a][f]=i[a-1][f-1]+1;else{var l=i[a-1][f],s=i[a][f-1];i[a][f]=l>s?l:s}return i[n][r]}})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[t],void 0===(o="function"==typeof(r=function(e){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.knapSack=function(e,t,n){for(var r=n.length,i=0,o=0,u=0;ut)return e(t,n,r,i-1);var o=r[i-1]+e(t-n[i-1],n,r,i-1),u=e(t,n,r,i-1);return o>u?o:u}})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[t],void 0===(o="function"==typeof(r=function(e){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.knapSack=function(e,t,n,r){for(var i=[],o=0;o<=r;o++)i[o]=[];for(var u=0;u<=r;u++)for(var a=0;a<=e;a++)if(0===u||0===a)i[u][a]=0;else if(t[u-1]<=a){var f=n[u-1]+i[u-1][a-t[u-1]],l=i[u-1][a];i[u][a]=f>l?f:l}else i[u][a]=i[u-1][a];return function(e,t,n){for(var r=e,i=t;r>0&&i>0;)n[r][i]!==n[r-1][i]?i-=n[--r][i]:r--}(r,e,i),i[r][e]}})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[t],void 0===(o="function"==typeof(r=function(e){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.minCoinChange=function(e,t){for(var n=[],r=0,i=e.length;i>=0;i--)for(var o=e[i];r+o<=t;)n.push(o),r+=o;return n}})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[t],void 0===(o="function"==typeof(r=function(e){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.minCoinChange=function(e,t){var n=[];return function t(r){if(!r)return[];if(n[r])return n[r];for(var i=[],o=void 0,u=void 0,a=0;a=0&&(o=t(u)),u>=0&&(o.length2&&void 0!==arguments[2]?arguments[2]:t.defaultCompare,o=(0,n.quickSort)(e).length-1;return function e(n,r,i,o){var u=arguments.length>4&&void 0!==arguments[4]?arguments[4]:t.defaultCompare;if(i<=o){var a=Math.floor((i+o)/2),f=n[a];return u(f,r)===t.Compare.LESS_THAN?e(n,r,a+1,o,u):u(f,r)===t.Compare.BIGGER_THAN?e(n,r,i,a-1,u):a}return t.DOES_NOT_EXIST}(e,r,0,o,i)}})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[t,n(0)],void 0===(o="function"==typeof(r=function(e,t){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.sequentialSearch=function(e,n){for(var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:t.defaultEquals,i=0;i2&&void 0!==arguments[2]?arguments[2]:t.defaultCompare,i=arguments.length>3&&void 0!==arguments[3]?arguments[3]:t.defaultEquals,o=arguments.length>4&&void 0!==arguments[4]?arguments[4]:t.defaultDiff,u=0,a=e.length-1,f=-1,l=-1;u<=a&&(0,t.biggerEquals)(n,e[u],r)&&(0,t.lesserEquals)(n,e[a],r);){if(l=o(n,e[u])/o(e[a],e[u]),f=u+Math.floor((a-u)*l),i(e[f],n))return f;r(e[f],n)===t.Compare.LESS_THAN?u=f+1:a=f-1}return t.DOES_NOT_EXIST}})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[t,n(0),n(4)],void 0===(o="function"==typeof(r=function(e,t,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.binarySearch=function(e,r){for(var i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:t.defaultCompare,o=(0,n.quickSort)(e),u=0,a=o.length-1;u<=a;){var f=Math.floor((u+a)/2),l=o[f];if(i(l,r)===t.Compare.LESS_THAN)u=f+1;else{if(i(l,r)!==t.Compare.BIGGER_THAN)return f;a=f-1}}return t.DOES_NOT_EXIST}})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[t,n(0)],void 0===(o="function"==typeof(r=function(e,t){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.shellSort=function(e){for(var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:t.defaultCompare,r=e.length/2;r>0;){for(var i=r;i=r&&n(e[o-r],u)===t.Compare.BIGGER_THAN;)e[o]=e[o-r],o-=r;e[o]=u}r=2===r?1:Math.floor(5*r/11)}return e}})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[t,n(0)],void 0===(o="function"==typeof(r=function(e,t){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.selectionSort=void 0,e.selectionSort=function(e){for(var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:t.defaultCompare,r=e.length,i=void 0,o=0;o1&&void 0!==arguments[1]?arguments[1]:10;if(e.length<2)return e;for(var i=(0,t.findMinValue)(e),o=(0,t.findMaxValue)(e),u=1;(o-i)/u>=1;)e=r(e,n,u,i),u*=n;return e};var n=function(e,t,n,r){return Math.floor((e-t)/n%r)},r=function(e,t,r,i){for(var o=void 0,u=[],a=[],f=0;f=0;c--)o=n(e[c],i,r,t),a[--u[o]]=e[c];for(var h=0;h1&&void 0!==arguments[1]?arguments[1]:t.defaultCompare;if(n.length>1){var i=n,o=i.length,u=Math.floor(o/2),a=e(n.slice(0,u),r),f=e(n.slice(u,o),r);n=function(e,n,r){for(var i=0,o=0,u=[];i0;)e[r++]=n,t--}),e}})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[t,n(8)],void 0===(o="function"==typeof(r=function(e,t){"use strict";function n(e){if(Array.isArray(e)){for(var t=0,n=Array(e.length);t1&&void 0!==arguments[1]?arguments[1]:5;return e.length<2?e:function(e){for(var r=[],i=0;ir&&(r=e[i]);for(var o=Math.floor((r-n)/t)+1,u=[],a=0;a1&&void 0!==arguments[1]?arguments[1]:t.defaultCompare,r=e.length,i=0;i1&&void 0!==arguments[1]?arguments[1]:t.defaultCompare,r=e.length,i=0;i0&&void 0!==arguments[0]&&arguments[0];!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.isDirected=t,this.vertices=[],this.adjList=new i.default}return o(e,[{key:"addVertex",value:function(e){this.vertices.includes(e)||(this.vertices.push(e),this.adjList.set(e,[]))}},{key:"addEdge",value:function(e,t){this.adjList.get(e)||this.addVertex(e),this.adjList.get(t)||this.addVertex(t),this.adjList.get(e).push(t),!0!==this.isDirected&&this.adjList.get(t).push(e)}},{key:"getVertices",value:function(){return this.vertices}},{key:"getAdjList",value:function(){return this.adjList}},{key:"toString",value:function(){for(var e="",t=0;t ";for(var n=this.adjList.get(this.vertices[t]),r=0;r0&&(u=a),f0&&(u=f),u!==t&&((0,n.swap)(e,t,u),r(e,u,i,o))}Object.defineProperty(t,"__esModule",{value:!0}),t.default=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:n.defaultCompare,i=e.length;for(!function(e,t){for(var n=Math.floor(e.length/2);n>=0;n-=1)r(e,n,e.length,t)}(e,t);i>1;)(0,n.swap)(e,0,--i),r(e,0,i,t);return e},e.exports=t.default})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[t,n(0)],void 0===(o="function"==typeof(r=function(e,t){"use strict";function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(e,"__esModule",{value:!0}),e.MaxHeap=e.MinHeap=void 0;var r=function(){function e(e,t){for(var n=0;n0&&void 0!==arguments[0]?arguments[0]:t.defaultCompare;n(this,e),this.compareFn=r,this.heap=[]}return r(e,[{key:"getLeftIndex",value:function(e){return 2*e+1}},{key:"getRightIndex",value:function(e){return 2*e+2}},{key:"getParentIndex",value:function(e){if(0!==e)return Math.floor((e-1)/2)}},{key:"size",value:function(){return this.heap.length}},{key:"isEmpty",value:function(){return this.size()<=0}},{key:"clear",value:function(){this.heap=[]}},{key:"findMinimum",value:function(){return this.isEmpty()?void 0:this.heap[0]}},{key:"insert",value:function(e){if(null!=e){var t=this.heap.length;return this.heap.push(e),this.siftUp(t),!0}return!1}},{key:"siftDown",value:function(e){var n=e,r=this.getLeftIndex(e),i=this.getRightIndex(e),o=this.size();r0&&this.compareFn(this.heap[n],this.heap[e])===t.Compare.BIGGER_THAN;)(0,t.swap)(this.heap,n,e),e=n,n=this.getParentIndex(e)}},{key:"extract",value:function(){if(!this.isEmpty()){if(1===this.size())return this.heap.shift();var e=this.heap[0];return this.heap[0]=this.heap.pop(),this.siftDown(0),e}}},{key:"heapify",value:function(e){e&&(this.heap=e);for(var t=Math.floor(this.size()/2)-1,n=0;n<=t;n++)this.siftDown(n);return this.heap}},{key:"getAsArray",value:function(){return this.heap}}]),e}();e.MaxHeap=function(e){function r(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:t.defaultCompare;n(this,r);var i=function(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}(this,(r.__proto__||Object.getPrototypeOf(r)).call(this,e));return i.compareFn=e,i.compareFn=(0,t.reverseCompare)(e),i}return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}(r,e),r}(i)})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[e,t,n(0),n(10),n(9)],void 0===(o="function"==typeof(r=function(e,t,n,r,i){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var o,u=(o=r)&&o.__esModule?o:{default:o},a=function(){function e(e,t){for(var n=0;n0&&void 0!==arguments[0]?arguments[0]:n.defaultCompare;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,t);var r=function(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e));return r.compareFn=e,r.root=null,r}return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}(t,e),a(t,[{key:"getNodeHeight",value:function(e){return null==e?-1:Math.max(this.getNodeHeight(e.left),this.getNodeHeight(e.right))+1}},{key:"rotationLL",value:function(e){var t=e.left;return e.left=t.right,t.right=e,t}},{key:"rotationRR",value:function(e){var t=e.right;return e.right=t.left,t.left=e,t}},{key:"rotationLR",value:function(e){return e.left=this.rotationRR(e.left),this.rotationLL(e)}},{key:"rotationRL",value:function(e){return e.right=this.rotationLL(e.right),this.rotationRR(e)}},{key:"getBalanceFactor",value:function(e){var t=this.getNodeHeight(e.left)-this.getNodeHeight(e.right);switch(t){case-2:return f.UNBALANCED_RIGHT;case-1:return f.SLIGHTLY_UNBALANCED_RIGHT;case 1:return f.SLIGHTLY_UNBALANCED_LEFT;case 2:return f.UNBALANCED_LEFT;default:return f.BALANCED}}},{key:"insert",value:function(e){this.root=this.insertNode(this.root,e)}},{key:"insertNode",value:function(e,t){if(null==e)return new i.Node(t);if(this.compareFn(t,e.key)===n.Compare.LESS_THAN)e.left=this.insertNode(e.left,t);else{if(this.compareFn(t,e.key)!==n.Compare.BIGGER_THAN)return e;e.right=this.insertNode(e.right,t)}var r=this.getBalanceFactor(e);if(r===f.UNBALANCED_LEFT){if(this.compareFn(t,e.left.key)!==n.Compare.LESS_THAN)return this.rotationLR(e);e=this.rotationLL(e)}if(r===f.UNBALANCED_RIGHT){if(this.compareFn(t,e.right.key)!==n.Compare.BIGGER_THAN)return this.rotationRL(e);e=this.rotationRR(e)}return e}},{key:"removeNode",value:function(e,n){if(null==(e=function e(t,n,r){null===t&&(t=Function.prototype);var i=Object.getOwnPropertyDescriptor(t,n);if(void 0===i){var o=Object.getPrototypeOf(t);return null===o?void 0:e(o,n,r)}if("value"in i)return i.value;var u=i.get;return void 0!==u?u.call(r):void 0}(t.prototype.__proto__||Object.getPrototypeOf(t.prototype),"removeNode",this).call(this,e,n)))return e;var r=this.getBalanceFactor(e);if(r===f.UNBALANCED_LEFT){if(this.getBalanceFactor(e.left)===f.BALANCED||this.getBalanceFactor(e.left)===f.SLIGHTLY_UNBALANCED_LEFT)return this.rotationLL(e);if(this.getBalanceFactor(e.left)===f.SLIGHTLY_UNBALANCED_RIGHT)return this.rotationLR(e.left)}if(r===f.UNBALANCED_RIGHT){if(this.getBalanceFactor(e.right)===f.BALANCED||this.getBalanceFactor(e.right)===f.SLIGHTLY_UNBALANCED_RIGHT)return this.rotationRR(e);if(this.getBalanceFactor(e.right)===f.SLIGHTLY_UNBALANCED_LEFT)return this.rotationRL(e.right)}return e}}]),t}(u.default);t.default=l,e.exports=t.default})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[t],void 0===(o="function"==typeof(r=function(e){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.fibonacci=function e(t){return t<1?0:t<=2?1:e(t-1)+e(t-2)},e.fibonacciIterative=function(e){if(e<1)return 0;for(var t=0,n=1,r=e,i=2;i<=e;i++)r=n+t,t=n,n=r;return r},e.fibonacciMemoization=function(e){if(e<1)return 0;var t=[0,1];return function e(n){return null!=t[n]?t[n]:t[n]=e(n-1)+e(n-2)}(e)}})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[t],void 0===(o="function"==typeof(r=function(e){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.factorialIterative=function(e){if(!(e<0)){for(var t=1,n=e;n>1;n--)t*=n;return t}},e.factorial=function e(t){if(!(t<0))return 1===t||0===t?1:t*e(t-1)}})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[t,n(1)],void 0===(o="function"==typeof(r=function(e,t){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.ValuePairLazy=void 0,e.ValuePairLazy=function(e){function t(e,n){var r=arguments.length>2&&void 0!==arguments[2]&&arguments[2];!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,t);var i=function(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e,n));return i.key=e,i.value=n,i.isDeleted=r,i}return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}(t,e),t}(t.ValuePair)})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[e,t,n(0),n(51)],void 0===(o="function"==typeof(r=function(e,t,n,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var i=function(){function e(e,t){for(var n=0;n0&&void 0!==arguments[0]?arguments[0]:n.defaultToString;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.toStrFn=t,this.table={}}return i(e,[{key:"loseloseHashCode",value:function(e){if("number"==typeof e)return e;for(var t=this.toStrFn(e),n=0,r=0;r "+this.table[e[0]].toString()+"}",n=1;n "+this.table[e[n]].toString()+"}";return t}}]),e}();t.default=o,e.exports=t.default})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[e,t,n(0),n(1)],void 0===(o="function"==typeof(r=function(e,t,n,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var i=function(){function e(e,t){for(var n=0;n0&&void 0!==arguments[0]?arguments[0]:n.defaultToString;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.toStrFn=t,this.table={}}return i(e,[{key:"loseloseHashCode",value:function(e){if("number"==typeof e)return e;for(var t=this.toStrFn(e),n=0,r=0;r "+this.table[e[0]].toString()+"}",n=1;n "+this.table[e[n]].toString()+"}";return t}}]),e}();t.default=o,e.exports=t.default})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[e,t,n(0),n(2),n(1)],void 0===(o="function"==typeof(r=function(e,t,n,r,i){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var o,u=(o=r)&&o.__esModule?o:{default:o},a=function(){function e(e,t){for(var n=0;n0&&void 0!==arguments[0]?arguments[0]:n.defaultToString;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.toStrFn=t,this.table={}}return a(e,[{key:"loseloseHashCode",value:function(e){if("number"==typeof e)return e;for(var t=this.toStrFn(e),n=0,r=0;r "+this.table[e[0]].toString()+"}",n=1;n "+this.table[e[n]].toString()+"}";return t}}]),e}();t.default=f,e.exports=t.default})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[e,t,n(0),n(1)],void 0===(o="function"==typeof(r=function(e,t,n,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var i=function(){function e(e,t){for(var n=0;n0&&void 0!==arguments[0]?arguments[0]:n.defaultToString;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.toStrFn=t,this.table={}}return i(e,[{key:"loseloseHashCode",value:function(e){if("number"==typeof e)return e;for(var t=this.toStrFn(e),n=0,r=0;r "+this.table[e[0]].toString()+"}",n=1;n "+this.table[e[n]].toString()+"}";return t}}]),e}();t.default=o,e.exports=t.default})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[e,t],void 0===(o="function"==typeof(r=function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var n=function(){function e(e,t){for(var n=0;n0&&(o=i,u=r),u.forEach(function(e){o.includes(e)&&n.add(e)}),n}},{key:"difference",value:function(t){var n=new e;return this.values().forEach(function(e){t.has(e)||n.add(e)}),n}},{key:"isSubsetOf",value:function(e){if(this.size()>e.size())return!1;var t=!0;return this.values().every(function(n){return!!e.has(n)||(t=!1,!1)}),t}},{key:"isEmpty",value:function(){return 0===this.size()}},{key:"size",value:function(){return Object.keys(this.items).length}},{key:"clear",value:function(){this.items={}}},{key:"toString",value:function(){if(this.isEmpty())return"";for(var e=this.values(),t=""+e[0],n=1;n0&&void 0!==arguments[0]?arguments[0]:n.defaultEquals,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:n.defaultCompare;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,t);var i=function(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e));return i.equalsFn=e,i.compareFn=r,i}return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}(t,e),u(t,[{key:"push",value:function(e){if(this.isEmpty())a(t.prototype.__proto__||Object.getPrototypeOf(t.prototype),"push",this).call(this,e);else{var n=this.getIndexNextSortedElement(e);a(t.prototype.__proto__||Object.getPrototypeOf(t.prototype),"insert",this).call(this,e,n)}}},{key:"insert",value:function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;if(this.isEmpty())return a(t.prototype.__proto__||Object.getPrototypeOf(t.prototype),"insert",this).call(this,e,0===n?n:0);var r=this.getIndexNextSortedElement(e);return a(t.prototype.__proto__||Object.getPrototypeOf(t.prototype),"insert",this).call(this,e,r)}},{key:"getIndexNextSortedElement",value:function(e){for(var t=this.head,r=0;r0&&void 0!==arguments[0]?arguments[0]:n.defaultEquals;return function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,t),function(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e))}return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}(t,e),a(t,[{key:"push",value:function(e){var t=new i.Node(e);null==this.head?this.head=t:this.getElementAt(this.size()-1).next=t,t.next=this.head,this.count++}},{key:"insert",value:function(e,t){if(t>=0&&t<=this.count){var n=new i.Node(e),r=this.head;if(0===t)null==this.head?(this.head=n,n.next=this.head):(n.next=r,r=this.getElementAt(this.size()),this.head=n,r.next=this.head);else{var o=this.getElementAt(t-1);n.next=o.next,o.next=n}return this.count++,!0}return!1}},{key:"removeAt",value:function(e){if(e>=0&&e1;)if(i=t.removeFront(),o=t.removeBack(),i!==o)return!1;return!0};var n,r=(n=t)&&n.__esModule?n:{default:n}})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[t,n(7)],void 0===(o="function"==typeof(r=function(e,t){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.hotPotato=function(e,t){for(var n=new r.default,i=[],o=0;o1;){for(var u=0;u=0?t.push(o):t.isEmpty()?n=!1:(u=t.pop(),"([{".indexOf(u)!==")]}".indexOf(o)&&(n=!1)),i++;return n&&t.isEmpty()};var n,r=(n=t)&&n.__esModule?n:{default:n}})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[t,n(3)],void 0===(o="function"==typeof(r=function(e,t){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.decimalToBinary=function(e){for(var t=new r.default,n=e,i=void 0,o="";n>0;)i=Math.floor(n%2),t.push(i),n=Math.floor(n/2);for(;!t.isEmpty();)o+=t.pop().toString();return o},e.baseConverter=function(e,t){var n=new r.default,i=e,o=void 0,u="";if(!(t>=2&&t<=36))return"";for(;i>0;)o=Math.floor(i%t),n.push(o),i=Math.floor(i/t);for(;!n.isEmpty();)u+="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[n.pop()];return u};var n,r=(n=t)&&n.__esModule?n:{default:n}})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[t,n(3)],void 0===(o="function"==typeof(r=function(e,t){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e.hanoiStack=function(e){for(var t=new r.default,n=new r.default,i=new r.default,o=e;o>0;o--)t.push(o);return function e(t,n,r,i,o,u,a){var f=arguments.length>7&&void 0!==arguments[7]?arguments[7]:[];if(t<=0)return f;if(1===t){i.push(n.pop());var l={};l[o]=n.toString(),l[u]=r.toString(),l[a]=i.toString(),f.push(l)}else{e(t-1,n,i,r,o,a,u,f),i.push(n.pop());var s={};s[o]=n.toString(),s[u]=r.toString(),s[a]=i.toString(),f.push(s),e(t-1,r,n,i,u,o,a,f)}return f}(e,t,i,n,"source","helper","dest")},e.hanoi=function e(t,n,r,i){var o=arguments.length>4&&void 0!==arguments[4]?arguments[4]:[];return t<=0?o:(1===t?o.push([n,i]):(e(t-1,n,i,r,o),o.push([n,i]),e(t-1,r,n,i,o)),o)};var n,r=(n=t)&&n.__esModule?n:{default:n}})?r.apply(t,i):r)||(e.exports=o)},function(e,t,n){var r,i,o;i=[e,t],void 0===(o="function"==typeof(r=function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var n=function(){function e(e,t){for(var n=0;n compareFn(b, a);\n}\n\nexport function defaultDiff(a, b) {\n return Number(a) - Number(b);\n}\n","export class ValuePair {\n constructor(key, value) {\n this.key = key;\n this.value = value;\n }\n toString() {\n return `[#${this.key}: ${this.value}]`;\n }\n}\n","import { defaultEquals } from '../util';\nimport { Node } from './models/linked-list-models';\n\nexport default class LinkedList {\n constructor(equalsFn = defaultEquals) {\n this.equalsFn = equalsFn;\n this.count = 0;\n this.head = undefined;\n }\n push(element) {\n const node = new Node(element);\n let current;\n if (this.head == null) {\n // catches null && undefined\n this.head = node;\n } else {\n current = this.head;\n while (current.next != null) {\n current = current.next;\n }\n current.next = node;\n }\n this.count++;\n }\n getElementAt(index) {\n if (index >= 0 && index <= this.count) {\n let node = this.head;\n for (let i = 0; i < index && node != null; i++) {\n node = node.next;\n }\n return node;\n }\n return undefined;\n }\n insert(element, index) {\n if (index >= 0 && index <= this.count) {\n const node = new Node(element);\n if (index === 0) {\n const current = this.head;\n node.next = current;\n this.head = node;\n } else {\n const previous = this.getElementAt(index - 1);\n node.next = previous.next;\n previous.next = node;\n }\n this.count++;\n return true;\n }\n return false;\n }\n removeAt(index) {\n if (index >= 0 && index < this.count) {\n let current = this.head;\n if (index === 0) {\n this.head = current.next;\n } else {\n const previous = this.getElementAt(index - 1);\n current = previous.next;\n previous.next = current.next;\n }\n this.count--;\n return current.element;\n }\n return undefined;\n }\n remove(element) {\n const index = this.indexOf(element);\n return this.removeAt(index);\n }\n indexOf(element) {\n let current = this.head;\n for (let i = 0; i < this.size() && current != null; i++) {\n if (this.equalsFn(element, current.element)) {\n return i;\n }\n current = current.next;\n }\n return -1;\n }\n isEmpty() {\n return this.size() === 0;\n }\n size() {\n return this.count;\n }\n getHead() {\n return this.head;\n }\n clear() {\n this.head = undefined;\n this.count = 0;\n }\n toString() {\n if (this.head == null) {\n return '';\n }\n let objString = `${this.head.element}`;\n let current = this.head.next;\n for (let i = 1; i < this.size() && current != null; i++) {\n objString = `${objString},${current.element}`;\n current = current.next;\n }\n return objString;\n }\n}\n","// @ts-check\n\nexport default class Stack {\n constructor() {\n this.count = 0;\n this.items = {};\n }\n push(element) {\n this.items[this.count] = element;\n this.count++;\n }\n pop() {\n if (this.isEmpty()) {\n return undefined;\n }\n this.count--;\n const result = this.items[this.count];\n delete this.items[this.count];\n return result;\n }\n peek() {\n if (this.isEmpty()) {\n return undefined;\n }\n return this.items[this.count - 1];\n }\n isEmpty() {\n return this.count === 0;\n }\n size() {\n return this.count;\n }\n clear() {\n /* while (!this.isEmpty()) {\n this.pop();\n } */\n this.items = {};\n this.count = 0;\n }\n toString() {\n if (this.isEmpty()) {\n return '';\n }\n let objString = `${this.items[0]}`;\n for (let i = 1; i < this.count; i++) {\n objString = `${objString},${this.items[i]}`;\n }\n return objString;\n }\n}\n","import { Compare, defaultCompare, swap } from '../../util';\n\nfunction partition(array, left, right, compareFn) {\n const pivot = array[Math.floor((right + left) / 2)];\n let i = left;\n let j = right;\n\n while (i <= j) {\n while (compareFn(array[i], pivot) === Compare.LESS_THAN) {\n i++;\n }\n while (compareFn(array[j], pivot) === Compare.BIGGER_THAN) {\n j--;\n }\n if (i <= j) {\n swap(array, i, j);\n i++;\n j--;\n }\n }\n return i;\n}\nfunction quick(array, left, right, compareFn) {\n let index;\n if (array.length > 1) {\n index = partition(array, left, right, compareFn);\n if (left < index - 1) {\n quick(array, left, index - 1, compareFn);\n }\n if (index < right) {\n quick(array, index, right, compareFn);\n }\n }\n return array;\n}\nexport function quickSort(array, compareFn = defaultCompare) {\n return quick(array, 0, array.length - 1, compareFn);\n}\n","import { defaultCompare, Compare } from '../../util';\n\nexport function findMaxValue(array, compareFn = defaultCompare) {\n if (array && array.length > 0) {\n let max = array[0];\n for (let i = 1; i < array.length; i++) {\n if (compareFn(max, array[i]) === Compare.LESS_THAN) {\n max = array[i];\n }\n }\n return max;\n }\n return undefined;\n}\nexport function findMinValue(array, compareFn = defaultCompare) {\n if (array && array.length > 0) {\n let min = array[0];\n for (let i = 1; i < array.length; i++) {\n if (compareFn(min, array[i]) === Compare.BIGGER_THAN) {\n min = array[i];\n }\n }\n return min;\n }\n return undefined;\n}\n","export class Node {\n constructor(element, next) {\n this.element = element;\n this.next = next;\n }\n}\nexport class DoublyNode extends Node {\n constructor(element, next, prev) {\n super(element, next);\n this.prev = prev;\n }\n}\n","// @ts-check\n\nexport default class Queue {\n constructor() {\n this.count = 0;\n this.lowestCount = 0;\n this.items = {};\n }\n\n enqueue(element) {\n this.items[this.count] = element;\n this.count++;\n }\n\n dequeue() {\n if (this.isEmpty()) {\n return undefined;\n }\n const result = this.items[this.lowestCount];\n delete this.items[this.lowestCount];\n this.lowestCount++;\n return result;\n }\n\n peek() {\n if (this.isEmpty()) {\n return undefined;\n }\n return this.items[this.lowestCount];\n }\n\n isEmpty() {\n return this.size() === 0;\n }\n\n clear() {\n this.items = {};\n this.count = 0;\n this.lowestCount = 0;\n }\n\n size() {\n return this.count - this.lowestCount;\n }\n\n toString() {\n if (this.isEmpty()) {\n return '';\n }\n let objString = `${this.items[this.lowestCount]}`;\n for (let i = this.lowestCount + 1; i < this.count; i++) {\n objString = `${objString},${this.items[i]}`;\n }\n return objString;\n }\n}\n","import { Compare, defaultCompare } from '../../util';\n\nexport const insertionSort = (array, compareFn = defaultCompare) => {\n const { length } = array;\n let temp;\n for (let i = 1; i < length; i++) {\n let j = i;\n temp = array[i];\n // console.log('to be inserted ' + temp);\n while (j > 0 && compareFn(array[j - 1], temp) === Compare.BIGGER_THAN) {\n // console.log('shift ' + array[j - 1]);\n array[j] = array[j - 1];\n j--;\n }\n // console.log('insert ' + temp);\n array[j] = temp;\n }\n return array;\n};\n","export class Node {\n constructor(key) {\n this.key = key;\n this.left = undefined;\n this.right = undefined;\n }\n toString() {\n return `${this.key}`;\n }\n}\n","import { Compare, defaultCompare } from '../util';\nimport { Node } from './models/node';\n\nexport default class BinarySearchTree {\n constructor(compareFn = defaultCompare) {\n this.compareFn = compareFn;\n this.root = undefined;\n }\n insert(key) {\n // special case: first key\n if (this.root == null) {\n this.root = new Node(key);\n } else {\n this.insertNode(this.root, key);\n }\n }\n insertNode(node, key) {\n if (this.compareFn(key, node.key) === Compare.LESS_THAN) {\n if (node.left == null) {\n node.left = new Node(key);\n } else {\n this.insertNode(node.left, key);\n }\n } else if (node.right == null) {\n node.right = new Node(key);\n } else {\n this.insertNode(node.right, key);\n }\n }\n getRoot() {\n return this.root;\n }\n search(key) {\n return this.searchNode(this.root, key);\n }\n searchNode(node, key) {\n if (node == null) {\n return false;\n }\n if (this.compareFn(key, node.key) === Compare.LESS_THAN) {\n return this.searchNode(node.left, key);\n } else if (this.compareFn(key, node.key) === Compare.BIGGER_THAN) {\n return this.searchNode(node.right, key);\n }\n return true;\n }\n inOrderTraverse(callback) {\n this.inOrderTraverseNode(this.root, callback);\n }\n inOrderTraverseNode(node, callback) {\n if (node != null) {\n this.inOrderTraverseNode(node.left, callback);\n callback(node.key);\n this.inOrderTraverseNode(node.right, callback);\n }\n }\n preOrderTraverse(callback) {\n this.preOrderTraverseNode(this.root, callback);\n }\n preOrderTraverseNode(node, callback) {\n if (node != null) {\n callback(node.key);\n this.preOrderTraverseNode(node.left, callback);\n this.preOrderTraverseNode(node.right, callback);\n }\n }\n postOrderTraverse(callback) {\n this.postOrderTraverseNode(this.root, callback);\n }\n postOrderTraverseNode(node, callback) {\n if (node != null) {\n this.postOrderTraverseNode(node.left, callback);\n this.postOrderTraverseNode(node.right, callback);\n callback(node.key);\n }\n }\n min() {\n return this.minNode(this.root);\n }\n minNode(node) {\n let current = node;\n while (current != null && current.left != null) {\n current = current.left;\n }\n return current;\n }\n max() {\n return this.maxNode(this.root);\n }\n maxNode(node) {\n let current = node;\n while (current != null && current.right != null) {\n current = current.right;\n }\n return current;\n }\n remove(key) {\n this.root = this.removeNode(this.root, key);\n }\n removeNode(node, key) {\n if (node == null) {\n return undefined;\n }\n if (this.compareFn(key, node.key) === Compare.LESS_THAN) {\n node.left = this.removeNode(node.left, key);\n return node;\n } else if (this.compareFn(key, node.key) === Compare.BIGGER_THAN) {\n node.right = this.removeNode(node.right, key);\n return node;\n }\n // key is equal to node.item\n // handle 3 special conditions\n // 1 - a leaf node\n // 2 - a node with only 1 child\n // 3 - a node with 2 children\n // case 1\n if (node.left == null && node.right == null) {\n node = undefined;\n return node;\n }\n // case 2\n if (node.left == null) {\n node = node.right;\n return node;\n } else if (node.right == null) {\n node = node.left;\n return node;\n }\n // case 3\n const aux = this.minNode(node.right);\n node.key = aux.key;\n node.right = this.removeNode(node.right, aux.key);\n return node;\n }\n}\n","import { defaultToString } from '../util';\nimport { ValuePair } from './models/value-pair';\n\nexport default class Dictionary {\n constructor(toStrFn = defaultToString) {\n this.toStrFn = toStrFn;\n this.table = {};\n }\n set(key, value) {\n if (key != null && value != null) {\n const tableKey = this.toStrFn(key);\n this.table[tableKey] = new ValuePair(key, value);\n return true;\n }\n return false;\n }\n get(key) {\n const valuePair = this.table[this.toStrFn(key)];\n return valuePair == null ? undefined : valuePair.value;\n }\n hasKey(key) {\n return this.table[this.toStrFn(key)] != null;\n }\n remove(key) {\n if (this.hasKey(key)) {\n delete this.table[this.toStrFn(key)];\n return true;\n }\n return false;\n }\n values() {\n return this.keyValues().map(valuePair => valuePair.value);\n }\n keys() {\n return this.keyValues().map(valuePair => valuePair.key);\n }\n keyValues() {\n return Object.values(this.table);\n }\n forEach(callbackFn) {\n const valuePairs = this.keyValues();\n for (let i = 0; i < valuePairs.length; i++) {\n const result = callbackFn(valuePairs[i].key, valuePairs[i].value);\n if (result === false) {\n break;\n }\n }\n }\n isEmpty() {\n return this.size() === 0;\n }\n size() {\n return Object.keys(this.table).length;\n }\n clear() {\n this.table = {};\n }\n toString() {\n if (this.isEmpty()) {\n return '';\n }\n const valuePairs = this.keyValues();\n let objString = `${valuePairs[0].toString()}`;\n for (let i = 1; i < valuePairs.length; i++) {\n objString = `${objString},${valuePairs[i].toString()}`;\n }\n return objString;\n }\n}\n","import { defaultEquals } from '../util';\nimport LinkedList from './linked-list';\nimport { DoublyNode } from './models/linked-list-models';\n\nexport default class DoublyLinkedList extends LinkedList {\n constructor(equalsFn = defaultEquals) {\n super(equalsFn);\n this.tail = undefined;\n }\n push(element) {\n const node = new DoublyNode(element);\n if (this.head == null) {\n this.head = node;\n this.tail = node; // NEW\n } else {\n // attach to the tail node // NEW\n this.tail.next = node;\n node.prev = this.tail;\n this.tail = node;\n }\n this.count++;\n }\n insert(element, index) {\n if (index >= 0 && index <= this.count) {\n const node = new DoublyNode(element);\n let current = this.head;\n if (index === 0) {\n if (this.head == null) { // NEW\n this.head = node;\n this.tail = node; // NEW\n } else {\n node.next = this.head;\n this.head.prev = node; // NEW\n this.head = node;\n }\n } else if (index === this.count) { // last item NEW\n current = this.tail;\n current.next = node;\n node.prev = current;\n this.tail = node;\n } else {\n const previous = this.getElementAt(index - 1);\n current = previous.next;\n node.next = current;\n previous.next = node;\n current.prev = node; // NEW\n node.prev = previous; // NEW\n }\n this.count++;\n return true;\n }\n return false;\n }\n removeAt(index) {\n if (index >= 0 && index < this.count) {\n let current = this.head;\n if (index === 0) {\n this.head = this.head.next;\n // if there is only one item, then we update tail as well //NEW\n if (this.count === 1) {\n // {2}\n this.tail = undefined;\n } else {\n this.head.prev = undefined;\n }\n } else if (index === this.count - 1) {\n // last item //NEW\n current = this.tail;\n this.tail = current.prev;\n this.tail.next = undefined;\n } else {\n current = this.getElementAt(index);\n const previous = current.prev;\n // link previous with current's next - skip it to remove\n previous.next = current.next;\n current.next.prev = previous; // NEW\n }\n this.count--;\n return current.element;\n }\n return undefined;\n }\n indexOf(element) {\n let current = this.head;\n let index = 0;\n while (current != null) {\n if (this.equalsFn(element, current.element)) {\n return index;\n }\n index++;\n current = current.next;\n }\n return -1;\n }\n getHead() {\n return this.head;\n }\n getTail() {\n return this.tail;\n }\n clear() {\n super.clear();\n this.tail = undefined;\n }\n toString() {\n if (this.head == null) {\n return '';\n }\n let objString = `${this.head.element}`;\n let current = this.head.next;\n while (current != null) {\n objString = `${objString},${current.element}`;\n current = current.next;\n }\n return objString;\n }\n inverseToString() {\n if (this.tail == null) {\n return '';\n }\n let objString = `${this.tail.element}`;\n let previous = this.tail.prev;\n while (previous != null) {\n objString = `${objString},${previous.element}`;\n previous = previous.prev;\n }\n return objString;\n }\n}\n","// @ts-check\n\nexport default class Deque {\n constructor() {\n this.count = 0;\n this.lowestCount = 0;\n this.items = {};\n }\n\n addFront(element) {\n if (this.isEmpty()) {\n this.addBack(element);\n } else if (this.lowestCount > 0) {\n this.lowestCount--;\n this.items[this.lowestCount] = element;\n } else {\n for (let i = this.count; i > 0; i--) {\n this.items[i] = this.items[i - 1];\n }\n this.count++;\n this.items[0] = element;\n }\n }\n\n addBack(element) {\n this.items[this.count] = element;\n this.count++;\n }\n\n removeFront() {\n if (this.isEmpty()) {\n return undefined;\n }\n const result = this.items[this.lowestCount];\n delete this.items[this.lowestCount];\n this.lowestCount++;\n return result;\n }\n\n removeBack() {\n if (this.isEmpty()) {\n return undefined;\n }\n this.count--;\n const result = this.items[this.count];\n delete this.items[this.count];\n return result;\n }\n\n peekFront() {\n if (this.isEmpty()) {\n return undefined;\n }\n return this.items[this.lowestCount];\n }\n\n peekBack() {\n if (this.isEmpty()) {\n return undefined;\n }\n return this.items[this.count - 1];\n }\n\n isEmpty() {\n return this.size() === 0;\n }\n\n clear() {\n this.items = {};\n this.count = 0;\n this.lowestCount = 0;\n }\n\n size() {\n return this.count - this.lowestCount;\n }\n\n toString() {\n if (this.isEmpty()) {\n return '';\n }\n let objString = `${this.items[this.lowestCount]}`;\n for (let i = this.lowestCount + 1; i < this.count; i++) {\n objString = `${objString},${this.items[i]}`;\n }\n return objString;\n }\n}\n","export const floydWarshall = graph => {\n const dist = [];\n const { length } = graph;\n for (let i = 0; i < length; i++) {\n dist[i] = [];\n for (let j = 0; j < length; j++) {\n if (i === j) {\n dist[i][j] = 0;\n } else if (!isFinite(graph[i][j])) {\n dist[i][j] = Infinity;\n } else {\n dist[i][j] = graph[i][j];\n }\n }\n }\n for (let k = 0; k < length; k++) {\n for (let i = 0; i < length; i++) {\n for (let j = 0; j < length; j++) {\n if (dist[i][k] + dist[k][j] < dist[i][j]) {\n dist[i][j] = dist[i][k] + dist[k][j];\n }\n }\n }\n }\n return dist;\n};\n","const UNASSIGNED = 0;\n\nfunction usedInRow(matrix, row, num) {\n for (let col = 0; col < matrix.length; col++) {\n if (matrix[row][col] === num) {\n return true;\n }\n }\n return false;\n}\n\nfunction usedInCol(matrix, col, num) {\n for (let row = 0; row < matrix.length; row++) {\n if (matrix[row][col] === num) {\n return true;\n }\n }\n return false;\n}\n\nfunction usedInBox(matrix, boxStartRow, boxStartCol, num) {\n for (let row = 0; row < 3; row++) {\n for (let col = 0; col < 3; col++) {\n if (matrix[row + boxStartRow][col + boxStartCol] === num) {\n return true;\n }\n }\n }\n return false;\n}\n\nfunction isSafe(matrix, row, col, num) {\n return (\n !usedInRow(matrix, row, num) &&\n !usedInCol(matrix, col, num) &&\n !usedInBox(matrix, row - (row % 3), col - (col % 3), num)\n );\n}\nfunction solveSudoku(matrix) {\n let row = 0;\n let col = 0;\n let checkBlankSpaces = false;\n\n for (row = 0; row < matrix.length; row++) {\n for (col = 0; col < matrix[row].length; col++) {\n if (matrix[row][col] === UNASSIGNED) {\n checkBlankSpaces = true;\n break;\n }\n }\n if (checkBlankSpaces === true) {\n break;\n }\n }\n if (checkBlankSpaces === false) {\n return true;\n }\n\n for (let num = 1; num <= 9; num++) {\n if (isSafe(matrix, row, col, num)) {\n matrix[row][col] = num;\n if (solveSudoku(matrix)) {\n return true;\n }\n matrix[row][col] = UNASSIGNED;\n }\n }\n return false;\n}\n\nexport function sudokuSolver(matrix) {\n if (solveSudoku(matrix) === true) {\n return matrix;\n }\n return 'NO SOLUTION EXISTS!';\n}\n","function isSafe(maze, x, y) {\n const n = maze.length;\n if (x >= 0 && y >= 0 && x < n && y < n && maze[x][y] !== 0) {\n return true;\n }\n return false;\n}\n\nfunction findPath(maze, x, y, solution) {\n const n = maze.length;\n if (x === n - 1 && y === n - 1) {\n solution[x][y] = 1;\n return true;\n }\n if (isSafe(maze, x, y) === true) {\n solution[x][y] = 1;\n if (findPath(maze, x + 1, y, solution)) {\n return true;\n }\n if (findPath(maze, x, y + 1, solution)) {\n return true;\n }\n solution[x][y] = 0;\n return false;\n }\n return false;\n}\n\nexport function ratInAMaze(maze) {\n const solution = [];\n for (let i = 0; i < maze.length; i++) {\n solution[i] = [];\n for (let j = 0; j < maze[i].length; j++) {\n solution[i][j] = 0;\n }\n }\n if (findPath(maze, 0, 0, solution) === true) {\n return solution;\n }\n return 'NO PATH FOUND';\n}\n","export function matrixChainOrder(p, i = 1, j = p.length - 1) {\n if (i === j) {\n return 0;\n }\n let min = Number.MAX_SAFE_INTEGER;\n for (let k = i; k < j; k++) {\n const count =\n matrixChainOrder(p, i, k) + matrixChainOrder(p, k + 1, j) + ((p[i - 1] * p[k]) * p[j]);\n if (count < min) {\n min = count;\n }\n }\n return min;\n}\n","function printOptimalParenthesis(s, i, j) {\n if (i === j) {\n // console.log('A[' + i + ']');\n } else {\n // console.log('(');\n printOptimalParenthesis(s, i, s[i][j]);\n printOptimalParenthesis(s, s[i][j] + 1, j);\n // console.log(')');\n }\n}\n\nexport function matrixChainOrder(p) {\n const n = p.length;\n const m = [];\n const s = [];\n for (let i = 1; i <= n; i++) {\n m[i] = [];\n m[i][i] = 0;\n }\n for (let i = 0; i <= n; i++) {\n // to help printing the optimal solution\n s[i] = []; // auxiliary\n for (let j = 0; j <= n; j++) {\n s[i][j] = 0;\n }\n }\n for (let l = 2; l < n; l++) {\n for (let i = 1; i <= (n - l) + 1; i++) {\n const j = (i + l) - 1;\n m[i][j] = Number.MAX_SAFE_INTEGER;\n for (let k = i; k <= j - 1; k++) {\n // q = cost/scalar multiplications\n const q = m[i][k] + m[k + 1][j] + ((p[i - 1] * p[k]) * p[j]);\n if (q < m[i][j]) {\n m[i][j] = q;\n s[i][j] = k; // s[i,j] = Second auxiliary table that stores k\n }\n }\n }\n }\n // console.log(m);\n // console.log(s);\n printOptimalParenthesis(s, 1, n - 1);\n return m[1][n - 1];\n}\n\n","export function lcs(wordX, wordY, m = wordX.length, n = wordY.length) {\n if (m === 0 || n === 0) {\n return 0;\n }\n if (wordX[m - 1] === wordY[n - 1]) {\n return 1 + lcs(wordX, wordY, m - 1, n - 1);\n }\n const a = lcs(wordX, wordY, m, n - 1);\n const b = lcs(wordX, wordY, m - 1, n);\n return a > b ? a : b;\n}\n","function printSolution(solution, wordX, m, n) {\n let a = m;\n let b = n;\n let x = solution[a][b];\n let answer = '';\n while (x !== '0') {\n if (solution[a][b] === 'diagonal') {\n answer = wordX[a - 1] + answer;\n a--;\n b--;\n } else if (solution[a][b] === 'left') {\n b--;\n } else if (solution[a][b] === 'top') {\n a--;\n }\n x = solution[a][b];\n }\n return answer;\n}\nexport function lcs(wordX, wordY) {\n const m = wordX.length;\n const n = wordY.length;\n const l = [];\n const solution = [];\n for (let i = 0; i <= m; i++) {\n l[i] = [];\n solution[i] = [];\n for (let j = 0; j <= n; j++) {\n l[i][j] = 0;\n solution[i][j] = '0';\n }\n }\n for (let i = 0; i <= m; i++) {\n for (let j = 0; j <= n; j++) {\n if (i === 0 || j === 0) {\n l[i][j] = 0;\n } else if (wordX[i - 1] === wordY[j - 1]) {\n l[i][j] = l[i - 1][j - 1] + 1;\n solution[i][j] = 'diagonal';\n } else {\n const a = l[i - 1][j];\n const b = l[i][j - 1];\n l[i][j] = a > b ? a : b; // max(a,b)\n solution[i][j] = l[i][j] === l[i - 1][j] ? 'top' : 'left';\n }\n }\n // console.log(l[i].join());\n // console.log(solution[i].join());\n }\n return printSolution(solution, wordX, m, n);\n}\n","export function lcs(wordX, wordY) {\n const m = wordX.length;\n const n = wordY.length;\n const l = [];\n for (let i = 0; i <= m; i++) {\n l[i] = [];\n for (let j = 0; j <= n; j++) {\n l[i][j] = 0;\n }\n }\n for (let i = 0; i <= m; i++) {\n for (let j = 0; j <= n; j++) {\n if (i === 0 || j === 0) {\n l[i][j] = 0;\n } else if (wordX[i - 1] === wordY[j - 1]) {\n l[i][j] = l[i - 1][j - 1] + 1;\n } else {\n const a = l[i - 1][j];\n const b = l[i][j - 1];\n l[i][j] = a > b ? a : b; // max(a,b)\n }\n }\n // console.log(l[i].join());\n }\n return l[m][n];\n}\n","export function knapSack(capacity, weights, values) {\n const n = values.length;\n let load = 0;\n let val = 0;\n for (let i = 0; i < n && load < capacity; i++) {\n if (weights[i] <= capacity - load) {\n val += values[i];\n load += weights[i];\n // console.log('using item ' + (i + 1) + ' for the solution');\n } else {\n const r = (capacity - load) / weights[i];\n val += r * values[i];\n load += weights[i];\n // console.log('using ratio of ' + r + ' for item ' + (i + 1) + ' for the solution');\n }\n }\n return val;\n}\n","export function knapSack(capacity, weights, values, n) {\n if (n === 0 || capacity === 0) {\n return 0;\n }\n if (weights[n - 1] > capacity) {\n return knapSack(capacity, weights, values, n - 1);\n }\n const a = values[n - 1] + knapSack(capacity - weights[n - 1], weights, values, n - 1);\n const b = knapSack(capacity, weights, values, n - 1);\n return a > b ? a : b;\n}\n","function findValues(n, capacity, kS) {\n let i = n;\n let k = capacity;\n // console.log('Items that are part of the solution:');\n while (i > 0 && k > 0) {\n if (kS[i][k] !== kS[i - 1][k]) {\n // console.log(\n // item ' + i + ' can be part of solution w,v: ' + weights[i - 1] + ',' + values[i - 1]\n // );\n i--;\n k -= kS[i][k];\n } else {\n i--;\n }\n }\n}\n\nexport function knapSack(capacity, weights, values, n) {\n const kS = [];\n for (let i = 0; i <= n; i++) {\n kS[i] = [];\n }\n for (let i = 0; i <= n; i++) {\n for (let w = 0; w <= capacity; w++) {\n if (i === 0 || w === 0) {\n kS[i][w] = 0;\n } else if (weights[i - 1] <= w) {\n const a = values[i - 1] + kS[i - 1][w - weights[i - 1]];\n const b = kS[i - 1][w];\n kS[i][w] = a > b ? a : b; // max(a,b)\n // console.log(a + ' can be part of the solution');\n } else {\n kS[i][w] = kS[i - 1][w];\n }\n }\n // console.log(kS[i].join());\n }\n // extra algorithm to find the items that are part of the solution\n findValues(n, capacity, kS);\n return kS[n][capacity];\n}\n","export function minCoinChange(coins, amount) {\n const change = [];\n let total = 0;\n for (let i = coins.length; i >= 0; i--) {\n const coin = coins[i];\n while (total + coin <= amount) {\n change.push(coin);\n total += coin;\n }\n }\n return change;\n}\n","export function minCoinChange(coins, amount) {\n const cache = [];\n\n const makeChange = (value) => {\n if (!value) {\n return [];\n }\n if (cache[value]) {\n return cache[value];\n }\n let min = [];\n let newMin;\n let newAmount;\n for (let i = 0; i < coins.length; i++) {\n const coin = coins[i];\n newAmount = value - coin;\n if (newAmount >= 0) {\n newMin = makeChange(newAmount);\n }\n if (\n newAmount >= 0 &&\n (newMin.length < min.length - 1 || !min.length) &&\n (newMin.length || !newAmount)\n ) {\n min = [coin].concat(newMin);\n // console.log('new Min ' + min + ' for ' + amount);\n }\n }\n return (cache[value] = min);\n };\n return makeChange(amount);\n}\n","import { Compare, defaultCompare, DOES_NOT_EXIST } from '../../util';\nimport { quickSort } from '../sorting/quicksort';\n\nfunction binarySearchRecursive(array, value, low, high, compareFn = defaultCompare) {\n if (low <= high) {\n const mid = Math.floor((low + high) / 2);\n const element = array[mid];\n if (compareFn(element, value) === Compare.LESS_THAN) {\n return binarySearchRecursive(array, value, mid + 1, high, compareFn);\n }\n if (compareFn(element, value) === Compare.BIGGER_THAN) {\n return binarySearchRecursive(array, value, low, mid - 1, compareFn);\n }\n return mid;\n }\n return DOES_NOT_EXIST;\n}\n\nexport function binarySearch(array, value, compareFn = defaultCompare) {\n const sortedArray = quickSort(array);\n const low = 0;\n const high = sortedArray.length - 1;\n return binarySearchRecursive(array, value, low, high, compareFn);\n}\n","import { defaultEquals, DOES_NOT_EXIST } from '../../util';\n\nexport function sequentialSearch(array, value, equalsFn = defaultEquals) {\n for (let i = 0; i < array.length; i++) {\n if (equalsFn(value, array[i])) {\n return i;\n }\n }\n return DOES_NOT_EXIST;\n}\n","import {\n biggerEquals,\n Compare,\n defaultCompare,\n defaultEquals,\n defaultDiff,\n DOES_NOT_EXIST,\n lesserEquals\n} from '../../util';\n\nexport function interpolationSearch(\n array,\n value,\n compareFn = defaultCompare,\n equalsFn = defaultEquals,\n diffFn = defaultDiff\n) {\n const { length } = array;\n let low = 0;\n let high = length - 1;\n let position = -1;\n let delta = -1;\n while (\n low <= high &&\n biggerEquals(value, array[low], compareFn) &&\n lesserEquals(value, array[high], compareFn)\n ) {\n delta = diffFn(value, array[low]) / diffFn(array[high], array[low]);\n position = low + Math.floor((high - low) * delta);\n if (equalsFn(array[position], value)) {\n return position;\n }\n if (compareFn(array[position], value) === Compare.LESS_THAN) {\n low = position + 1;\n } else {\n high = position - 1;\n }\n }\n return DOES_NOT_EXIST;\n}\n","import { Compare, defaultCompare, DOES_NOT_EXIST } from '../../util';\nimport { quickSort } from '../sorting/quicksort';\n\nexport function binarySearch(array, value, compareFn = defaultCompare) {\n const sortedArray = quickSort(array);\n let low = 0;\n let high = sortedArray.length - 1;\n while (low <= high) {\n const mid = Math.floor((low + high) / 2);\n const element = sortedArray[mid];\n // console.log('mid element is ' + element);\n if (compareFn(element, value) === Compare.LESS_THAN) {\n low = mid + 1;\n // console.log('low is ' + low);\n } else if (compareFn(element, value) === Compare.BIGGER_THAN) {\n high = mid - 1;\n // console.log('high is ' + high);\n } else {\n // console.log('found it');\n return mid;\n }\n }\n return DOES_NOT_EXIST;\n}\n","import { Compare, defaultCompare } from '../../util';\n\nexport function shellSort(array, compareFn = defaultCompare) {\n let increment = array.length / 2;\n while (increment > 0) {\n for (let i = increment; i < array.length; i++) {\n let j = i;\n const temp = array[i];\n while (j >= increment && compareFn(array[j - increment], temp) === Compare.BIGGER_THAN) {\n array[j] = array[j - increment];\n j -= increment;\n }\n array[j] = temp;\n }\n if (increment === 2) {\n increment = 1;\n } else {\n increment = Math.floor((increment * 5) / 11);\n }\n }\n return array;\n}\n","import { Compare, defaultCompare, swap } from '../../util';\n\nexport const selectionSort = (array, compareFn = defaultCompare) => {\n const { length } = array;\n let indexMin;\n for (let i = 0; i < length - 1; i++) {\n indexMin = i;\n // console.log('index ' + array[i]);\n for (let j = i; j < length; j++) {\n if (compareFn(array[indexMin], array[j]) === Compare.BIGGER_THAN) {\n // console.log('new index min ' + array[j]);\n indexMin = j;\n }\n }\n if (i !== indexMin) {\n // console.log('swap ' + array[i] + ' with ' + array[indexMin]);\n swap(array, i, indexMin);\n }\n }\n return array;\n};\n","import { findMaxValue, findMinValue } from '../search/min-max-search';\n\nconst getBucketIndex = (value, minValue, significantDigit, radixBase) =>\n Math.floor(((value - minValue) / significantDigit) % radixBase);\n\nconst countingSortForRadix = (array, radixBase, significantDigit, minValue) => {\n let bucketsIndex;\n const buckets = [];\n const aux = [];\n for (let i = 0; i < radixBase; i++) {\n buckets[i] = 0;\n }\n for (let i = 0; i < array.length; i++) {\n bucketsIndex = getBucketIndex(array[i], minValue, significantDigit, radixBase);\n buckets[bucketsIndex]++;\n }\n for (let i = 1; i < radixBase; i++) {\n buckets[i] += buckets[i - 1];\n }\n for (let i = array.length - 1; i >= 0; i--) {\n bucketsIndex = getBucketIndex(array[i], minValue, significantDigit, radixBase);\n aux[--buckets[bucketsIndex]] = array[i];\n }\n for (let i = 0; i < array.length; i++) {\n array[i] = aux[i];\n }\n return array;\n};\nexport function radixSort(array, radixBase = 10) {\n if (array.length < 2) {\n return array;\n }\n const minValue = findMinValue(array);\n const maxValue = findMaxValue(array);\n // Perform counting sort for each significant digit, starting at 1\n let significantDigit = 1;\n while ((maxValue - minValue) / significantDigit >= 1) {\n // console.log('radix sort for digit ' + significantDigit);\n array = countingSortForRadix(array, radixBase, significantDigit, minValue);\n // console.log(array.join());\n significantDigit *= radixBase;\n }\n return array;\n}\n","import { Compare, defaultCompare } from '../../util';\n\nfunction merge(left, right, compareFn) {\n let i = 0;\n let j = 0;\n const result = [];\n while (i < left.length && j < right.length) {\n result.push(compareFn(left[i], right[j]) === Compare.LESS_THAN ? left[i++] : right[j++]);\n }\n return result.concat(i < left.length ? left.slice(i) : right.slice(j));\n}\nexport function mergeSort(array, compareFn = defaultCompare) {\n if (array.length > 1) {\n const { length } = array;\n const middle = Math.floor(length / 2);\n const left = mergeSort(array.slice(0, middle), compareFn);\n const right = mergeSort(array.slice(middle, length), compareFn);\n array = merge(left, right, compareFn);\n }\n return array;\n}\n","import { findMaxValue } from '../search/min-max-search';\n\nexport function countingSort(array) {\n if (array.length < 2) {\n return array;\n }\n const maxValue = findMaxValue(array);\n let sortedIndex = 0;\n const counts = new Array(maxValue + 1);\n array.forEach(element => {\n if (!counts[element]) {\n counts[element] = 0;\n }\n counts[element]++;\n });\n // console.log('Frequencies: ' + counts.join());\n counts.forEach((element, i) => {\n while (element > 0) {\n array[sortedIndex++] = i;\n element--;\n }\n });\n return array;\n}\n","import { insertionSort } from './insertion-sort';\n\nfunction createBuckets(array, bucketSize) {\n let minValue = array[0];\n let maxValue = array[0];\n for (let i = 1; i < array.length; i++) {\n if (array[i] < minValue) {\n minValue = array[i];\n } else if (array[i] > maxValue) {\n maxValue = array[i];\n }\n }\n const bucketCount = Math.floor((maxValue - minValue) / bucketSize) + 1;\n const buckets = [];\n for (let i = 0; i < bucketCount; i++) {\n buckets[i] = [];\n }\n for (let i = 0; i < array.length; i++) {\n buckets[Math.floor((array[i] - minValue) / bucketSize)].push(array[i]);\n }\n return buckets;\n}\nfunction sortBuckets(buckets) {\n const sortedArray = [];\n for (let i = 0; i < buckets.length; i++) {\n if (buckets[i] != null) {\n insertionSort(buckets[i]);\n sortedArray.push(...buckets[i]);\n }\n }\n return sortedArray;\n}\nexport function bucketSort(array, bucketSize = 5) {\n if (array.length < 2) {\n return array;\n }\n const buckets = createBuckets(array, bucketSize);\n return sortBuckets(buckets);\n}\n","import { Compare, defaultCompare, swap } from '../../util';\n\nexport function modifiedBubbleSort(array, compareFn = defaultCompare) {\n const { length } = array;\n for (let i = 0; i < length; i++) {\n // console.log('--- ');\n for (let j = 0; j < length - 1 - i; j++) {\n // console.log('compare ' + array[j] + ' with ' + array[j + 1]);\n if (compareFn(array[j], array[j + 1]) === Compare.BIGGER_THAN) {\n // console.log('swap ' + array[j] + ' with ' + array[j + 1]);\n swap(array, j, j + 1);\n }\n }\n }\n return array;\n}\n","import { Compare, defaultCompare, swap } from '../../util';\n\nexport function bubbleSort(array, compareFn = defaultCompare) {\n const { length } = array;\n for (let i = 0; i < length; i++) {\n // console.log('--- ');\n for (let j = 0; j < length - 1; j++) {\n // console.log('compare ' + array[j] + ' with ' + array[j + 1]);\n if (compareFn(array[j], array[j + 1]) === Compare.BIGGER_THAN) {\n // console.log('swap ' + array[j] + ' with ' + array[j + 1]);\n swap(array, j, j + 1);\n }\n }\n }\n return array;\n}\n","import { swap } from '../../util';\n\nexport function shuffle(array) {\n let currentIndex = array.length;\n while (currentIndex !== 0) {\n const randomIndex = Math.floor(Math.random() * currentIndex);\n currentIndex--;\n swap(array, currentIndex, randomIndex);\n }\n return array;\n}\n","const INF = Number.MAX_SAFE_INTEGER;\nconst find = (i, parent) => {\n while (parent[i]) {\n i = parent[i]; // eslint-disable-line prefer-destructuring\n }\n return i;\n};\nconst union = (i, j, parent) => {\n if (i !== j) {\n parent[j] = i;\n return true;\n }\n return false;\n};\nconst initializeCost = graph => {\n const cost = [];\n const { length } = graph;\n for (let i = 0; i < length; i++) {\n cost[i] = [];\n for (let j = 0; j < length; j++) {\n if (graph[i][j] === 0) {\n cost[i][j] = INF;\n } else {\n cost[i][j] = graph[i][j];\n }\n }\n }\n return cost;\n};\nexport const kruskal = graph => {\n const { length } = graph;\n const parent = [];\n let ne = 0;\n let a;\n let b;\n let u;\n let v;\n const cost = initializeCost(graph);\n while (ne < length - 1) {\n for (let i = 0, min = INF; i < length; i++) {\n for (let j = 0; j < length; j++) {\n if (cost[i][j] < min) {\n min = cost[i][j];\n a = u = i;\n b = v = j;\n }\n }\n }\n u = find(u, parent);\n v = find(v, parent);\n if (union(u, v, parent)) {\n ne++;\n }\n cost[a][b] = cost[b][a] = INF;\n }\n return parent;\n};\n","const INF = Number.MAX_SAFE_INTEGER;\nconst minKey = (graph, key, visited) => {\n // Initialize min value\n let min = INF;\n let minIndex = 0;\n for (let v = 0; v < graph.length; v++) {\n if (visited[v] === false && key[v] < min) {\n min = key[v];\n minIndex = v;\n }\n }\n return minIndex;\n};\nexport const prim = graph => {\n const parent = [];\n const key = [];\n const visited = [];\n const { length } = graph;\n for (let i = 0; i < length; i++) {\n key[i] = INF;\n visited[i] = false;\n }\n key[0] = 0;\n parent[0] = -1;\n for (let i = 0; i < length - 1; i++) {\n const u = minKey(graph, key, visited);\n visited[u] = true;\n for (let v = 0; v < length; v++) {\n if (graph[u][v] && !visited[v] && graph[u][v] < key[v]) {\n parent[v] = u;\n key[v] = graph[u][v];\n }\n }\n }\n return parent;\n};\n","const INF = Number.MAX_SAFE_INTEGER;\nconst minDistance = (dist, visited) => {\n let min = INF;\n let minIndex = -1;\n for (let v = 0; v < dist.length; v++) {\n if (visited[v] === false && dist[v] <= min) {\n min = dist[v];\n minIndex = v;\n }\n }\n return minIndex;\n};\nexport const dijkstra = (graph, src) => {\n const dist = [];\n const visited = [];\n const { length } = graph;\n for (let i = 0; i < length; i++) {\n dist[i] = INF;\n visited[i] = false;\n }\n dist[src] = 0;\n for (let i = 0; i < length - 1; i++) {\n const u = minDistance(dist, visited);\n visited[u] = true;\n for (let v = 0; v < length; v++) {\n if (!visited[v] && graph[u][v] !== 0 && dist[u] !== INF && dist[u] + graph[u][v] < dist[v]) {\n dist[v] = dist[u] + graph[u][v];\n }\n }\n }\n return dist;\n};\n","// import Graph from '../../data-structures/graph';\n\nconst Colors = {\n WHITE: 0,\n GREY: 1,\n BLACK: 2\n};\n\nconst initializeColor = vertices => {\n const color = {};\n for (let i = 0; i < vertices.length; i++) {\n color[vertices[i]] = Colors.WHITE;\n }\n return color;\n};\n\nconst depthFirstSearchVisit = (u, color, adjList, callback) => {\n color[u] = Colors.GREY;\n if (callback) {\n callback(u);\n }\n // console.log('Discovered ' + u);\n const neighbors = adjList.get(u);\n for (let i = 0; i < neighbors.length; i++) {\n const w = neighbors[i];\n if (color[w] === Colors.WHITE) {\n depthFirstSearchVisit(w, color, adjList, callback);\n }\n }\n color[u] = Colors.BLACK;\n // console.log('explored ' + u);\n};\n\nexport const depthFirstSearch = (graph, callback) => {\n const vertices = graph.getVertices();\n const adjList = graph.getAdjList();\n const color = initializeColor(vertices);\n\n for (let i = 0; i < vertices.length; i++) {\n if (color[vertices[i]] === Colors.WHITE) {\n depthFirstSearchVisit(vertices[i], color, adjList, callback);\n }\n }\n};\n\nconst DFSVisit = (u, color, d, f, p, time, adjList) => {\n // console.log('discovered ' + u);\n color[u] = Colors.GREY;\n d[u] = ++time.count;\n const neighbors = adjList.get(u);\n for (let i = 0; i < neighbors.length; i++) {\n const w = neighbors[i];\n if (color[w] === Colors.WHITE) {\n p[w] = u;\n DFSVisit(w, color, d, f, p, time, adjList);\n }\n }\n color[u] = Colors.BLACK;\n f[u] = ++time.count;\n // console.log('explored ' + u);\n};\n\nexport const DFS = graph => {\n const vertices = graph.getVertices();\n const adjList = graph.getAdjList();\n const color = initializeColor(vertices);\n const d = {};\n const f = {};\n const p = {};\n const time = { count: 0 };\n for (let i = 0; i < vertices.length; i++) {\n f[vertices[i]] = 0;\n d[vertices[i]] = 0;\n p[vertices[i]] = null;\n }\n for (let i = 0; i < vertices.length; i++) {\n if (color[vertices[i]] === Colors.WHITE) {\n DFSVisit(vertices[i], color, d, f, p, time, adjList);\n }\n }\n return {\n discovery: d,\n finished: f,\n predecessors: p\n };\n};\n","import Queue from '../../data-structures/queue';\n\nconst Colors = {\n WHITE: 0,\n GREY: 1,\n BLACK: 2\n};\n\nconst initializeColor = vertices => {\n const color = {};\n for (let i = 0; i < vertices.length; i++) {\n color[vertices[i]] = Colors.WHITE;\n }\n return color;\n};\n\nexport const breadthFirstSearch = (graph, startVertex, callback) => {\n const vertices = graph.getVertices();\n const adjList = graph.getAdjList();\n const color = initializeColor(vertices);\n const queue = new Queue();\n\n queue.enqueue(startVertex);\n\n while (!queue.isEmpty()) {\n const u = queue.dequeue();\n const neighbors = adjList.get(u);\n color[u] = Colors.GREY;\n for (let i = 0; i < neighbors.length; i++) {\n const w = neighbors[i];\n if (color[w] === Colors.WHITE) {\n color[w] = Colors.GREY;\n queue.enqueue(w);\n }\n }\n color[u] = Colors.BLACK;\n if (callback) {\n callback(u);\n }\n }\n};\n\nexport const BFS = (graph, startVertex) => {\n const vertices = graph.getVertices();\n const adjList = graph.getAdjList();\n const color = initializeColor(vertices);\n const queue = new Queue();\n const distances = {};\n const predecessors = {};\n queue.enqueue(startVertex);\n for (let i = 0; i < vertices.length; i++) {\n distances[vertices[i]] = 0;\n predecessors[vertices[i]] = null;\n }\n while (!queue.isEmpty()) {\n const u = queue.dequeue();\n const neighbors = adjList.get(u);\n color[u] = Colors.GREY;\n for (let i = 0; i < neighbors.length; i++) {\n const w = neighbors[i];\n if (color[w] === Colors.WHITE) {\n color[w] = Colors.GREY;\n distances[w] = distances[u] + 1;\n predecessors[w] = u;\n queue.enqueue(w);\n }\n }\n color[u] = Colors.BLACK;\n }\n return {\n distances,\n predecessors\n };\n};\n","import Dictionary from './dictionary';\n\nexport default class Graph {\n constructor(isDirected = false) {\n this.isDirected = isDirected;\n this.vertices = [];\n this.adjList = new Dictionary();\n }\n addVertex(v) {\n if (!this.vertices.includes(v)) {\n this.vertices.push(v);\n this.adjList.set(v, []); // initialize adjacency list with array as well;\n }\n }\n addEdge(a, b) {\n if (!this.adjList.get(a)) {\n this.addVertex(a);\n }\n if (!this.adjList.get(b)) {\n this.addVertex(b);\n }\n this.adjList.get(a).push(b);\n if (this.isDirected !== true) {\n this.adjList.get(b).push(a);\n }\n }\n getVertices() {\n return this.vertices;\n }\n getAdjList() {\n return this.adjList;\n }\n toString() {\n let s = '';\n for (let i = 0; i < this.vertices.length; i++) {\n s += `${this.vertices[i]} -> `;\n const neighbors = this.adjList.get(this.vertices[i]);\n for (let j = 0; j < neighbors.length; j++) {\n s += `${neighbors[j]} `;\n }\n s += '\\n';\n }\n return s;\n }\n}\n","import { defaultCompare, swap } from '../../util';\n\nfunction heapify(array, index, heapSize, compareFn) {\n let largest = index;\n const left = (2 * index) + 1;\n const right = (2 * index) + 2;\n if (left < heapSize && compareFn(array[left], array[index]) > 0) {\n largest = left;\n }\n if (right < heapSize && compareFn(array[right], array[largest]) > 0) {\n largest = right;\n }\n if (largest !== index) {\n swap(array, index, largest);\n heapify(array, largest, heapSize, compareFn);\n }\n}\n\nfunction buildMaxHeap(array, compareFn) {\n for (let i = Math.floor(array.length / 2); i >= 0; i -= 1) {\n heapify(array, i, array.length, compareFn);\n }\n return array;\n}\n\nexport default function heapSort(array, compareFn = defaultCompare) {\n let heapSize = array.length;\n buildMaxHeap(array, compareFn);\n while (heapSize > 1) {\n swap(array, 0, --heapSize);\n heapify(array, 0, heapSize, compareFn);\n }\n return array;\n}\n","import { Compare, defaultCompare, reverseCompare, swap } from '../util';\n\nexport class MinHeap {\n constructor(compareFn = defaultCompare) {\n this.compareFn = compareFn;\n this.heap = [];\n }\n getLeftIndex(index) {\n return (2 * index) + 1;\n }\n getRightIndex(index) {\n return (2 * index) + 2;\n }\n getParentIndex(index) {\n if (index === 0) {\n return undefined;\n }\n return Math.floor((index - 1) / 2);\n }\n size() {\n return this.heap.length;\n }\n isEmpty() {\n return this.size() <= 0;\n }\n clear() {\n this.heap = [];\n }\n findMinimum() {\n return this.isEmpty() ? undefined : this.heap[0];\n }\n insert(value) {\n if (value != null) {\n const index = this.heap.length;\n this.heap.push(value);\n this.siftUp(index);\n return true;\n }\n return false;\n }\n siftDown(index) {\n let element = index;\n const left = this.getLeftIndex(index);\n const right = this.getRightIndex(index);\n const size = this.size();\n if (\n left < size &&\n this.compareFn(this.heap[element], this.heap[left]) === Compare.BIGGER_THAN\n ) {\n element = left;\n }\n if (\n right < size &&\n this.compareFn(this.heap[element], this.heap[right]) === Compare.BIGGER_THAN\n ) {\n element = right;\n }\n if (index !== element) {\n swap(this.heap, index, element);\n this.siftDown(element);\n }\n }\n siftUp(index) {\n let parent = this.getParentIndex(index);\n while (\n index > 0 &&\n this.compareFn(this.heap[parent], this.heap[index]) === Compare.BIGGER_THAN\n ) {\n swap(this.heap, parent, index);\n index = parent;\n parent = this.getParentIndex(index);\n }\n }\n extract() {\n if (this.isEmpty()) {\n return undefined;\n }\n if (this.size() === 1) {\n return this.heap.shift();\n }\n const removedValue = this.heap[0];\n this.heap[0] = this.heap.pop();\n this.siftDown(0);\n return removedValue;\n }\n heapify(array) {\n if (array) {\n this.heap = array;\n }\n const maxIndex = Math.floor(this.size() / 2) - 1;\n for (let i = 0; i <= maxIndex; i++) {\n this.siftDown(i);\n }\n return this.heap;\n }\n getAsArray() {\n return this.heap;\n }\n}\nexport class MaxHeap extends MinHeap {\n constructor(compareFn = defaultCompare) {\n super(compareFn);\n this.compareFn = compareFn;\n this.compareFn = reverseCompare(compareFn);\n }\n}\n","import { Compare, defaultCompare } from '../util';\nimport BinarySearchTree from './binary-search-tree';\nimport { Node } from './models/node';\n\nconst BalanceFactor = {\n UNBALANCED_RIGHT: 1,\n SLIGHTLY_UNBALANCED_RIGHT: 2,\n BALANCED: 3,\n SLIGHTLY_UNBALANCED_LEFT: 4,\n UNBALANCED_LEFT: 5\n};\n\nexport default class AVLTree extends BinarySearchTree {\n constructor(compareFn = defaultCompare) {\n super(compareFn);\n this.compareFn = compareFn;\n this.root = null;\n }\n getNodeHeight(node) {\n if (node == null) {\n return -1;\n }\n return Math.max(this.getNodeHeight(node.left), this.getNodeHeight(node.right)) + 1;\n }\n /**\n * Left left case: rotate right\n *\n * b a\n * / \\ / \\\n * a e -> rotationLL(b) -> c b\n * / \\ / \\\n * c d d e\n *\n * @param node Node\n */\n rotationLL(node) {\n const tmp = node.left;\n node.left = tmp.right;\n tmp.right = node;\n return tmp;\n }\n /**\n * Right right case: rotate left\n *\n * a b\n * / \\ / \\\n * c b -> rotationRR(a) -> a e\n * / \\ / \\\n * d e c d\n *\n * @param node Node\n */\n rotationRR(node) {\n const tmp = node.right;\n node.right = tmp.left;\n tmp.left = node;\n return tmp;\n }\n /**\n * Left right case: rotate left then right\n * @param node Node\n */\n rotationLR(node) {\n node.left = this.rotationRR(node.left);\n return this.rotationLL(node);\n }\n /**\n * Right left case: rotate right then left\n * @param node Node\n */\n rotationRL(node) {\n node.right = this.rotationLL(node.right);\n return this.rotationRR(node);\n }\n getBalanceFactor(node) {\n const heightDifference = this.getNodeHeight(node.left) - this.getNodeHeight(node.right);\n switch (heightDifference) {\n case -2:\n return BalanceFactor.UNBALANCED_RIGHT;\n case -1:\n return BalanceFactor.SLIGHTLY_UNBALANCED_RIGHT;\n case 1:\n return BalanceFactor.SLIGHTLY_UNBALANCED_LEFT;\n case 2:\n return BalanceFactor.UNBALANCED_LEFT;\n default:\n return BalanceFactor.BALANCED;\n }\n }\n insert(key) {\n this.root = this.insertNode(this.root, key);\n }\n insertNode(node, key) {\n if (node == null) {\n return new Node(key);\n } else if (this.compareFn(key, node.key) === Compare.LESS_THAN) {\n node.left = this.insertNode(node.left, key);\n } else if (this.compareFn(key, node.key) === Compare.BIGGER_THAN) {\n node.right = this.insertNode(node.right, key);\n } else {\n return node; // duplicated key\n }\n // verify if tree is balanced\n const balanceFactor = this.getBalanceFactor(node);\n if (balanceFactor === BalanceFactor.UNBALANCED_LEFT) {\n if (this.compareFn(key, node.left.key) === Compare.LESS_THAN) {\n // Left left case\n node = this.rotationLL(node);\n } else {\n // Left right case\n return this.rotationLR(node);\n }\n }\n if (balanceFactor === BalanceFactor.UNBALANCED_RIGHT) {\n if (this.compareFn(key, node.right.key) === Compare.BIGGER_THAN) {\n // Right right case\n node = this.rotationRR(node);\n } else {\n // Right left case\n return this.rotationRL(node);\n }\n }\n return node;\n }\n removeNode(node, key) {\n node = super.removeNode(node, key); // {1}\n if (node == null) {\n return node;\n }\n // verify if tree is balanced\n const balanceFactor = this.getBalanceFactor(node);\n if (balanceFactor === BalanceFactor.UNBALANCED_LEFT) {\n // Left left case\n if (\n this.getBalanceFactor(node.left) === BalanceFactor.BALANCED ||\n this.getBalanceFactor(node.left) === BalanceFactor.SLIGHTLY_UNBALANCED_LEFT\n ) {\n return this.rotationLL(node);\n }\n // Left right case\n if (this.getBalanceFactor(node.left) === BalanceFactor.SLIGHTLY_UNBALANCED_RIGHT) {\n return this.rotationLR(node.left);\n }\n }\n if (balanceFactor === BalanceFactor.UNBALANCED_RIGHT) {\n // Right right case\n if (\n this.getBalanceFactor(node.right) === BalanceFactor.BALANCED ||\n this.getBalanceFactor(node.right) === BalanceFactor.SLIGHTLY_UNBALANCED_RIGHT\n ) {\n return this.rotationRR(node);\n }\n // Right left case\n if (this.getBalanceFactor(node.right) === BalanceFactor.SLIGHTLY_UNBALANCED_LEFT) {\n return this.rotationRL(node.right);\n }\n }\n return node;\n }\n}\n","export function fibonacci(n) {\n if (n < 1) {\n return 0;\n }\n if (n <= 2) {\n return 1;\n }\n return fibonacci(n - 1) + fibonacci(n - 2);\n}\n\nexport function fibonacciIterative(n) {\n if (n < 1) { return 0; }\n let fibNMinus2 = 0;\n let fibNMinus1 = 1;\n let fibN = n;\n for (let i = 2; i <= n; i++) {\n fibN = fibNMinus1 + fibNMinus2;\n fibNMinus2 = fibNMinus1;\n fibNMinus1 = fibN;\n }\n return fibN;\n}\n\nexport function fibonacciMemoization(n) {\n if (n < 1) { return 0; }\n const memo = [0, 1];\n const fibonacciMem = num => {\n if (memo[num] != null) { return memo[num]; }\n return (memo[num] = fibonacciMem(num - 1) + fibonacciMem(num - 2));\n };\n return fibonacciMem(n);\n}\n","export function factorialIterative(number) {\n if (number < 0) {\n return undefined;\n }\n let total = 1;\n for (let n = number; n > 1; n--) {\n total *= n;\n }\n return total;\n}\n\nexport function factorial(n) {\n if (n < 0) {\n return undefined;\n }\n if (n === 1 || n === 0) {\n return 1;\n }\n return n * factorial(n - 1);\n}\n","import { ValuePair } from './value-pair';\n\nexport class ValuePairLazy extends ValuePair {\n constructor(key, value, isDeleted = false) {\n super(key, value);\n this.key = key;\n this.value = value;\n this.isDeleted = isDeleted;\n }\n}\n","import { defaultToString } from '../util';\nimport { ValuePairLazy } from './models/value-pair-lazy';\n\nexport default class HashTableLinearProbingLazy {\n constructor(toStrFn = defaultToString) {\n this.toStrFn = toStrFn;\n this.table = {};\n }\n loseloseHashCode(key) {\n if (typeof key === 'number') {\n return key;\n }\n const tableKey = this.toStrFn(key);\n let hash = 0;\n for (let i = 0; i < tableKey.length; i++) {\n hash += tableKey.charCodeAt(i);\n }\n return hash % 37;\n }\n hashCode(key) {\n return this.loseloseHashCode(key);\n }\n put(key, value) {\n if (key != null && value != null) {\n const position = this.hashCode(key);\n if (\n this.table[position] == null ||\n (this.table[position] != null && this.table[position].isDeleted)\n ) {\n this.table[position] = new ValuePairLazy(key, value);\n } else {\n let index = position + 1;\n while (this.table[index] != null && !this.table[position].isDeleted) {\n index++;\n }\n this.table[index] = new ValuePairLazy(key, value);\n }\n return true;\n }\n return false;\n }\n get(key) {\n const position = this.hashCode(key);\n if (this.table[position] != null) {\n if (this.table[position].key === key && !this.table[position].isDeleted) {\n return this.table[position].value;\n }\n let index = position + 1;\n while (\n this.table[index] != null &&\n (this.table[index].key !== key || this.table[index].isDeleted)\n ) {\n if (this.table[index].key === key && this.table[index].isDeleted) {\n return undefined;\n }\n index++;\n }\n if (\n this.table[index] != null &&\n this.table[index].key === key &&\n !this.table[index].isDeleted\n ) {\n return this.table[position].value;\n }\n }\n return undefined;\n }\n remove(key) {\n const position = this.hashCode(key);\n if (this.table[position] != null) {\n if (this.table[position].key === key && !this.table[position].isDeleted) {\n this.table[position].isDeleted = true;\n return true;\n }\n let index = position + 1;\n while (\n this.table[index] != null &&\n (this.table[index].key !== key || this.table[index].isDeleted)\n ) {\n index++;\n }\n if (\n this.table[index] != null &&\n this.table[index].key === key &&\n !this.table[index].isDeleted\n ) {\n this.table[index].isDeleted = true;\n return true;\n }\n }\n return false;\n }\n isEmpty() {\n return this.size() === 0;\n }\n size() {\n let count = 0;\n Object.values(this.table).forEach(valuePair => {\n count += valuePair.isDeleted === true ? 0 : 1;\n });\n return count;\n }\n clear() {\n this.table = {};\n }\n getTable() {\n return this.table;\n }\n toString() {\n if (this.isEmpty()) {\n return '';\n }\n const keys = Object.keys(this.table);\n let objString = `{${keys[0]} => ${this.table[keys[0]].toString()}}`;\n for (let i = 1; i < keys.length; i++) {\n objString = `${objString},{${keys[i]} => ${this.table[\n keys[i]\n ].toString()}}`;\n }\n return objString;\n }\n}\n","import { defaultToString } from '../util';\nimport { ValuePair } from './models/value-pair';\n\nexport default class HashTableLinearProbing {\n constructor(toStrFn = defaultToString) {\n this.toStrFn = toStrFn;\n this.table = {};\n }\n loseloseHashCode(key) {\n if (typeof key === 'number') {\n return key;\n }\n const tableKey = this.toStrFn(key);\n let hash = 0;\n for (let i = 0; i < tableKey.length; i++) {\n hash += tableKey.charCodeAt(i);\n }\n return hash % 37;\n }\n hashCode(key) {\n return this.loseloseHashCode(key);\n }\n put(key, value) {\n if (key != null && value != null) {\n const position = this.hashCode(key);\n if (this.table[position] == null) {\n this.table[position] = new ValuePair(key, value);\n } else {\n let index = position + 1;\n while (this.table[index] != null) {\n index++;\n }\n this.table[index] = new ValuePair(key, value);\n }\n return true;\n }\n return false;\n }\n get(key) {\n const position = this.hashCode(key);\n if (this.table[position] != null) {\n if (this.table[position].key === key) {\n return this.table[position].value;\n }\n let index = position + 1;\n while (this.table[index] != null && this.table[index].key !== key) {\n index++;\n }\n if (this.table[index] != null && this.table[index].key === key) {\n return this.table[position].value;\n }\n }\n return undefined;\n }\n remove(key) {\n const position = this.hashCode(key);\n if (this.table[position] != null) {\n if (this.table[position].key === key) {\n delete this.table[position];\n this.verifyRemoveSideEffect(key, position);\n return true;\n }\n let index = position + 1;\n while (this.table[index] != null && this.table[index].key !== key) {\n index++;\n }\n if (this.table[index] != null && this.table[index].key === key) {\n delete this.table[index];\n this.verifyRemoveSideEffect(key, index);\n return true;\n }\n }\n return false;\n }\n verifyRemoveSideEffect(key, removedPosition) {\n const hash = this.hashCode(key);\n let index = removedPosition + 1;\n while (this.table[index] != null) {\n const posHash = this.hashCode(this.table[index].key);\n if (posHash <= hash || posHash <= removedPosition) {\n this.table[removedPosition] = this.table[index];\n delete this.table[index];\n removedPosition = index;\n }\n index++;\n }\n }\n isEmpty() {\n return this.size() === 0;\n }\n size() {\n return Object.keys(this.table).length;\n }\n clear() {\n this.table = {};\n }\n getTable() {\n return this.table;\n }\n toString() {\n if (this.isEmpty()) {\n return '';\n }\n const keys = Object.keys(this.table);\n let objString = `{${keys[0]} => ${this.table[keys[0]].toString()}}`;\n for (let i = 1; i < keys.length; i++) {\n objString = `${objString},{${keys[i]} => ${this.table[\n keys[i]\n ].toString()}}`;\n }\n return objString;\n }\n}\n","import { defaultToString } from '../util';\nimport LinkedList from './linked-list';\nimport { ValuePair } from './models/value-pair';\n\nexport default class HashTableSeparateChaining {\n constructor(toStrFn = defaultToString) {\n this.toStrFn = toStrFn;\n this.table = {};\n }\n loseloseHashCode(key) {\n if (typeof key === 'number') {\n return key;\n }\n const tableKey = this.toStrFn(key);\n let hash = 0;\n for (let i = 0; i < tableKey.length; i++) {\n hash += tableKey.charCodeAt(i);\n }\n return hash % 37;\n }\n hashCode(key) {\n return this.loseloseHashCode(key);\n }\n put(key, value) {\n if (key != null && value != null) {\n const position = this.hashCode(key);\n if (this.table[position] == null) {\n this.table[position] = new LinkedList();\n }\n this.table[position].push(new ValuePair(key, value));\n return true;\n }\n return false;\n }\n get(key) {\n const position = this.hashCode(key);\n const linkedList = this.table[position];\n if (linkedList != null && !linkedList.isEmpty()) {\n let current = linkedList.getHead();\n while (current != null) {\n if (current.element.key === key) {\n return current.element.value;\n }\n current = current.next;\n }\n }\n return undefined;\n }\n remove(key) {\n const position = this.hashCode(key);\n const linkedList = this.table[position];\n if (linkedList != null && !linkedList.isEmpty()) {\n let current = linkedList.getHead();\n while (current != null) {\n if (current.element.key === key) {\n linkedList.remove(current.element);\n if (linkedList.isEmpty()) {\n delete this.table[position];\n }\n return true;\n }\n current = current.next;\n }\n }\n return false;\n }\n isEmpty() {\n return this.size() === 0;\n }\n size() {\n let count = 0;\n Object.values(this.table).forEach(linkedList => {\n count += linkedList.size();\n });\n return count;\n }\n clear() {\n this.table = {};\n }\n getTable() {\n return this.table;\n }\n toString() {\n if (this.isEmpty()) {\n return '';\n }\n const keys = Object.keys(this.table);\n let objString = `{${keys[0]} => ${this.table[keys[0]].toString()}}`;\n for (let i = 1; i < keys.length; i++) {\n objString = `${objString},{${keys[i]} => ${this.table[\n keys[i]\n ].toString()}}`;\n }\n return objString;\n }\n}\n","import { defaultToString } from '../util';\nimport { ValuePair } from './models/value-pair';\n\nexport default class HashTable {\n constructor(toStrFn = defaultToString) {\n this.toStrFn = toStrFn;\n this.table = {};\n }\n loseloseHashCode(key) {\n if (typeof key === 'number') {\n return key;\n }\n const tableKey = this.toStrFn(key);\n let hash = 0;\n for (let i = 0; i < tableKey.length; i++) {\n hash += tableKey.charCodeAt(i);\n }\n return hash % 37;\n }\n /* djb2HashCode(key) {\n const tableKey = this.toStrFn(key);\n let hash = 5381;\n for (let i = 0; i < tableKey.length; i++) {\n hash = (hash * 33) + tableKey.charCodeAt(i);\n }\n return hash % 1013;\n } */\n hashCode(key) {\n return this.loseloseHashCode(key);\n }\n put(key, value) {\n if (key != null && value != null) {\n const position = this.hashCode(key);\n this.table[position] = new ValuePair(key, value);\n return true;\n }\n return false;\n }\n get(key) {\n const valuePair = this.table[this.hashCode(key)];\n return valuePair == null ? undefined : valuePair.value;\n }\n remove(key) {\n const hash = this.hashCode(key);\n const valuePair = this.table[hash];\n if (valuePair != null) {\n delete this.table[hash];\n return true;\n }\n return false;\n }\n getTable() {\n return this.table;\n }\n isEmpty() {\n return this.size() === 0;\n }\n size() {\n return Object.keys(this.table).length;\n }\n clear() {\n this.table = {};\n }\n toString() {\n if (this.isEmpty()) {\n return '';\n }\n const keys = Object.keys(this.table);\n let objString = `{${keys[0]} => ${this.table[keys[0]].toString()}}`;\n for (let i = 1; i < keys.length; i++) {\n objString = `${objString},{${keys[i]} => ${this.table[keys[i]].toString()}}`;\n }\n return objString;\n }\n}\n","export default class Set {\n constructor() {\n this.items = {};\n }\n add(element) {\n if (!this.has(element)) {\n this.items[element] = element;\n return true;\n }\n return false;\n }\n delete(element) {\n if (this.has(element)) {\n delete this.items[element];\n return true;\n }\n return false;\n }\n has(element) {\n return Object.prototype.hasOwnProperty.call(this.items, element);\n }\n values() {\n return Object.values(this.items);\n }\n union(otherSet) {\n const unionSet = new Set();\n this.values().forEach(value => unionSet.add(value));\n otherSet.values().forEach(value => unionSet.add(value));\n return unionSet;\n }\n intersection(otherSet) {\n const intersectionSet = new Set();\n const values = this.values();\n const otherValues = otherSet.values();\n let biggerSet = values;\n let smallerSet = otherValues;\n if (otherValues.length - values.length > 0) {\n biggerSet = otherValues;\n smallerSet = values;\n }\n smallerSet.forEach(value => {\n if (biggerSet.includes(value)) {\n intersectionSet.add(value);\n }\n });\n return intersectionSet;\n }\n difference(otherSet) {\n const differenceSet = new Set();\n this.values().forEach(value => {\n if (!otherSet.has(value)) {\n differenceSet.add(value);\n }\n });\n return differenceSet;\n }\n isSubsetOf(otherSet) {\n if (this.size() > otherSet.size()) {\n return false;\n }\n let isSubset = true;\n this.values().every(value => {\n if (!otherSet.has(value)) {\n isSubset = false;\n return false;\n }\n return true;\n });\n return isSubset;\n }\n isEmpty() {\n return this.size() === 0;\n }\n size() {\n return Object.keys(this.items).length;\n }\n clear() {\n this.items = {};\n }\n toString() {\n if (this.isEmpty()) {\n return '';\n }\n const values = this.values();\n let objString = `${values[0]}`;\n for (let i = 1; i < values.length; i++) {\n objString = `${objString},${values[i].toString()}`;\n }\n return objString;\n }\n}\n","import DoublyLinkedList from './doubly-linked-list';\n\nexport default class StackLinkedList {\n constructor() {\n this.items = new DoublyLinkedList();\n }\n push(element) {\n this.items.push(element);\n }\n pop() {\n if (this.isEmpty()) {\n return undefined;\n }\n const result = this.items.removeAt(this.size() - 1);\n return result;\n }\n peek() {\n if (this.isEmpty()) {\n return undefined;\n }\n return this.items.getElementAt(this.size() - 1).element;\n }\n isEmpty() {\n return this.items.isEmpty();\n }\n size() {\n return this.items.size();\n }\n clear() {\n this.items.clear();\n }\n toString() {\n return this.items.toString();\n }\n}\n","import { Compare, defaultCompare, defaultEquals } from '../util';\nimport LinkedList from './linked-list';\n\nexport default class SortedLinkedList extends LinkedList {\n constructor(equalsFn = defaultEquals, compareFn = defaultCompare) {\n super(equalsFn);\n this.equalsFn = equalsFn;\n this.compareFn = compareFn;\n }\n push(element) {\n if (this.isEmpty()) {\n super.push(element);\n } else {\n const index = this.getIndexNextSortedElement(element);\n super.insert(element, index);\n }\n }\n insert(element, index = 0) {\n if (this.isEmpty()) {\n return super.insert(element, index === 0 ? index : 0);\n }\n const pos = this.getIndexNextSortedElement(element);\n return super.insert(element, pos);\n }\n getIndexNextSortedElement(element) {\n let current = this.head;\n let i = 0;\n for (; i < this.size() && current; i++) {\n const comp = this.compareFn(element, current.element);\n if (comp === Compare.LESS_THAN) {\n return i;\n }\n current = current.next;\n }\n return i;\n }\n}\n","import { defaultEquals } from '../util';\nimport LinkedList from './linked-list';\nimport { Node } from './models/linked-list-models';\n\nexport default class CircularLinkedList extends LinkedList {\n constructor(equalsFn = defaultEquals) {\n super(equalsFn);\n }\n push(element) {\n const node = new Node(element);\n let current;\n if (this.head == null) {\n this.head = node;\n } else {\n current = this.getElementAt(this.size() - 1);\n current.next = node;\n }\n // set node.next to head - to have circular list\n node.next = this.head;\n this.count++;\n }\n insert(element, index) {\n if (index >= 0 && index <= this.count) {\n const node = new Node(element);\n let current = this.head;\n if (index === 0) {\n if (this.head == null) {\n // if no node in list\n this.head = node;\n node.next = this.head;\n } else {\n node.next = current;\n current = this.getElementAt(this.size());\n // update last element\n this.head = node;\n current.next = this.head;\n }\n } else {\n const previous = this.getElementAt(index - 1);\n node.next = previous.next;\n previous.next = node;\n }\n this.count++;\n return true;\n }\n return false;\n }\n removeAt(index) {\n if (index >= 0 && index < this.count) {\n let current = this.head;\n if (index === 0) {\n if (this.size() === 1) {\n this.head = undefined;\n } else {\n const removed = this.head;\n current = this.getElementAt(this.size() - 1);\n this.head = this.head.next;\n current.next = this.head;\n current = removed;\n }\n } else {\n // no need to update last element for circular list\n const previous = this.getElementAt(index - 1);\n current = previous.next;\n previous.next = current.next;\n }\n this.count--;\n return current.element;\n }\n return undefined;\n }\n}\n","import Deque from '../data-structures/deque';\n\nexport function palindromeChecker(aString) {\n if (\n aString === undefined ||\n aString === null ||\n (aString !== null && aString.length === 0)\n ) {\n return false;\n }\n const deque = new Deque();\n const lowerString = aString.toLocaleLowerCase().split(' ').join('');\n let firstChar;\n let lastChar;\n\n for (let i = 0; i < lowerString.length; i++) {\n deque.addBack(lowerString.charAt(i));\n }\n\n while (deque.size() > 1) {\n firstChar = deque.removeFront();\n lastChar = deque.removeBack();\n if (firstChar !== lastChar) {\n return false;\n }\n }\n\n return true;\n}\n","import Queue from '../data-structures/queue';\n\nexport function hotPotato(elementsList, num) {\n const queue = new Queue();\n const elimitatedList = [];\n\n for (let i = 0; i < elementsList.length; i++) {\n queue.enqueue(elementsList[i]);\n }\n\n while (queue.size() > 1) {\n for (let i = 0; i < num; i++) {\n queue.enqueue(queue.dequeue());\n }\n elimitatedList.push(queue.dequeue());\n }\n\n return {\n eliminated: elimitatedList,\n winner: queue.dequeue()\n };\n}\n","// @ts-check\nimport Stack from '../data-structures/stack';\n\nexport function parenthesesChecker(symbols) {\n const stack = new Stack();\n const opens = '([{';\n const closers = ')]}';\n let balanced = true;\n let index = 0;\n let symbol;\n let top;\n\n while (index < symbols.length && balanced) {\n symbol = symbols[index];\n if (opens.indexOf(symbol) >= 0) {\n stack.push(symbol);\n } else if (stack.isEmpty()) {\n balanced = false;\n } else {\n top = stack.pop();\n if (!(opens.indexOf(top) === closers.indexOf(symbol))) {\n balanced = false;\n }\n }\n index++;\n }\n return balanced && stack.isEmpty();\n}\n","// @ts-check\nimport Stack from '../data-structures/stack';\n\nexport function decimalToBinary(decNumber) {\n const remStack = new Stack();\n let number = decNumber;\n let rem;\n let binaryString = '';\n\n while (number > 0) {\n rem = Math.floor(number % 2);\n remStack.push(rem);\n number = Math.floor(number / 2);\n }\n\n while (!remStack.isEmpty()) {\n binaryString += remStack.pop().toString();\n }\n\n return binaryString;\n}\n\nexport function baseConverter(decNumber, base) {\n const remStack = new Stack();\n const digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';\n let number = decNumber;\n let rem;\n let baseString = '';\n\n if (!(base >= 2 && base <= 36)) {\n return '';\n }\n\n while (number > 0) {\n rem = Math.floor(number % base);\n remStack.push(rem);\n number = Math.floor(number / base);\n }\n\n while (!remStack.isEmpty()) {\n baseString += digits[remStack.pop()];\n }\n\n return baseString;\n}\n","// @ts-check\nimport Stack from '../data-structures/stack';\n\nfunction towerOfHanoi(plates, source, helper, dest, sourceName, helperName, destName, moves = []) {\n if (plates <= 0) {\n return moves;\n }\n if (plates === 1) {\n dest.push(source.pop());\n const move = {};\n move[sourceName] = source.toString();\n move[helperName] = helper.toString();\n move[destName] = dest.toString();\n moves.push(move);\n } else {\n towerOfHanoi(plates - 1, source, dest, helper, sourceName, destName, helperName, moves);\n dest.push(source.pop());\n const move = {};\n move[sourceName] = source.toString();\n move[helperName] = helper.toString();\n move[destName] = dest.toString();\n moves.push(move);\n towerOfHanoi(plates - 1, helper, source, dest, helperName, sourceName, destName, moves);\n }\n return moves;\n}\n\nexport function hanoiStack(plates) {\n const source = new Stack();\n const dest = new Stack();\n const helper = new Stack();\n\n for (let i = plates; i > 0; i--) {\n source.push(i);\n }\n\n return towerOfHanoi(plates, source, helper, dest, 'source', 'helper', 'dest');\n}\n\nexport function hanoi(plates, source, helper, dest, moves = []) {\n if (plates <= 0) {\n return moves;\n }\n if (plates === 1) {\n moves.push([source, dest]);\n } else {\n hanoi(plates - 1, source, dest, helper, moves);\n moves.push([source, dest]);\n hanoi(plates - 1, helper, source, dest, moves);\n }\n return moves;\n}\n","// @ts-check\n\nexport default class StackArray {\n constructor() {\n this.items = [];\n }\n push(element) {\n this.items.push(element);\n }\n\n pop() {\n return this.items.pop();\n }\n\n peek() {\n return this.items[this.items.length - 1];\n }\n\n isEmpty() {\n return this.items.length === 0;\n }\n\n size() {\n return this.items.length;\n }\n\n clear() {\n this.items = [];\n }\n\n toArray() {\n return this.items;\n }\n\n toString() {\n return this.items.toString();\n }\n}\n","import * as _util from './util';\n\n// chapters 05 and 07\nexport const util = _util;\n\n// chapter 03\nexport { default as StackArray } from './data-structures/stack-array';\nexport { default as Stack } from './data-structures/stack';\nexport { hanoi } from './others/hanoi';\nexport { hanoiStack } from './others/hanoi';\nexport { baseConverter } from './others/base-converter';\nexport { decimalToBinary } from './others/base-converter';\nexport { parenthesesChecker } from './others/balanced-symbols';\n\n// chapter 04\nexport { default as Queue } from './data-structures/queue';\nexport { default as Deque } from './data-structures/deque';\nexport { hotPotato } from './others/hot-potato';\nexport { palindromeChecker } from './others/palindrome-checker';\n\n// chapter 05\nexport { default as LinkedList } from './data-structures/linked-list';\nexport { default as DoublyLinkedList } from './data-structures/doubly-linked-list';\nexport { default as CircularLinkedList } from './data-structures/circular-linked-list';\nexport { default as SortedLinkedList } from './data-structures/sorted-linked-list';\nexport { default as StackLinkedList } from './data-structures/stack-linked-list';\n\n// chapter 06\nexport { default as Set } from './data-structures/set';\n\n// chapter 07\nexport { default as Dictionary } from './data-structures/dictionary';\nexport { default as HashTable } from './data-structures/hash-table';\nexport { default as HashTableSeparateChaining } from './data-structures/hash-table-separate-chaining';\nexport { default as HashTableLinearProbing } from './data-structures/hash-table-linear-probing';\nexport { default as HashTableLinearProbingLazy } from './data-structures/hash-table-linear-probing-lazy';\n\n// chapter 08\nexport { default as factorialIterative } from './others/factorial';\nexport { default as factorial } from './others/factorial';\nexport { default as fibonacci } from './others/fibonacci';\nexport { default as fibonacciIterative } from './others/fibonacci';\nexport { default as fibonacciMemoization } from './others/fibonacci';\n\n// chapter 09\nexport { default as BinarySearchTree } from './data-structures/binary-search-tree';\nexport { default as AVLTree } from './data-structures/avl-tree';\n\n// chapter 10\nexport { MinHeap } from './data-structures/heap';\nexport { MaxHeap } from './data-structures/heap';\nexport { default as heapSort } from './algorithms/sorting/heap-sort';\n\n// chapter 11\nexport { default as Graph } from './data-structures/graph';\nexport { breadthFirstSearch } from './algorithms/graph/breadth-first-search';\nexport { BFS } from './algorithms/graph/breadth-first-search';\nexport { depthFirstSearch } from './algorithms/graph/depth-first-search';\nexport { DFS } from './algorithms/graph/depth-first-search';\nexport { dijkstra } from './algorithms/graph/dijkstra';\nexport { floydWarshall } from './algorithms/graph/floyd-warshall';\nexport { prim } from './algorithms/graph/prim';\nexport { kruskal } from './algorithms/graph/kruskal';\n\n// chapter 12\nexport { shuffle } from './algorithms/shuffle/fisher–yates';\n\nexport { bubbleSort } from './algorithms/sorting/bubble-sort';\nexport { modifiedBubbleSort } from './algorithms/sorting/bubble-sort-improved';\nexport { bucketSort } from './algorithms/sorting/bucket-sort';\nexport { countingSort } from './algorithms/sorting/counting-sort';\nexport { insertionSort } from './algorithms/sorting/insertion-sort';\nexport { mergeSort } from './algorithms/sorting/merge-sort';\nexport { quickSort } from './algorithms/sorting/quicksort';\nexport { radixSort } from './algorithms/sorting/radix-sort';\nexport { selectionSort } from './algorithms/sorting/selection-sort';\nexport { shellSort } from './algorithms/sorting/shell-sort';\n\nexport { binarySearch } from './algorithms/search/binary-search';\nexport { interpolationSearch } from './algorithms/search/interpolation-search';\nexport { sequentialSearch } from './algorithms/search/sequential-search';\nexport { findMaxValue } from './algorithms/search/min-max-search';\nexport { findMinValue } from './algorithms/search/min-max-search';\n\n// chapter 14\nexport { binarySearch as binarySearchRecursive } from './algorithms/search/binary-search-recursive';\nexport { minCoinChange } from './algorithms/dynamic-programing/min-coin-change';\nexport { minCoinChange as minCoinChangeGreedy } from './algorithms/greedy/min-coin-change';\nexport { knapSack } from './algorithms/dynamic-programing/knapsack';\nexport { knapSack as knapSackRecursive } from './algorithms/dynamic-programing/knapsack-recursive';\nexport { knapSack as knapSackGreedy } from './algorithms/greedy/knapsack';\nexport { lcs } from './algorithms/dynamic-programing/longest-common-subsequence';\nexport { lcs as lcsPrint } from './algorithms/dynamic-programing/longest-common-subsequence-print';\nexport { lcs as lcsRecursive } from './algorithms/greedy/longest-common-subsequence';\nexport { matrixChainOrder } from './algorithms/dynamic-programing/matrix-chain-multiplication';\nexport { matrixChainOrder as matrixChainOrderGreedy } from './algorithms/greedy/matrix-chain-multiplication';\nexport { ratInAMaze } from './algorithms/backtracking/rat-in-maze';\nexport { sudokuSolver } from './algorithms/backtracking/sudoku-solver';\n"],"sourceRoot":""}
\ No newline at end of file
diff --git a/examples/chapter01/01-HelloWorld.html b/examples/chapter01_02/01-HelloWorld.html
old mode 100644
new mode 100755
similarity index 100%
rename from examples/chapter01/01-HelloWorld.html
rename to examples/chapter01_02/01-HelloWorld.html
diff --git a/examples/chapter01/01-HelloWorld.js b/examples/chapter01_02/01-HelloWorld.js
similarity index 100%
rename from examples/chapter01/01-HelloWorld.js
rename to examples/chapter01_02/01-HelloWorld.js
diff --git a/examples/chapter01/02-Variables.html b/examples/chapter01_02/02-Variables.html
old mode 100644
new mode 100755
similarity index 100%
rename from examples/chapter01/02-Variables.html
rename to examples/chapter01_02/02-Variables.html
diff --git a/examples/chapter01/02-Variables.js b/examples/chapter01_02/02-Variables.js
similarity index 100%
rename from examples/chapter01/02-Variables.js
rename to examples/chapter01_02/02-Variables.js
diff --git a/examples/chapter01/03-Operators.html b/examples/chapter01_02/03-Operators.html
old mode 100644
new mode 100755
similarity index 100%
rename from examples/chapter01/03-Operators.html
rename to examples/chapter01_02/03-Operators.html
diff --git a/examples/chapter01/03-Operators.js b/examples/chapter01_02/03-Operators.js
old mode 100644
new mode 100755
similarity index 100%
rename from examples/chapter01/03-Operators.js
rename to examples/chapter01_02/03-Operators.js
diff --git a/examples/chapter01/04-TruthyFalsy.html b/examples/chapter01_02/04-TruthyFalsy.html
old mode 100644
new mode 100755
similarity index 100%
rename from examples/chapter01/04-TruthyFalsy.html
rename to examples/chapter01_02/04-TruthyFalsy.html
diff --git a/examples/chapter01/04-TruthyFalsy.js b/examples/chapter01_02/04-TruthyFalsy.js
similarity index 88%
rename from examples/chapter01/04-TruthyFalsy.js
rename to examples/chapter01_02/04-TruthyFalsy.js
index 96264875..0090ee74 100644
--- a/examples/chapter01/04-TruthyFalsy.js
+++ b/examples/chapter01_02/04-TruthyFalsy.js
@@ -10,6 +10,7 @@ testTruthy(false); // false
testTruthy(new Boolean(false)); // true (object is always true)
testTruthy(''); // false
+testTruthy('a'); // true
testTruthy('Packt'); // true
testTruthy(new String('')); // true (object is always true)
@@ -23,4 +24,4 @@ testTruthy({}); // true (object is always true)
var obj = { name: 'John' };
testTruthy(obj); // true
testTruthy(obj.name); // true
-testTruthy(obj.age); // age (property does not exist)
+testTruthy(obj.age); // false (property age does not exist)
diff --git a/examples/chapter01/05-EqualsOperators.html b/examples/chapter01_02/05-EqualsOperators.html
old mode 100644
new mode 100755
similarity index 100%
rename from examples/chapter01/05-EqualsOperators.html
rename to examples/chapter01_02/05-EqualsOperators.html
diff --git a/examples/chapter01/05-EqualsOperators.js b/examples/chapter01_02/05-EqualsOperators.js
similarity index 100%
rename from examples/chapter01/05-EqualsOperators.js
rename to examples/chapter01_02/05-EqualsOperators.js
diff --git a/examples/chapter01/06-ConditionalStatements.html b/examples/chapter01_02/06-ConditionalStatements.html
old mode 100644
new mode 100755
similarity index 100%
rename from examples/chapter01/06-ConditionalStatements.html
rename to examples/chapter01_02/06-ConditionalStatements.html
diff --git a/examples/chapter01/06-ConditionalStatements.js b/examples/chapter01_02/06-ConditionalStatements.js
similarity index 100%
rename from examples/chapter01/06-ConditionalStatements.js
rename to examples/chapter01_02/06-ConditionalStatements.js
diff --git a/examples/chapter01/07-Loops.html b/examples/chapter01_02/07-Loops.html
old mode 100644
new mode 100755
similarity index 100%
rename from examples/chapter01/07-Loops.html
rename to examples/chapter01_02/07-Loops.html
diff --git a/examples/chapter01/07-Loops.js b/examples/chapter01_02/07-Loops.js
old mode 100644
new mode 100755
similarity index 100%
rename from examples/chapter01/07-Loops.js
rename to examples/chapter01_02/07-Loops.js
diff --git a/examples/chapter01/08-Functions.html b/examples/chapter01_02/08-Functions.html
old mode 100644
new mode 100755
similarity index 100%
rename from examples/chapter01/08-Functions.html
rename to examples/chapter01_02/08-Functions.html
diff --git a/examples/chapter01/08-Functions.js b/examples/chapter01_02/08-Functions.js
similarity index 100%
rename from examples/chapter01/08-Functions.js
rename to examples/chapter01_02/08-Functions.js
diff --git a/examples/chapter01/09-ObjectOrientedJS.html b/examples/chapter01_02/09-ObjectOrientedJS.html
similarity index 100%
rename from examples/chapter01/09-ObjectOrientedJS.html
rename to examples/chapter01_02/09-ObjectOrientedJS.html
diff --git a/examples/chapter01/09-ObjectOrientedJS.js b/examples/chapter01_02/09-ObjectOrientedJS.js
similarity index 100%
rename from examples/chapter01/09-ObjectOrientedJS.js
rename to examples/chapter01_02/09-ObjectOrientedJS.js
diff --git a/examples/chapter01/10-ES2015-ES6-letconst.html b/examples/chapter01_02/10-ES2015-ES6-letconst.html
similarity index 100%
rename from examples/chapter01/10-ES2015-ES6-letconst.html
rename to examples/chapter01_02/10-ES2015-ES6-letconst.html
diff --git a/examples/chapter01/10-ES2015-ES6-letconst.js b/examples/chapter01_02/10-ES2015-ES6-letconst.js
similarity index 100%
rename from examples/chapter01/10-ES2015-ES6-letconst.js
rename to examples/chapter01_02/10-ES2015-ES6-letconst.js
diff --git a/examples/chapter01/11-ES2015-ES6-variableScope.html b/examples/chapter01_02/11-ES2015-ES6-variableScope.html
similarity index 100%
rename from examples/chapter01/11-ES2015-ES6-variableScope.html
rename to examples/chapter01_02/11-ES2015-ES6-variableScope.html
diff --git a/examples/chapter01/11-ES2015-ES6-variableScope.html.js b/examples/chapter01_02/11-ES2015-ES6-variableScope.html.js
similarity index 100%
rename from examples/chapter01/11-ES2015-ES6-variableScope.html.js
rename to examples/chapter01_02/11-ES2015-ES6-variableScope.html.js
diff --git a/examples/chapter01/12-ES2015-ES6-StringTemplates.html b/examples/chapter01_02/12-ES2015-ES6-StringTemplates.html
similarity index 100%
rename from examples/chapter01/12-ES2015-ES6-StringTemplates.html
rename to examples/chapter01_02/12-ES2015-ES6-StringTemplates.html
diff --git a/examples/chapter01/12-ES2015-ES6-StringTemplates.js b/examples/chapter01_02/12-ES2015-ES6-StringTemplates.js
similarity index 100%
rename from examples/chapter01/12-ES2015-ES6-StringTemplates.js
rename to examples/chapter01_02/12-ES2015-ES6-StringTemplates.js
diff --git a/examples/chapter01/13-ES2015-ES6-ArrowFunctions.html b/examples/chapter01_02/13-ES2015-ES6-ArrowFunctions.html
similarity index 100%
rename from examples/chapter01/13-ES2015-ES6-ArrowFunctions.html
rename to examples/chapter01_02/13-ES2015-ES6-ArrowFunctions.html
diff --git a/examples/chapter01/13-ES2015-ES6-ArrowFunctions.js b/examples/chapter01_02/13-ES2015-ES6-ArrowFunctions.js
similarity index 100%
rename from examples/chapter01/13-ES2015-ES6-ArrowFunctions.js
rename to examples/chapter01_02/13-ES2015-ES6-ArrowFunctions.js
diff --git a/examples/chapter01/14-ES2015-ES6-ParameterHandling.html b/examples/chapter01_02/14-ES2015-ES6-ParameterHandling.html
similarity index 100%
rename from examples/chapter01/14-ES2015-ES6-ParameterHandling.html
rename to examples/chapter01_02/14-ES2015-ES6-ParameterHandling.html
diff --git a/examples/chapter01/14-ES2015-ES6-ParameterHandling.js b/examples/chapter01_02/14-ES2015-ES6-ParameterHandling.js
similarity index 100%
rename from examples/chapter01/14-ES2015-ES6-ParameterHandling.js
rename to examples/chapter01_02/14-ES2015-ES6-ParameterHandling.js
diff --git a/examples/chapter01/15-ES2015-ES6-EnhancedObjectProperties.html b/examples/chapter01_02/15-ES2015-ES6-EnhancedObjectProperties.html
similarity index 100%
rename from examples/chapter01/15-ES2015-ES6-EnhancedObjectProperties.html
rename to examples/chapter01_02/15-ES2015-ES6-EnhancedObjectProperties.html
diff --git a/examples/chapter01/15-ES2015-ES6-EnhancedObjectProperties.js b/examples/chapter01_02/15-ES2015-ES6-EnhancedObjectProperties.js
similarity index 100%
rename from examples/chapter01/15-ES2015-ES6-EnhancedObjectProperties.js
rename to examples/chapter01_02/15-ES2015-ES6-EnhancedObjectProperties.js
diff --git a/examples/chapter01/16-ES2015-ES6-Classes.html b/examples/chapter01_02/16-ES2015-ES6-Classes.html
similarity index 100%
rename from examples/chapter01/16-ES2015-ES6-Classes.html
rename to examples/chapter01_02/16-ES2015-ES6-Classes.html
diff --git a/examples/chapter01/16-ES2015-ES6-Classes.js b/examples/chapter01_02/16-ES2015-ES6-Classes.js
similarity index 100%
rename from examples/chapter01/16-ES2015-ES6-Classes.js
rename to examples/chapter01_02/16-ES2015-ES6-Classes.js
diff --git a/examples/chapter01/17-Book.js b/examples/chapter01_02/17-Book.js
similarity index 100%
rename from examples/chapter01/17-Book.js
rename to examples/chapter01_02/17-Book.js
diff --git a/examples/chapter01/17-CalcArea.js b/examples/chapter01_02/17-CalcArea.js
similarity index 100%
rename from examples/chapter01/17-CalcArea.js
rename to examples/chapter01_02/17-CalcArea.js
diff --git a/examples/chapter01/17-ES2015-ES6-Modules-node.js b/examples/chapter01_02/17-ES2015-ES6-Modules-node.js
similarity index 100%
rename from examples/chapter01/17-ES2015-ES6-Modules-node.js
rename to examples/chapter01_02/17-ES2015-ES6-Modules-node.js
diff --git a/examples/chapter01/17-ES2015-ES6-Modules.html b/examples/chapter01_02/17-ES2015-ES6-Modules.html
similarity index 100%
rename from examples/chapter01/17-ES2015-ES6-Modules.html
rename to examples/chapter01_02/17-ES2015-ES6-Modules.html
diff --git a/examples/chapter01/17-ES2015-ES6-Modules.js b/examples/chapter01_02/17-ES2015-ES6-Modules.js
similarity index 100%
rename from examples/chapter01/17-ES2015-ES6-Modules.js
rename to examples/chapter01_02/17-ES2015-ES6-Modules.js
diff --git a/examples/chapter01/17-ES2015-Modules-node/17-Book.mjs b/examples/chapter01_02/17-ES2015-Modules-node/17-Book.mjs
similarity index 100%
rename from examples/chapter01/17-ES2015-Modules-node/17-Book.mjs
rename to examples/chapter01_02/17-ES2015-Modules-node/17-Book.mjs
diff --git a/examples/chapter01/17-ES2015-Modules-node/17-CalcArea.mjs b/examples/chapter01_02/17-ES2015-Modules-node/17-CalcArea.mjs
similarity index 100%
rename from examples/chapter01/17-ES2015-Modules-node/17-CalcArea.mjs
rename to examples/chapter01_02/17-ES2015-Modules-node/17-CalcArea.mjs
diff --git a/examples/chapter01/17-ES2015-Modules-node/17-ES2015-ES6-Modules.mjs b/examples/chapter01_02/17-ES2015-Modules-node/17-ES2015-ES6-Modules.mjs
similarity index 100%
rename from examples/chapter01/17-ES2015-Modules-node/17-ES2015-ES6-Modules.mjs
rename to examples/chapter01_02/17-ES2015-Modules-node/17-ES2015-ES6-Modules.mjs
diff --git a/examples/chapter01/18-ES2016-ES7-ExponentiationOperator.html b/examples/chapter01_02/18-ES2016-ES7-ExponentiationOperator.html
similarity index 100%
rename from examples/chapter01/18-ES2016-ES7-ExponentiationOperator.html
rename to examples/chapter01_02/18-ES2016-ES7-ExponentiationOperator.html
diff --git a/examples/chapter01/18-ES2016-ES7-ExponentiationOperator.js b/examples/chapter01_02/18-ES2016-ES7-ExponentiationOperator.js
similarity index 100%
rename from examples/chapter01/18-ES2016-ES7-ExponentiationOperator.js
rename to examples/chapter01_02/18-ES2016-ES7-ExponentiationOperator.js
diff --git a/examples/chapter01/lib/17-Book.js b/examples/chapter01_02/lib/17-Book.js
similarity index 100%
rename from examples/chapter01/lib/17-Book.js
rename to examples/chapter01_02/lib/17-Book.js
diff --git a/examples/chapter01/lib/17-CalcArea.js b/examples/chapter01_02/lib/17-CalcArea.js
similarity index 100%
rename from examples/chapter01/lib/17-CalcArea.js
rename to examples/chapter01_02/lib/17-CalcArea.js
diff --git a/examples/chapter01/lib/17-ES2015-ES6-Modules-bundle.js b/examples/chapter01_02/lib/17-ES2015-ES6-Modules-bundle.js
similarity index 100%
rename from examples/chapter01/lib/17-ES2015-ES6-Modules-bundle.js
rename to examples/chapter01_02/lib/17-ES2015-ES6-Modules-bundle.js
diff --git a/examples/chapter01/lib/17-ES2015-ES6-Modules.js b/examples/chapter01_02/lib/17-ES2015-ES6-Modules.js
similarity index 100%
rename from examples/chapter01/lib/17-ES2015-ES6-Modules.js
rename to examples/chapter01_02/lib/17-ES2015-ES6-Modules.js
diff --git a/examples/chapter01/typescript/hello-world.js b/examples/chapter01_02/typescript/hello-world.js
similarity index 100%
rename from examples/chapter01/typescript/hello-world.js
rename to examples/chapter01_02/typescript/hello-world.js
diff --git a/examples/chapter01/typescript/hello-world.ts b/examples/chapter01_02/typescript/hello-world.ts
similarity index 100%
rename from examples/chapter01/typescript/hello-world.ts
rename to examples/chapter01_02/typescript/hello-world.ts
diff --git a/examples/chapter02/01-Introduction.html b/examples/chapter03/01-Introduction.html
similarity index 100%
rename from examples/chapter02/01-Introduction.html
rename to examples/chapter03/01-Introduction.html
diff --git a/examples/chapter02/01-Introduction.js b/examples/chapter03/01-Introduction.js
similarity index 100%
rename from examples/chapter02/01-Introduction.js
rename to examples/chapter03/01-Introduction.js
diff --git a/examples/chapter03/01-Stack.html b/examples/chapter03/01-Stack.html
deleted file mode 100755
index c322ded8..00000000
--- a/examples/chapter03/01-Stack.html
+++ /dev/null
@@ -1,12 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/examples/chapter02/02-CreatingAndInitialingArrays.html b/examples/chapter03/02-CreatingAndInitialingArrays.html
similarity index 100%
rename from examples/chapter02/02-CreatingAndInitialingArrays.html
rename to examples/chapter03/02-CreatingAndInitialingArrays.html
diff --git a/examples/chapter02/02-CreatingAndInitialingArrays.js b/examples/chapter03/02-CreatingAndInitialingArrays.js
similarity index 100%
rename from examples/chapter02/02-CreatingAndInitialingArrays.js
rename to examples/chapter03/02-CreatingAndInitialingArrays.js
diff --git a/examples/chapter02/03-AddingRemovingElements.html b/examples/chapter03/03-AddingRemovingElements.html
similarity index 100%
rename from examples/chapter02/03-AddingRemovingElements.html
rename to examples/chapter03/03-AddingRemovingElements.html
diff --git a/examples/chapter02/03-AddingRemovingElements.js b/examples/chapter03/03-AddingRemovingElements.js
similarity index 100%
rename from examples/chapter02/03-AddingRemovingElements.js
rename to examples/chapter03/03-AddingRemovingElements.js
diff --git a/examples/chapter03/04-TowerOfHanoi.js b/examples/chapter03/04-TowerOfHanoi.js
deleted file mode 100755
index e611aef6..00000000
--- a/examples/chapter03/04-TowerOfHanoi.js
+++ /dev/null
@@ -1,6 +0,0 @@
-const hanoiStack = PacktDataStructuresAlgorithms.hanoiStack;
-const hanoi = PacktDataStructuresAlgorithms.hanoi;
-
-console.log(hanoiStack(3));
-
-console.log(hanoi(3, 'source', 'helper', 'dest'));
diff --git a/examples/chapter02/04-TwoDimensionalMultiDimensional.html b/examples/chapter03/04-TwoDimensionalMultiDimensional.html
similarity index 100%
rename from examples/chapter02/04-TwoDimensionalMultiDimensional.html
rename to examples/chapter03/04-TwoDimensionalMultiDimensional.html
diff --git a/examples/chapter02/04-TwoDimensionalMultiDimensional.js b/examples/chapter03/04-TwoDimensionalMultiDimensional.js
similarity index 100%
rename from examples/chapter02/04-TwoDimensionalMultiDimensional.js
rename to examples/chapter03/04-TwoDimensionalMultiDimensional.js
diff --git a/examples/chapter02/05-ArrayMethods.html b/examples/chapter03/05-ArrayMethods.html
similarity index 100%
rename from examples/chapter02/05-ArrayMethods.html
rename to examples/chapter03/05-ArrayMethods.html
diff --git a/examples/chapter02/05-ArrayMethods.js b/examples/chapter03/05-ArrayMethods.js
similarity index 100%
rename from examples/chapter02/05-ArrayMethods.js
rename to examples/chapter03/05-ArrayMethods.js
diff --git a/examples/chapter02/06-ES2015Methods.html b/examples/chapter03/06-ES2015Methods.html
similarity index 100%
rename from examples/chapter02/06-ES2015Methods.html
rename to examples/chapter03/06-ES2015Methods.html
diff --git a/examples/chapter02/06-ES2015Methods.js b/examples/chapter03/06-ES2015Methods.js
similarity index 100%
rename from examples/chapter02/06-ES2015Methods.js
rename to examples/chapter03/06-ES2015Methods.js
diff --git a/examples/chapter02/07-Sorting.html b/examples/chapter03/07-Sorting.html
similarity index 100%
rename from examples/chapter02/07-Sorting.html
rename to examples/chapter03/07-Sorting.html
diff --git a/examples/chapter02/07-Sorting.js b/examples/chapter03/07-Sorting.js
similarity index 100%
rename from examples/chapter02/07-Sorting.js
rename to examples/chapter03/07-Sorting.js
diff --git a/examples/chapter02/08-Searching.html b/examples/chapter03/08-Searching.html
similarity index 100%
rename from examples/chapter02/08-Searching.html
rename to examples/chapter03/08-Searching.html
diff --git a/examples/chapter02/08-Searching.js b/examples/chapter03/08-Searching.js
similarity index 100%
rename from examples/chapter02/08-Searching.js
rename to examples/chapter03/08-Searching.js
diff --git a/examples/chapter02/09-TypedArrays.html b/examples/chapter03/09-TypedArrays.html
similarity index 100%
rename from examples/chapter02/09-TypedArrays.html
rename to examples/chapter03/09-TypedArrays.html
diff --git a/examples/chapter02/09-TypedArrays.js b/examples/chapter03/09-TypedArrays.js
similarity index 100%
rename from examples/chapter02/09-TypedArrays.js
rename to examples/chapter03/09-TypedArrays.js
diff --git a/examples/chapter02/10-ArraysAndTypeScript.ts b/examples/chapter03/10-ArraysAndTypeScript.ts
similarity index 100%
rename from examples/chapter02/10-ArraysAndTypeScript.ts
rename to examples/chapter03/10-ArraysAndTypeScript.ts
diff --git a/examples/chapter04/01-Stack.html b/examples/chapter04/01-Stack.html
new file mode 100755
index 00000000..3025a012
--- /dev/null
+++ b/examples/chapter04/01-Stack.html
@@ -0,0 +1,13 @@
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter03/01-Stack.js b/examples/chapter04/01-Stack.js
similarity index 88%
rename from examples/chapter03/01-Stack.js
rename to examples/chapter04/01-Stack.js
index 972793b2..d3994b08 100755
--- a/examples/chapter03/01-Stack.js
+++ b/examples/chapter04/01-Stack.js
@@ -1,7 +1,8 @@
-import Stack from './../../src/js/data-structures/stack.js'; // ES2015 modules
+// import Stack from './../../src/js/data-structures/stack.js'; // ES2015 modules
// import StackArray from './../../src/js/data-structures/stack-array.js'; // ES2015 modules
// const Stack = require('../../dist/js/data-structures/stack'); // for node
// const Stack = stack; // older browsers - remove from html script import: type="module"
+const { Stack } = PacktDataStructuresAlgorithms;
const stack = new Stack(); // new StackArray();
diff --git a/examples/chapter03/01-StackSymbol.js b/examples/chapter04/01-StackSymbol.js
similarity index 100%
rename from examples/chapter03/01-StackSymbol.js
rename to examples/chapter04/01-StackSymbol.js
diff --git a/examples/chapter03/01-StackWeakMap.js b/examples/chapter04/01-StackWeakMap.js
similarity index 100%
rename from examples/chapter03/01-StackWeakMap.js
rename to examples/chapter04/01-StackWeakMap.js
diff --git a/examples/chapter03/02-BalancedSymbols.html b/examples/chapter04/02-BalancedSymbols.html
similarity index 100%
rename from examples/chapter03/02-BalancedSymbols.html
rename to examples/chapter04/02-BalancedSymbols.html
diff --git a/examples/chapter03/02-BalancedSymbols.js b/examples/chapter04/02-BalancedSymbols.js
similarity index 71%
rename from examples/chapter03/02-BalancedSymbols.js
rename to examples/chapter04/02-BalancedSymbols.js
index 5084f479..614f44e7 100755
--- a/examples/chapter03/02-BalancedSymbols.js
+++ b/examples/chapter04/02-BalancedSymbols.js
@@ -1,4 +1,4 @@
-const parenthesesChecker = PacktDataStructuresAlgorithms.parenthesesChecker;
+const { parenthesesChecker } = PacktDataStructuresAlgorithms;
console.log('{([])}', parenthesesChecker('{([])}')); // true
console.log('{{([][])}()}', parenthesesChecker('{{([][])}()}')); // true
diff --git a/examples/chapter03/03-DecimalToBinary.html b/examples/chapter04/03-DecimalToBinary.html
similarity index 100%
rename from examples/chapter03/03-DecimalToBinary.html
rename to examples/chapter04/03-DecimalToBinary.html
diff --git a/examples/chapter03/03-DecimalToBinary.js b/examples/chapter04/03-DecimalToBinary.js
similarity index 73%
rename from examples/chapter03/03-DecimalToBinary.js
rename to examples/chapter04/03-DecimalToBinary.js
index eebdf916..f39fcb1d 100755
--- a/examples/chapter03/03-DecimalToBinary.js
+++ b/examples/chapter04/03-DecimalToBinary.js
@@ -1,5 +1,5 @@
-const decimalToBinary = PacktDataStructuresAlgorithms.parenthesesChecker;
-const baseConverter = PacktDataStructuresAlgorithms.baseConverter;
+const { decimalToBinary } = PacktDataStructuresAlgorithms;
+const { baseConverter } = PacktDataStructuresAlgorithms;
// 233 == 11101001
// 2x(10x10) + 3x(10) + 3x(1)
diff --git a/examples/chapter03/04-TowerOfHanoi.html b/examples/chapter04/04-TowerOfHanoi.html
similarity index 100%
rename from examples/chapter03/04-TowerOfHanoi.html
rename to examples/chapter04/04-TowerOfHanoi.html
diff --git a/examples/chapter04/04-TowerOfHanoi.js b/examples/chapter04/04-TowerOfHanoi.js
new file mode 100755
index 00000000..f5d85cf7
--- /dev/null
+++ b/examples/chapter04/04-TowerOfHanoi.js
@@ -0,0 +1,6 @@
+const { hanoiStack } = PacktDataStructuresAlgorithms;
+const { hanoi } = PacktDataStructuresAlgorithms;
+
+console.log(hanoiStack(3));
+
+console.log(hanoi(3, 'source', 'helper', 'dest'));
diff --git a/examples/chapter05/01-Queue.html b/examples/chapter05/01-Queue.html
new file mode 100755
index 00000000..2e67f7f4
--- /dev/null
+++ b/examples/chapter05/01-Queue.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter05/01-Queue.js b/examples/chapter05/01-Queue.js
new file mode 100755
index 00000000..852a1b4d
--- /dev/null
+++ b/examples/chapter05/01-Queue.js
@@ -0,0 +1,14 @@
+const { Queue } = PacktDataStructuresAlgorithms;
+
+const queue = new Queue();
+console.log(queue.isEmpty()); // outputs true
+queue.enqueue('John');
+queue.enqueue('Jack');
+console.log(queue.toString()); // John,Jack
+queue.enqueue('Camila');
+console.log(queue.toString()); // John,Jack,Camila
+console.log(queue.size()); // outputs 3
+console.log(queue.isEmpty()); // outputs false
+queue.dequeue(); // remove John
+queue.dequeue(); // remove Jack
+console.log(queue.toString()); // Camila
diff --git a/examples/chapter05/02-Deque.html b/examples/chapter05/02-Deque.html
new file mode 100755
index 00000000..99b838af
--- /dev/null
+++ b/examples/chapter05/02-Deque.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter05/02-Deque.js b/examples/chapter05/02-Deque.js
new file mode 100755
index 00000000..4c84b6de
--- /dev/null
+++ b/examples/chapter05/02-Deque.js
@@ -0,0 +1,17 @@
+const { Deque } = PacktDataStructuresAlgorithms;
+
+const deque = new Deque();
+console.log(deque.isEmpty()); // outputs true
+deque.addBack('John');
+deque.addBack('Jack');
+console.log(deque.toString()); // John,Jack
+deque.addBack('Camila');
+console.log(deque.toString()); // John,Jack,Camila
+console.log(deque.size()); // outputs 3
+console.log(deque.isEmpty()); // outputs false
+deque.removeFront(); // remove John
+console.log(deque.toString()); // Jack,Camila
+deque.removeBack(); // Camila decides to leave
+console.log(deque.toString()); // Jack
+deque.addFront('John'); // John comes back for information
+console.log(deque.toString()); // John,Jack
diff --git a/examples/chapter05/03-HotPotato.html b/examples/chapter05/03-HotPotato.html
new file mode 100755
index 00000000..85d45104
--- /dev/null
+++ b/examples/chapter05/03-HotPotato.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter05/03-HotPotato.js b/examples/chapter05/03-HotPotato.js
new file mode 100755
index 00000000..686f6791
--- /dev/null
+++ b/examples/chapter05/03-HotPotato.js
@@ -0,0 +1,16 @@
+const { hotPotato } = PacktDataStructuresAlgorithms;
+
+const names = ['John', 'Jack', 'Camila', 'Ingrid', 'Carl'];
+const result = hotPotato(names, 7);
+
+result.eliminated.forEach(name => {
+ console.log(`${name} was eliminated from the Hot Potato game.`);
+});
+
+console.log(`The winner is: ${result.winner}`);
+
+// Camila was eliminated from the Hot Potato game.
+// Jack was eliminated from the Hot Potato game.
+// Carl was eliminated from the Hot Potato game.
+// Ingrid was eliminated from the Hot Potato game.
+// The winner is: John
diff --git a/examples/chapter05/04-PalindromeChecker.html b/examples/chapter05/04-PalindromeChecker.html
new file mode 100755
index 00000000..785e0482
--- /dev/null
+++ b/examples/chapter05/04-PalindromeChecker.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter05/04-PalindromeChecker.js b/examples/chapter05/04-PalindromeChecker.js
new file mode 100755
index 00000000..99c29cf9
--- /dev/null
+++ b/examples/chapter05/04-PalindromeChecker.js
@@ -0,0 +1,8 @@
+const { palindromeChecker } = PacktDataStructuresAlgorithms;
+
+console.log('a', palindromeChecker('a'));
+console.log('aa', palindromeChecker('aa'));
+console.log('kayak', palindromeChecker('kayak'));
+console.log('level', palindromeChecker('level'));
+console.log('Was it a car or a cat I saw', palindromeChecker('Was it a car or a cat I saw'));
+console.log('Step on no pets', palindromeChecker('Step on no pets'));
diff --git a/examples/chapter06/01-LinkedList.html b/examples/chapter06/01-LinkedList.html
new file mode 100644
index 00000000..df7c5591
--- /dev/null
+++ b/examples/chapter06/01-LinkedList.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter06/01-LinkedList.js b/examples/chapter06/01-LinkedList.js
new file mode 100644
index 00000000..8d7c6e49
--- /dev/null
+++ b/examples/chapter06/01-LinkedList.js
@@ -0,0 +1,48 @@
+const { LinkedList } = PacktDataStructuresAlgorithms;
+
+const list = new LinkedList();
+
+console.log('push element 15');
+list.push(15);
+
+console.log('list.indexOf(15) => ', list.indexOf(15));
+
+console.log('push element 10');
+list.push(10);
+
+console.log('list.toString() => ', list.toString());
+console.log('list.indexOf(10) => ', list.indexOf(10));
+
+console.log('push element 13');
+list.push(13);
+
+console.log('list.toString() => ', list.toString());
+console.log('list.indexOf(13) => ', list.indexOf(13));
+console.log('list.indexOf(10) => ', list.indexOf(10));
+
+console.log('push elements 11 and 12');
+list.push(11);
+list.push(12);
+
+console.log('list.toString() => ', list.toString());
+console.log('list.removeAt(1) => ', list.removeAt(1));
+console.log('list.toString() => ', list.toString());
+console.log('list.removeAt(3) => ', list.removeAt(3));
+console.log('list.toString() => ', list.toString());
+
+console.log('push element 14');
+list.push(14);
+
+console.log('list.toString() => ', list.toString());
+console.log('insert element 16 pos 0 => ', list.insert(16, 0));
+console.log('list.toString() => ', list.toString());
+console.log('insert element 17 pos 1 => ', list.insert(17, 1));
+console.log('list.toString() => ', list.toString());
+console.log('insert element 18 pos list.size() => ', list.insert(18, list.size()));
+console.log('list.toString() => ', list.toString());
+console.log('remove element 16 => ', list.remove(16));
+console.log('list.toString() => ', list.toString());
+console.log('remove element 11 => ', list.remove(11));
+console.log('list.toString() => ', list.toString());
+console.log('remove element 18 => ', list.remove(18));
+console.log('list.toString() => ', list.toString());
diff --git a/examples/chapter06/02-DoublyLinkedList.html b/examples/chapter06/02-DoublyLinkedList.html
new file mode 100644
index 00000000..1aea7c24
--- /dev/null
+++ b/examples/chapter06/02-DoublyLinkedList.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter06/02-DoublyLinkedList.js b/examples/chapter06/02-DoublyLinkedList.js
new file mode 100644
index 00000000..333f701c
--- /dev/null
+++ b/examples/chapter06/02-DoublyLinkedList.js
@@ -0,0 +1,54 @@
+const { DoublyLinkedList } = PacktDataStructuresAlgorithms;
+
+const list = new DoublyLinkedList();
+
+console.log('push element 15');
+list.push(15);
+console.log('list.toString() => ', list.toString());
+console.log('list.inverseToString() => ', list.inverseToString());
+
+console.log('push element 16');
+list.push(16);
+console.log('list.toString() => ', list.toString());
+console.log('list.inverseToString() => ', list.inverseToString());
+
+console.log('push element 17');
+list.push(17);
+console.log('list.toString() => ', list.toString());
+console.log('list.inverseToString() => ', list.inverseToString());
+
+console.log('insert element 13 pos 0 => ', list.insert(13, 0));
+console.log('list.toString() => ', list.toString());
+console.log('list.inverseToString() => ', list.inverseToString());
+
+console.log('insert element 18 pos 4 => ', list.insert(18, 4));
+console.log('list.toString() => ', list.toString());
+console.log('list.inverseToString() => ', list.inverseToString());
+
+console.log('insert element 14 pos 1 => ', list.insert(14, 1));
+console.log('list.toString() => ', list.toString());
+console.log('list.inverseToString() => ', list.inverseToString());
+
+console.log('list.removeAt(0) => ', list.removeAt(0));
+console.log('list.toString() => ', list.toString());
+console.log('list.inverseToString() => ', list.inverseToString());
+
+console.log('list.removeAt(list.size() - 1) => ', list.removeAt(list.size() - 1));
+console.log('list.toString() => ', list.toString());
+console.log('list.inverseToString() => ', list.inverseToString());
+
+console.log('list.removeAt(1) => ', list.removeAt(1));
+console.log('list.toString() => ', list.toString());
+console.log('list.inverseToString() => ', list.inverseToString());
+
+console.log('remove element 16 => ', list.remove(16));
+console.log('list.toString() => ', list.toString());
+console.log('list.inverseToString() => ', list.inverseToString());
+
+console.log('remove element 14 => ', list.remove(14));
+console.log('list.toString() => ', list.toString());
+console.log('list.inverseToString() => ', list.inverseToString());
+
+console.log('remove element 17 => ', list.remove(17));
+console.log('list.toString() => ', list.toString());
+console.log('list.inverseToString() => ', list.inverseToString());
diff --git a/examples/chapter06/03-CircularLinkedList.html b/examples/chapter06/03-CircularLinkedList.html
new file mode 100644
index 00000000..10fbd6b6
--- /dev/null
+++ b/examples/chapter06/03-CircularLinkedList.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter06/03-CircularLinkedList.js b/examples/chapter06/03-CircularLinkedList.js
new file mode 100644
index 00000000..05178956
--- /dev/null
+++ b/examples/chapter06/03-CircularLinkedList.js
@@ -0,0 +1,32 @@
+const { CircularLinkedList } = PacktDataStructuresAlgorithms;
+
+const list = new CircularLinkedList();
+
+console.log('push element 15');
+list.push(15);
+console.log('list.toString() => ', list.toString());
+
+console.log('push element 16');
+list.push(16);
+console.log('list.toString() => ', list.toString());
+
+console.log('insert element 14 pos 0 => ', list.insert(14, 0));
+console.log('list.toString() => ', list.toString());
+
+console.log('insert element 14.5 pos 1 => ', list.insert(14.5, 1));
+console.log('list.toString() => ', list.toString());
+
+console.log('insert element 17 pos 4 => ', list.insert(17, 4));
+console.log('list.toString() => ', list.toString());
+
+console.log('list.removeAt(0) => ', list.removeAt(0));
+console.log('list.toString() => ', list.toString());
+
+console.log('list.removeAt(1) => ', list.removeAt(1));
+console.log('list.toString() => ', list.toString());
+
+console.log('list.removeAt(2) => ', list.removeAt(2));
+console.log('list.toString() => ', list.toString());
+
+console.log('list.indexOf(14.5) => ', list.indexOf(14.5));
+console.log('list.indexOf(16) => ', list.indexOf(16));
diff --git a/examples/chapter06/04-SortedLinkedList.html b/examples/chapter06/04-SortedLinkedList.html
new file mode 100644
index 00000000..9feb1a70
--- /dev/null
+++ b/examples/chapter06/04-SortedLinkedList.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter06/04-SortedLinkedList.js b/examples/chapter06/04-SortedLinkedList.js
new file mode 100644
index 00000000..c6351f8f
--- /dev/null
+++ b/examples/chapter06/04-SortedLinkedList.js
@@ -0,0 +1,53 @@
+const { SortedLinkedList } = PacktDataStructuresAlgorithms;
+const { util } = PacktDataStructuresAlgorithms;
+
+const list = new SortedLinkedList();
+
+console.log('');
+for (let i = 5; i > 0; i--) {
+ list.push(i);
+}
+
+console.log('list after pushing 5, 4, 3, 2, and 1 => ', list.toString());
+
+console.log('list.removeAt(1) => ', list.removeAt(1));
+
+console.log('remove element 16 => ', list.remove(5));
+
+console.log('list.toString() => ', list.toString());
+
+// ------- Example 02
+
+class MyObj {
+ constructor(el1, el2) {
+ this.el1 = el1;
+ this.el2 = el2;
+ }
+ toString() {
+ return `${this.el1.toString()}|${this.el2.toString()}`;
+ }
+}
+
+function myObjCompare(a, b) {
+ return a.toString().localeCompare(b.toString());
+}
+
+const ds = new SortedLinkedList(util.defaultEquals, myObjCompare);
+
+console.log('*** SortedLinkedList with custom sorting function');
+
+ds.push(new MyObj(3, 4));
+console.log('push MyObj(3, 4)');
+console.log('list.toString() => ', ds.toString());
+
+ds.push(new MyObj(1, 2));
+console.log('push MyObj(1, 2)');
+console.log('list.toString() => ', ds.toString());
+
+ds.push(new MyObj(5, 6));
+console.log('push MyObj(5, 6)');
+console.log('list.toString() => ', ds.toString());
+
+ds.insert(new MyObj(0, 0), 4);
+console.log('insert MyObj(0, 0) pos 4 (pos ignored)');
+console.log('list.toString() => ', ds.toString());
diff --git a/examples/chapter06/05-StackLinkedList.html b/examples/chapter06/05-StackLinkedList.html
new file mode 100644
index 00000000..c511e977
--- /dev/null
+++ b/examples/chapter06/05-StackLinkedList.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter06/05-StackLinkedList.js b/examples/chapter06/05-StackLinkedList.js
new file mode 100644
index 00000000..866e7f91
--- /dev/null
+++ b/examples/chapter06/05-StackLinkedList.js
@@ -0,0 +1,24 @@
+const { StackLinkedList } = PacktDataStructuresAlgorithms;
+
+const stack = new StackLinkedList();
+
+console.log('stack.isEmpty() => ', stack.isEmpty()); // outputs true
+
+stack.push(5);
+stack.push(8);
+
+console.log('stack after push 5 and 8 => ', stack.toString());
+
+console.log('stack.peek() => ', stack.peek()); // outputs 8
+
+stack.push(11);
+
+console.log('stack.size() after push 11 => ', stack.size()); // outputs 3
+console.log('stack.isEmpty() => ', stack.isEmpty()); // outputs false
+
+stack.push(15);
+
+stack.pop();
+stack.pop();
+
+console.log('stack.size() after push 15 and pop twice => ', stack.size()); // outputs 2
diff --git a/examples/chapter07/01-Set.html b/examples/chapter07/01-Set.html
new file mode 100755
index 00000000..cf2f575b
--- /dev/null
+++ b/examples/chapter07/01-Set.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter07/01-Set.js b/examples/chapter07/01-Set.js
new file mode 100755
index 00000000..e893c2c8
--- /dev/null
+++ b/examples/chapter07/01-Set.js
@@ -0,0 +1,19 @@
+const { Set } = PacktDataStructuresAlgorithms;
+
+const set = new Set();
+
+set.add(1);
+console.log(set.values()); // outputs [1]
+console.log(set.has(1)); // outputs true
+console.log(set.size()); // outputs 1
+
+set.add(2);
+console.log(set.values()); // outputs [1, 2]
+console.log(set.has(2)); // true
+console.log(set.size()); // 2
+
+set.delete(1);
+console.log(set.values()); // outputs [2]
+
+set.delete(2);
+console.log(set.values()); // outputs []
diff --git a/examples/chapter07/02-SetOperations.html b/examples/chapter07/02-SetOperations.html
new file mode 100755
index 00000000..b55d7e0d
--- /dev/null
+++ b/examples/chapter07/02-SetOperations.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter07/02-SetOperations.js b/examples/chapter07/02-SetOperations.js
new file mode 100755
index 00000000..2608c7ef
--- /dev/null
+++ b/examples/chapter07/02-SetOperations.js
@@ -0,0 +1,66 @@
+const { Set } = PacktDataStructuresAlgorithms;
+
+// --------- Union ----------
+
+let setA = new Set();
+setA.add(1);
+setA.add(2);
+setA.add(3);
+
+let setB = new Set();
+setB.add(3);
+setB.add(4);
+setB.add(5);
+setB.add(6);
+
+const unionAB = setA.union(setB);
+console.log(unionAB.values()); // [1, 2, 3, 4, 5, 6]
+
+// --------- Intersection ----------
+
+setA = new Set();
+setA.add(1);
+setA.add(2);
+setA.add(3);
+
+setB = new Set();
+setB.add(2);
+setB.add(3);
+setB.add(4);
+
+const intersectionAB = setA.intersection(setB);
+console.log(intersectionAB.values()); // [2, 3]
+
+// --------- Difference ----------
+
+setA = new Set();
+setA.add(1);
+setA.add(2);
+setA.add(3);
+
+setB = new Set();
+setB.add(2);
+setB.add(3);
+setB.add(4);
+
+const differenceAB = setA.difference(setB);
+console.log(differenceAB.values()); // [1]
+
+// --------- Subset ----------
+
+setA = new Set();
+setA.add(1);
+setA.add(2);
+
+setB = new Set();
+setB.add(1);
+setB.add(2);
+setB.add(3);
+
+const setC = new Set();
+setC.add(2);
+setC.add(3);
+setC.add(4);
+
+console.log(setA.isSubsetOf(setB)); // true
+console.log(setA.isSubsetOf(setC)); // false
diff --git a/examples/chapter07/03-ES2015Set.html b/examples/chapter07/03-ES2015Set.html
new file mode 100755
index 00000000..7fe40e1b
--- /dev/null
+++ b/examples/chapter07/03-ES2015Set.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter07/03-ES2015Set.js b/examples/chapter07/03-ES2015Set.js
new file mode 100755
index 00000000..721ea4d1
--- /dev/null
+++ b/examples/chapter07/03-ES2015Set.js
@@ -0,0 +1,72 @@
+const set = new Set();
+
+set.add(1);
+console.log(set.values()); // outputs @Iterator
+console.log(set.has(1)); // outputs true
+console.log(set.size); // outputs 1
+
+set.add(2);
+console.log(set.values()); // outputs [1, 2]
+console.log(set.has(2)); // true
+console.log(set.size); // 2
+
+set.delete(1);
+console.log(set.values()); // outputs [2]
+
+set.delete(2);
+console.log(set.values()); // outputs []
+
+const setA = new Set();
+setA.add(1);
+setA.add(2);
+setA.add(3);
+
+const setB = new Set();
+setB.add(2);
+setB.add(3);
+setB.add(4);
+
+// --------- Union ----------
+const union = (set1, set2) => {
+ const unionAb = new Set();
+ set1.forEach(value => unionAb.add(value));
+ set2.forEach(value => unionAb.add(value));
+ return unionAb;
+};
+console.log(union(setA, setB));
+
+console.log(new Set([...setA, ...setB]));
+
+// --------- Intersection ----------
+const intersection = (set1, set2) => {
+ const intersectionSet = new Set();
+ set1.forEach(value => {
+ if (set2.has(value)) {
+ intersectionSet.add(value);
+ }
+ });
+ return intersectionSet;
+};
+console.log(intersection(setA, setB));
+
+console.log(new Set([...setA].filter(x => setB.has(x))));
+
+// alternative - works on FF only
+// console.log(new Set([x for (x of setA) if (setB.has(x))]));
+
+// --------- Difference ----------
+const difference = (set1, set2) => {
+ const differenceSet = new Set();
+ set1.forEach(value => {
+ if (!set2.has(value)) {
+ differenceSet.add(value);
+ }
+ });
+ return differenceSet;
+};
+console.log(difference(setA, setB));
+
+console.log(new Set([...setA].filter(x => !setB.has(x))));
+
+// alternative - works on FF only
+// console.log(new Set([x for (x of setA) if (!setB.has(x))]));
diff --git a/examples/chapter08/01-Dictionaries.html b/examples/chapter08/01-Dictionaries.html
new file mode 100755
index 00000000..d972458c
--- /dev/null
+++ b/examples/chapter08/01-Dictionaries.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter08/01-Dictionaries.js b/examples/chapter08/01-Dictionaries.js
new file mode 100755
index 00000000..53433cc0
--- /dev/null
+++ b/examples/chapter08/01-Dictionaries.js
@@ -0,0 +1,28 @@
+const { Dictionary } = PacktDataStructuresAlgorithms;
+
+const dictionary = new Dictionary();
+
+dictionary.set('Gandalf', 'gandalf@email.com');
+dictionary.set('John', 'johnsnow@email.com');
+dictionary.set('Tyrion', 'tyrion@email.com');
+
+console.log(dictionary.hasKey('Gandalf')); // true
+console.log(dictionary.size()); // 3
+
+console.log(dictionary.keys()); // ["Gandalf", "John", "Tyrion"]
+console.log(dictionary.values()); // ["gandalf@email.com", "johnsnow@email.com", "tyrion@email.com"]
+console.log(dictionary.get('Tyrion')); // tyrion@email.com
+
+dictionary.remove('John');
+
+console.log(dictionary.keys()); // ["Gandalf", "Tyrion"]
+console.log(dictionary.values()); // ["gandalf@email.com", "tyrion@email.com"]
+
+console.log(dictionary.keyValues()); // [{key: "Gandalf", value: "gandalf@email.com"}, {key: "Tyrion", value: "tyrion@email.com"}]
+console.log(dictionary.toString()); // [#Gandalf: gandalf@email.com],[#Tyrion: tyrion@email.com]
+
+dictionary.forEach((k, v) => {
+ console.log('forEach: ', `key: ${k}, value: ${v}`);
+});
+// forEach: key: Gandalf, value: gandalf@email.com
+// forEach: key: Tyrion, value: tyrion@email.com
diff --git a/examples/chapter08/02-HashTable.html b/examples/chapter08/02-HashTable.html
new file mode 100755
index 00000000..67ad128c
--- /dev/null
+++ b/examples/chapter08/02-HashTable.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter08/02-HashTable.js b/examples/chapter08/02-HashTable.js
new file mode 100755
index 00000000..fbee2022
--- /dev/null
+++ b/examples/chapter08/02-HashTable.js
@@ -0,0 +1,51 @@
+const { HashTable } = PacktDataStructuresAlgorithms;
+
+const hash = new HashTable();
+
+console.log(hash.hashCode('Gandalf') + ' - Gandalf');
+console.log(hash.hashCode('John') + ' - John');
+console.log(hash.hashCode('Tyrion') + ' - Tyrion');
+
+console.log(' ');
+
+console.log(hash.hashCode('Ygritte') + ' - Ygritte');
+console.log(hash.hashCode('Jonathan') + ' - Jonathan');
+console.log(hash.hashCode('Jamie') + ' - Jamie');
+console.log(hash.hashCode('Jack') + ' - Jack');
+console.log(hash.hashCode('Jasmine') + ' - Jasmine');
+console.log(hash.hashCode('Jake') + ' - Jake');
+console.log(hash.hashCode('Nathan') + ' - Nathan');
+console.log(hash.hashCode('Athelstan') + ' - Athelstan');
+console.log(hash.hashCode('Sue') + ' - Sue');
+console.log(hash.hashCode('Aethelwulf') + ' - Aethelwulf');
+console.log(hash.hashCode('Sargeras') + ' - Sargeras');
+
+hash.put('Ygritte', 'ygritte@email.com');
+hash.put('Jonathan', 'jonathan@email.com');
+hash.put('Jamie', 'jamie@email.com');
+hash.put('Jack', 'jack@email.com');
+hash.put('Jasmine', 'jasmine@email.com');
+hash.put('Jake', 'jake@email.com');
+hash.put('Nathan', 'nathan@email.com');
+hash.put('Athelstan', 'athelstan@email.com');
+hash.put('Sue', 'sue@email.com');
+hash.put('Aethelwulf', 'aethelwulf@email.com');
+hash.put('Sargeras', 'sargeras@email.com');
+
+console.log('**** Printing Hash **** ');
+
+console.log(hash.toString());
+// {4 => [#Ygritte: ygritte@email.com]},{5 => [#Aethelwulf: aethelwulf@email.com]},{7 => [#Athelstan: athelstan@email.com]},{8 => [#Jasmine: jasmine@email.com]},{9 => [#Jake: jake@email.com]},{10 => [#Sargeras: sargeras@email.com]}
+
+console.log('**** Get **** ');
+
+console.log(hash.get('Ygritte')); // ygritte@email.com
+console.log(hash.get('Loiane')); // jasmine@email.com
+
+console.log('**** Remove **** ');
+
+hash.remove('Ygritte');
+console.log(hash.get('Ygritte')); // undefined
+
+console.log(hash.toString());
+// {5 => [#Aethelwulf: aethelwulf@email.com]},{7 => [#Athelstan: athelstan@email.com]},{8 => [#Jasmine: jasmine@email.com]},{9 => [#Jake: jake@email.com]},{10 => [#Sargeras: sargeras@email.com]}
diff --git a/examples/chapter08/03-HashCollisionSeparateChaining.html b/examples/chapter08/03-HashCollisionSeparateChaining.html
new file mode 100755
index 00000000..cddcc3db
--- /dev/null
+++ b/examples/chapter08/03-HashCollisionSeparateChaining.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter08/03-HashCollisionSeparateChaining.js b/examples/chapter08/03-HashCollisionSeparateChaining.js
new file mode 100755
index 00000000..f791ba89
--- /dev/null
+++ b/examples/chapter08/03-HashCollisionSeparateChaining.js
@@ -0,0 +1,46 @@
+const { HashTableSeparateChaining } = PacktDataStructuresAlgorithms;
+
+const hashTable = new HashTableSeparateChaining();
+
+hashTable.put('Ygritte', 'ygritte@email.com');
+hashTable.put('Jonathan', 'jonathan@email.com');
+hashTable.put('Jamie', 'jamie@email.com');
+hashTable.put('Jack', 'jack@email.com');
+hashTable.put('Jasmine', 'jasmine@email.com');
+hashTable.put('Jake', 'jake@email.com');
+hashTable.put('Nathan', 'nathan@email.com');
+hashTable.put('Athelstan', 'athelstan@email.com');
+hashTable.put('Sue', 'sue@email.com');
+hashTable.put('Aethelwulf', 'aethelwulf@email.com');
+hashTable.put('Sargeras', 'sargeras@email.com');
+
+console.log('**** Printing Hash **** ');
+
+console.log(hashTable.toString());
+// {4 => [#Ygritte: ygritte@email.com]},{5 => [#Jonathan: jonathan@email.com],[#Jamie: jamie@email.com],[#Sue: sue@email.com],[#Aethelwulf: aethelwulf@email.com]},{7 => [#Jack: jack@email.com],[#Athelstan: athelstan@email.com]},{8 => [#Jasmine: jasmine@email.com]},{9 => [#Jake: jake@email.com]},{10 => [#Nathan: nathan@email.com],[#Sargeras: sargeras@email.com]}
+
+console.log('**** Get **** ');
+
+console.log(hashTable.get('Jamie')); // jamie@email.com
+console.log(hashTable.get('Sue')); // sue@email.com
+console.log(hashTable.get('Jonathan')); // jonathan@email.com
+console.log(hashTable.get('Loiane')); // undefined
+
+console.log('**** Remove **** ');
+
+console.log(hashTable.remove('Ygritte')); // true
+console.log(hashTable.get('Ygritte')); // undefined
+console.log(hashTable.toString());
+// {5 => [#Jonathan: jonathan@email.com],[#Jamie: jamie@email.com],[#Sue: sue@email.com],[#Aethelwulf: aethelwulf@email.com]},{7 => [#Jack: jack@email.com],[#Athelstan: athelstan@email.com]},{8 => [#Jasmine: jasmine@email.com]},{9 => [#Jake: jake@email.com]},{10 => [#Nathan: nathan@email.com],[#Sargeras: sargeras@email.com]}
+
+console.log(hashTable.remove('Sue')); // true
+console.log(hashTable.toString());
+// {5 => [#Jonathan: jonathan@email.com],[#Jamie: jamie@email.com],[#Aethelwulf: aethelwulf@email.com]},{7 => [#Jack: jack@email.com],[#Athelstan: athelstan@email.com]},{8 => [#Jasmine: jasmine@email.com]},{9 => [#Jake: jake@email.com]},{10 => [#Nathan: nathan@email.com],[#Sargeras: sargeras@email.com]}
+
+console.log(hashTable.remove('Jamie')); // true
+console.log(hashTable.toString());
+// {5 => [#Jonathan: jonathan@email.com],[#Aethelwulf: aethelwulf@email.com]},{7 => [#Jack: jack@email.com],[#Athelstan: athelstan@email.com]},{8 => [#Jasmine: jasmine@email.com]},{9 => [#Jake: jake@email.com]},{10 => [#Nathan: nathan@email.com],[#Sargeras: sargeras@email.com]}
+
+console.log(hashTable.remove('Aethelwulf')); // true
+console.log(hashTable.toString());
+// {5 => [#Jonathan: jonathan@email.com]},{7 => [#Jack: jack@email.com],[#Athelstan: athelstan@email.com]},{8 => [#Jasmine: jasmine@email.com]},{9 => [#Jake: jake@email.com]},{10 => [#Nathan: nathan@email.com],[#Sargeras: sargeras@email.com]}
diff --git a/examples/chapter08/04-HashCollisionLinearProbing.html b/examples/chapter08/04-HashCollisionLinearProbing.html
new file mode 100755
index 00000000..6f8bbe52
--- /dev/null
+++ b/examples/chapter08/04-HashCollisionLinearProbing.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter08/04-HashCollisionLinearProbing.js b/examples/chapter08/04-HashCollisionLinearProbing.js
new file mode 100755
index 00000000..b75cd98d
--- /dev/null
+++ b/examples/chapter08/04-HashCollisionLinearProbing.js
@@ -0,0 +1,42 @@
+const { HashTableLinearProbing } = PacktDataStructuresAlgorithms;
+
+const hashTable = new HashTableLinearProbing();
+
+hashTable.put('Ygritte', 'ygritte@email.com');
+hashTable.put('Jonathan', 'jonathan@email.com');
+hashTable.put('Jamie', 'jamie@email.com');
+hashTable.put('Jack', 'jack@email.com');
+hashTable.put('Jasmine', 'jasmine@email.com');
+hashTable.put('Jake', 'jake@email.com');
+hashTable.put('Nathan', 'nathan@email.com');
+hashTable.put('Athelstan', 'athelstan@email.com');
+hashTable.put('Sue', 'sue@email.com');
+hashTable.put('Aethelwulf', 'aethelwulf@email.com');
+hashTable.put('Sargeras', 'sargeras@email.com');
+
+console.log('**** Printing Hash **** ');
+
+console.log(hashTable.toString());
+// {4 => [#Ygritte: ygritte@email.com]},{5 => [#Jonathan: jonathan@email.com]},{6 => [#Jamie: jamie@email.com]},{7 => [#Jack: jack@email.com]},{8 => [#Jasmine: jasmine@email.com]},{9 => [#Jake: jake@email.com]},{10 => [#Nathan: nathan@email.com]},{11 => [#Athelstan: athelstan@email.com]},{12 => [#Sue: sue@email.com]},{13 => [#Aethelwulf: aethelwulf@email.com]},{14 => [#Sargeras: sargeras@email.com]}
+console.log('**** Get **** ');
+
+console.log(hashTable.get('Nathan')); // nathan@email.com
+console.log(hashTable.get('Loiane')); // undefined
+
+console.log('**** Remove **** ');
+
+hashTable.remove('Ygritte');
+console.log(hashTable.get('Ygritte')); // undefined
+console.log(hashTable.toString());
+// {5 => [#Jonathan: jonathan@email.com]},{6 => [#Jamie: jamie@email.com]},{7 => [#Jack: jack@email.com]},{8 => [#Jasmine: jasmine@email.com]},{9 => [#Jake: jake@email.com]},{10 => [#Nathan: nathan@email.com]},{11 => [#Athelstan: athelstan@email.com]},{12 => [#Sue: sue@email.com]},{13 => [#Aethelwulf: aethelwulf@email.com]},{14 => [#Sargeras: sargeras@email.com]}
+
+console.log('**** Remove Test 2 **** ');
+console.log('Removing Jonathan', hashTable.remove('Jonathan')); // true
+console.log('**** Print **** ');
+console.log(hashTable.toString());
+// {5 => [#Jamie: jamie@email.com]},{6 => [#Sue: sue@email.com]},{7 => [#Jack: jack@email.com]},{8 => [#Jasmine: jasmine@email.com]},{9 => [#Jake: jake@email.com]},{10 => [#Nathan: nathan@email.com]},{11 => [#Athelstan: athelstan@email.com]},{12 => [#Aethelwulf: aethelwulf@email.com]},{13 => [#Sargeras: sargeras@email.com]}
+
+console.log(hashTable.get('Jamie')); // jamie@email.com
+console.log('**** Print **** ');
+console.log(hashTable.toString());
+// {5 => [#Jamie: jamie@email.com]},{6 => [#Sue: sue@email.com]},{7 => [#Jack: jack@email.com]},{8 => [#Jasmine: jasmine@email.com]},{9 => [#Jake: jake@email.com]},{10 => [#Nathan: nathan@email.com]},{11 => [#Athelstan: athelstan@email.com]},{12 => [#Aethelwulf: aethelwulf@email.com]},{13 => [#Sargeras: sargeras@email.com]}
diff --git a/examples/chapter08/05-ES2015Map.html b/examples/chapter08/05-ES2015Map.html
new file mode 100755
index 00000000..8a2ade05
--- /dev/null
+++ b/examples/chapter08/05-ES2015Map.html
@@ -0,0 +1,10 @@
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter08/05-ES2015Map.js b/examples/chapter08/05-ES2015Map.js
new file mode 100755
index 00000000..e944f988
--- /dev/null
+++ b/examples/chapter08/05-ES2015Map.js
@@ -0,0 +1,17 @@
+const map = new Map();
+
+map.set('Gandalf', 'gandalf@email.com');
+map.set('John', 'johnsnow@email.com');
+map.set('Tyrion', 'tyrion@email.com');
+
+console.log(map.has('Gandalf')); // true
+console.log(map.size); // 3
+
+console.log(map.keys()); // MapIterator {"Gandalf", "John", "Tyrion"}
+console.log(map.values()); // MapIterator {"gandalf@email.com", "johnsnow@email.com", "tyrion@email.com"}
+console.log(map.get('Tyrion')); // tyrion@email.com
+
+map.delete('John');
+
+console.log(map.keys()); // MapIterator {"Gandalf", "Tyrion"}
+console.log(map.values()); // MapIterator {"gandalf@email.com", "tyrion@email.com"}
diff --git a/examples/chapter08/06-ES2015WeakMap.html b/examples/chapter08/06-ES2015WeakMap.html
new file mode 100755
index 00000000..15daf46d
--- /dev/null
+++ b/examples/chapter08/06-ES2015WeakMap.html
@@ -0,0 +1,10 @@
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter08/06-ES2015WeakMap.js b/examples/chapter08/06-ES2015WeakMap.js
new file mode 100755
index 00000000..1f2d01bc
--- /dev/null
+++ b/examples/chapter08/06-ES2015WeakMap.js
@@ -0,0 +1,18 @@
+const map = new WeakMap();
+
+const ob1 = { name: 'Gandalf' };
+const ob2 = { name: 'John' };
+const ob3 = { name: 'Tyrion' };
+
+map.set(ob1, 'gandalf@email.com');
+map.set(ob2, 'johnsnow@email.com');
+map.set(ob3, 'tyrion@email.com');
+
+console.log(map.has(ob1)); // true
+console.log(map.has(ob2)); // true
+console.log(map.has(ob3)); // true
+
+console.log(map.get(ob3)); // tyrion@email.com
+
+map.delete(ob2);
+console.log(map.has(ob2)); // false
diff --git a/examples/chapter08/07-ES2015WeakSet.html b/examples/chapter08/07-ES2015WeakSet.html
new file mode 100755
index 00000000..7a4f82f4
--- /dev/null
+++ b/examples/chapter08/07-ES2015WeakSet.html
@@ -0,0 +1,10 @@
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter08/07-ES2015WeakSet.js b/examples/chapter08/07-ES2015WeakSet.js
new file mode 100755
index 00000000..0e849f1a
--- /dev/null
+++ b/examples/chapter08/07-ES2015WeakSet.js
@@ -0,0 +1,16 @@
+var set = new WeakSet();
+
+const ob1 = { name: 'Gandalf' };
+const ob2 = { name: 'John' };
+const ob3 = { name: 'Tyrion' };
+
+set.add(ob1);
+set.add(ob2);
+set.add(ob3);
+
+console.log(set.has(ob1)); // true
+console.log(set.has(ob2)); // true
+console.log(set.has(ob3)); // true
+
+set.delete(ob2);
+console.log(set.has(ob2)); // false
diff --git a/examples/chapter09/01-IntroRecursion.html b/examples/chapter09/01-IntroRecursion.html
new file mode 100644
index 00000000..ae42e54f
--- /dev/null
+++ b/examples/chapter09/01-IntroRecursion.html
@@ -0,0 +1,10 @@
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter09/01-IntroRecursion.js b/examples/chapter09/01-IntroRecursion.js
new file mode 100644
index 00000000..d2843215
--- /dev/null
+++ b/examples/chapter09/01-IntroRecursion.js
@@ -0,0 +1,26 @@
+
+// functions commented below - inifite loop
+
+/*
+function recursiveFunction(someParam) {
+ recursiveFunction(someParam);
+}
+
+function recursiveFunction1(someParam) {
+ recursiveFunction2(someParam);
+}
+
+function recursiveFunction2(someParam) {
+ recursiveFunction1(someParam);
+}
+*/
+
+function understandRecursion(doIunderstandRecursion) {
+ const recursionAnswer = confirm('Do you understand recursion?'); // function logic
+ if (recursionAnswer === true) { // base case or stop point
+ return true;
+ }
+ understandRecursion(recursionAnswer); // recursive call
+}
+
+understandRecursion(false);
diff --git a/examples/chapter09/02-Factorial.html b/examples/chapter09/02-Factorial.html
new file mode 100755
index 00000000..94c6c103
--- /dev/null
+++ b/examples/chapter09/02-Factorial.html
@@ -0,0 +1,10 @@
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter09/02-Factorial.js b/examples/chapter09/02-Factorial.js
new file mode 100644
index 00000000..774006f9
--- /dev/null
+++ b/examples/chapter09/02-Factorial.js
@@ -0,0 +1,26 @@
+// @ts-check
+
+function factorialIterative(number) {
+ if (number < 0) {
+ return undefined;
+ }
+ let total = 1;
+ for (let n = number; n > 1; n--) {
+ total = total * n;
+ }
+ return total;
+}
+
+console.log('factorialIterative(5): ', factorialIterative(5));
+console.log('factorialIterative(3): ', factorialIterative(3));
+
+function factorial(n) {
+ // console.trace();
+ if (n === 1 || n === 0) {
+ return 1;
+ }
+ return n * factorial(n - 1);
+}
+
+console.log('factorial(5): ', factorial(5));
+console.log('factorial(3): ', factorial(3));
diff --git a/examples/chapter09/03-JSCallStack.html b/examples/chapter09/03-JSCallStack.html
new file mode 100644
index 00000000..0f88040d
--- /dev/null
+++ b/examples/chapter09/03-JSCallStack.html
@@ -0,0 +1,10 @@
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter09/03-JSCallStack.js b/examples/chapter09/03-JSCallStack.js
new file mode 100644
index 00000000..fcfd6ef8
--- /dev/null
+++ b/examples/chapter09/03-JSCallStack.js
@@ -0,0 +1,11 @@
+let i = 0;
+function recursiveFn() {
+ i++;
+ recursiveFn();
+}
+
+try {
+ recursiveFn();
+} catch (ex) {
+ console.log('i = ' + i + ' error: ' + ex);
+}
diff --git a/examples/chapter09/04-Fibonacci.html b/examples/chapter09/04-Fibonacci.html
new file mode 100755
index 00000000..8413c457
--- /dev/null
+++ b/examples/chapter09/04-Fibonacci.html
@@ -0,0 +1,10 @@
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter09/04-Fibonacci.js b/examples/chapter09/04-Fibonacci.js
new file mode 100644
index 00000000..67eae205
--- /dev/null
+++ b/examples/chapter09/04-Fibonacci.js
@@ -0,0 +1,43 @@
+function fibonacci(n){
+ if (n < 1) return 0; // {1}
+ if (n <= 2) return 1; // {2}
+ return fibonacci(n - 1) + fibonacci(n - 2); // {3}
+}
+
+console.log('fibonacci(2)', fibonacci(2));
+console.log('fibonacci(3)', fibonacci(3));
+console.log('fibonacci(4)', fibonacci(4));
+console.log('fibonacci(5)', fibonacci(5));
+
+function fibonacciIterative(n){
+ let fibNMinus2 = 0;
+ let fibNMinus1 = 1;
+ let fibN = n;
+ for (let i = 2; i <= n; i++) { // n >= 2
+ fibN = fibNMinus1 + fibNMinus2; // f(n-1) + f(n-2)
+ fibNMinus2 = fibNMinus1;
+ fibNMinus1 = fibN;
+ }
+ return fibN;
+}
+
+console.log('fibonacciIterative(2)', fibonacciIterative(2));
+console.log('fibonacciIterative(3)', fibonacciIterative(3));
+console.log('fibonacciIterative(4)', fibonacciIterative(4));
+console.log('fibonacciIterative(5)', fibonacciIterative(5));
+
+function fibonacciMemoization(n) {
+ const memo = [0, 1];
+ const fibonacci = (n) => {
+ if (memo[n] != null) return memo[n];
+ return memo[n] = fibonacci(n - 1) + fibonacci(n - 2);
+ };
+ return fibonacci(n);
+}
+
+console.log('fibonacciMemoization(2)', fibonacciMemoization(2));
+console.log('fibonacciMemoization(3)', fibonacciMemoization(3));
+console.log('fibonacciMemoization(4)', fibonacciMemoization(4));
+console.log('fibonacciMemoization(5)', fibonacciMemoization(5));
+
+// https://jsperf.com/fibonacci-comparison-jsbook
diff --git a/examples/chapter10/01-UsingMinHeap.html b/examples/chapter10/01-UsingMinHeap.html
new file mode 100644
index 00000000..83814ba1
--- /dev/null
+++ b/examples/chapter10/01-UsingMinHeap.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter10/01-UsingMinHeap.js b/examples/chapter10/01-UsingMinHeap.js
new file mode 100644
index 00000000..59b7753b
--- /dev/null
+++ b/examples/chapter10/01-UsingMinHeap.js
@@ -0,0 +1,27 @@
+const { MinHeap } = PacktDataStructuresAlgorithms;
+
+let heap = new MinHeap();
+
+heap.insert(2);
+heap.insert(3);
+heap.insert(4);
+heap.insert(5);
+
+heap.insert(2);
+
+console.log(heap.getAsArray());
+
+console.log('Heap size: ', heap.size()); // 5
+console.log('Heap is empty: ', heap.isEmpty()); // false
+console.log('Heap min value: ', heap.findMinimum()); // 1
+
+heap = new MinHeap();
+for (let i = 1; i < 10; i++) {
+ heap.insert(i);
+}
+
+console.log(heap.getAsArray());
+
+console.log('Extract minimum: ', heap.extract()); // 1
+console.log(heap.getAsArray()); // [2, 4, 3, 8, 5, 6, 7, 9]
+
diff --git a/examples/chapter10/02-UsingMaxHeap.html b/examples/chapter10/02-UsingMaxHeap.html
new file mode 100644
index 00000000..9d573bda
--- /dev/null
+++ b/examples/chapter10/02-UsingMaxHeap.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter10/02-UsingMaxHeap.js b/examples/chapter10/02-UsingMaxHeap.js
new file mode 100644
index 00000000..f1a5b97d
--- /dev/null
+++ b/examples/chapter10/02-UsingMaxHeap.js
@@ -0,0 +1,27 @@
+const { MaxHeap } = PacktDataStructuresAlgorithms;
+
+const maxHeap = new MaxHeap();
+
+maxHeap.insert(2);
+maxHeap.insert(3);
+maxHeap.insert(4);
+maxHeap.insert(5);
+
+maxHeap.insert(1);
+
+console.log(maxHeap.getAsArray());
+
+console.log('Heap size: ', maxHeap.size()); // 5
+console.log('Heap is empty: ', maxHeap.isEmpty()); // false
+console.log('Heap min value: ', maxHeap.findMinimum()); // 5
+
+maxHeap.insert(6);
+maxHeap.insert(9);
+maxHeap.insert(10);
+maxHeap.insert(14);
+
+console.log(maxHeap.getAsArray());
+
+console.log('Extract minimum: ', maxHeap.extract());
+console.log(maxHeap.getAsArray());
+
diff --git a/examples/chapter10/03-HeapSort.html b/examples/chapter10/03-HeapSort.html
new file mode 100644
index 00000000..e52c43f4
--- /dev/null
+++ b/examples/chapter10/03-HeapSort.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter10/03-HeapSort.js b/examples/chapter10/03-HeapSort.js
new file mode 100644
index 00000000..9d9b1ac0
--- /dev/null
+++ b/examples/chapter10/03-HeapSort.js
@@ -0,0 +1,7 @@
+const { heapSort } = PacktDataStructuresAlgorithms;
+
+console.log('********** Heap Sort **********');
+const array = [7, 6, 3, 5, 4, 1, 2];
+
+console.log('Before sorting: ', array);
+console.log('After sorting: ', heapSort(array));
diff --git a/examples/chapter11/01-UsingMinHeap.html b/examples/chapter11/01-UsingMinHeap.html
new file mode 100644
index 00000000..83814ba1
--- /dev/null
+++ b/examples/chapter11/01-UsingMinHeap.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter11/01-UsingMinHeap.js b/examples/chapter11/01-UsingMinHeap.js
new file mode 100644
index 00000000..59b7753b
--- /dev/null
+++ b/examples/chapter11/01-UsingMinHeap.js
@@ -0,0 +1,27 @@
+const { MinHeap } = PacktDataStructuresAlgorithms;
+
+let heap = new MinHeap();
+
+heap.insert(2);
+heap.insert(3);
+heap.insert(4);
+heap.insert(5);
+
+heap.insert(2);
+
+console.log(heap.getAsArray());
+
+console.log('Heap size: ', heap.size()); // 5
+console.log('Heap is empty: ', heap.isEmpty()); // false
+console.log('Heap min value: ', heap.findMinimum()); // 1
+
+heap = new MinHeap();
+for (let i = 1; i < 10; i++) {
+ heap.insert(i);
+}
+
+console.log(heap.getAsArray());
+
+console.log('Extract minimum: ', heap.extract()); // 1
+console.log(heap.getAsArray()); // [2, 4, 3, 8, 5, 6, 7, 9]
+
diff --git a/examples/chapter11/02-UsingMaxHeap.html b/examples/chapter11/02-UsingMaxHeap.html
new file mode 100644
index 00000000..9d573bda
--- /dev/null
+++ b/examples/chapter11/02-UsingMaxHeap.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter11/02-UsingMaxHeap.js b/examples/chapter11/02-UsingMaxHeap.js
new file mode 100644
index 00000000..f1a5b97d
--- /dev/null
+++ b/examples/chapter11/02-UsingMaxHeap.js
@@ -0,0 +1,27 @@
+const { MaxHeap } = PacktDataStructuresAlgorithms;
+
+const maxHeap = new MaxHeap();
+
+maxHeap.insert(2);
+maxHeap.insert(3);
+maxHeap.insert(4);
+maxHeap.insert(5);
+
+maxHeap.insert(1);
+
+console.log(maxHeap.getAsArray());
+
+console.log('Heap size: ', maxHeap.size()); // 5
+console.log('Heap is empty: ', maxHeap.isEmpty()); // false
+console.log('Heap min value: ', maxHeap.findMinimum()); // 5
+
+maxHeap.insert(6);
+maxHeap.insert(9);
+maxHeap.insert(10);
+maxHeap.insert(14);
+
+console.log(maxHeap.getAsArray());
+
+console.log('Extract minimum: ', maxHeap.extract());
+console.log(maxHeap.getAsArray());
+
diff --git a/examples/chapter11/03-HeapSort.html b/examples/chapter11/03-HeapSort.html
new file mode 100644
index 00000000..e52c43f4
--- /dev/null
+++ b/examples/chapter11/03-HeapSort.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter11/03-HeapSort.js b/examples/chapter11/03-HeapSort.js
new file mode 100644
index 00000000..9d9b1ac0
--- /dev/null
+++ b/examples/chapter11/03-HeapSort.js
@@ -0,0 +1,7 @@
+const { heapSort } = PacktDataStructuresAlgorithms;
+
+console.log('********** Heap Sort **********');
+const array = [7, 6, 3, 5, 4, 1, 2];
+
+console.log('Before sorting: ', array);
+console.log('After sorting: ', heapSort(array));
diff --git a/examples/chapter12/01-UsingGraphs.html b/examples/chapter12/01-UsingGraphs.html
new file mode 100644
index 00000000..49eb9bff
--- /dev/null
+++ b/examples/chapter12/01-UsingGraphs.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter12/01-UsingGraphs.js b/examples/chapter12/01-UsingGraphs.js
new file mode 100644
index 00000000..dde70c0d
--- /dev/null
+++ b/examples/chapter12/01-UsingGraphs.js
@@ -0,0 +1,23 @@
+const { Graph } = PacktDataStructuresAlgorithms;
+
+const graph = new Graph();
+
+const myVertices = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'];
+
+for (let i = 0; i < myVertices.length; i++) {
+ graph.addVertex(myVertices[i]);
+}
+graph.addEdge('A', 'B');
+graph.addEdge('A', 'C');
+graph.addEdge('A', 'D');
+graph.addEdge('C', 'D');
+graph.addEdge('C', 'G');
+graph.addEdge('D', 'G');
+graph.addEdge('D', 'H');
+graph.addEdge('B', 'E');
+graph.addEdge('B', 'F');
+graph.addEdge('E', 'I');
+
+console.log('********* printing graph ***********');
+
+console.log(graph.toString());
diff --git a/examples/chapter12/02-BFS.html b/examples/chapter12/02-BFS.html
new file mode 100644
index 00000000..044da5d6
--- /dev/null
+++ b/examples/chapter12/02-BFS.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter12/02-BFS.js b/examples/chapter12/02-BFS.js
new file mode 100644
index 00000000..d65c8b66
--- /dev/null
+++ b/examples/chapter12/02-BFS.js
@@ -0,0 +1,54 @@
+const { Graph } = PacktDataStructuresAlgorithms;
+const { Stack } = PacktDataStructuresAlgorithms;
+const { BFS } = PacktDataStructuresAlgorithms;
+const { breadthFirstSearch } = PacktDataStructuresAlgorithms;
+
+const graph = new Graph();
+
+const myVertices = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'];
+
+for (let i = 0; i < myVertices.length; i++) {
+ graph.addVertex(myVertices[i]);
+}
+graph.addEdge('A', 'B');
+graph.addEdge('A', 'C');
+graph.addEdge('A', 'D');
+graph.addEdge('C', 'D');
+graph.addEdge('C', 'G');
+graph.addEdge('D', 'G');
+graph.addEdge('D', 'H');
+graph.addEdge('B', 'E');
+graph.addEdge('B', 'F');
+graph.addEdge('E', 'I');
+
+console.log('********* printing graph ***********');
+
+console.log(graph.toString());
+
+console.log('********* bfs with callback ***********');
+
+const printVertex = (value) => console.log('Visited vertex: ' + value);
+
+breadthFirstSearch(graph, myVertices[0], printVertex);
+
+console.log('********* sorthest path - BFS ***********');
+const shortestPathA = BFS(graph, myVertices[0]);
+console.log(shortestPathA.distances);
+console.log(shortestPathA.predecessors);
+
+//from A to all other vertices
+const fromVertex = myVertices[0];
+
+for (let i = 1; i < myVertices.length; i++) {
+ const toVertex = myVertices[i];
+ const path = new Stack();
+ for (let v = toVertex; v !== fromVertex; v = shortestPathA.predecessors[v]) {
+ path.push(v);
+ }
+ path.push(fromVertex);
+ let s = path.pop();
+ while (!path.isEmpty()) {
+ s += ' - ' + path.pop();
+ }
+ console.log(s);
+}
diff --git a/examples/chapter12/03-DFS.html b/examples/chapter12/03-DFS.html
new file mode 100644
index 00000000..1a5bd4f3
--- /dev/null
+++ b/examples/chapter12/03-DFS.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter12/03-DFS.js b/examples/chapter12/03-DFS.js
new file mode 100644
index 00000000..5c440a74
--- /dev/null
+++ b/examples/chapter12/03-DFS.js
@@ -0,0 +1,67 @@
+const { Graph } = PacktDataStructuresAlgorithms;
+const { depthFirstSearch } = PacktDataStructuresAlgorithms;
+const { DFS } = PacktDataStructuresAlgorithms;
+
+let graph = new Graph(true);
+
+let myVertices = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'];
+
+for (let i = 0; i < myVertices.length; i++) {
+ graph.addVertex(myVertices[i]);
+}
+graph.addEdge('A', 'B');
+graph.addEdge('A', 'C');
+graph.addEdge('A', 'D');
+graph.addEdge('C', 'D');
+graph.addEdge('C', 'G');
+graph.addEdge('D', 'G');
+graph.addEdge('D', 'H');
+graph.addEdge('B', 'E');
+graph.addEdge('B', 'F');
+graph.addEdge('E', 'I');
+
+console.log('********* printing graph ***********');
+
+console.log(graph.toString());
+
+console.log('********* dfs with callback ***********');
+
+const printVertex = value => console.log('Visited vertex: ' + value);
+
+depthFirstSearch(graph, printVertex);
+
+console.log('********* topological sort - DFS ***********');
+
+graph = new Graph(true); // directed graph
+
+myVertices = ['A', 'B', 'C', 'D', 'E', 'F'];
+for (i = 0; i < myVertices.length; i++) {
+ graph.addVertex(myVertices[i]);
+}
+graph.addEdge('A', 'C');
+graph.addEdge('A', 'D');
+graph.addEdge('B', 'D');
+graph.addEdge('B', 'E');
+graph.addEdge('C', 'F');
+graph.addEdge('F', 'E');
+
+const result = DFS(graph);
+console.log('discovery', result.discovery);
+console.log('finished', result.finished);
+console.log('predecessors', result.predecessors);
+
+const fTimes = result.finished;
+s = '';
+for (let count = 0; count < myVertices.length; count++) {
+ let max = 0;
+ let maxName = null;
+ for (i = 0; i < myVertices.length; i++) {
+ if (fTimes[myVertices[i]] > max) {
+ max = fTimes[myVertices[i]];
+ maxName = myVertices[i];
+ }
+ }
+ s += ' - ' + maxName;
+ delete fTimes[maxName];
+}
+console.log(s);
diff --git a/examples/chapter12/04-Dijkstra.html b/examples/chapter12/04-Dijkstra.html
new file mode 100644
index 00000000..8b9e0f74
--- /dev/null
+++ b/examples/chapter12/04-Dijkstra.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter12/04-Dijkstra.js b/examples/chapter12/04-Dijkstra.js
new file mode 100644
index 00000000..dfa58854
--- /dev/null
+++ b/examples/chapter12/04-Dijkstra.js
@@ -0,0 +1,18 @@
+const { dijkstra } = PacktDataStructuresAlgorithms;
+
+const graph = [
+ [0, 2, 4, 0, 0, 0],
+ [0, 0, 2, 4, 2, 0],
+ [0, 0, 0, 0, 3, 0],
+ [0, 0, 0, 0, 0, 2],
+ [0, 0, 0, 3, 0, 2],
+ [0, 0, 0, 0, 0, 0]
+];
+
+console.log("********* Dijkstra's Algorithm - Shortest Path ***********");
+
+const dist = dijkstra(graph, 0);
+
+for (i = 0; i < dist.length; i++){
+ console.log(i + '\t\t' + dist[i]);
+}
diff --git a/examples/chapter12/05-Floyd-Warshall.html b/examples/chapter12/05-Floyd-Warshall.html
new file mode 100644
index 00000000..be411625
--- /dev/null
+++ b/examples/chapter12/05-Floyd-Warshall.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter12/05-Floyd-Warshall.js b/examples/chapter12/05-Floyd-Warshall.js
new file mode 100644
index 00000000..18c2dee6
--- /dev/null
+++ b/examples/chapter12/05-Floyd-Warshall.js
@@ -0,0 +1,25 @@
+const { floydWarshall } = PacktDataStructuresAlgorithms;
+
+const INF = Infinity;
+const graph = [
+ [INF, 2, 4, INF, INF, INF],
+ [INF, INF, 2, 4, 2, INF],
+ [INF, INF, INF, INF, 3, INF],
+ [INF, INF, INF, INF, INF, 2],
+ [INF, INF, INF, 3, INF, 2],
+ [INF, INF, INF, INF, INF, INF]
+];
+
+console.log('********* Floyd-Warshall Algorithm - All-Pairs Shortest Path ***********');
+
+dist = floydWarshall(graph);
+
+let s = '';
+for (let i = 0; i < dist.length; ++i) {
+ s = '';
+ for (var j = 0; j < dist.length; ++j) {
+ if (dist[i][j] === INF) s += 'INF ';
+ else s += dist[i][j] + ' ';
+ }
+ console.log(s);
+}
diff --git a/examples/chapter12/06-Prim.html b/examples/chapter12/06-Prim.html
new file mode 100644
index 00000000..7cbb2537
--- /dev/null
+++ b/examples/chapter12/06-Prim.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter12/06-Prim.js b/examples/chapter12/06-Prim.js
new file mode 100644
index 00000000..cb971a4e
--- /dev/null
+++ b/examples/chapter12/06-Prim.js
@@ -0,0 +1,19 @@
+const { prim } = PacktDataStructuresAlgorithms;
+
+const graph = [
+ [0, 2, 4, 0, 0, 0],
+ [2, 0, 2, 4, 2, 0],
+ [4, 2, 0, 0, 3, 0],
+ [0, 4, 0, 0, 3, 2],
+ [0, 2, 3, 3, 0, 2],
+ [0, 0, 0, 2, 2, 0]
+];
+
+console.log("********* Prim's Algorithm - Minimum Spanning Tree ***********");
+
+const parent = prim(graph);
+
+console.log('Edge Weight');
+for (let i = 1; i < graph.length; i++) {
+ console.log(parent[i] + ' - ' + i + ' ' + graph[i][parent[i]]);
+}
diff --git a/examples/chapter12/07-Kruskal.html b/examples/chapter12/07-Kruskal.html
new file mode 100644
index 00000000..89e214fb
--- /dev/null
+++ b/examples/chapter12/07-Kruskal.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter12/07-Kruskal.js b/examples/chapter12/07-Kruskal.js
new file mode 100644
index 00000000..460fbd83
--- /dev/null
+++ b/examples/chapter12/07-Kruskal.js
@@ -0,0 +1,19 @@
+const { kruskal } = PacktDataStructuresAlgorithms;
+
+const graph = [
+ [0, 2, 4, 0, 0, 0],
+ [2, 0, 2, 4, 2, 0],
+ [4, 2, 0, 0, 3, 0],
+ [0, 4, 0, 0, 3, 2],
+ [0, 2, 3, 3, 0, 2],
+ [0, 0, 0, 2, 2, 0]
+];
+
+console.log('********* Kruskal Algorithm - Minimum Spanning Tree ***********');
+
+const parent = kruskal(graph);
+
+console.log('Edge Weight');
+for (i = 1; i < graph.length; i++) {
+ console.log(parent[i] + ' - ' + i + ' ' + graph[i][parent[i]]);
+}
diff --git a/examples/chapter13/01-BubbleSort.html b/examples/chapter13/01-BubbleSort.html
new file mode 100644
index 00000000..88d9a1da
--- /dev/null
+++ b/examples/chapter13/01-BubbleSort.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter13/01-BubbleSort.js b/examples/chapter13/01-BubbleSort.js
new file mode 100644
index 00000000..fff8970e
--- /dev/null
+++ b/examples/chapter13/01-BubbleSort.js
@@ -0,0 +1,12 @@
+const { bubbleSort } = PacktDataStructuresAlgorithms;
+
+function createNonSortedArray(){
+ var array = [];
+ for (let i = 5; i > 0; i--){
+ array.push(i);
+ }
+ return array;
+}
+
+const array = bubbleSort(createNonSortedArray());
+console.log(array);
diff --git a/examples/chapter13/01-BucketSort.html b/examples/chapter13/01-BucketSort.html
new file mode 100644
index 00000000..88d9a1da
--- /dev/null
+++ b/examples/chapter13/01-BucketSort.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter13/01-BucketSort.js b/examples/chapter13/01-BucketSort.js
new file mode 100644
index 00000000..fff8970e
--- /dev/null
+++ b/examples/chapter13/01-BucketSort.js
@@ -0,0 +1,12 @@
+const { bubbleSort } = PacktDataStructuresAlgorithms;
+
+function createNonSortedArray(){
+ var array = [];
+ for (let i = 5; i > 0; i--){
+ array.push(i);
+ }
+ return array;
+}
+
+const array = bubbleSort(createNonSortedArray());
+console.log(array);
diff --git a/examples/chapter13/01-CountingSort.html b/examples/chapter13/01-CountingSort.html
new file mode 100644
index 00000000..88d9a1da
--- /dev/null
+++ b/examples/chapter13/01-CountingSort.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter13/01-CountingSort.js b/examples/chapter13/01-CountingSort.js
new file mode 100644
index 00000000..fff8970e
--- /dev/null
+++ b/examples/chapter13/01-CountingSort.js
@@ -0,0 +1,12 @@
+const { bubbleSort } = PacktDataStructuresAlgorithms;
+
+function createNonSortedArray(){
+ var array = [];
+ for (let i = 5; i > 0; i--){
+ array.push(i);
+ }
+ return array;
+}
+
+const array = bubbleSort(createNonSortedArray());
+console.log(array);
diff --git a/examples/chapter13/01-InsertionSort.html b/examples/chapter13/01-InsertionSort.html
new file mode 100644
index 00000000..88d9a1da
--- /dev/null
+++ b/examples/chapter13/01-InsertionSort.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter13/01-InsertionSort.js b/examples/chapter13/01-InsertionSort.js
new file mode 100644
index 00000000..fff8970e
--- /dev/null
+++ b/examples/chapter13/01-InsertionSort.js
@@ -0,0 +1,12 @@
+const { bubbleSort } = PacktDataStructuresAlgorithms;
+
+function createNonSortedArray(){
+ var array = [];
+ for (let i = 5; i > 0; i--){
+ array.push(i);
+ }
+ return array;
+}
+
+const array = bubbleSort(createNonSortedArray());
+console.log(array);
diff --git a/examples/chapter13/01-MergeSort.html b/examples/chapter13/01-MergeSort.html
new file mode 100644
index 00000000..88d9a1da
--- /dev/null
+++ b/examples/chapter13/01-MergeSort.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter13/01-MergeSort.js b/examples/chapter13/01-MergeSort.js
new file mode 100644
index 00000000..fff8970e
--- /dev/null
+++ b/examples/chapter13/01-MergeSort.js
@@ -0,0 +1,12 @@
+const { bubbleSort } = PacktDataStructuresAlgorithms;
+
+function createNonSortedArray(){
+ var array = [];
+ for (let i = 5; i > 0; i--){
+ array.push(i);
+ }
+ return array;
+}
+
+const array = bubbleSort(createNonSortedArray());
+console.log(array);
diff --git a/examples/chapter13/01-QuickSort.html b/examples/chapter13/01-QuickSort.html
new file mode 100644
index 00000000..88d9a1da
--- /dev/null
+++ b/examples/chapter13/01-QuickSort.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter13/01-QuickSort.js b/examples/chapter13/01-QuickSort.js
new file mode 100644
index 00000000..fff8970e
--- /dev/null
+++ b/examples/chapter13/01-QuickSort.js
@@ -0,0 +1,12 @@
+const { bubbleSort } = PacktDataStructuresAlgorithms;
+
+function createNonSortedArray(){
+ var array = [];
+ for (let i = 5; i > 0; i--){
+ array.push(i);
+ }
+ return array;
+}
+
+const array = bubbleSort(createNonSortedArray());
+console.log(array);
diff --git a/examples/chapter13/01-RadixSort.html b/examples/chapter13/01-RadixSort.html
new file mode 100644
index 00000000..88d9a1da
--- /dev/null
+++ b/examples/chapter13/01-RadixSort.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter13/01-RadixSort.js b/examples/chapter13/01-RadixSort.js
new file mode 100644
index 00000000..fff8970e
--- /dev/null
+++ b/examples/chapter13/01-RadixSort.js
@@ -0,0 +1,12 @@
+const { bubbleSort } = PacktDataStructuresAlgorithms;
+
+function createNonSortedArray(){
+ var array = [];
+ for (let i = 5; i > 0; i--){
+ array.push(i);
+ }
+ return array;
+}
+
+const array = bubbleSort(createNonSortedArray());
+console.log(array);
diff --git a/examples/chapter13/01-SelectionSort.html b/examples/chapter13/01-SelectionSort.html
new file mode 100644
index 00000000..88d9a1da
--- /dev/null
+++ b/examples/chapter13/01-SelectionSort.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter13/01-SelectionSort.js b/examples/chapter13/01-SelectionSort.js
new file mode 100644
index 00000000..fff8970e
--- /dev/null
+++ b/examples/chapter13/01-SelectionSort.js
@@ -0,0 +1,12 @@
+const { bubbleSort } = PacktDataStructuresAlgorithms;
+
+function createNonSortedArray(){
+ var array = [];
+ for (let i = 5; i > 0; i--){
+ array.push(i);
+ }
+ return array;
+}
+
+const array = bubbleSort(createNonSortedArray());
+console.log(array);
diff --git a/examples/chapter14/01-DC-BinarySearch.html b/examples/chapter14/01-DC-BinarySearch.html
new file mode 100644
index 00000000..b110b636
--- /dev/null
+++ b/examples/chapter14/01-DC-BinarySearch.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter14/01-DC-BinarySearch.js b/examples/chapter14/01-DC-BinarySearch.js
new file mode 100644
index 00000000..e69de29b
diff --git a/examples/chapter14/02-MinCoinChangeDP.html b/examples/chapter14/02-MinCoinChangeDP.html
new file mode 100644
index 00000000..4c2027b3
--- /dev/null
+++ b/examples/chapter14/02-MinCoinChangeDP.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter14/02-MinCoinChangeDP.js b/examples/chapter14/02-MinCoinChangeDP.js
new file mode 100644
index 00000000..51ce7ed3
--- /dev/null
+++ b/examples/chapter14/02-MinCoinChangeDP.js
@@ -0,0 +1,5 @@
+const { minCoinChange } = PacktDataStructuresAlgorithms;
+
+console.log(minCoinChange([1, 5, 10], 15)); // [5, 10]
+console.log(minCoinChange([1, 3, 4], 6)); // [3, 3]
+
diff --git a/examples/chapter14/03-MinCoinChangeGreedy.html b/examples/chapter14/03-MinCoinChangeGreedy.html
new file mode 100644
index 00000000..92b0618f
--- /dev/null
+++ b/examples/chapter14/03-MinCoinChangeGreedy.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter14/03-MinCoinChangeGreedy.js b/examples/chapter14/03-MinCoinChangeGreedy.js
new file mode 100644
index 00000000..584603f2
--- /dev/null
+++ b/examples/chapter14/03-MinCoinChangeGreedy.js
@@ -0,0 +1,5 @@
+const { minCoinChangeGreedy } = PacktDataStructuresAlgorithms;
+
+console.log(minCoinChangeGreedy([1, 5, 10], 15)); // [5, 10]
+console.log(minCoinChangeGreedy([1, 3, 4], 6)); // [4, 1, 1]
+
diff --git a/examples/chapter14/04-KnapsackProblemDP.html b/examples/chapter14/04-KnapsackProblemDP.html
new file mode 100644
index 00000000..1f06c983
--- /dev/null
+++ b/examples/chapter14/04-KnapsackProblemDP.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter14/04-KnapsackProblemDP.js b/examples/chapter14/04-KnapsackProblemDP.js
new file mode 100644
index 00000000..e9ff4369
--- /dev/null
+++ b/examples/chapter14/04-KnapsackProblemDP.js
@@ -0,0 +1,8 @@
+const { knapSack } = PacktDataStructuresAlgorithms;
+
+const values = [3,4,5];
+const weights = [2,3,4];
+const capacity = 5;
+const n = values.length;
+
+console.log(knapSack(capacity, weights, values, n));
diff --git a/examples/chapter14/05-KnapSackProblemRecursive.html b/examples/chapter14/05-KnapSackProblemRecursive.html
new file mode 100644
index 00000000..610d7310
--- /dev/null
+++ b/examples/chapter14/05-KnapSackProblemRecursive.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter14/05-KnapSackProblemRecursive.js b/examples/chapter14/05-KnapSackProblemRecursive.js
new file mode 100644
index 00000000..ba76971b
--- /dev/null
+++ b/examples/chapter14/05-KnapSackProblemRecursive.js
@@ -0,0 +1,8 @@
+const { knapSackRecursive } = PacktDataStructuresAlgorithms;
+
+const values = [3,4,5];
+const weights = [2,3,4];
+const capacity = 5;
+const n = values.length;
+
+console.log(knapSackRecursive(capacity, weights, values, n));
diff --git a/examples/chapter14/06-KnapSackProblemGreedy.html b/examples/chapter14/06-KnapSackProblemGreedy.html
new file mode 100644
index 00000000..0d2e3c04
--- /dev/null
+++ b/examples/chapter14/06-KnapSackProblemGreedy.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter14/06-KnapSackProblemGreedy.js b/examples/chapter14/06-KnapSackProblemGreedy.js
new file mode 100644
index 00000000..d9858107
--- /dev/null
+++ b/examples/chapter14/06-KnapSackProblemGreedy.js
@@ -0,0 +1,7 @@
+const { knapSackGreedy } = PacktDataStructuresAlgorithms;
+
+const values = [3,4,5];
+const weights = [2,3,4];
+const capacity = 5;
+
+console.log(knapSackGreedy(capacity, weights, values));
diff --git a/examples/chapter14/07-LongestCommonSubsequenceDP.html b/examples/chapter14/07-LongestCommonSubsequenceDP.html
new file mode 100644
index 00000000..f4b8b8a8
--- /dev/null
+++ b/examples/chapter14/07-LongestCommonSubsequenceDP.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter14/07-LongestCommonSubsequenceDP.js b/examples/chapter14/07-LongestCommonSubsequenceDP.js
new file mode 100644
index 00000000..75e86015
--- /dev/null
+++ b/examples/chapter14/07-LongestCommonSubsequenceDP.js
@@ -0,0 +1,8 @@
+const { lcs } = PacktDataStructuresAlgorithms;
+const { lcsPrint } = PacktDataStructuresAlgorithms;
+
+const wordX = 'acbaed';
+const wordY = 'abcadf';
+
+console.log('lcs', lcs(wordX, wordY));
+console.log('lcsPrint', lcsPrint(wordX, wordY));
diff --git a/examples/chapter14/08-LongestCommonSubsequenceRecursive.html b/examples/chapter14/08-LongestCommonSubsequenceRecursive.html
new file mode 100644
index 00000000..25fa3a3f
--- /dev/null
+++ b/examples/chapter14/08-LongestCommonSubsequenceRecursive.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter14/08-LongestCommonSubsequenceRecursive.js b/examples/chapter14/08-LongestCommonSubsequenceRecursive.js
new file mode 100644
index 00000000..63c2daf7
--- /dev/null
+++ b/examples/chapter14/08-LongestCommonSubsequenceRecursive.js
@@ -0,0 +1,6 @@
+const { lcsRecursive } = PacktDataStructuresAlgorithms;
+
+const wordX = 'acbaed';
+const wordY = 'abcadf';
+
+console.log('lcsRecursive', lcsRecursive(wordX, wordY));
diff --git a/examples/chapter14/09-MatrixChainMultiplicationDP.html b/examples/chapter14/09-MatrixChainMultiplicationDP.html
new file mode 100644
index 00000000..110165d7
--- /dev/null
+++ b/examples/chapter14/09-MatrixChainMultiplicationDP.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter14/09-MatrixChainMultiplicationDP.js b/examples/chapter14/09-MatrixChainMultiplicationDP.js
new file mode 100644
index 00000000..50e92fd5
--- /dev/null
+++ b/examples/chapter14/09-MatrixChainMultiplicationDP.js
@@ -0,0 +1,4 @@
+const { matrixChainOrder } = PacktDataStructuresAlgorithms;
+
+const p = [10, 100, 5, 50, 1];
+console.log(matrixChainOrder(p));
diff --git a/examples/chapter14/10-MatrixChainMultiplicationRecursive.html b/examples/chapter14/10-MatrixChainMultiplicationRecursive.html
new file mode 100644
index 00000000..eba9ceaf
--- /dev/null
+++ b/examples/chapter14/10-MatrixChainMultiplicationRecursive.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter14/10-MatrixChainMultiplicationRecursive.js b/examples/chapter14/10-MatrixChainMultiplicationRecursive.js
new file mode 100644
index 00000000..97245276
--- /dev/null
+++ b/examples/chapter14/10-MatrixChainMultiplicationRecursive.js
@@ -0,0 +1,4 @@
+const { matrixChainOrderGreedy } = PacktDataStructuresAlgorithms;
+
+const p = [10, 100, 5, 50, 1];
+console.log(matrixChainOrderGreedy(p));
diff --git a/examples/chapter14/11-RatInMaze.html b/examples/chapter14/11-RatInMaze.html
new file mode 100644
index 00000000..da73582f
--- /dev/null
+++ b/examples/chapter14/11-RatInMaze.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter14/11-RatInMaze.js b/examples/chapter14/11-RatInMaze.js
new file mode 100644
index 00000000..221e7f19
--- /dev/null
+++ b/examples/chapter14/11-RatInMaze.js
@@ -0,0 +1,10 @@
+const { ratInAMaze } = PacktDataStructuresAlgorithms;
+
+const maze = [
+ [1, 0, 0, 0],
+ [1, 1, 1, 1],
+ [0, 0, 1, 0],
+ [0, 1, 1, 1]
+];
+
+console.log(ratInAMaze(maze));
diff --git a/examples/chapter14/12-SudokuSolver.html b/examples/chapter14/12-SudokuSolver.html
new file mode 100644
index 00000000..b3126287
--- /dev/null
+++ b/examples/chapter14/12-SudokuSolver.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter14/12-SudokuSolver.js b/examples/chapter14/12-SudokuSolver.js
new file mode 100644
index 00000000..bf52ddf3
--- /dev/null
+++ b/examples/chapter14/12-SudokuSolver.js
@@ -0,0 +1,15 @@
+const { sudokuSolver } = PacktDataStructuresAlgorithms;
+
+const sudokuGrid = [
+ [5, 3, 0, 0, 7, 0, 0, 0, 0],
+ [6, 0, 0, 1, 9, 5, 0, 0, 0],
+ [0, 9, 8, 0, 0, 0, 0, 6, 0],
+ [8, 0, 0, 0, 6, 0, 0, 0, 3],
+ [4, 0, 0, 8, 0, 3, 0, 0, 1],
+ [7, 0, 0, 0, 2, 0, 0, 0, 6],
+ [0, 6, 0, 0, 0, 0, 2, 8, 0],
+ [0, 0, 0, 4, 1, 9, 0, 0, 5],
+ [0, 0, 0, 0, 8, 0, 0, 7, 9]
+];
+
+console.log(sudokuSolver(sudokuGrid));
diff --git a/examples/chapter14/13-IntroFunctionalProgramming.html b/examples/chapter14/13-IntroFunctionalProgramming.html
new file mode 100644
index 00000000..0d7cd0ed
--- /dev/null
+++ b/examples/chapter14/13-IntroFunctionalProgramming.html
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter14/13-IntroFunctionalProgramming.js b/examples/chapter14/13-IntroFunctionalProgramming.js
new file mode 100644
index 00000000..692bebd6
--- /dev/null
+++ b/examples/chapter14/13-IntroFunctionalProgramming.js
@@ -0,0 +1,147 @@
+console.log('Using imperative JS');
+
+var printArray = function(array){
+ for (var i=0; i array[i]){
+ minValue = array[i];
+ }
+ }
+
+ return minValue;
+};
+
+console.log(findMinArray([8,6,4,5,9]));
+
+console.log('Finding the min value in an array - functional ES2015');
+const min_ = function(array){
+ return Math.min(...array)
+};
+
+//simplifying using arrow functions
+const min = arr => Math.min(...arr);
+
+console.log(min_([8,6,4,5,9]));
+console.log(min([8,6,4,5,9]));
+
+//concat + reduce
+console.log('merge arrays - imperative');
+
+var mergeArrays_ = function(arrays){
+ var count = arrays.length,
+ newArray = [],
+ k =0;
+ for (var i=0; i [].concat(...arrays);
+console.log(mergeArrays([1, 2, 3], [4, 5], [6]));
+
+console.log('sum values of arrays - imperative');
+var sumValues = function(array){
+ var total = array[0];
+ for (var i=1; i arr.reduce((a, b) => a + b);
+
+console.log(sum([1, 2, 3, 4, 5]));
+
+//map
+var daysOfWeek = [
+ {name: 'Monday', value: 1},
+ {name: 'Tuesday', value: 2},
+ {name: 'Wednesday', value: 7}
+];
+
+var daysOfWeekValues_ = [];
+for (var i = 0; i < daysOfWeek.length; i++) {
+ daysOfWeekValues_.push(daysOfWeek[i].value);
+}
+
+//to
+var daysOfWeekValues = daysOfWeek.map(function(day) {
+ return day.value;
+});
+console.log(daysOfWeekValues);
+
+
+//filter
+var positiveNumbers_ = function(array){
+ var positive = [];
+ for (var i = 0; i < array.length; i++) {
+ if (array[i] >= 0){
+ positive.push(array[i]);
+ }
+ }
+ return positive;
+}
+console.log(positiveNumbers_([-1,1,2,-2]));
+
+var positiveNumbers = function(array){
+ return array.filter(function(num){
+ return num >= 0;
+ })
+};
+console.log(positiveNumbers([-1,1,2,-2]));
diff --git a/examples/chapter15/01-BigONotation.html b/examples/chapter15/01-BigONotation.html
new file mode 100755
index 00000000..f8f26334
--- /dev/null
+++ b/examples/chapter15/01-BigONotation.html
@@ -0,0 +1,10 @@
+
+
+
+
+
+
+
+
+
+
diff --git a/examples/chapter15/01-BigONotation.js b/examples/chapter15/01-BigONotation.js
new file mode 100755
index 00000000..a48bd6ab
--- /dev/null
+++ b/examples/chapter15/01-BigONotation.js
@@ -0,0 +1,65 @@
+//*************** o(1)
+function increment(num){
+ console.log('cost for increment with input ' + num + ' is 1');
+ return ++num;
+}
+
+increment(1);
+increment(2);
+
+//*************** o(n)
+
+function createNonSortedArray(size){
+ var array = [];
+
+ for (var i = size; i> 0; i--){
+ array[i] = i;
+ }
+
+ return array;
+}
+
+function sequentialSearch(array, item){
+ var cost = 0;
+ for (var i=0; i array[j+1]){
+ swap(array, j, j+1);
+ }
+ }
+ }
+ console.log('cost for bubbleSort with input size ' + length + ' is ' + cost);
+}
+
+var array1 = createNonSortedArray(99);
+var array2 = createNonSortedArray(999);
+var array3 = createNonSortedArray(9999);
+bubbleSort(array1);
+bubbleSort(array2);
+bubbleSort(array3);
\ No newline at end of file
diff --git a/examples/chapter15/bigOChart/chart.js b/examples/chapter15/bigOChart/chart.js
new file mode 100755
index 00000000..765794df
--- /dev/null
+++ b/examples/chapter15/bigOChart/chart.js
@@ -0,0 +1,37 @@
+google.load('visualization', '1.0', {'packages':['corechart']});
+google.setOnLoadCallback(drawChart);
+
+function drawChart() {
+
+ var data = new google.visualization.DataTable();
+ data.addColumn('string', 'n');
+ data.addColumn('number', 'O(1)');
+ data.addColumn('number', 'O(log n)');
+ data.addColumn('number', 'O(n)');
+ data.addColumn('number', 'O(n log n)');
+ data.addColumn('number', 'O(n^2)');
+ data.addColumn('number', 'O(2^n)');
+
+ for (var i = 0; i <= 30; i++) {
+ data.addRow([i+'', 1, Math.log(i), i, Math.log(i)*i, Math.pow(i,2), Math.pow(2,i)]);
+ }
+
+ var options = {'title':'Big O Notation Complexity Chart',
+ 'width':700,
+ 'height':600,
+ 'backgroundColor':{stroke:'#CCC',strokeWidth:1},
+ 'curveType':'function',
+ 'hAxis':{
+ title:'Elements (n)',
+ showTextEvery:5
+ },
+ 'vAxis':{
+ title:'Operations',
+ viewWindowMode:'explicit',
+ viewWindow:{min:0,max:450}
+ }
+ };
+
+ var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
+ chart.draw(data, options);
+}
\ No newline at end of file
diff --git a/examples/chapter15/bigOChart/index.html b/examples/chapter15/bigOChart/index.html
new file mode 100755
index 00000000..31118263
--- /dev/null
+++ b/examples/chapter15/bigOChart/index.html
@@ -0,0 +1,14 @@
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/examples/examples-screenshot.png b/examples/examples-screenshot.png
new file mode 100644
index 00000000..a264d142
Binary files /dev/null and b/examples/examples-screenshot.png differ
diff --git a/examples/index.html b/examples/index.html
index 81003bd3..7e3e6629 100644
--- a/examples/index.html
+++ b/examples/index.html
@@ -3,7 +3,7 @@
- DataStructures and Algorithms with JavaScript
+ Data Structures and Algorithms with JavaScript
@@ -13,12 +13,15 @@
.mdl-layout__content {
padding: 10px;
}
+
.mdl-layout__drawer {
width: 290px;
}
+
.iframe-padding {
padding-left: 295px;
}
+
.mdl-layout__drawer .mdl-navigation .mdl-navigation__link {
color: rgb(103, 58, 183);
}
@@ -35,83 +38,255 @@
Please open the Developer Tools Console to see the output
-
+
+
+
-