Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
83% found this document useful (6 votes)
28K views

Learning Javascript Data Structures and Algorithms

Learning JS Data Structures and Algorithms

Uploaded by

Lemuel Uhuru
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
83% found this document useful (6 votes)
28K views

Learning Javascript Data Structures and Algorithms

Learning JS Data Structures and Algorithms

Uploaded by

Lemuel Uhuru
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 307

Published by Packt Publishing Ltd.

Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.

www.packtpub.com
http://loiane.com

https://www.facebook.c
om/loianegroner @loiane https://github.com/loia
ne
@tessaract
http://tessaract.info
www.PacktPub.com

customercare@packtpub.com

www.PacktPub.com

https://www2.packtpub.com/books/subscription/packtlib
2
Chapter 1

Chapter 2

Chapter 3
Chapter 4

Chapter 5

Chapter 6

Chapter 7

Chapter 8

Chapter 9

Chapter 10

Chapter 11

Chapter 12
https://www.google.com/chrome/browser/
https://www.mozilla.org/en-US/firefox/new/

https://www.apachefriends.org

http://nodejs.org/
http-server

isEmpty
function Stack() {
//properties and methods go here
}

class Stack {

push(element){
this.items.push(element);
}
//other methods
}

feedback@packtpub.com
www.packtpub.com/authors

http://www.
packtpub.com http://www.packtpu
b.com/support

https://github.com/loiane/
javascript-datastructures-algorithms
https://github.com/PacktPublishing/
https://www.packtpub.com/sites/default/files/
downloads/LearningJavaScriptDataStructuresandAlgorithmsSecondEdition_Co
lorImages.pdf

http://www.packtpub.com/submit-errata

https://www.packtpub.com/books/con
tent/support

copyright@packtpub.com

questions@packtpub.com

https://github.com
http://goo.gl/ZFx6m
g

https://www.npmjs.org/
https://getfire
bug.com/
https://www.apachefriends.org
htdocs

htdocs
http://nodejs.org/

http-server

http-server
http-server

https://github.com/loiane/javascript-data
structures-algorithms
https://github.com/Pack
tPublishing/

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<script>
alert('Hello, World!');
</script>
</body>
</html>

script
script

01-
HelloWorld.js

alert('Hello, World!');

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<script src="01-HelloWorld.js">
</script>
</body>
</html>

include
head
body
int num = 1; var

var num = 1; //{1}


num = 3; //{2}
var price = 1.5; //{3}
var name = 'Packt'; //{4}
var trueValue = true; //{5}
var nullVar = null; //{6}
var und; //{7}

{1}
var

{2}

{3}
{4} {5}
{6} null {7}
null undefined

console.log("num: "+ num);


console.log("name: "+ name);
console.log("trueValue: "+ trueValue);
console.log("price: "+ price);
console.log("nullVar: "+ nullVar);
console.log("und: "+ und);

console.log

alert('My text here')

console.log('My text here')

document.write('My text here')

console.log
console.log("num: "+ num) console.log("num: ", num)

var myVariable = 'global';


myOtherVariable = 'global';

function myFunction(){
var myVariable = 'local';
return myVariable;
}

function myOtherFunction(){
myOtherVariable = 'local';
return myOtherVariable;
}

console.log(myVariable); //{1}
console.log(myFunction()); //{2}

console.log(myOtherVariable); //{3}
console.log(myOtherFunction()); //{4}
console.log(myOtherVariable); //{5}

{1} global global


{2} local myVariable
myFunction
myFunction
{3} global
myOtherVariable
{4} local myOtherFunction
myOtherVariable
local var
{5} local
myOtherFunction

var num = 0; // {1}


num = num + 2;
num = num * 3;
num = num / 2;
num++;
num--;

num += 1; // {2}
num -= 2;
num *= 3;
num /= 2;
num %= 3;

console.log('num == 1 : ' + (num == 1)); // {3}


console.log('num === 1 : ' + (num === 1));
console.log('num != 1 : ' + (num != 1));
console.log('num > 1 : ' + (num > 1));
console.log('num < 1 : ' + (num < 1));
console.log('num >= 1 : ' + (num >= 1));
console.log('num <= 1 : ' + (num <= 1));

console.log('true && false : ' + (true && false)); // {4}


console.log('true || false : ' + (true || false));
console.log('!true : ' + (!true));

{1}

+
-
*
/
%
++
--

{2}

=
+=
-=
*=
/=
%=
{3}

==
===
!=
>
>=
<
<=

{4}

&&
||
!

console.log('5 & 1:', (5 & 1));


console.log('5 | 1:', (5 | 1));
console.log('~ 5:', (~5));
console.log('5 ^ 1:', (5 ^ 1));
console.log('5 << 1:', (5 << 1));
console.log('5 >> 1:', (5 >> 1));

&
|
~
^
<<
>>

typeof

console.log('typeof num:', typeof num);


console.log('typeof Packt:', typeof 'Packt');
console.log('typeof true:', typeof true);
console.log('typeof [1,2,3]:', typeof [1,2,3]);
console.log('typeof {name:John}:', typeof {name:'John'});

delete

var myObj = {name: 'John', age: 21};


delete myObj.age;
console.log(myObj); //outputs Object {name: "John"}

true false
false
false
true false
false +0 -0 NaN true
false
true
true

function testTruthy(val){
return val  console.log('truthy') : console.log('falsy');
}

testTruthy(true); //true
testTruthy(false); //false
testTruthy(new Boolean(false)); //true (object is always true)

testTruthy(''); //false
testTruthy('Packt'); //true
testTruthy(new String('')); //true (object is always true)

testTruthy(1); //true
testTruthy(-1); //true
testTruthy(NaN); //false
testTruthy(new Number(NaN)); //true (object is always true)

testTruthy({}); //true (object is always true)

var obj = {name:'John'};


testTruthy(obj); //true
testTruthy(obj.name); //true
testTruthy(obj.age); //false (age does not exist)
==
==

true
true
x == toNumber(y)
toNumber(x) == y
toNumber(x) == y
x == toNumber(y)
x == toPrimitive(y)
toPrimitive(x) == y

equals

toNumber toPrimitive

toNumber

NaN
+0
true 1 false +0
NaN

toNumber(toPrimitive(value))

toPrimitive

valueOf
toString

true

console.log('packt'  true : false);

console.log('packt' == true);

false

toNumber packt == 1
toNumber
NaN NaN == 1

console.log('packt' == false);

false

toNumber packt == 0
toNumber
NaN NaN == 0
===

NaN true
true
true false true
true

console.log('packt' === true); //false

console.log('packt' === 'packt'); //true

var person1 = {name:'John'};


var person2 = {name:'John'};
console.log(person1 === person2); //false, different objects

if...else switch while


do...while for

if...else
if...else

if true

var num = 1;
if (num === 1) {
console.log("num is equal to 1");
}

if...else
true false else

var num = 0;
if (num === 1) {
console.log("num is equal to 1");
} else {
console.log("num is not equal to 1, the value of num is " + num);
}

if...else
if...else

if (num === 1){


num--;
} else {
num++;
}

(num === 1)  num-- : num++;

if...else

var month = 5;
if (month === 1) {
console.log("January");
} else if (month === 2){
console.log("February");
} else if (month === 3){
console.log("March");
} else {
console.log("Month is not January, February or March");
}

switch
switch

var month = 5;
switch(month) {
case 1:
console.log("January");
break;
case 2:
console.log("February");
break;
case 3:
console.log("March");
break;
default:
console.log("Month is not January, February or March");
}

switch case break


case switch
case break switch
case
break case
default true
case break

for

for

for

for i
i 10 i

for (var i=0; i<10; i++) {


console.log(i);
}

while
while
i i
i
var i = 0;
while(i<10)
{
console.log(i);
i++;
}

do...while while
while
do...while
do...while

var i = 0;
do {
console.log(i);
i++;
} while (i<10)

return

function sayHello() {
console.log('Hello!');
}

sayHello();

function output(text) {
console.log(text);
}
output('Hello!');

output('Hello!', 'Other text');

function sum(num1, num2) {


return num1 + num2;
}

var result = sum(1,2);


output(result);

var obj = new Object();

var obj = {};

obj = {
name: {
first: 'Gandalf',
last: 'the Grey'
},
address: 'Middle Earth'
};
Stack Set LinkedList
Dictionary Tree Graph

function Book(title, pages, isbn){


this.title = title;
this.pages = pages;
this.isbn = isbn;
}

var book = new Book('title', 'pag', 'isbn');

console.log(book.title); //outputs the book title


book.title = 'new title'; //updates the value of the book title
console.log(book.title); //outputs the updated value

Book.prototype.printTitle = function(){
console.log(this.title);
};
book.printTitle();

function Book(title, pages, isbn){


this.title = title;
this.pages = pages;
this.isbn = isbn;
this.printIsbn = function(){
console.log(this.isbn);
}
}
book.printIsbn();
printTitle

https://develop
er.chrome.com/devtools/docs/javascript-debugging

http://www.aptana.com/

http://www.jetbrains.com/webstorm/

http://www.sublimetext.com/

https
://atom.io/
http://kangax.github.io/compat-table/es6/
http://kangax.github.io/compat-table/es7/
chrome://flags

https://babeljs.io
https://babeljs.io/docs/setup/
https://babeljs.io/repl/

let const

=>
var framework = 'Angular';
var framework = 'React';
console.log(framework);

React

let var
var let

let language = 'JavaScript!'; //{1}


let language = 'Ruby!'; // {2} - throws error
console.log(language);

{2}
{1} let

https://goo.gl/he0udZ

let
https://goo.gl/NbsVvg

let movie = 'Lord of the Rings'; //{1}


//var movie = 'Batman v Superman'; //throws error, variable movie already
declared

function starWarsFan(){
let movie = 'Star Wars'; //{2}
return movie;
}

function marvelFan(){
movie = 'The Avengers'; //{3}
return movie;
}

function blizzardFan(){
let isFan = true;
let phrase = 'Warcraft'; //{4}
console.log('Before if: ' + phrase);
if (isFan){
let phrase = 'initial text'; //{5}
phrase = 'For the Horde!'; //{6}
console.log('Inside if: ' + phrase);
}
phrase = 'For the Alliance!'; //{7}
console.log('After if: ' + phrase);
}

console.log(movie); //{8}
console.log(starWarsFan()); //{9}
console.log(marvelFan()); //{10}
console.log(movie); //{11}
blizzardFan(); //{12}

{1} movie Lord of the Rings


{8}

{9} starWarsFan
movie {2}
Star Wars {2}
{10} marvelFan
movie {3}
{1}
The Avengers {10} {11}

blizzardFan {12}
phrase {4}
{5} phrase
if
{6} phrase if
{5}
{7} phrase
if {4}

const let
const

const PI = 3.141593;
PI = 3.0; //throws error
console.log(PI);

PI var PI let PI
PI

https://goo.gl/4xuWrC
var book = {
name: 'Learning JavaScript DataStructures and Algorithms'
};

console.log

`
${}
book.name

\n
and
this is a new line

https://goo.gl/PTqnwO

var circleArea = function circleArea(r) {


var PI = 3.14;
var area = PI * r * r;
return area;
};
console.log(circleArea(2));

let circleArea = (r) => { //{1}


const PI = 3.14;
let area = PI * r * r;
return area;
}
console.log(circleArea(2));

{1}
=>

return

let circleArea2 = (r) => 3.14 * r * r;


console.log(circleArea2(2));

https://goo.gl/CigniJ

function sum (x = 1, y = 2, z = 3) {
return x + y + z
};
console.log(sum(4,2)); //outputs 9

z 4 + 2 + 3 ==
9

function sum (x, y, z) {


if (x === undefined)
x = 1;
if (y === undefined)
y = 2;
if (z === undefined)
z = 3;
return x + y + z;
};

https://goo.gl/2MiJ59

apply()
...
x y z

var params = [3, 4, 5];


console.log(sum(...params));

var params = [3, 4, 5];


console.log(sum.apply(undefined, params));

...
arguments

function restParamaterFunction (x, y, ...a) {


return (x + y) * a.length;
}
console.log(restParamaterFunction(1, 2, "hello", true, 7)); //outputs 9;

function restParamaterFunction (x, y) {


var a = Array.prototype.slice.call(arguments, 2);
return (x + y) * a.length;
};

https://goo.gl/8equk
5 https://goo.gl/L
aJZqU
var [x, y] = ['a', 'b'];

var x = 'a';
var y = 'b';

[x, y] = [y, x];

var temp = x;
x = y;
y = temp;

var [x, y] = ['a', 'b'];


var obj = { x, y };
console.log(obj); // { x: "a", y: "b" }

var x = 'a';
var y = 'b';
var obj2 = { x: x, y: y };
console.log(obj2); // { x: "a", y: "b" }

var hello = {
name : 'abcdef',
printHello(){
console.log('Hello');
}
}
console.log(hello.printHello());

var hello = {
name: 'abcdef',
printHello: function printHello() {
console.log('Hello');
}
};

https://goo.gl/VsLecp
https://goo.gl/EyFAII
https://goo.gl/DKU2PN

Book

function Book(title, pages, isbn){ //{1}


this.title = title;
this.pages = pages;
this.isbn = isbn;
}
Book.prototype.printTitle = function(){
console.log(this.title);
};

class Book { //{2}


constructor (title, pages, isbn) {
this.title = title;
this.pages = pages;
this.isbn = isbn;
}
printIsbn(){
console.log(this.isbn);
}
}
class constructor
printIsbn
Book {1} {2}

let book = new Book('title', 'pag', 'isbn');


console.log(book.title); //outputs the book title
book.title = 'new title'; //update the value of the book title
console.log(book.title); //outputs the book title

https://goo.gl/UhK1n4

class ITBook extends Book { //{1}


constructor (title, pages, isbn, technology) {
super(title, pages, isbn); //{2}
this.technology = technology;
}
printTechnology(){
console.log(this.technology);
}
}

let jsBook = new ITBook('Learning JS Algorithms', '200', '1234567890',


'JavaScript');
console.log(jsBook.title);
console.log(jsBook.printTechnology());

extends {1}
constructor
super {2}
https://goo.gl/hgQvo9

get set

class Person {
constructor (name) {
this._name = name; //{1}
}
get name() { //{2}
return this._name;
}
set name(value) { //{3}
this._name = value;
}
}

let lotrChar = new Person('Frodo');


console.log(lotrChar.name); //{4}
lotrChar.name = 'Gandalf'; //{5}
console.log(lotrChar.name);
lotrChar._name = 'Sam'; //{6}
console.log(lotrChar.name);

get set get set


{2} {3}

{1}

get set
{4} {5}

_name

https://goo.gl/SMRYsv
http://www.ecma-international.org/ecma-26
2/6.0/

Array.prototype.includes

Object.values Object.entries

Math.pow(2, 3) 2 ** 3 **

Array.prototype.includes
Chapter 2

https://tc39.github.io/ecma2
62/
https://github.com/loiane/javascript-datastructures-algo
rithms


var averageTempJan = 31.9;


var averageTempFeb = 35.3;
var averageTempMar = 42.4;
var averageTempApr = 52;
var averageTempMay = 60.8;

var averageTemp = [];


averageTemp[0] = 31.9;
averageTemp[1] = 35.3;
averageTemp[2] = 42.4;
averageTemp[3] = 52;
averageTemp[4] = 60.8;

averageTemp

var daysOfWeek = new Array(); //{1}


var daysOfWeek = new Array(7); //{2}
var daysOfWeek = new Array('Sunday', 'Monday', 'Tuesday', 'Wednes day',
'Thursday', 'Friday', 'Saturday'); //{3}

new {1}
new
{2}
{3}

new
[]

var daysOfWeek = [];

var daysOfWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday',


'Friday', 'Saturday'];

length
7
daysOfWeek

for (var i=0; i<daysOfWeek.length; i++){


console.log(daysOfWeek[i]);
}

var fibonacci = []; //{1}


fibonacci[1] = 1; //{2}
fibonacci[2] = 1; //{3}

for(var i = 3; i < 20; i++){


fibonacci[i] = fibonacci[i-1] + fibonacci[i-2]; ////{4}
}

for(var i = 1; i<fibonacci.length; i++){ //{5}


console.log(fibonacci[i]); //{6}
}

{1}
{2} {3}

{4}
{6}
{5}

console.log {5}
{6} console.log(fibonacci)
console.log
20

var numbers = [0,1,2,3,4,5,6,7,8,9];

numbers[numbers.length] = 10;

push
push

numbers.push(11);
numbers.push(12, 13);

numbers

+ 1
-1
for (var i=numbers.length; i>=0; i--){
numbers[i] = numbers[i-1];
}
numbers[0] = -1;

array unshift

numbers.unshift(-2);
numbers.unshift(-4, -3);

unshift -2 -3 -4
numbers

pop

numbers.pop();
push pop stack

for (var i=0; i<numbers.length; i++){


numbers[i] = numbers[i+1];
}

i+1

numbers.length -1
shift

numbers.shift();

shift unshift queue


Chapter 4

splice

numbers.splice(5,3);

delete
remove
numbers[0]
undefined numbers[0] =
undefined splice pop
shift
splice

numbers.splice(5,0,2,3,4);

numbers.splice(5,3,2,3,4);

var averageTempDay1 = [72,75,79,79,81,81];


var averageTempDay2 = [81,79,75,75,73,72];

var averageTemp = [];


averageTemp[0] = [72,75,79,79,81,81];
averageTemp[1] = [81,79,75,75,73,72];
//day 1
averageTemp[0] = [];
averageTemp[0][0] = 72;
averageTemp[0][1] = 75;
averageTemp[0][2] = 79;
averageTemp[0][3] = 79;
averageTemp[0][4] = 81;
averageTemp[0][5] = 81;
//day 2
averageTemp[1] = [];
averageTemp[1][0] = 81;
averageTemp[1][1] = 79;
averageTemp[1][2] = 75;
averageTemp[1][3] = 75;
averageTemp[1][4] = 73;
averageTemp[1][5] = 72;

function printMatrix(myMatrix) {
for (var i=0; i<myMatrix.length; i++){
for (var j=0; j<myMatrix[i].length; j++){
console.log(myMatrix[i][j]);
}
}
}

for
i j
averageTemp

printMatrix(averageTemp);

i j z

var matrix3x3x3 = [];


for (var i=0; i<3; i++){
matrix3x3x3[i] = [];
for (var j=0; j<3; j++){
matrix3x3x3[i][j] = [];
for (var z=0; z<3; z++){
matrix3x3x3[i][j][z] = i+j+z;
}
}
}
for (var i=0; i<matrix3x3x3.length; i++){
for (var j=0; j<matrix3x3x3[i].length; j++){
for (var z=0; z<matrix3x3x3[i][j].length; z++){
console.log(matrix3x3x3[i][j][z]);
}
}
}

for

concat
every
false
filter true

forEach
join
indexOf
lastIndexOf

map
reverse
slice
some
true
sort
toString
valueOf toString

push pop shift unshift splice

concat

var zero = 0;
var positiveNumbers = [1,2,3];
var negativeNumbers = [-3,-2,-1];
var numbers = negativeNumbers.concat(zero, positiveNumbers);

zero negativeNumbers
positiveNumbers numbers

for
true
false

var isEven = function (x) {


// returns true if x is a multiple of 2.
console.log(x);
return (x % 2 == 0)  true : false;
};
var numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];

return (x % 2 == 0)  true : false


return (x % 2 == 0)

every every
false

numbers.every(isEven);

numbers
isEven false

some every
some true

numbers.some(isEven);

numbers
false
true
forEach
for

numbers.forEach(function(x){
console.log((x % 2 == 0));
});

map

var myMap = numbers.map(isEven);

myMap [false, true, false, true, false,


true, false, true, false, true, false, true, false, true, false]
isEven map
myMap[0] false
myMap[1] true

filter
true

var evenNumbers = numbers.filter(isEven);

evenNumbers [2,
4, 6, 8, 10, 12, 14]

reduce reduce
previousValue currentValue index array

reduce

numbers.reduce(function(previous, current, index){


return previous + current;
});
120

Array map
reduce map
reduce

map filter reduce


Chapter
11

Chapter 1

@@iterator

copyWithin
entries @@iterator
includes true false

find

findIndex

fill
from
keys @@iterator
of
values @@iterator
for..of

forEach

numbers.forEach(function (x) {
console.log(x % 2 == 0);
});

numbers.forEach(x => {
console.log((x % 2 == 0));
});

for forEach
for..of
for..of

for (let n of numbers) {


console.log((n % 2 == 0)  'even' : 'odd');
}

https://goo.gl/qHYAN1
Array @@iterator
Symbol.iterator

let iterator = numbers[Symbol.iterator]();


console.log(iterator.next().value); //1
console.log(iterator.next().value); //2
console.log(iterator.next().value); //3
console.log(iterator.next().value); //4
console.log(iterator.next().value); //5

next
iterator.next()

iterator.next() undefined

numbers.value()

https://goo.gl/L81UQW

entries

entries @@iterator

let aEntries = numbers.entries(); //retrieve iterator of key/value


console.log(aEntries.next().value); //[0, 1] - position 0, value 1
console.log(aEntries.next().value); //[1, 2] - position 1, value 2
console.log(aEntries.next().value); //[2, 3] - position 2, value 3

number key
value
keys @@iterator

let aKeys = numbers.keys(); //retrieve iterator of keys


console.log(aKeys.next()); // {value: 0, done: false }
console.log(aKeys.next()); // {value: 1, done: false }
console.log(aKeys.next()); // {value: 2, done: false }

numbers keys
aKeys.next() undefined value done true
done false

values @@iterator

let aValues = numbers.values();


console.log(aValues.next()); // {value: 1, done: false }
console.log(aValues.next()); // {value: 2, done: false }
console.log(aValues.next()); // {value: 3, done: false }

https://goo.gl/eojEGk

Array.from

let numbers2 = Array.from(numbers);

let evens = Array.from(numbers, x => (x % 2 == 0));


evens

https://goo.gl/n4rOY4

Array.of

let numbers3 = Array.of(1);


let numbers4 = Array.of(1,2,3,4,5,6);

let numbers3 = [1];


let numbers4 = [1,2,3,4,5,6];

let numbersCopy = Array.of(...numbers4);

Array.from(numbers4)
spread Chapter 1
... numbers4

https://goo.gl/FoJYNf

fill

let numbersCopy = Array.of(1,2,3,4,5,6);


numbersCopy

numbersCopy.fill(0);

numbersCopy [0,0,0,0,0,0]

numbersCopy.fill(2, 1);

2
1 [0,2,2,2,2,2]

numbersCopy.fill(1, 3, 5);

1 3 5
[0,2,2,1,1,2]

fill

let ones = Array(6).fill(1);

6 1 [1,1,1,1,1,1]

https://goo.gl/sqiHSK

copyWithin

let copyArray = [1, 2, 3, 4, 5, 6];

45 6
[4,5,6,4,5,6]

copyArray.copyWithin(0, 3);
4 5

copyArray = [1, 2, 3, 4, 5, 6];


copyArray.copyWithin(1, 3, 5);

[1,4,5,4,5,6]

https://goo.gl/hZhBE1

numbers
reverse

numbers.reverse();

numbers [15, 14, 13, 12, 11, 10, 9, 8,


7, 6, 5, 4, 3, 2, 1] sort

numbers.sort();

[1, 10, 11, 12, 13, 14, 15, 2,


3, 4, 5, 6, 7, 8, 9] sort

numbers.sort(function(a,b){
return a-b;
});
b a a
b
a b sort

function compare(a, b) {
if (a < b) {
return -1;
}
if (a > b) {
return 1;
}
// a must be equal to b
return 0;
}

numbers.sort(compare);

sort Array
compareFunction

compareFunction

name age age

var friends = [
{name: 'John', age: 30},
{name: 'Ana', age: 20},
{name: 'Chris', age: 25}
];

function comparePerson(a, b){


if (a.age < b.age){
return -1
}
if (a.age > b.age){
return 1
}
return 0;
}

console.log(friends.sort(comparePerson));

Ana (20) Chris (25) John


(30)

var names =['Ana', 'ana', 'john', 'John'];


console.log(names.sort());

ana John

http://www.asciitab
le.com/

compareFunction
["Ana", "ana", "John", "john"]

names.sort(function(a, b){
if (a.toLowerCase() < b.toLowerCase()){
return -1
}
if (a.toLowerCase() > b.toLowerCase()){
return 1
}
return 0;
});
localeCompare

var names2 = ['Maève', 'Maeve'];


console.log(names2.sort(function(a, b){
return a.localeCompare(b);
}));

["Maeve", "Maève"]

indexOf
lastIndexOf
numbers

console.log(numbers.indexOf(10));
console.log(numbers.indexOf(100));

9 -1

numbers.push(10);
console.log(numbers.lastIndexOf(10));
console.log(numbers.lastIndexOf(100));

10 15
-1
100

let numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];


function multipleOf13(element, index, array) {
return (element % 13 == 0)  true : false;
}
console.log(numbers.find(multipleOf13));
console.log(numbers.findIndex(multipleOf13));
find findIndex

find findIndex find


findIndex

undefined

https://goo.gl/2vAaCh

includes true false

console.log(numbers.includes(15));
console.log(numbers.includes(20));

includes(15) true includes(20) false


20 numbers

let numbers2 = [7,6,5,4,3,2,1];


console.log(numbers2.includes(4,5));

false 4
5

https://goo.gl/tTY9bc
toString join

toString

console.log(numbers.toString());

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15


10

- join

var numbersString = numbers.join('-');


console.log(numbersString);

http://www.w3school
s.com/js/js_arrays.asp
http://www
.w3schools.com/js/js_array_methods.asp

https://developer.mozilla.org/en-US/docs/Web/Jav
aScript/Reference/Global_Objects/Array http://goo.gl/vu1d
iT

http://underscorejs.org/
http://lodash.com/
TypeArray
let myArray = new TypedArray(length) TypedArray
TypedArray

Int8Array
Uint8Array
Uint8ClampedArray
Int16Array
Uint16Array
Int32Array
Uint32Array
Float32Array
Float64Array

let length = 5;
let int16 = new Int16Array(length);

let array16 = [];


array16.length = length;

for (let i=0; i<length; i++){


int16[i] = i+1;
}
console.log(int16);
http://goo.gl/kZBsGx

Array

function Stack() {
//properties and methods go here
}

let items = [];

push(element(s))

pop()
peek()

isEmpty()

clear()
size()
length

push

push

this.push = function(element){
items.push(element);
};

push
array

pop

pop
array pop

this.pop = function(){
return items.pop();
};

push pop
Stack
helper
peek

this.peek = function(){
return items[items.length-1];
};

length - 1

length - 1

isEmpty true
false

this.isEmpty = function(){
return items.length == 0;
};

isEmpty
length array length
Stack size length

length

this.size = function(){
return items.length;
};

clear clear

this.clear = function(){
items = [];
};

pop

Stack

helper print

this.print = function(){
console.log(items.toString());
};

Stack

Stack
true,

let stack = new Stack();


5 8

peek 8

11 size 3
5 8 11 isEmpty
false

push

pop
pop
pop 5 8
pop

Stack
items
private Stack
items
Stack

Stack

class Stack {

push(element){
this.items.push(element);
}
//other methods
}
Stack Stack

constructor
{1} this.nameofVariable

items public

private
Stack

Symbol
items Stack

class Stack {
constructor () {

}
//Stack methods
}

_items Symbol {1}


constructor {2}
this.items this[_items]

Object.getOwnPropertySymbols
Symbols
Stack
_items
stack[objectSymbols[0]] _items

WeakMap Chapter 7
WeakMap

Stack WeakMap

class Stack {
constructor () {

}
push(element){
let s = items.get(this); //{3}
s.push(element);
}
pop(){
let s = items.get(this);
let r = s.pop();
return r;
}
//other methods
}

{1}, WeakMap
{2} items
Stack WeakMap

{3}, items
WeakMap {2}

items Stack
items Stack
Stack closure
WeakMap

let Stack = (function () {


const items = new WeakMap();
class Stack {
constructor () {
items.set(this, []);
}
//other methods
}
return Stack; //{5}
})();

Stack Stack
{5}

http://www.w3scho
ols.com/js/js_function_closures.asp

Stack items

Stack

function Stack() {
let items = [];
//other methods
}
WeakMap

Stack
function divideBy2(decNumber){

var remStack = new Stack(),


rem,
binaryString = '';

while (decNumber > 0){ //{1}


rem = Math.floor(decNumber % 2); //{2}
remStack.push(rem); //{3}
decNumber = Math.floor(decNumber / 2); //{4}
}

while (!remStack.isEmpty()){ //{5}


binaryString += remStack.pop().toString();
}

return binaryString;
}

{1}
{2} {3}
{4}

Math.floor

{5}
function baseConverter(decNumber, base){

var remStack = new Stack(),


rem,
baseString = '',
digits = '0123456789ABCDEF'; //{6}

while (decNumber > 0){


rem = Math.floor(decNumber % base);
remStack.push(rem);
decNumber = Math.floor(decNumber / base);
}

while (!remStack.isEmpty()){
baseString += digits[remStack.pop()]; //{7}
}

return baseString;
}

{6} {7}
push pop

function Queue() {
//properties and methods go here
}

Stack
Queue Stack

let items = [];

enqueue(element(s))

dequeue()

front()

peek Stack
isEmpty() true
false
size()
length
enqueue

this.enqueue = function(element){
items.push(element);
};

push
array Chapter 2 Chapter
3

dequeue

shift
array Chapter 2
shift

this.dequeue = function(){
return items.shift();
};

enqueue dequeue
Queue

helper
front

this.front = function(){
return items[0];
};
isEmpty true
false Stack

this.isEmpty = function(){
return items.length == 0;
};

isEmpty

length array
Queue size Stack

this.size = function(){
return items.length;
};

Queue Stack
print

this.print = function(){
console.log(items.toString());
};

Queue Stack
dequeue front

Queue
true

let queue = new Queue();


console.log(queue.isEmpty()); //outputs true
enqueue "John" "Jack"

John Jack Camila


3

enqueue

dequeue dequeue
dequeue
Camila

Chapter 3 Queue
WeakMap items
closure Queue

let Queue2 = (function () {

const items = new WeakMap();

class Queue2 {
constructor () {
items.set(this, []);
}
enqueue(element) {
let q = items.get(this);
q.push(element);
}
dequeue() {
let q = items.get(this);
let r = q.shift();
return r;
}
//other methods
}
return Queue2;
})();

Queue
dequeue

function PriorityQueue() {
let items = [];
function QueueElement (element, priority){ // {1}
this.element = element;
this.priority = priority;
}

this.enqueue = function(element, priority){


let queueElement = new QueueElement(element, priority);

let added = false;


for (let i=0; i<items.length; i++){
if (queueElement.priority < items[i].priority){ // {2}
items.splice(i,0,queueElement); // {3}
added = true;
break; // {4}
}
}
if (!added){
items.push(queueElement); //{5}
}
};

this.print = function(){
for (let i=0; i<items.length; i++){
console.log(`${items[i].element} -
${items[i].priority}`);
}
};
//other methods - same as default Queue implementation
}

Queue PriorityQueue
{1} PriorityQueue

{2}

splice
array Chapter 2
{3}
{4}

{5}

PriorityQueue
John 2
Jack 1 Jack
John Camila
1 Camila Jack Jack
Camila John
function hotPotato (nameList, num){

let queue = new Queue(); // {1}

for (let i=0; i<nameList.length; i++){


queue.enqueue(nameList[i]); // {2}
}

let eliminated = '';


while (queue.size() > 1){
for (let i=0; i<num; i++){
queue.enqueue(queue.dequeue()); // {3}
}
eliminated = queue.dequeue();// {4}
console.log(eliminated + ' was eliminated from the Hot Potato game.');
}

return queue.dequeue();// {5}


}

Queue
{1}
{2}
{3}

{4}
{5}
hotPotato

https://goo.gl/ayF840
queue

enqueue dequeue

Chapter 2

LinkedList

[]

array
LinkedList

function LinkedList() {

let Node = function(element){ // {1}


this.element = element;
this.next = null;
};

let length = 0; // {2}


let head = null; // {3}

this.append = function(element){};
this.insert = function(position, element){};
this.removeAt = function(position){};
this.remove = function(element){};
this.indexOf = function(element){};
this.isEmpty = function() {};
this.size = function() {};
this.toString = function(){};
this.print = function(){};
}

LinkedList helper Node {1}


Node element
next

length {2} LinkedList


head {3}

LinkedList

append(element)
insert(position, element)

remove(element)
indexOf(element)

removeAt(position)
isEmpty() true
false
size()
length
toString() Node
toString

LinkedList

append

this.append = function(element){

let node = new Node(element), //{1}


current; //{2}

if (head === null){ //first node on list //{3}


head = node;

} else {

current = head; //{4}


//loop the list until find last item
while(current.next){
current = current.next;
}

//get last item and assign next to node to make the link
current.next = node; //{5}
}

length++; //update size of list //{6}


};

Node element
{1}

LinkedList head null

head null {3}


head node
node null

null

{4}
current
{2}
current.next
null
{5}

Node null

{6}

let list = new LinkedList();


list.append(15);
list.append(10);

LinkedList

remove
remove

this.removeAt = function(position){

//check for out-of-bounds values


if (position > -1 && position < length){ // {1}

let current = head, // {2}


previous, // {3}
index = 0; // {4}

//removing first item


if (position === 0){ // {5}
head = current.next;
} else {

while (index++ < position){ // {6}

previous = current; // {7}


current = current.next; // {8}
}

//link previous with current's next: skip it to remove


previous.next = current.next; // {9}
}

length--; // {10}

return current.element;

} else {
return null; // {11}
}
};

{1} size - 1
null

position === 0 {5}

head

current {2}
current
head current.next
{6}

previous.next
current.next {9}

https://developer.mozilla.org/en-US/docs/Web/JavaScrip
t/Memory_Management

{6} current

current.next null
previous previous.next
current current
previous.next current.next
current previous

current previous.next
current.next

insert

this.insert = function(position, element){

//check for out-of-bounds values


if (position >= 0 && position <= length){ //{1}

let node = new Node(element),


current = head,
previous,
index = 0;

if (position === 0){ //add on first position

node.next = current; //{2}


head = node;

} else {
while (index++ < position){ //{3}
previous = current;
current = current.next;
}
node.next = current; //{4}
previous.next = node; //{5}
}

length++; //update size of list


return true;

} else {
return false; //{6}
}
};

{1}
remove false
{6}

current
node.next current
head node.next current
head node {2}

{3}
current
previous

previous current
node {4}
previous current previous.next
node {5}
previous
current node.next current
previous.next node

node previous
current node.next current
previous.next node

previous
LinkedList
toString indexOf isEmpty size

toString LinkedList
toString

this.toString = function(){

let current = head, //{1}


string = ''; //{2}

while (current) { //{3}


string +=current.element +(current.next  'n' : '');//{4}
current = current.next; //{5}
}
return string; //{6}
};

head
current {1}

{2}

{3} current

null while
{4}
{5} {6}

indexOf indexOf

-1
this.indexOf = function(element){

let current = head, //{1}


index = -1;

while (current) { //{2}


if (element === current.element) {
return index; //{3}
}
index++; //{4}
current = current.next; //{5}
}

return -1;
};

current head
index {1}
{2}
{3} {4}
{5}

current
= current.next null -1

remove

this.remove = function(element){
let index = this.indexOf(element);
return this.removeAt(index);
};

removeAt
indexOf
removeAt
removeAt

removeAt
isEmpty size

this.isEmpty = function() {
return length === 0;
};

isEmpty true false

this.size = function() {
return length;
};

size length
length LinkedList

getHead

this.getHead = function(){
return head;
};

head LinkedList
LinkedList
DoublyLinkedList

function DoublyLinkedList() {

let Node = function(element){

this.element = element;
this.next = null;
this.prev = null; //NEW
};

let length = 0;
let head = null;
let tail = null; //NEW

//methods here
}

LinkedList
DoublyLinkedList NEW Node prev
DoublyLinkedList tail
next
next prev

this.insert = function(position, element){

//check for out-of-bounds values


if (position >= 0 && position <= length){

let node = new Node(element),


current = head,
previous,
index = 0;

if (position === 0){ //add on first position

if (!head){ //NEW {1}


head = node;
tail = node;
} else {
node.next = current;
current.prev = node; //NEW {2}
head = node;
}
} else if (position === length) { //last item //NEW

current = tail; // {3}


current.next = node;
node.prev = current;
tail = node;

} else {
while (index++ < position){ //{4}
previous = current;
current = current.next;
}
node.next = current; //{5}
previous.next = node;

current.prev = node; //NEW


node.prev = previous; //NEW
}
length++; //update size of list

return true;

} else {
return false;
}
};

{1} head tail


current
node.next current head
node
current.prev
node {2}

current {3}
node.prev current current.next
null node node.next null
tail node
current
{4} current previous
node.next current {5} previous.next node

current.prev node node.prev previous

insert remove

position length/2
this.removeAt = function(position){

//look for out-of-bounds values


if (position > -1 && position < length){

let current = head,


previous,
index = 0;

//removing first item


if (position === 0){

head = current.next; // {1}

//if there is only one item, update tail //NEW


if (length === 1){ // {2}
tail = null;
} else {
head.prev = null; // {3}
}

} else if (position === length-1){ //last item //NEW

current = tail; // {4}


tail = current.prev;
tail.next = null;

} else {

while (index++ < position){ // {5}

previous = current;
current = current.next;
}

//link previous with current's next - skip it


previous.next = current.next; // {6}
current.next.prev = previous; //NEW
}

length--;
return current.element;

} else {
return null;
}
};

current

head current current.next


{1}
{3}

{2}

tail
tail current {4}
tail current.prev
tail.prev tail
next null tail.next = null
{5}
current
previous.next current.next.prev
previous.next current.next current.next.prev
previous
tail.next
null head

tail.next head head.prev


tail

CircularLinkedList
LinkedList DoublyLinkedList

Set

{}
Array
Set

Set

Set

function Set() {
let items = {};
}

items

Set

add(value)
delete(value)
has(value) true false
clear()
size()
length
values()
has(value)
add remove

this.has = function(value){
return value in items;
};

in
items

this.has = function(value){
return items.hasOwnProperty(value);
};

hasOwnProperty

add

this.add = function(value){
if (!this.has(value)){
items[value] = value; //{1}
return true;
}
return false;
};

value
value {1} true
false
remove

this.delete = function(value){
if (this.has(value)){
delete items[value]; //{2}
return true;
}
return false;
};

remove value
value {2} true
false

items delete
items {2}

Set

let set = new Set();


set.add(1);
set.add(2);

items console.log

clear

this.clear = function(){
items = {}; // {3}
};
items {3}
remove

size

length add
remove LinkedList

Object

this.size = function(){
return Object.keys(items).length; //{4}
};

Object keys
length
{4} items

items

this.sizeLegacy = function(){
let count = 0;
for(let key in items) { //{5}
if(items.hasOwnProperty(key)) //{6}
++count; //{7}
}
return count;
};

items {5}

{6} count {7}


for-in
items count
has items

Object

values
items

this.values = function(){
let values = [];
for (let i=0, keys=Object.keys(items); i<keys.length; i++) {
values.push(items[keys[i]]);
}
return values;
};

this.valuesLegacy = function(){
let values = [];
for(let key in items) { //{7}
if(items.hasOwnProperty(key)) { //{8}
values.push(items[key]);
}
}
return values;
};

items {7}
{8} sizeLegacy
Set

let 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.remove(1);
console.log(set.values()); //outputs ["2"]

set.remove(2);
console.log(set.values()); //outputs []

Set

Chapter 2 Chapter 3 Chapter


4
union Set

this.union = function(otherSet){
let unionSet = new Set(); //{1}

let values = this.values(); //{2}


for (let i=0; i<values.length; i++){
unionSet.add(values[i]);
}

values = otherSet.values(); //{3}


for (let i=0; i<values.length; i++){
unionSet.add(values[i]);
}

return unionSet;
};
{1}
values Set
{2}
{3}

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);

let unionAB = setA.union(setB);


console.log(unionAB.values());

["1", "2", "3", "4", "5", "6"] 3


intersection Set

this.intersection = function(otherSet){
let intersectionSet = new Set(); //{1}

let values = this.values();


for (let i=0; i<values.length; i++){ //{2}
if (otherSet.has(values[i])){ //{3}
intersectionSet.add(values[i]); //{4}
}
}

return intersectionSet;
}

intersection
Set Set Set
{1}
values Set {2}
otherSet {3} has

Set Set
intersectionSet {4}

let setA = new Set();


setA.add(1);
setA.add(2);
setA.add(3);

let setB = new Set();


setB.add(2);
setB.add(3);
setB.add(4);

let intersectionAB = setA.intersection(setB);


console.log(intersectionAB.values());

["2", "3"] 2 3

difference Set

this.difference = function(otherSet){
let differenceSet = new Set(); //{1}

let values = this.values();


for (let i=0; i<values.length; i++){ //{2}
if (!otherSet.has(values[i])){ //{3}
differenceSet.add(values[i]); //{4}
}
}

return differenceSet;
};
intersection values difference

{3}
otherSet {1} {2}
{4}

intersection

let setA = new Set();


setA.add(1);
setA.add(2);
setA.add(3);

let setB = new Set();


setB.add(2);
setB.add(3);
setB.add(4);

let differenceAB = setA.difference(setB);


console.log(differenceAB.values());

["1"] 1 setA
subset Set

this.subset = function(otherSet){

if (this.size() > otherSet.size()){ //{1}


return false;
} else {
let values = this.values();
for (let i=0; i<values.length; i++){ //{2}
if (!otherSet.has(values[i])){ //{3}
return false; //{4}
}
}
return true; //{5}
}
};

Set
otherSet
{3}

{2}
otherSet {3} otherSet
false {4}
otherSet {4} true {5}

let setA = new Set();


setA.add(1);
setA.add(2);
let setB = new Set();
setB.add(1);
setB.add(2);
setB.add(3);

let setC = new Set();


setC.add(2);
setC.add(3);
setC.add(4);

console.log(setA.subset(setB));
console.log(setA.subset(setC));

setA setB true setA


setC setC 2 setA 1 2
false

Set Set

Set
https://developer.mozilla.org/en-US/docs/Web/JavaScript/R
eference/Global_Objects/Set http://goo.gl/2li2a5

Set

Set

let 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 Set values


Iterator Chapter 2
size
set Set size
delete set

set.delete(1);

clear set

Set Set

let setA = new Set();


setA.add(1);
setA.add(2);
setA.add(3);

let setB = new Set();


setB.add(2);
setB.add(3);
setB.add(4);

{1}
{2} {3}

let unionAb = new Set(); //{1}


for (let x of setA) unionAb.add(x); //{2}
for (let x of setB) unionAb.add(x); //{3}
setA setB {1}

let intersection = function(setA, setB){


let intersectionSet = new Set();
for (let x of setA){
if (setB.has(x)){ //{1}
intersectionSet.add(x);
}
}
return intersectionSet;
};
let intersectionAB = intersection(setA, setB);

intersectionAb = new Set([x for (x of setA) if (setB.has(x))]);

intersection

intersection

setA setB
setA setB

let difference = function(setA, setB){


let differenceSet = new Set();
for (let x of setA){
if (!setB.has(x)){ //{1}
differenceSet.add(x);
}
}
return differenceSet;
};
let differenceAB = difference(setA, setB);
intersection difference
{1} setA setB

differenceAB = new Set([x for (x of setA) if (!setB.has(x))]);

difference

Set
Set

Set Set


Set Map
Map
Set

Dictionary

function Dictionary(){
var items = {};
}

Set Object

set(key,value)
delete(key)
has(key) true false

get(key)
clear()
size()
length
keys()
values()

has(key)
set remove

this.has = function(key){
return key in items;
};

Set
in key items
set

this.set = function(key, value){


items[key] = value; //{1}
};

key value key


items

delete delete
Set key value

this.delete = function(key){
if (this.has(key)){
delete items[key];
return true;
}
return false;
};

remove key
items

this.get = function(key) {
return this.has(key)  items[key] : undefined;
};

get
key undefined
undefined null
Chapter 1
values
values

this.values = function(){
var values = [];
for (var k in items) { //{1}
if (this.has(k)) {
values.push(items[k]); //{2}
}
}
return values;
};

items {1}
has key
values {2}

for-in
items has
items

Object

clear size Set Chapter 6

keys Dictionary
keys Object

Dictionary

this.keys = function(){
return Object.keys(items);
};
items
getItems items

this.getItems = function(){
return items;
}

Dictionary
dictionary

var dictionary = new Dictionary();


dictionary.set('Gandalf', 'gandalf@email.com');
dictionary.set('John', 'johnsnow@email.com');
dictionary.set('Tyrion', 'tyrion@email.com');

true

console.log(dictionary.has('Gandalf'));

console.log(dictionary.size());

console.log(dictionary.keys());
console.log(dictionary.values());
console.log(dictionary.get('Tyrion'));

dictionary.delete('John');
console.log(dictionary.keys());
console.log(dictionary.values());
console.log(dictionary.getItems());

dictionary
items

HashTable HashMap
Dictionary

get
function HashTable() {
var table = [];
}

put(key,value)
remove(key)
get(key)

hash HashTable

var loseloseHashCode = function (key) {


var hash = 0; //{1}
for (var i = 0; i < key.length; i++) { //{2}
hash += key.charCodeAt(i); //{3}
}
return hash % 37; //{4}
};
key
key {1}
key {2}
hash charCodeAt
String {3} hash
mod
{4}

http://www.asciitable.com/

hash put

this.put = function (key, value) {


var position = loseloseHashCode(key); //{5}
console.log(position + ' - ' + key); //{6}
table[position] = value; //{7}
};

key hash
{5}
{6}
value position hash
{7}

HashTable get

this.get = function (key) {


return table[loseloseHashCode(key)];
};

key hash

table

remove

this.remove = function(key){
table[loseloseHashCode (key)] = undefined;
};
HashTable
hash undefined

HashTable table
ArrayList
undefined

hash

HashTable

var hash = new HashTable();


hash.put('Gandalf', 'gandalf@email.com');
hash.put('John', 'johnsnow@email.com');
hash.put('Tyrion', 'tyrion@email.com');

HashTable
get

console.log(hash.get('Gandalf'));
console.log(hash.get('Loiane'));

Gandalf HashTable get


Loiane
hash undefined

Gandalf HashTable

hash.remove('Gandalf');
console.log(hash.get('Gandalf'));

hash.get('Gandalf') undefined
Gandalf

hash

HashTable

var hash = new HashTable();


hash.put('Gandalf', 'gandalf@email.com');
hash.put('John', 'johnsnow@email.com');
hash.put('Tyrion', 'tyrion@email.com');
hash.put('Aaron', 'aaron@email.com');
hash.put('Donnie', 'donnie@email.com');
hash.put('Ana', 'ana@email.com');
hash.put('Jonathan', 'jonathan@email.com');
hash.put('Jamie', 'jamie@email.com');
hash.put('Sue', 'sue@email.com');
hash.put('Mindy', 'mindy@email.com');
hash.put('Paul', 'paul@email.com');
hash.put('Nathan', 'nathan@email.com');

Tyrion Aaron 16 Donnie


Ana 13 Jonathan Jamie Sue 5
Mindy Paul 32

HashTable

helper print
HashTable

this.print = function () {
for (var i = 0; i < table.length; ++i) { //{1}
if (table[i] !== undefined) { //{2}
console.log(i + ": " + table[i]); //{3}
}
}
};

{1}
{2} {3}
hash.print();

Jonathan Jamie Sue 5 Sue


Sue 5 HashTable Jonathan
Jamie Sue

HashTable
LinkedList
LinkedList
LinkedList

put
get remove

HashTable
helper LinkedList
ValuePair HashTable

var ValuePair = function(key, value){


this.key = key;
this.value = value;
this.toString = function() {
return '[' + this.key + ' - ' + this.value + ']';
}
};

key value Object


toString

put

this.put = function(key, value){


var position = loseloseHashCode(key);

if (table[position] == undefined) { //{1}


table[position] = new LinkedList();
}
table[position].append(new ValuePair(key, value)); //{2}
};

{1}
LinkedList
Chapter 5 ValuePair key
value LinkedList append {2}
Chapter 5

get

this.get = function(key) {
var position = loseloseHashCode(key);

if (table[position] !== undefined){ //{3}

//iterate linked list to find key/value


var current = table[position].getHead(); //{4}

while(current.next){ //{5}
if (current.element.key === key){ //{6}
return current.element.value; //{7}
}
current = current.next; //{8}
}

//check in case first or last element


if (current.element.key === key){ //{9}
return current.element.value;
}
}
return undefined; //{10}
};

{3} undefined
HashTable {10}
LinkedList

{4}
{5} current.next null

Node next element element


ValuePair value key key
Node current.element.key
key {6} helper
ValuePair
key
Node {7}
{8}

while {9}

HashTable
remove
LinkedList LinkedList
remove

this.remove = function(key){
var position = loseloseHashCode(key);

if (table[position] !== undefined){

var current = table[position].getHead();


while(current.next){
if (current.element.key === key){ //{11}
table[position].remove(current.element); //{12}
if (table[position].isEmpty()){ //{13}
table[position] = undefined; //{14}
}
return true; //{15}
}
current = current.next;
}

//check in case first or last element


if (current.element.key === key){ //{16}
table[position].remove(current.element);
if (table[position].isEmpty()){
table[position] = undefined;
}
return true;
}
}

return false; //{17}


};

remove get
LinkedList
current {11}
remove LinkedList {12}
{13}
position undefined {14}
true
{15} false
HashTable {17}
{16} get

HashMap
put

this.put = function(key, value){


var position = loseloseHashCode(key); // {1}

if (table[position] == undefined) { // {2}


table[position] = new ValuePair(key, value); // {3}
} else {
var index = ++position; // {4}
while (table[index] != undefined){ // {5}
index++; // {6}
}
table[index] = new ValuePair(key, value); // {7}
}
};

hash {1}

{2} {3} ValuePair

position
undefined index position + 1
{4} ++
index {5}
index {6}
{7}
get

this.get = function(key) {
var position = loseloseHashCode(key);

if (table[position] !== undefined){ //{8}


if (table[position].key === key) { //{9}
return table[position].value; //{10}
} else {
var index = ++position;
while (table[index] === undefined
|| table[index].key !== key){ //{11}
index++;
}
if (table[index].key === key) { //{12}
return table[index].value; //{13}
}
}
}
return undefined; //{14}
};

{8}
undefined
{14}
{9} {10}
HashTable

{11}
{12} {13}

ValuePair HashTable

remove get
{10} {13}

table[index] = undefined;

undefined

var djb2HashCode = function (key) {


var hash = 5381; //{1}
for (var i = 0; i < key.length; i++) { //{2}
hash = hash * 33 + key.charCodeAt(i); //{3}
}
return hash % 1013; //{4}
};

hash {1}
5381 key {2}
hash 33
{3}
HashTable

djb2HashCode loseloseHashCode

http://goo.gl/VtdN2x

Map
Dictionary Map

Map

https://developer.mozilla.org/en-US/docs/Web/JavaScript/R
eference/Global_Objects/Map http://goo.gl/dm8VP6

Map
var 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')); //outputs true


console.log(map.size); //outputs 3
console.log(map.keys()); //outputs ["Gandalf", "John", "Tyrion"]
console.log(map.values()); //outputs ["gandalf@email.com",
"johnsnow@email.com", "tyrion@email.com"]
console.log(map.get('Tyrion')); //outputs tyrion@email.com

Dictionary Map values


keys Iterator Chapter 2
size
Map
size

delete

map.delete('John');

clear map
Dictionary

Set Map
WeakMap WeakSet

Map Set

WeakSet WeakMap entries keys values


WeakSet
WeakMap

entries keys values


WeakMap
Chapter 3

WeakMap

var map = new WeakMap();

var ob1 = {name:'Gandalf'}, //{1}


ob2 = {name:'John'},
ob3 = {name:'Tyrion'};

map.set(ob1, 'gandalf@email.com'); //{2}


map.set(ob2, 'johnsnow@email.com');
map.set(ob3, 'tyrion@email.com');

console.log(map.has(ob1)); //{3} outputs true


console.log(map.get(ob3)); //{4} outputs tyrion@email.com
map.delete(ob2); //{5}

WeakMap

{1} {2}

{3} {4} {5}

WeakSet
Map WeakMap WeakSet

tree

BinarySearchTree

function BinarySearchTree() {

var Node = function(key){ //{1}


this.key = key;
this.left = null;
this.right = null;
};

var root = null; //{2}


}

Node
{1}

LinkedList Chapter 5

{2}
insert(key)
search(key) true
false
inOrderTraverse
preOrderTraverse
postOrderTraverse

min
max
remove(key)

Chapter 11

this.insert = function(key){

var newNode = new Node(key); //{1}

if (root === null){ //{2}


root = newNode;
} else {
insertNode(root,newNode); //{3}
}
};

Node
{1}
left right null
{2}

helper {3}

var insertNode = function(node, newNode){


if (newNode.key < node.key){ //{4}
if (node.left === null){ //{5}
node.left = newNode; //{6}
} else {
insertNode(node.left, newNode); //{7}
}
} else {
if (node.right === null){ //{8}
node.right = newNode; //{9}
} else {
insertNode(node.right, newNode); //{10}
}
}
};

insertNode

insertNode
{3}

{4}
{5} {6}
insertNode {7}

{8} {9}
insertNode
{10}
var tree = new BinarySearchTree();
tree.insert(11);

{2}

11

tree.insert(7);
tree.insert(15);
tree.insert(5);
tree.insert(3);
tree.insert(9);
tree.insert(8);
tree.insert(10);
tree.insert(13);
tree.insert(12);
tree.insert(14);
tree.insert(20);
tree.insert(18);
tree.insert(25);

tree.insert(6);
{3}
insertNode root key[6]
{4} key[6] < root[11] true
{5} node.left[7] null {7}
insertNode node.left[7] key[6]
insertNode
{4} key[6] < node[7] true
{5} node.left[5] null {7}
insertNode node.left[5] key[6]
insertNode {4}
key[6] < node[5] false {8} node.right
null
{9} 6

6
this.inOrderTraverse = function(callback){
inOrderTraverseNode(root, callback); //{1}
};

inOrderTraverse callback

http://en.wikipedia.org/wiki/Visitor_pattern
helper
node callback {1}

var inOrderTraverseNode = function (node, callback) {


if (node !== null) { //{2}
inOrderTraverseNode(node.left, callback); //{3}
callback(node.key); //{4}
inOrderTraverseNode(node.right, callback); //{5}
}
};

node
null
{2}

{3}
{4} callback
{5}

function printNode(value){ //{6}


console.log(value);
}
tree.inOrderTraverse(printNode); //{7}
{6}
inOrderTraverse
{7}

inOrderTraverse

this.preOrderTraverse = function(callback){
preOrderTraverseNode(root, callback);
};

preOrderTraverseNode

var preOrderTraverseNode = function (node, callback) {


if (node !== null) {
callback(node.key); //{1}
preOrderTraverseNode(node.left, callback); //{2}
preOrderTraverseNode(node.right, callback); //{3}
}
};
{1} {2}
{3} {2} {1}
{3}

preOrderTraverse

this.postOrderTraverse = function(callback){
postOrderTraverseNode(root, callback);
};

postOrderTraverseNode

var postOrderTraverseNode = function (node, callback) {


if (node !== null) {
postOrderTraverseNode(node.left, callback); //{1}
postOrderTraverseNode(node.right, callback); //{2}
callback(node.key); //{3}
}
};

{1}
{2} {3}

{1} {2} {3}

postOrderTraverse
this.min = function() {
return minNode(root); //{1}
};

min minNode
{1}

var minNode = function (node) {


if (node){
while (node && node.left !== null) { //{2}
node = node.left; //{3}
}

return node.key;
}
return null; //{4}
};
minNode

minNode {1}

minNode left {2} {3}

max

this.max = function() {
return maxNode(root);
};

var maxNode = function (node) {


if (node){
while (node && node.right !== null) { //{5}
node = node.right;
}

return node.key;
}
return null;
};

{5}

find search get


has
search

this.search = function(key){
return searchNode(root, key); //{1}
};

var searchNode = function(node, key){


if (node === null){ //{2}
return false;
}
if (key < node.key){ //{3}
return searchNode(node.left, key); //{4}

} else if (key > node.key){ //{5}


return searchNode(node.right, key); //{6}

} else {
return true; //{7}
}
};

search
helper {1}

searchNode
{1} root

node
null false

null
{3}
{4} {5}
{6}

true {7}

searchNode {1}
node[root[11]] null {2} {3}
key[1] < node[11] true {3} {4}
searchNode node[7] key[1]
node[7] null {2} {3}
key[1] < node[7] true {3} {4}
searchNode node[5] key[1]
node[5] null {2} {3}
key[1] < node[5] true {3} {4}
searchNode node[3] key[1]
node[3] null {2} {3}
key[1] < node[3] true {3} {4}
searchNode null key[1] null
node[3]
null
node null {2} node null
false

searchNode root {1}


node[root[11]] null {2} {3}
key[8] < node[11] true {3} {4}
searchNode node[7] key[8]
node[7] null {2} {3}
key[8] < node[7] false {3} {5}
key[8] > node[7] true {5} {6}
searchNode node[9] key[8]
node[9] null {2} {3}
key[8] < node[9] true {3} {4}
searchNode node[8] key[8]
node[8] null {2} {3}
key[8] < node[8] false {3} {5}
key[8] > node[8] false {5} {7}
true node[8]
remove

this.remove = function(key){ root = removeNode(root, key); //{1} };

key removeNode
root key {1}
removeNode

removeNode

removeNode

var removeNode = function(node, key){

if (node === null){ //{2}


return null;
}
if (key < node.key){ //{3}
node.left = removeNode(node.left, key); //{4}
return node; //{5}

} else if (key > node.key){ //{6}


node.right = removeNode(node.right, key); //{7}
return node; //{8}

} else { // key is equal to node.key

//case 1 - a leaf node


if (node.left === null && node.right === null){ //{9}
node = null; //{10}
return node; //{11}
}

//case 2 - a node with only 1 child


if (node.left === null){ //{12}
node = node.right; //{13}
return node; //{14}

} else if (node.right === null){ //{15}


node = node.left; //{16}
return node; //{17}
}

//case 3 - a node with 2 children


var aux = findMinNode(node.right); //{18}
node.key = aux.key; //{19}
node.right = removeNode(node.right, aux.key); //{20}
return node; //{21}
}
};

{2} null
null

{3}
{4}
{6}
{7}

key is equal to node.key

findMinNode

var findMinNode = function(node){


while (node && node.left !== null) {
node = node.left;
}
return node;
};

{9}
null {9}
null

null
null {11}

null
{4} {7}
{5} {8}

{12}
{13}
{14} {15}
{16}
{17}
{18}

{19}

{20}
{21}

findMinNode min
min
findMinNode
var insertNode = function(node, element) {
if (node === null) {
node = new Node(element);
} else if (element < node.key) {
node.left = insertNode(node.left, element);

if (node.left !== null) {

}
} else if (element > node.key) {
node.right = insertNode(node.right, element);

if (node.right !== null) {

}
}
return node;
};

{1} {2}
var heightNode = function(node) {
if (node === null) {
return -1;
} else {
return Math.max(heightNode(node.left),
heightNode(node.right)) + 1;
}
};

//this code replaces line {1} from insertNode method


if ((heightNode(node.left) - heightNode(node.right)) > 1){

//this code replaces line {2} from insertNode method


if ((heightNode(node.right) - heightNode(node.left)) > 1){

}
{1}

{2}
{3}

var rotationRR = function(node) {


var tmp = node.right; //{1}
node.right = tmp.left; //{2}
tmp.left = node; //{3}
return tmp;
};

{1}

{2}
{3}

var rotationLL = function(node) {


var tmp = node.left; //{1}
node.left = tmp.right; //{2}
tmp.right = node; //{3}
return tmp;
};
var rotationLR = function(node) {
node.left = rotationRR(node.left);
return rotationLL(node);
};
var rotationRL = function(node) {
node.right = rotationLL(node.right);
return rotationRR(node);
};

//this code replaces line {1} from insertNode method


if ((heightNode(node.left) - heightNode(node.right)) > 1){
// do rotations {3}
if (element < node.left.key){
node = rotationLL(node);
} else {
node = rotationLR(node);
}
}

//this code replaces line {2} from insertNode method


if ((heightNode(node.right) - heightNode(node.left)) > 1){
// do rotations {4}
if (element > node.right.key){
node = rotationRR(node);
} else {
node = rotationRL(node);
}
}

http://goo.gl/OxED8K

http://goo.gl/SFlhW6

function Graph() {
var vertices = []; //{1}
var adjList = new Dictionary(); //{2}
}

{1}
Chapter 7
{2}
vertices adjList
Graph

addVertex

this.addVertex = function(v){
vertices.push(v); //{3}
adjList.set(v, []); //{4}
};

v
{3}
v {4}

addEdge

this.addEdge = function(v, w){


adjList.get(v).push(w); //{5}
adjList.get(w).push(v); //{6}
};

v
w {5} w v
{5}
w v {6}

{4}
var graph = new Graph();
var myVertices = ['A','B','C','D','E','F','G','H','I']; //{7}
for (var i=0; i<myVertices.length; i++){ //{8}
graph.addVertex(myVertices[i]);
}
graph.addEdge('A', 'B'); //{9}
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');

{7} vertices
{8} {9}

toString Graph

this.toString = function(){
var s = '';
for (var i=0; i<vertices.length; i++){ //{10}
s += vertices[i] + ' -> ';
var neighbors = adjList.get(vertices[i]); //{11}
for (var j=0; j<neighbors.length; j++){ //{12}
s += neighbors[j] + ' ';
}
s += '\n'; //{13}
}
return s;
};

vertices {10}
{11} {12}

{13}

console.log(graph.toString());
A
B C D
Chapter 3

Chapter 4
var initializeColor = function(){
var color = [];
for (var i=0; i<vertices.length; i++){
color[vertices[i]] = 'white'; //{1}
}
return color;
};

this.bfs = function(v, callback){

var color = initializeColor(), //{2}


queue = new Queue(); //{3}
queue.enqueue(v); //{4}

while (!queue.isEmpty()){ //{5}


var u = queue.dequeue(), //{6}
neighbors = adjList.get(u); //{7}
color[u] = 'grey'; //{8}
for (var i=0; i<neighbors.length; i++){ //{9}
var w = neighbors[i]; //{10}
if (color[w] === 'white'){ //{11}
color[w] = 'grey'; //{12}
queue.enqueue(w); //{13}
}
}
color[u] = 'black'; //{14}
if (callback) { //{15}
callback(u);
}
}
};
helper
color
white {1} helper
initializeColor

initializeColor color white {2}


Queue {3}

bfs

enqueue {4}

{5}
{6} {7}
grey

u {9}
{10} white {11}
grey {12}
{13} dequeue

black {14}

bfs
Chapter 8
callback {15}

function printNode(value){ //{16}


console.log('Visited vertex: ' + value); //{17}
}
graph.bfs(myVertices[0], printNode); //{18}
callback {16}
{17}
bfs A
myVertices callback

bfs
BFS

this.BFS = function(v){

var color = initializeColor(),


queue = new Queue(),
d = [], //{1}
pred = []; //{2}
queue.enqueue(v);

for (var i=0; i<vertices.length; i++){ //{3}


d[vertices[i]] = 0; //{4}
pred[vertices[i]] = null; //{5}
}

while (!queue.isEmpty()){
var u = queue.dequeue(),
neighbors = adjList.get(u);
color[u] = 'grey';
for (i=0; i<neighbors.length; i++){
var w = neighbors[i];
if (color[w] === 'white'){
color[w] = 'grey';
d[w] = d[u] + 1; //{6}
pred[w] = u; //{7}
queue.enqueue(w);
}
}
color[u] = 'black';
}
return { //{8}
distances: d,
predecessors: pred
};
};

BFS

bfs bfs
BFS

d {1} pred
{2}
d {4} pred null {5}
{3}
w u w u
{7} {6} v w 1
u u w d[u]

d pred {8}

BFS

var shortestPathA = graph.BFS(myVertices[0]);


console.log(shortestPathA);

BFS A

A 1 B C D 2
E F G H 3 I

var fromVertex = myVertices[0]; //{9}


for (var i=1; i<myVertices.length; i++){ //{10}
var toVertex = myVertices[i], //{11}
path = new Stack(); //{12}
for (var v=toVertex; v!== fromVertex;
v=shortestPathA.predecessors[v]) { //{13}
path.push(v); //{14}
}
path.push(fromVertex); //{15}
var s = path.pop(); //{16}
while (!path.isEmpty()){ //{17}
s += ' - ' + path.pop(); //{18}
}
console.log(s); //{19}
}

A {9}
A {10} A
toVertex vertices {11}
{12}
toVertex fromVertex {13} v

v {14}
{15}

s
{16}
{17}
s {18} {19}

A
this.dfs = function(callback){
var color = initializeColor(); //{1}

for (var i=0; i<vertices.length; i++){ //{2}


if (color[vertices[i]] === 'white'){ //{3}
dfsVisit(vertices[i], color, callback); //{4}
}
}
};

var dfsVisit = function(u, color, callback){


color[u] = 'grey'; //{5}
if (callback) { //{6}
callback(u);
}
var neighbors = adjList.get(u); //{7}
for (var i=0; i<neighbors.length; i++){ //{8}
var w = neighbors[i]; //{9}
if (color[w] === 'white'){ //{10}
dfsVisit(w, color, callback); //{11}
}
}
color[u] = 'black'; //{12}
};

color {1}
white
{2} {3} Graph
dfsVisit color
callback {4}

u grey {5}
callback {6}
u {7}
white {10} {8} w {9} u dfsVisit
w {11} w

black {12}

dfs

graph.dfs(printNode);
{4}
dfsVisit A
B {4}
A
dfs

BFS

var time = 0; //{1}


this.DFS = function(){
var color = initializeColor(), //{2}
d = [],
f = [],
p = [];
time = 0;

for (var i=0; i<vertices.length; i++){ //{3}


f[vertices[i]] = 0;
d[vertices[i]] = 0;
p[vertices[i]] = null;
}
for (i=0; i<vertices.length; i++){
if (color[vertices[i]] === 'white'){
DFSVisit(vertices[i], color, d, f, p);
}
}
return { //{4}
discovery: d,
finished: f,
predecessors: p
};
};

var DFSVisit = function(u, color, d, f, p){


console.log('discovered ' + u);
color[u] = 'grey';
d[u] = ++time; //{5}
var neighbors = adjList.get(u);
for (var i=0; i<neighbors.length; i++){
var w = neighbors[i];
if (color[w] === 'white'){
p[w] = u; //{6}
DFSVisit(w,color, d, f, p);
}
}
color[u] = 'black';
f[u] = ++time; //{7}
console.log('explored ' + u);
};

{1}

d f p {2}
{3}
{4}

{5}
u {6}
{7}

time
2|V|
u d[u] < f[u]

1 ≤ d[u] < f[u] ≤ 2|V|


graph = new 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');
var result = graph.DFS();

result
Chapter 11

var graph = [[0, 2, 4, 0, 0, 0],


[0, 0, 1, 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]];

this.dijkstra = function(src){
var dist = [], visited = [],
length = this.graph.length;

for (var i = 0; i < length; i++) { //{1}


dist[i] = INF;
visited[i] = false;
}
dist[src] = 0; //{2}

for (var i = 0; i < length-1; i++){ //{3}

var u = minDistance(dist, visited); //{4}

visited[u] = true; //{5}

for (var v = 0; v < length; v++){


if (!visited[v] &&
this.graph[u][v]!=0 && dist[u] != INF &&
dist[u]+this.graph[u][v] < dist[v]){ //{6}
dist[v] = dist[u] + this.graph[u][v]; //{7}
}
}
}
return dist; //{8}
};

{1} dist
INF = Number.MAX_SAFE_INTEGER visited[] false
{2}
{3}
{4}

{5} visited

{6}
{7}
{8}
src

minDistance dist

var minDistance = function(dist, visited){


var min = INF, minIndex = -1;

for (var v = 0; v < dist.length; v++){


if (visited[v] == false && dist[v] <= min){
min = dist[v];
minIndex = v;
}
}
return minIndex;
};

Chapter 11

this.floydWarshall = function(){
var dist = [], length = this.graph.length, i, j, k;

for (i = 0; i < length; i++){ //{1}


dist[i] = [];
for (j = 0; j < length; j++){
dist[i][j] = this.graph[i][j];
}
}

for (k = 0; k < length; k++){ //{2}


for (i = 0; i < length; i++){
for (j = 0; j < length; j++){
if (dist[i][k] + dist[k][j] < dist[i][j]){ //{3}
dist[i][j] = dist[i][k] + dist[k][j]; //{4}
}
}
}
}
return dist;
};

{1}
i j

{2} 0...k
i j k
{3} i j
k {3}
{4}

{3}

INF i j
this.prim = function() {
var parent = [], key = [], visited = [];
length = this.graph.length, i;

for (i = 0; i < length; i++){ //{1}


key[i] = INF;
visited[i] = false;
}

key[0] = 0; //{1}
parent[0] = -1;

for (i = 0; i < length-1; i++) { //{3}


var u = minKey(key, visited); //{4}
visited[u] = true; //{5}

for (var v = 0; v < length; v++){


if (this.graph[u][v] && visited[v] == false
&& this.graph[u][v] < key[v]){ //{6}
parent[v] = u; //{7}
key[v] = this.graph[u][v]; //{8}
}
}
}
return parent; //{9}
};

{1} keys
INF = Number.MAX_SAFE_INTEGER visited[] false
{2}
parent[0] = -1

{3}
{4}

{5} visited

{6}
parent {7} {8}
{9}

{7} {8} {7}


{8}

key
parent

var 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]];
this.kruskal = function(){
var length = this.graph.length,
parent = [], cost,
ne = 0, a, b, u, v, i, j, min;
cost = initializeCost(); //{1}

while(ne<length-1) { //{2}

for(i=0, min = INF;i < length; i++) { //{3}


for(j=0;j < length; j++) {
if(cost[i][j] < min) {
min=cost[i][j];
u = i;
v = j;
}
}
}

u = find(u, parent); //{4}


v = find(v, parent); //{5}

if (union(u, v, parent)){ //{6}


ne++;
}

cost[u][v] = cost[v][u] = INF; //{7}


}
return parent;
}
{1}
{7}
{2}
{3}
{4} {5}
{6} u v
{7}
{8}

var find = function(i, parent){


while(parent[i]){
i = parent[i];
}
return i;
};

var union = function(i, j, parent){


if(i != j) {
parent[j] = i;
return true;
}
return false;
};

array

function ArrayList(){

var array = []; //{1}

this.insert = function(item){ //{2}


array.push(item);
};

this.toString= function(){ //{3}


return array.join();
};
}

ArrayList
{1} insert {2}
push Array
Chapter 2 toString {3}

join Array

join

ArrayList
this.bubbleSort = function(){
var length = array.length; //{1}
for (var i=0; i<length; i++){ //{2}
for (var j=0; j<length-1; j++ ){ //{3}
if (array[j] > array[j+1]){ //{4}
swap(array, j, j+1); //{5}
}
}
}
};

length array {1}


array {2} {3}
{2}

{3}

{4}
{5}
j+1 j

swap
ArrayList

var swap = function(array, index1, index2){


var aux = array[index1];
array[index1] = array[index2];
array[index2] = aux;
};
Chapter 1

[array[index1], array[index2]] = [array[index2], array[index1]];

{2}
{3}

function createNonSortedArray(size){ //{6}


var array = new ArrayList();
for (var i = size; i> 0; i--){
array.insert(i);
}
return array;
}

var array = createNonSortedArray(5); //{7}


console.log(array.toString()); //{8}
array.bubbleSort(); //{9}
console.log(array.toString()); //{10}

{6} 5
[5, 4, 3, 2, 1]
ArrayList
{7}
console {8} {9}
console
{10}

ArrayList

4 5

{1}

this.modifiedBubbleSort = function(){
var length = array.length;
for (var i=0; i<length; i++){
for (var j=0; j<length-1-i; j++ ){ //{1}
if (array[j] > array[j+1]){
swap(j, j+1);
}
}
}
};
Chapter 12
this.selectionSort = function(){
var length = array.length, //{1}
indexMin;
for (var i=0; i<length-1; i++){ //{2}
indexMin = i; //{3}
for (var j=i; j<length; j++){ //{4}
if(array[indexMin]>array[j]){ //{5}
indexMin = j; //{6}
}
}
if (i !== indexMin){ //{7}
swap(i, indexMin);
}
}
};

{1}
{2}
min
{3}
i {4}
j {5}
{6}
{4}
{7}

array = createNonSortedArray(5);
console.log(array.toString());
array.selectionSort();
console.log(array.toString());
[5, 4, 3, 2, 1]

{4}
{2}

this.insertionSort = function(){
var length = array.length, //{1}
j, temp;
for (var i=1; i<length; i++){ //{2}
j = i; //{3}
temp = array[i]; //{4}
while (j>0 && array[j-1] > temp){ //{5}
array[j] = array[j-1]; //{6}
j--;
}
array[j] = temp; //{7}
}
};

{1}
{2}

i {3} {4}

{5}
{6} j
Array sort
Array.prototype.sort

Array.prototype.sort

this.mergeSort = function(){
array = mergeSortRec(array);
};

helper
mergeSort
mergeSort mergeSortRec

var mergeSortRec = function(array){


var length = array.length;
if(length === 1) { //{1}
return array; //{2}
}
var mid = Math.floor(length / 2), //{3}
left = array.slice(0, mid), //{4}
right = array.slice(mid, length); //{5}

return merge(mergeSortRec(left), mergeSortRec(right)); //{6}


};

1 {1} 1 {2}
1
{3}
left {4} right {5}
left right

merge {6}

mergeSortRec

var merge = function(left, right){


var result = [], // {7}
il = 0,
ir = 0;
while(il < left.length && ir < right.length) { // {8}
if(left[il] < right[ir]) {
result.push(left[il++]); // {9}
} else{
result.push(right[ir++]); // {10}
}
}

while (il < left.length){ // {11}


result.push(left[il++]);
}

while (ir < right.length){ // {12}


result.push(right[ir++]);
}

return result; // {13}


};

merge

{7}
left right
{8} left
right left
{9}
right
{10}
left {11}
right {12}
{13}

mergeSort

pivot
this.quickSort = function(){
quick(array, 0, array.length - 1);
};

var quick = function(array, left, right){

var index; //{1}

if (array.length > 1) { //{2}

index = partition(array, left, right); //{3}

if (left < index - 1) { //{4}


quick(array, left, index - 1); //{5}
}

if (index < right) { //{6}


quick(array, index, right); //{7}
}
}
};

index {1}
quick
index partition {3}
{2}
index {3}
{4} {5}

{6} {7}

pivot

var partition = function(array, left, right) {

var pivot = array[Math.floor((right + left) / 2)], //{8}


i = left, //{9}
j = right; //{10}

while (i <= j) { //{11}


while (array[i] < pivot) { //{12}
i++;
}
while (array[j] > pivot) { //{13}
j--;
}
if (i <= j) { //{14}
swap(array, i, j); //{15}
i++;
j--;
}
}
return i; //{16}
};
pivot {8}
left {9}
right {10}

left right {11}


pivot {12}
left right
pivot right {13}

left pivot right pivot


left right
{14}
{15}
{11}

{3}

swap

[array[index1], array[index2]] = [array[index2], array[index1]];


{5}

{7}

partition
this.heapSort = function(){
var heapSize = array.length;
buildHeap(array); //{1}

while (heapSize > 1) {


heapSize--;
swap(array, 0, heapSize); //{2}
heapify(array, heapSize, 0); //{3}
}
};

{1} array[parent(i)] ≥
array[i]

{2}
{2}
heapify

buildHeap

var buildHeap = function(array){


var heapSize = array.length;
for (var i = Math.floor(array.length / 2); i >= 0; i--) {
heapify(array, heapSize, i);
}
};

heapify

var heapify = function(array, heapSize, i){


var left = i * 2 + 1,
right = i * 2 + 2,
largest = i;

if (left < heapSize && array[left] > array[largest]) {


largest = left;
}

if (right < heapSize && array[right] > array[largest]) {


largest = right;
}
if (largest !== i) {
swap(array, i, largest);
heapify(array, heapSize, largest);
}
};

{2}
{3}
https://github.com/loiane/javascript-datastructures-algo
rithms

search BinarySearchTree
Chapter 8 indexOf LinkedList Chapter 5

this.sequentialSearch = function(item){
for (var i=0; i<array.length; i++){ //{1}
if (item === array[i]){ //{2}
return i; //{3}
}
}
return -1; //{4}
};

{1}
{2}
true {3}

-1 {4} false null


this.binarySearch = function(item){
this.quickSort(); //{1}

var low = 0, //{2}


high = array.length - 1, //{3}
mid, element;

while (low <= high){ //{4}


mid = Math.floor((low + high) / 2); //{5}
element = array[mid]; //{6}
if (element < item) { //{7}
low = mid + 1; //{8}
} else if (element > item) { //{9}
high = mid - 1; //{10}
} else {
return mid; //{11}
}
}
return -1; //{12}
};

{1} low {2} high


{3}

low high {4} low high


-1 {12}
{5} {6}
{7}
{8}
{9} {10}

{11}
BinarySearchTree Chapter 8
search

Chapter 8

function recursiveFunction(someParam){
recursiveFunction(someParam);
};

function recursiveFunction1(someParam){
recursiveFunction2(someParam);
};

function recursiveFunction2(someParam){
recursiveFunction1(someParam);
};
recursiveFunction

var i = 0;

function recursiveFn () {
i++;
recursiveFn();
}

try {
recursiveFn();
} catch (ex) {
alert('i = ' + i + ' error: ' + ex);
}

RangeError: Maximum call stack size exceeded


InternalError: too
much recursion
http://goo.gl/Z
dTZzg

Chapter 10

function fibonacci(num){
if (num === 1 || num === 2){ //{1}
return 1;
}
}

{1}

function fibonacci(num){
if (num === 1 || num === 2){
return 1;
}
return fibonacci(num - 1) + fibonacci(num - 2);
}
function fib(num){
var n1 = 1,
n2 = 1,
n = 1;
for (var i = 3; i<=num; i++){
n = n1 + n2;
n1 = n2;
n2 = n;
}
return n;
}
Chapter 9
Chapter 9

function MinCoinChange(coins){
var coins = coins; //{1}
var cache = {}; //{2}

this.makeChange = function(amount) {
var me = this;
if (!amount) { //{3}
return [];
}
if (cache[amount]) { //{4}
return cache[amount];
}
var min = [], newMin, newAmount;
for (var i=0; i<coins.length; i++){ //{5}
var coin = coins[i];
newAmount = amount - coin; //{6}
if (newAmount >= 0){
newMin = me.makeChange(newAmount); //{7}
}
if (
newAmount >= 0 && //{8}
(newMin.length < min.length-1 || !min.length)//{9}
&& (newMin.length || !newAmount) //{10})
{
min = [coin].concat(newMin); //{11}
console.log('new Min ' + min + ' for ' + amount);
}
}
return (cache[amount] = min); //{12}
};
}

MinCoinChange coins {1}


[1, 5, 10, 25]

cache {2}

makeChange
amount < 0
{3}

cache {4}

coins
{5} newAmount {6}

makeChange x < amount


newAmount
{7}
newAmount minValue
minValue newAmount
{10}
{11}
{12}

var minCoinChange = new MinCoinChange([1, 5, 10, 25]);


console.log(minCoinChange.makeChange(36));

cache
[1, 10, 25]

1 3 4
function knapSack(capacity, weights, values, n) {

var i, w, a, b, kS = [];

for (i = 0; i <= n; i++) { //{1}


kS[i] = [];
}

for (i = 0; i <= n; i++){


for (w = 0; w <= capacity; w++){
if (i == 0 || w == 0){ //{2}
kS[i][w] = 0;
} else if (weights[i-1] <= w){ //{3}
a = values[i-1] + kS[i-1][w-weights[i-1]];
b = kS[i-1][w];
kS[i][w] = (a > b)  a : b; //{4} max(a,b)
} else{
kS[i][w] = kS[i-1][w]; //{5}
}
}
}
return kS[n][capacity]; //{6}
}
{1}
ks[n+1][capacity+1]
{2}

{3} i

{5}
{4}

{6}

var values = [3,4,5],


weights = [2,3,4],
capacity = 5,
n = values.length;
console.log(knapSack(capacity, weights, values, n)); //outputs 7

kS
function findValues(n, capacity, kS, weights, values){
var i=n, k=capacity;
console.log('Items that are part of the solution:');

while (i>0 && k>0){


if (kS[i][k] !== kS[i-1][k]){
console.log('item '+i+' can be part of solution w,v: ' +
weights[i-1] + ',' + values[i-1]);
i--;
k = k - kS[i][k];
} else {
i--;
}
}
}

{7}
function lcs(wordX, wordY) {

var m = wordX.length,
n = wordY.length,
l = [],
i, j, a, b;

for (i = 0; i <= m; ++i) {


l[i] = [];
//{1}
for (j = 0; j <= n; ++j) {
l[i][j] = 0;
//{2}
}
}

for (i=0; i<=m; i++) {


for (j=0; j<=n; j++) {
if (i == 0 || j == 0){
l[i][j] = 0;
} else if (wordX[i-1] == wordY[j-1]) {
l[i][j] = l[i-1][j-1] + 1;
//{3}
} else {
a = l[i-1][j];
b = l[i][j-1];
l[i][j] = (a > b)  a : b; //max(a,b)
//{4}
}
}
}
//{5}
return l[m][n];
}
solution

{1} solution[i] = [];


{2} solution[i][j] = '0';
{3} solution[i][j] = 'diagonal';
{4} solution[i][j]=(l[i][j] == l[i-1][j])  'top' : 'left';
{5} printSolution(solution, l, wordX, wordY, m, n);

printSolution

function printSolution(solution, l, wordX, wordY, m, n){

var a = m, b = n, i, j,
x = solution[a][b],
answer = '';

while (x !== '0') {


if (solution[a][b] === 'diagonal') {
answer = wordX[a - 1] + answer;
a--;
b--;
} else if (solution[a][b] === 'left') {
b--;
} else if (solution[a][b] === 'top') {
a--;
}
x = solution[a][b];
}
console.log('lcs: '+ answer);
}
'acbaed' 'abcadf'
4
function matrixChainOrder(p, n) {
var i, j, k, l, q, m = [];

for (i = 1; i <= n; i++){


m[i] = [];
m[i][i] = 0;
}

for (l=2; l<n; l++) {


for (i=1; i<=n-l+1; i++) {
j = i+l-1;
m[i][j] = Number.MAX_SAFE_INTEGER;
for (k=i; k<=j-1; k++) {
q = m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j]; //{1}
if (q < m[i][j]){
m[i][j] = q;
//{2}
}
}
}
}
//{3}
return m[1][n-1];
}

{1}

m
7500

var p = [10, 100, 5, 50, 1],


n = p.length;
console.log(matrixChainOrder(p, n));

var s=[];
for (i = 0; i <= n; i++){
s[i] = [];
for (j=0; j<=n; j++){
s[i][j] = 0;
}
}

{2} matrixChainOrder

s[i][j]=k;

{3}

printOptimalParenthesis(s, 1, n-1);

printOptimalParenthesis

function printOptimalParenthesis(s, i, j){


if(i == j) {
console.log("A[" + i + "]");
} else {
console.log("(");
printOptimalParenthesis(s, i, s[i][j]);
printOptimalParenthesis(s, s[i][j] + 1, j);
console.log(")");
}
}

(A[1](A[2](A[3]A[4]))) (A(B(CD)))
Chapter 9

function MinCoinChange(coins){
var coins = coins; //{1}

this.makeChange = function(amount) {
var change = [],
total = 0;
for (var i=coins.length; i>=0; i--){ //{2}
var coin = coins[i];
while (total + coin <= amount) { //{3}
change.push(coin); //{4}
total += coin; //{5}
}
}
return change;
};
}

MinCoinChange
MinCoinChange
{1}
{2}
total total amount {3} coin
{4} total {5}

var minCoinChange = new MinCoinChange([1, 5, 10, 25]);


console.log(minCoinChange.makeChange(36));

[25, 10, 1]

[1, 3, 4]
[4, 1, 1]
[3, 3]
function knapSack(capacity, values, weights) {
var n = values.length,
load = 0, i = 0, val = 0;

for (i=0; i<n && load < capacity; i++) { //{1}

if (weights[i] <= (capacity-load)) { //{2}


val += values[i];
load += weights[i];
} else {
var r = (capacity-load)/weights[i]; //{3}
val += r * values[i];
load += weights[i];
}
}
return w;
}

{1}
{2}
val load
{3}
r
var printArray = function(array){
for (var i=0; i<array.length; i++){
console.log(array[i]);
}
};
printArray([1, 2, 3, 4, 5]);

var forEach = function(array, action){


for (var i=0; i<array.length; i++){
action(array[i]);
}
};

var logItem = function (item) {


console.log(item);
};

forEach([1, 2, 3, 4, 5], logItem);

var findMinArray = function(array){


var minValue = array[0];
for (var i=1; i<array.length; i++){
if (minValue > array[i]){
minValue = array[i];
}
}
return minValue;
};
console.log(findMinArray([8,6,4,5,9])); //outputs 4

Math.min

...

const min_ = function(array){


return Math.min(...array)
};
console.log(min_([8,6,4,5,9])); //outputs 4

const min = arr => Math.min(...arr);


console.log(min([8,6,4,5,9]));

map filter reduce Chapter 2

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);
}

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]));

reduce

var sumValues = function(array){


var total = array[0];
for (var i=1; i<array.length; i++){
total += array[i];
}
return total;
};
console.log(sumValues([1, 2, 3, 4, 5]));

var sum_ = function(array){


return array.reduce(function(a, b){
return a + b;
})
};
console.log(sum_([1, 2, 3, 4, 5]));
const sum = arr => arr.reduce((a, b) => a + b);
console.log(sum([1, 2, 3, 4, 5]));

var mergeArrays_ = function(arrays){


var count = arrays.length,
newArray = [],
k =0;
for (var i=0; i<count; i++){
for (var j=0; j<arrays[i].length; j++){
newArray[k++] = arrays[i][j];
}
}
return newArray;
};
console.log(mergeArrays_([[1, 2, 3], [4, 5], [6]]));

var mergeArraysConcat = function(arrays){


return arrays.reduce( function(p,n){
return p.concat(n);
});
};
console.log(mergeArraysConcat([[1, 2, 3], [4, 5], [6]]));

const mergeArrays = (...arrays) => [].concat(...arrays);


console.log(mergeArrays([1, 2, 3], [4, 5], [6]));

http://rea
ctivex.io/learnrx/
http://underscorejs.org/
http://bilby.brianmckenna.org/
http://danieltao.com/lazy.js/
https://baconjs.github.io/
http://eliperelman.com/fn.js/
http://functionaljs.com/
http://ramdajs.com/0.20.1/index.html
http://swannodette.github.io/mori/

https://www.pack
tpub.com/web-development/functional-programming-javascrip
t

Chapter 10

Chapter 10
function increment(num){
return ++num;
}

increment(1)
num
2

Chapter 10

function sequentialSearch(array, item){


for (var i=0; i<array.length; i++){
if (item === array[i]){ //{1}
return i;
}
}
return -1;
}
{1}

{1}

-1 {1}

{1} -1

sequentialSearch

{1}
{1}

sequentialSearch

sequentialSearch

function sequentialSearch(array, item){


var cost = 0;
for (var i=0; i<array.length; i++){
cost++;
if (item === array[i]){ //{1}
return i;
}
}
console.log('cost for sequentialSearch with input size ' +
array.length + ' is ' + cost);
return -1;
}
function swap(array, index1, index2){
var aux = array[index1];
array[index1] = array[index2];
array[index2] = aux;
}

function bubbleSort(array){
var length = array.length;
for (var i=0; i<length; i++){ //{1}
for (var j=0; j<length-1; j++ ){ //{2}
if (array[j] > array[j+1]){
swap(array, j, j+1);
}
}
}
}

{1} {2}

function bubbleSort(array){
var length = array.length;
var cost = 0;
for (var i=0; i<length; i++){ //{1}
cost++;
for (var j=0; j<length-1; j++ ){ //{2}
cost++;
if (array[j] > array[j+1]){
swap(array, j, j+1);
}
}
}
console.log('cost for bubbleSort with input size ' + length + '
is ' + cost);
}

bubbleSort
bubbleSort
bigOChart chapter12
https://en.wikipe
dia.org/wiki/NP-completeness

http://goo.gl/gxIu9w

http://uva.onlinejudge.org/
http://www.spoj.com/

http://coderbyte.com/

https://projecteuler.net/

https://www.hackerrank.com

http://www.codechef.com/

http://www.topcoder.com/

https://github.com
71
53
193 46

125 68
184
195 30
195 30
193
187
283 188
189
30 189
30 187

40 184
41 187
185 186
60 190
62 63 185
64
65
66
64 33
63 32 33
61
61 32
62 63 32
62
62 63 274
185
46 100 86
47
48 275 276
47 279
48 276 278
53
274

247
248 249 123
30
166
167 168 30
166 30
282 30
30
199 200 30
201 202 203 126
201
203 204 205 199 207
206 210 211
207 208 209
225 226 212 213 214
226 227 228
228 229 144 145

140
270 143
121 140 141
96 97 98 142
126 142 143
144
284 141 142
284 143
141 142
284 143
284 142 143

149 151 215


151 215
215
26 212
26 194
24
25 246
25 246
246
24 246
24 25 159
26 121
113 50
118 119 50
115 116 117 51
52
254 52
257 258 259 260
260 66 67
263 67
255 256 257 68 69
254 123
217
8
12
9 10
33 10
37 271
36 270
38 45
40 160 161
41
39 136 137
37 138 139
34 36 138
34 137
137
80 161 162
81 161 162
82 83 84
80 81 98
44 98

60
60
252
31 253
31
31 8
167 192
217
230 218

49 274
52 258
49 267
49 268
145
269 149
270 151
274 146 147 148
274 148 149
271 149
269 145
243 244
274
191
31
27 28
269
172
196
7 284
10 231
231
197 198 233
232
195 169
195 8
195
196 57 58
199 200 58
59
192 58
194 59
195 59
194

215 266
267 268 56
266 267 57 58
57

13
284 22 23 24
284 20 21
282 17
125 20 21
16
159 160 15
149
272
273 113
271 111 112
272 113
111
107 113
111
7 100
8 260
28 29
26
27
167 26
26
258 145 159
259 260
257

221 141
221 222
264
265
263
274 262

261 233
262 263 234 235 236
74
266
155 267
158 255
156 157 158
159 218 219
221
100 101 219
102
274
101 53 55 56
103
105
108 109
110 7
105 106
108 12
111
192
185 254
181 254
180 181
182 284
183 284
281
41
281 282
283
283 88
282
123 93
91 92 93

29 88 89
28 29 90
90
41 90
43 91
42 89
44 91

17 236
18 237
18 238 239
19 239 242
19
20
19
20 274

250
174 175 252 253
173 174 251 252
191
219
219 220

94 95 246
96 247
96 246

255 175
254
254
176 177
180 181 224 225
177 179 225
246
69 243 244
69 70 231 233
69 70 233 234 235 236
70 236 237 238
229 230 231
229 195
230
231 284
284
184
184 78 79 80

151 74
153 77
153 78
154 76
246 78
76
129 75
77 78
129
129 133 134 74 75
129 131 132 75
129 134 135
129 130 131 84
84 85 86
123
125 30
126 30
124 134 135
126
125
127
128 252
252
206
206 214 284
206 284
206 215
206 217 213
127 212 213
164 194

165 283
166 283
166
166
128
171
172 173 41
174 175
173 174 15
16
164 192
168 169 170 171

53 72
54
72 30
30

274
11

You might also like