GithubHelp home page GithubHelp logo

apress / js-data-structures-and-algorithms Goto Github PK

View Code? Open in Web Editor NEW
319.0 16.0 166.0 126 KB

Source code for 'JavaScript Data Structures and Algorithms' by Sammie Bae

Home Page: http://www.apress.com/9781484239872

License: Other

JavaScript 93.67% Python 0.30% HTML 6.04%

js-data-structures-and-algorithms's Introduction

Apress Source Code

This repository accompanies JavaScript Data Structures and Algorithms by Sammie Bae (Apress, 2019).

Cover image

Download the files as a zip using the green button, or clone the repository to your machine using Git.

Releases

Release v1.0 corresponds to the code in the published book, without corrections or updates.

Contributions

See the file Contributing.md for more information on how you can contribute to this repository.

js-data-structures-and-algorithms's People

Contributors

mingyujeon avatar sherifghoz 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

js-data-structures-and-algorithms's Issues

min heap different implementation on bubbleDown

In chapter 16 the MinHeap.bubbleDown() has two different implementations. When first shown it has

while (this.leftChild(index) && this.leftChild(index) < this.items[index])

But in the full implementation it also checks rightChild which the original does not

while (this.leftChild(index) && this.leftChild(index) < this.items[index] || this.rightChild(index) < this.items[index])

I also wonder if rightChild needs to be checked like leftChild is

Chapter 2: JavaScript: Unique Parts

Global Declaration: Global Scope

"In JavaScript, variables can be declared without using any operators"

The above statement is correct saying only about the "non-strict" mode. In the strict mode "use strict" would cause a ReferenceError.

Declaration with let: Block Scope

function scope3(print) {
    if(print) {
        let insideIf = '12';
    }
    console.log(insideIf)
}

scope3(true);

In the snippet above there would be a ReferenceError because as the title says let (and const) are Block Scoped variables.

Truthy/Falsy Check

"Here are commonly used expressions that evaluate to true:"

  • true
  • Any number other than 0
  • Non-empty strings
  • Non-empty object

Objects in JavaScript even if it's empty will always give the truthy result. Hence the last statement in the list is not correct.
I suppose it would be better to say:object (any object)

[Chapter 15] Bug - BST remove method isn't performing correctly in removing nodes.

BinarySearchTree.prototype.remove fails to remove parent node in the following example.

example

var bst1 = new BinarySearchTree();
bst1.insert(1);
bst1.insert(3);
bst1.insert(2);
console.log(bst1.findNode(3)); // true
console.log(bst1.findNode(5)); // false

bst1.remove(1);
bst1.remove(3);
bst1.remove(2);
console.log(bst1.findNode(1));  // true <-- should be false

Chapter 5: JavaScript Arrays

Spread Operator

It says:

Both the Math.max and Math.min functions take an unlimited number of parameters, so you can use the spread operator for the following operations.
To find the maximum in an array, use this:

and the snippet with a type goes below:

var array1 = [1,2,3,4,5];
Math.max(array1); // 5

var array2 = [3,2,-123,2132,12]; 
Math.min(array2); // -123

I suppose it should be:

var array1 = [1,2,3,4,5];
Math.max(...array1); // 5

var array2 = [3,2,-123,2132,12]; 
Math.min(...array2); // -123

Exercises

FIND TWO ARRAY ELEMENTS IN AN ARRAY THAT ADD UP TO A NUMBER

problem: Given the array arr, find and return two indices of the array that add up to weight or return -1 if there is no combination that adds up to weight.

In the code snippet below there is a typo: return [i, hashtable[weight - currentElement]];

function findSumBetter(arr, weight) {
    var hashtable = {};

    for (var i = 0, arrLength = arr.length; i < arrLength; i++) {
        var currentElement = arr[i],
            difference = weight - currentElement;

        // check the right one already exists
        if (hashtable[currentElement] != undefined) {
            return [i, hashtable[weight - currentElement]];
        } else {
            // store index
            hashtable[difference] = i;
        }
    }
    return -1;
}

I suppose it should be like this: return [i, hashtable[currentElement]];. You should return the index of the previously found match from the hashtable

chapter 16 minHeap misspelled method

if (this.rightChild(index) && this.rightChild(index) < this.items[smallerIndex]) {
    // if right is smaller, right swaps
    smallerIndex = this.rightChildrenIndex(index);
}

rightChildrenIndex doesn't exist I think you meant rightChildIndex

EDIT: I realized this is a mistake on my part. rightChildrenIndex is defined and rightChildIndex is not. It was a little confusing when typing up the implementation because the left child is called leftChildIndex and leftChild and the right side node getter is rightChild so it made sense for the right side index getter to be rightChildIndex

[Chapter 1] EXERCISE 5, infinite loop

function someFunction(n) {
for (var i = 0; i < n; i * 2) {
console.log(n);
}
}
'answer: O(log_2n)';

i * 2, when i=0, always gives 0, so this is an endless loop, not O(log_2n) O(log_2n)

[Chapter12] TypeError: stack1.push is not a function

Push function must be defined in advance

$ node Chapter12-StackQueue.js 
Stack { array: [] }
/Users/Documents/js-data-structures-and-algorithms/Chapter12-StackQueue.js:22
stack1.push(10);
       ^

TypeError: stack1.push is not a function
    at Object.<anonymous> (/Users/Documents/js-data-structures-and-algorithms/Chapter12-StackQueue.js:22:8)
    at Module._compile (internal/modules/cjs/loader.js:956:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
    at Module.load (internal/modules/cjs/loader.js:812:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
    at internal/main/run_main_module.js:17:11

Chapter 5 two array element exercise problem

Hello, I don't understand why the inner loop has to start at i+1 what if the number we're trying to find (weight) is 8?
Then 4+4 would work. Thus we should loop through everything again in the inner loop no? More details are required.

Chapter 3. Prime Factorization. Complexity.

function primeFactors(n){
    // Print the number of 2s that divide n
    while (n % 2 == 0) {
        console.log(2);
        n = n/2;
    }

    // n must be odd at this point. So we can skip one element
    // (Note i = i +2)
    for (var i = 3; i * i <= n; i = i + 2) {
        // While i divides n, print i and divide n
        while (n % i == 0) {
            console.log(i);
            n = n / i;
        }
    }
    // This condition is to handle the case when n is a prime number
    // greater than 2
    if (n > 2) {
        console.log(n);
    }
}

This function has Big-O =
image
In your book you put it sqrt(x) only, but inside of the for loop there is another loop while, that has Big-O = log (x). Am I correct?

[Chapter 15], AVL Trees, Single Rotation, RotateLeft and RotateRight write up and functions

The write-up under Rotate Left seems not to be talking about Rotate Left.
Then, at Rotate Right, the write-up is the same. One can infer that at least one of the two write-ups is wrong.

Likewise, the methods that follow are not clear as they do not follow the preceding write ups. Perhaps for each line, if you printed out the output, one can see how everything comes together

For example

var valueBefore = this.value; // please print out
var rightBefore = this.right; // please print out

[Chapter 5] Inaccurate implementation of the slice function

Earlier in chapter 5 about Arrays, it was mentioned that passing no argument to the slice function returns a copy of the array. So if we tried to do:

var array = [1, 2, 3];
console.log(array.slice() === array); // this will return false

While in your example here you are returning the array itself so:

var array = [1, 2, 3];
console.log(arraySlice(array) === array); // this will return true

I think it should be replaced by:

    if (!beginIndex && !endIndex) {
        return Array.from(array);
    }

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.