GithubHelp home page GithubHelp logo

packtpublishing / learning-javascript-data-structures-and-algorithms-third-edition Goto Github PK

View Code? Open in Web Editor NEW
1.0K 26.0 428.0 777 KB

Learning JavaScript Data Structures and Algorithms (Third Edition), published by Packt

License: MIT License

HTML 5.94% JavaScript 52.16% TypeScript 41.90%

learning-javascript-data-structures-and-algorithms-third-edition's Introduction

Learning JavaScript Data Structures and Algorithms - Third Edition

This is the code repository for Learning JavaScript Data Structures and Algorithms - Third Edition, published by Packt. It contains all the supporting project files necessary to work through the book from start to finish.

About the Book

JavaScript is one of the most popular programming language nowadays. It is known as the internet language due the fact that the browser understands JavaScript natively, without installing any plugins in it. JavaScript has grown so much that is no longer just a frontend language; it is also present now on the server (NodeJS), database (MongoDB), and mobile devices and is also used in embedded and Internet of Things (IoT) devices.

Learning data structures is very important for any technology professional. Working as a developer means you are able to solve problems with the help of programming languages, and data structures are an indispensable piece of the solutions we need to create to solve these problems. Choosing a wrong data structure can also have an impact on the performance of the program we are writing. That is why, it's important to get to know different data structures and how to apply them properly.

Algorithms are the state of art of computer science. There are so many ways of solving the same problem, and some approaches are better than the others. That is why, it's also very important to know the most famous algorithms. This book was written for beginners who want to learn data structures and algorithms and also for those who are already familiar with data structures and algorithms, but want to learn it using JavaScript.

Happy coding!

Instructions and Navigation

All of the code is organized into folders. Each folder starts with a number followed by the application name. For example, Chapter02.

The code will look like the following:

class Stack {
  constructor() {
    this.items = []; // {1}
  }
}

Although this book provides a brief introduction on JavaScript in its first chapter, you will need a basic knowledge of JavaScript and programming logic.

To test the code examples provided by this book, you will need a code editor (such as Atom or Visual Studio Code) so that you can read the code and also a browser (Chrome, Firefox, or Edge).

Also, remember to open the developer tools of the browser so that you can see what is being outputted in the browser's console.

Related Products

learning-javascript-data-structures-and-algorithms-third-edition's People

Contributors

dev-zuo avatar hefeng6500 avatar jabacchetta avatar jiangdonghan avatar jucttr avatar loiane avatar packt-itservice avatar packt-vaibhavd avatar yamiqu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

learning-javascript-data-structures-and-algorithms-third-edition's Issues

Test to fail

I execute the NPM test run on the terminal and prompt the properties attribute for definition. What is the case? Is it a source problem? (in the case of chapter 4 stack)

"TypeError: Cannot read property 'properties' of undefined" webpack-cli error fix

I ran npm run dev and got this error:

...
> webpack --env build

/Learning-JavaScript-Data-Structures-and-Algorithms-Third-Edition/LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/node_modules/webpack-cli/bin/config-yargs.js:136
				describe: optionsSchema.definitions.output.properties.path.description,
				                                           ^

TypeError: Cannot read property 'properties' of undefined
    at module.exports (/Learning-JavaScript-Data-Structures-and-Algorithms-Third-Edition/LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/node_modules/webpack-cli/bin/config-yargs.js:136:48)
    at /Learning-JavaScript-Data-Structures-and-Algorithms-Third-Edition/LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/node_modules/webpack-cli/bin/webpack.js:66:27
    at Object.<anonymous> (/Learning-JavaScript-Data-Structures-and-Algorithms-Third-Edition/LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/node_modules/webpack-cli/bin/webpack.js:521:3)
    at Module._compile (internal/modules/cjs/loader.js:702:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
    at Module.load (internal/modules/cjs/loader.js:612:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
    at Function.Module._load (internal/modules/cjs/loader.js:543:3)
    at Module.require (internal/modules/cjs/loader.js:650:17)
    at require (internal/modules/cjs/helpers.js:20:18)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] webpack: `webpack --env build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] webpack script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/cfree/.npm/_logs/2018-12-15T18_04_10_202Z-debug.log
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command

I updated to 3.x of webpack-cli, which resulted in this error:

...
> webpack --env build

/Learning-JavaScript-Data-Structures-and-Algorithms-Third-Edition/LearningJavaScriptDataStructuresandAlgorithmsThirdEdition_Code/node_modules/webpack-cli/bin/cli.js:244
				throw err;
				^

Error: Cannot find module 'uglifyjs-webpack-plugin'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:594:15)
    ...

I ran npm install -D uglifyjs-webpack-plugin and it worked! 🎉

kruskal output different from prim

i think the kruskal test should be same as prim does,
but it comes to
********* Kruskal Algorithm - Minimum Spanning Tree ***********
Edge Weight
0 - 1 2
1 - 2 2
1 - 3 4 // may be wrong here
1 - 4 2
3 - 5 2

verifyRemoveSideEffect possible simplification

Hi Loiane!

Thank you for this excellent book! I enjoy reading it a lot!

The question is regarding hash-table-linear-probing.js:

Can we simplify the function verifyRemoveSideEffect?

It's original version in the file is:

 verifyRemoveSideEffect(key, removedPosition) {
    const hash = this.hashCode(key);
    let index = removedPosition + 1;
    while (this.table[index] != null) {
      const posHash = this.hashCode(this.table[index].key);
      if (posHash <= hash || posHash <= removedPosition) { // QUESTION IS ABOUT THIS LINE
        this.table[removedPosition] = this.table[index];
        delete this.table[index];
        removedPosition = index;
      }
      index++;
    }
  }

However, since hash is always less than removedPosition, can we just check for posHash <= removedPosition instead of posHash <= hash || posHash <= removedPosition?

This way the function would look like:

  verifyRemoveSideEffect(key, removedPosition) {
    const hash = this.hashCode(key);
    let index = removedPosition + 1;
    while (this.table[index] != null) {
      const posHash = this.hashCode(this.table[index].key);
      if (posHash <= removedPosition) { // THIS LINE IS CHANGED
        this.table[removedPosition] = this.table[index];
        delete this.table[index];
        removedPosition = index;
      }
      index++;
    }
  }

I tried this change and it passes all your tests.

Also, this way we do not need const hash = this.hashCode(key); and subsequently key param of the function:

verifyRemoveSideEffect(removedPosition) { // THIS LINE IS CHANGED
    // const hash = this.hashCode(key); - THIS LINE IS REMOVED
    let index = removedPosition + 1;
    while (this.table[index] != null) {
      const posHash = this.hashCode(this.table[index].key);
      if (posHash <= removedPosition) { // THIS LINE IS CHANGED
        this.table[removedPosition] = this.table[index];
        delete this.table[index];
        removedPosition = index;
      }
      index++;
    }
  }

(of course we would need to change the remove function as well, so it calls this.verifyRemoveSideEffect(position); instead of this.verifyRemoveSideEffect(key, position);

With those changes it also passes all tests.

Does it make sense or there are some edge cases that I do not see and they need this check for both possibilities (posHash <= hash || posHash <= removedPosition)?

Thank you again for sharing your wisdom :)

AVL-tree removeNode func is not correct

removeNode(node, key) {
node = super.removeNode(node, key); // {1}
if (node == null) {
return node;
}
// verify if tree is balanced
const balanceFactor = this.getBalanceFactor(node);
if (balanceFactor === BalanceFactor.UNBALANCED_LEFT) {
// Left left case
if (
this.getBalanceFactor(node.left) === BalanceFactor.BALANCED ||
this.getBalanceFactor(node.left) === BalanceFactor.SLIGHTLY_UNBALANCED_LEFT
) {
return this.rotationLL(node);
}
// Left right case
if (this.getBalanceFactor(node.left) === BalanceFactor.SLIGHTLY_UNBALANCED_RIGHT) {
return this.rotationLR(node.left);
}
}
if (balanceFactor === BalanceFactor.UNBALANCED_RIGHT) {
// Right right case
if (
this.getBalanceFactor(node.right) === BalanceFactor.BALANCED ||
this.getBalanceFactor(node.right) === BalanceFactor.SLIGHTLY_UNBALANCED_RIGHT
) {
return this.rotationRR(node);
}
// Right left case
if (this.getBalanceFactor(node.right) === BalanceFactor.SLIGHTLY_UNBALANCED_LEFT) {
return this.rotationRL(node.right);
}
}
return node;
}

     // Left right case 
     if (this.getBalanceFactor(node.left) === BalanceFactor.SLIGHTLY_UNBALANCED_RIGHT) { 
       return this.rotationLR(node.left); 
     } 

I think this code is not correct;
eg:

                      10
                 ↙          ↘  
            6                     。。。
        ↙       ↘         
      3             8       
         ↘       
             4
  1. remove node key = 8;
  2. node key=6 is not balanced;
  3. condition accord with balanceFactor === BalanceFactor.UNBALANCED_RIGHT ;
  4. run return this.rotationLR(node.left);, it's will be throw error;
in fact, this condition need return this.rotationLR(node);

also, Right-left-case has same error

 // Right left case
  if (this.getBalanceFactor(node.right) === BalanceFactor.SLIGHTLY_UNBALANCED_LEFT) {
       // need change code
        return this.rotationRL(node.right);
        ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
        return this.rotationRL(node);
   }

kruskal Func Seems wrong

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], 
]

kruskal(graph) // parent return [ <1 empty item>, 0, 1, 1, 1, 3 ]

parent should be [<1 empty item>, 0, 1, 5, 1, 3]

Calculation of the number of elements in bucket sorting

Hello, I am learning bucket sorting. I don't understand the logic of a piece of code very well; or it is about the principle of calculating the number of elements in the bucket sorting bucket. code show as below:

....
const buketCount: number =
    Math.floor((maxValue - minxValue) / bucketsSize) + 1;   // --> Why is it necessary to add 1 when calculating the number of elements in the bucket?  I do not really understand.
  const bukets: number[][] = [];
  for (let i: number = 0; i < buketCount; i++) {
    bukets[i] = [];
  }
....

If you see, I hope to help me explain, thank you very much

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.