Skip to content

Commit c05ebe3

Browse files
author
AndreyGeonya
committed
resolvemerge conflict
2 parents 9a22cc5 + ea88a54 commit c05ebe3

59 files changed

Lines changed: 283 additions & 255 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.jscsrc‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"requireSpacesInFunctionExpression": {
55
"beforeOpeningCurlyBrace": true
66
},
7+
"disallowMultipleVarDecl": true,
78
"requireSpacesInsideObjectBrackets": "allButNested",
89
"disallowSpacesInsideArrayBrackets": true,
910
"disallowSpacesInsideParentheses": true,
@@ -22,6 +23,7 @@
2223
"disallowMultipleLineBreaks": true,
2324
"disallowKeywordsOnNewLine": ["else"],
2425
"requireLineFeedAtFileEnd": true,
26+
"disallowTrailingWhitespace": true,
2527
"excludeFiles": ["node_modules/**", "bower_components/**"],
2628
"validateIndentation": 2
2729
}

‎doc-config.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"./src/graphs/spanning-trees/",
1111
"./src/data-structures/",
1212
"./src/combinatorics/"
13+
"./src/primes/"
1314
],
1415
"includePattern": ".+\\.js(doc)?$",
1516
"excludePattern": "docs"

‎gulpfile.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ gulp.task('jscs', function () {
2828
.pipe(jscs());
2929
});
3030

31-
gulp.task('build', ['lint', 'test']);
31+
gulp.task('build', ['lint', 'jscs', 'test']);

‎readme.md‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ npm install
2020

2121
**To setup repository with documentation**
2222

23-
1) go to the parent directory of the `javascript-algorithms` folder and call:
23+
- Go to the parent directory of the `javascript-algorithms` folder and call:
2424

2525
```bash
2626
git clone git@github.com:mgechev/javascript-algorithms.git javascript-algorithms-docs
2727
```
2828

29-
2) go to the `javascript-algorithms-docs` folder and change current branch to `gh-pages`:
29+
- Go to the `javascript-algorithms-docs` folder and change current branch to `gh-pages`:
3030

3131
```bash
3232
git checkout gh-pages
@@ -46,16 +46,10 @@ and all files in `javascript-algorithms-docs` folder will be updated.
4646

4747
**To run tests**
4848

49-
You should install `jasmine-node`:
50-
51-
```bash
52-
npm install -g jasmine-node
53-
```
54-
5549
Call:
5650

5751
```bash
58-
jasmine-node test/
52+
gulp test
5953
```
6054

6155
and all `*.spec.js` files will be executed.
@@ -77,7 +71,7 @@ If the build is not successful fix your code in order the tests and jshint valid
7771

7872
## Contributors
7973

80-
[![mgechev](https://avatars.githubusercontent.com/u/455023?v=3&s=117)](https://github.com/mgechev)[![AndreyGeonya](https://avatars.githubusercontent.com/u/773648?v=3&s=117)](https://github.com/AndreyGeonya)[![Microfed](https://avatars.githubusercontent.com/u/613179?v=3&s=117)](https://github.com/Microfed)[![contra](https://avatars.githubusercontent.com/u/425716?v=3&s=117)](https://github.com/contra)
74+
[![mgechev](https://avatars.githubusercontent.com/u/455023?v=3&s=117)](https://github.com/mgechev)[![AndreyGeonya](https://avatars.githubusercontent.com/u/773648?v=3&s=117)](https://github.com/AndreyGeonya)[![pvoznenko](https://avatars.githubusercontent.com/u/1098414?v=3&s=117)](https://github.com/pvoznenko)[![Microfed](https://avatars.githubusercontent.com/u/613179?v=3&s=117)](https://github.com/Microfed)[![contra](https://avatars.githubusercontent.com/u/425716?v=3&s=117)](https://github.com/contra)
8175

8276
## License
8377

‎src/compression/runlength/runlength.js‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* Run-length encoding.
3-
* The idea of this algorithm is to remove the usless zeros and
3+
* The idea of this algorithm is to remove the usless zeros and
44
* give us representation of string in binary which in which the
55
* zeros will be stripped and replaced with their count.
66
*/
@@ -14,9 +14,9 @@
1414
* This takes O(n).
1515
*/
1616
function convertToAscii(str) {
17-
var result = '',
18-
currentChar = '',
19-
i = 0;
17+
var result = '';
18+
var currentChar = '';
19+
var i = 0;
2020
for (; i < str.length; i += 1) {
2121
currentChar = str[i].charCodeAt(0).toString(2);
2222
if (currentChar.length < 8) {
@@ -34,11 +34,11 @@
3434
* Takes O(n^2).
3535
*/
3636
function runLength(vector) {
37-
var result = '',
38-
zeros = 0,
39-
zerosTemp = '',
40-
wordLength = 0,
41-
i = 0;
37+
var result = '';
38+
var zeros = 0;
39+
var zerosTemp = '';
40+
var wordLength = 0;
41+
var i = 0;
4242
for (; i < vector.length; i += 1) {
4343
if (vector[i] === '0') {
4444
zeros += 1;

‎src/data-structures/binary-search-tree.js‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@
190190
/**
191191
* Finds a node by it's value in a given sub-tree.
192192
* Average time complexity: O(log N).
193-
*
193+
*
194194
* @private
195195
* @param {Number|String} Value of the node which should be found.
196196
* @param {Node} Current node to be checked.
@@ -221,7 +221,7 @@
221221
* @param {Node} oldChild Child to be replaced.
222222
* @param {Node} newChild Child replacement.
223223
*/
224-
exports.BinaryTree.prototype._replaceChild =
224+
exports.BinaryTree.prototype._replaceChild =
225225
function (parent, oldChild, newChild) {
226226
if (!parent) {
227227
this._root = newChild;
@@ -254,8 +254,8 @@
254254
}
255255

256256
if (node._left && node._right) {
257-
var min = this._findMin(node._right),
258-
temp = node.value;
257+
var min = this._findMin(node._right);
258+
var temp = node.value;
259259

260260
node.value = min.value;
261261
min.value = temp;
@@ -362,9 +362,9 @@
362362
if (!root) {
363363
return 0;
364364
}
365-
var leftHeight = this._getHeight(root._left),
366-
rightHeight = this._getHeight(root._right),
367-
path = leftHeight + rightHeight + 1;
365+
var leftHeight = this._getHeight(root._left);
366+
var rightHeight = this._getHeight(root._right);
367+
var path = leftHeight + rightHeight + 1;
368368
return Math.max(path, getDiameter(root._left), getDiameter(root._right));
369369
}.bind(this);
370370
return getDiameter(this._root);
@@ -401,10 +401,10 @@
401401

402402
exports.BinaryTree.prototype._lowestCommonAncestor =
403403
function (firstNode, secondNode, current) {
404-
var firstNodeInLeft = this._existsInSubtree(firstNode, current._left),
405-
secondNodeInLeft = this._existsInSubtree(secondNode, current._left),
406-
firstNodeInRight = this._existsInSubtree(firstNode, current._right),
407-
secondNodeInRight = this._existsInSubtree(secondNode, current._right);
404+
var firstNodeInLeft = this._existsInSubtree(firstNode, current._left);
405+
var secondNodeInLeft = this._existsInSubtree(secondNode, current._left);
406+
var firstNodeInRight = this._existsInSubtree(firstNode, current._right);
407+
var secondNodeInRight = this._existsInSubtree(secondNode, current._right);
408408
if ((firstNodeInLeft && secondNodeInRight) ||
409409
(firstNodeInRight && secondNodeInLeft)) {
410410
return current;

‎src/data-structures/heap.js‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@
7171
* @param {Number} index The parent.
7272
*/
7373
exports.Heap.prototype._heapify = function (index) {
74-
var extr = index,
75-
left = 2 * index + 1,
76-
right = 2 * index + 2,
77-
temp;
74+
var extr = index;
75+
var left = 2 * index + 1;
76+
var right = 2 * index + 2;
77+
var temp;
7878

7979
if (left < this._heap.length &&
8080
this._cmp(this._heap[left], this._heap[index]) > 0) {
@@ -97,17 +97,17 @@
9797
/**
9898
* Changes the key.<br><br>
9999
* Complexity: O(log N).
100-
*
100+
*
101101
* @public
102102
* @param {Number} index Index of the value which should be changed.
103103
* @param {Number|Object} value New value according to the index.
104104
* @return {Number} New position of the element.
105105
*/
106106
exports.Heap.prototype.changeKey = function (index, value) {
107107
this._heap[index] = value;
108-
var elem = this._heap[index],
109-
parent = Math.floor(index / 2),
110-
temp;
108+
var elem = this._heap[index];
109+
var parent = Math.floor(index / 2);
110+
var temp;
111111
if (elem !== undefined) {
112112
while (parent >= 0 && this._cmp(elem, this._heap[parent]) > 0) {
113113
temp = this._heap[parent];

‎src/data-structures/interval-tree.js‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
/**
2626
* Node which describes an interval.
27-
*
27+
*
2828
* @public
2929
* @constructor
3030
* @param {Number} start Start of the interval.
@@ -39,7 +39,7 @@
3939
*/
4040
this.interval = [start, end];
4141
/**
42-
* Max endpoint in subtree which starts from this node.
42+
* Max endpoint in subtree which starts from this node.
4343
* @member {Number}
4444
*/
4545
this.max = -Infinity;
@@ -106,7 +106,7 @@
106106

107107
/**
108108
* Add new interval to the tree.
109-
*
109+
*
110110
* @public
111111
* @param {Array} intreval Array with start and end points of the interval.
112112
*/
@@ -125,7 +125,8 @@
125125
if (node.interval[0] <= point && node.interval[1] >= point) {
126126
return true;
127127
}
128-
var result = false, temp;
128+
var result = false;
129+
var temp;
129130
['left', 'right'].forEach(function (key) {
130131
temp = node[key];
131132
if (temp) {
@@ -140,7 +141,7 @@
140141
/**
141142
* Checks or point belongs to at least one intarval from the tree.<br><br>
142143
* Complexity: O(log N).
143-
*
144+
*
144145
* @public
145146
* @method
146147
* @param {Number} point Point which should be checked.
@@ -157,7 +158,8 @@
157158
if (intersects(node.interval, interval)) {
158159
return true;
159160
}
160-
var result = false, temp;
161+
var result = false;
162+
var temp;
161163
['left', 'right'].forEach(function (side) {
162164
temp = node[side];
163165
if (temp && temp.max >= interval[0]) {
@@ -194,7 +196,7 @@
194196

195197
/**
196198
* Returns height of the tree.
197-
*
199+
*
198200
* @public
199201
* @method
200202
* @return {Number} Height of the tree.
@@ -212,8 +214,10 @@
212214
* @return {Node} Node with the largest endpoint.
213215
*/
214216
exports.IntervalTree.prototype.findMax = function (node) {
215-
var stack = [node],
216-
current, max = -Infinity, maxNode;
217+
var stack = [node];
218+
var current;
219+
var max = -Infinity;
220+
var maxNode;
217221
while (stack.length) {
218222
current = stack.pop();
219223
if (current.left) {
@@ -275,8 +279,8 @@
275279
// Adjust the max value
276280
var p = node.parentNode;
277281
if (p) {
278-
var maxNode = this.findMax(p),
279-
max = maxNode.interval[1];
282+
var maxNode = this.findMax(p);
283+
var max = maxNode.interval[1];
280284
while (maxNode) {
281285
if (maxNode.max === node.interval[1]) {
282286
maxNode.max = max;

‎src/data-structures/linked-list.js‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,9 @@
138138
if (this.first === null) {
139139
return false;
140140
}
141-
var temp = this.first,
142-
next, prev;
141+
var temp = this.first;
142+
var next;
143+
var prev;
143144
while (temp) {
144145
if (temp.data === data) {
145146
next = temp.next;
@@ -171,8 +172,8 @@
171172
* @return {Boolean} Returns true if linked list contains cycle.
172173
*/
173174
exports.LinkedList.prototype.hasCycle = function () {
174-
var fast = this.first,
175-
slow = this.first;
175+
var fast = this.first;
176+
var slow = this.first;
176177
while (true) {
177178
if (fast === null) {
178179
return false;
@@ -245,9 +246,9 @@
245246
if (!this.first || !this.first.next) {
246247
return;
247248
}
248-
var current = this.first.next,
249-
prev = this.first,
250-
temp;
249+
var current = this.first.next;
250+
var prev = this.first;
251+
var temp;
251252
while (current) {
252253
temp = current.next;
253254
current.next = prev;

‎src/data-structures/red-black-tree.js‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Red-Black tree is a data structure which is
33
* a type of self-balancing binary search tree.
4-
*
4+
*
55
* @example
66
*
77
* var RBTree = require('../src/data-structures/red-black-tree').RBTree;
@@ -25,7 +25,7 @@
2525
* });
2626
*
2727
* console.log(rbTree.get(1989)); // { name: 'Garry', surname: 'Fisher' }
28-
*
28+
*
2929
* @module data-structures/red-black-tree
3030
*/
3131
(function (exports) {
@@ -43,7 +43,7 @@
4343

4444
/**
4545
* Node of the Red-Black tree.
46-
*
46+
*
4747
* @private
4848
* @constructor
4949
* @param {Number} key Key of the node.
@@ -105,7 +105,7 @@
105105

106106
/**
107107
* Red-Black Tree.
108-
*
108+
*
109109
* @public
110110
* @constructor
111111
*/
@@ -116,7 +116,7 @@
116116
/**
117117
* Add value associated with a given key.<br><br>
118118
* Complexity: O(log N).
119-
*
119+
*
120120
* @public
121121
* @method
122122
* @param {Number} key Key.
@@ -130,7 +130,7 @@
130130
/**
131131
* Returns true or false depending on whether
132132
* given node is red.
133-
*
133+
*
134134
* @private
135135
* @method
136136
* @param {Node} node Node which sould be checked.
@@ -146,7 +146,7 @@
146146
/**
147147
* Helper function for insertion of given key,
148148
* value pair into the Red-Black tree.
149-
*
149+
*
150150
* @private
151151
* @method
152152
* @param {Number} key Key.

0 commit comments

Comments
 (0)