Skip to content

Commit 7bac8f5

Browse files
committed
Update algorithms formatting
1 parent 205fbe0 commit 7bac8f5

38 files changed

Lines changed: 190 additions & 120 deletions

‎src/combinatorics/cartesianproduct.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@
2424
exports.cartesianProduct = cartesianProduct;
2525

2626
}(typeof exports === 'undefined' ? window : exports));
27+

‎src/combinatorics/combinations.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@
2828
exports.combinations = combinations;
2929

3030
}(typeof exports === 'undefined' ? window : exports));
31+

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

Lines changed: 59 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
}
3535

3636
/**
37-
* Inserts a node into the binary tree. The method's complexity is O(log n) in the average case and
37+
* Inserts a node into the binary tree. The method's
38+
* complexity is O(log n) in the average case and
3839
* O(n) in the worst case.
3940
*
4041
* @public
@@ -48,29 +49,34 @@
4849
}
4950
var insertKey;
5051
current = current || this._root;
51-
if (current.value > value)
52+
if (current.value > value) {
5253
insertKey = '_left';
53-
else
54+
} else {
5455
insertKey = '_right';
55-
if (!current[insertKey])
56+
}
57+
if (!current[insertKey]) {
5658
current[insertKey] = new Node(value, null, null, current);
57-
else
59+
} else {
5860
this.insert(value, current[insertKey]);
61+
}
5962
};
6063

6164
/**
62-
* Prints the nodes of the tree in order. It starts the tree traversal from a given node.
65+
* Prints the nodes of the tree in order.
66+
* It starts the tree traversal from a given node.
6367
*
6468
* @private
6569
* @param {Node} Node from which to start the traversal
6670
* @param {Function} Callback which will be called for each traversed node
6771
*/
6872
BinaryTree.prototype._inorder = function (current, callback) {
69-
if (!current)
73+
if (!current) {
7074
return;
75+
}
7176
this._inorder(current._left, callback);
72-
if (typeof callback === 'function')
77+
if (typeof callback === 'function') {
7378
callback(current);
79+
}
7480
this._inorder(current._right, callback);
7581
};
7682

@@ -92,10 +98,12 @@
9298
* @param {Function} Callback which will be called for each traversed node
9399
*/
94100
BinaryTree.prototype._postorder = function (current, callback) {
95-
if (!current)
101+
if (!current) {
96102
return;
97-
if (typeof callback === 'function')
103+
}
104+
if (typeof callback === 'function') {
98105
callback(current);
106+
}
99107
this._postorder(current._left, callback);
100108
this._postorder(current._right, callback);
101109
};
@@ -118,10 +126,12 @@
118126
* @param {Function} Callback which will be called for each traversed node
119127
*/
120128
BinaryTree.prototype._preorder = function (current, callback) {
121-
if (!current)
129+
if (!current) {
122130
return;
123-
if (typeof callback === 'function')
131+
}
132+
if (typeof callback === 'function') {
124133
callback(current);
134+
}
125135
this._preorder(current._left, callback);
126136
this._preorder(current._right, callback);
127137
};
@@ -147,25 +157,29 @@
147157
};
148158

149159
/**
150-
* Finds a node by it's value in given sub-tree. Average runtime complexity: O(log n).
160+
* Finds a node by it's value in
161+
* given sub-tree. Average runtime complexity: O(log n).
151162
*
152163
* @private
153164
* @param {Number|String} Value of the node which should be found
154165
* @param {Node} Current node to be checked
155166
*/
156167
BinaryTree.prototype._find = function (value, current) {
157-
if (!current)
168+
if (!current) {
158169
return null;
170+
}
159171

160-
if (current.value === value)
172+
if (current.value === value) {
161173
return current;
174+
}
162175

163-
if (current.value > value)
176+
if (current.value > value) {
164177
return this._find(value, current._left);
178+
}
165179

166-
if (current.value < value)
180+
if (current.value < value) {
167181
return this._find(value, current._right);
168-
182+
}
169183
};
170184

171185
/**
@@ -182,10 +196,11 @@
182196
this._root._parent = null;
183197
} else {
184198

185-
if (parent._left === oldChild)
199+
if (parent._left === oldChild) {
186200
parent._left = newChild;
187-
else
201+
} else {
188202
parent._right = newChild;
203+
}
189204

190205
if (newChild) {
191206
newChild._parent = parent;
@@ -198,11 +213,13 @@
198213
*
199214
* @public
200215
* @param {Node} Node to be removed
201-
* @returns {boolean} True/false depending on whether the given node is removed
216+
* @returns {boolean} True/false depending
217+
* on whether the given node is removed
202218
*/
203219
BinaryTree.prototype.remove = function (node) {
204-
if (!node)
220+
if (!node) {
205221
return false;
222+
}
206223

207224
if (node._left && node._right) {
208225
var min = this._findMin(node._right),
@@ -212,12 +229,13 @@
212229
min.value = temp;
213230
return this.remove(min);
214231
} else {
215-
if (node._left)
232+
if (node._left) {
216233
this._replaceChild(node._parent, node, node._left);
217-
else if (node._right)
234+
} else if (node._right) {
218235
this._replaceChild(node._parent, node, node._right);
219-
else
236+
} else {
220237
this._replaceChild(node._parent, node, null);
238+
}
221239
return true;
222240
}
223241
};
@@ -232,10 +250,12 @@
232250
*/
233251
BinaryTree.prototype._findMin = function (node, current) {
234252
current = current || { value: Infinity };
235-
if (!node)
253+
if (!node) {
236254
return current;
237-
if (current.value > node.value)
255+
}
256+
if (current.value > node.value) {
238257
current = node;
258+
}
239259
return this._findMin(node._left, current);
240260
};
241261

@@ -249,10 +269,12 @@
249269
*/
250270
BinaryTree.prototype._findMax = function (node, current) {
251271
current = current || { value: -Infinity };
252-
if (!node)
272+
if (!node) {
253273
return current;
254-
if (current.value < node.value)
274+
}
275+
if (current.value < node.value) {
255276
current = node;
277+
}
256278
return this._findMax(node._right, current);
257279
};
258280

@@ -331,7 +353,8 @@
331353
if (!node) {
332354
return 0;
333355
}
334-
return 1 + Math.max(this._getHeight(node._left), this._getHeight(node._right));
356+
return 1 + Math.max(this._getHeight(node._left),
357+
this._getHeight(node._right));
335358
};
336359

337360
/**
@@ -340,11 +363,13 @@
340363
* @public
341364
* @returns {Node} The lowest common ancestor of the two nodes or null
342365
*/
343-
BinaryTree.prototype.lowestCommonAncestor = function (firstNode, secondNode, current) {
366+
BinaryTree.prototype.lowestCommonAncestor =
367+
function (firstNode, secondNode) {
344368
return this._lowestCommonAncestor(firstNode, secondNode, this._root);
345369
};
346370

347-
BinaryTree.prototype._lowestCommonAncestor = function (firstNode, secondNode, current) {
371+
BinaryTree.prototype._lowestCommonAncestor =
372+
function (firstNode, secondNode, current) {
348373
var firstNodeInLeft = this._existsInSubtree(firstNode, current._left),
349374
secondNodeInLeft = this._existsInSubtree(secondNode, current._left),
350375
firstNodeInRight = this._existsInSubtree(firstNode, current._right),
@@ -369,7 +394,8 @@
369394
if (node === root.value) {
370395
return true;
371396
}
372-
return this._existsInSubtree(node, root._left) || this._existsInSubtree(node, root._right);
397+
return this._existsInSubtree(node, root._left) ||
398+
this._existsInSubtree(node, root._right);
373399
};
374400

375401
exports.BinaryTree = BinaryTree;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@
131131
};
132132

133133
// adjust the max value
134-
IntervalTree.prototype._removeHelper = function (interval, node) {
134+
IntervalTree.prototype._removeHelper =
135+
function (interval, node) {
135136
if (!node) {
136137
return;
137138
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,5 @@
182182

183183
global.RBTree = RBTree;
184184

185-
186185
}(typeof window === 'undefined' ? module.exports : window));
187186

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// TODO
2-
// 1) The algorithm is quite ineffective, better use
3-
// Ukkomen's algorithm to build it in O(n) complexity.
41
(function (exports) {
52
'use strict';
63

@@ -94,10 +91,6 @@
9491
// var suffix = new SuffixTree();
9592
// suffix.build('banana');
9693
// console.log(suffix);
97-
//
98-
// var st = new SuffixTree();
99-
// st.build('Since these cases are very common use cases, the problem was easily solved by choosing either a random index for the pivot, choosing the middle index of the partition or (especially for longer partitions) choosing the median of the first, middle and last element of the partition for the pivot. With these modifications, the worst case of Quick sort has less chances to occur, but worst case can still occur if the input array is such that the maximum (or minimum) element is always chosen as pivot.');
100-
// console.log(JSON.stringify(st));
10194

10295
exports.Node = Node;
10396
exports.SuffixTree = SuffixTree;

‎src/graphs/others/topological-sort.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
* @returns {Array} Ordered vertices.
3131
*
3232
* @example
33-
* var topsort = require('path-to-algorithms/src/graphs/others/topological-sort').topologicalSort;
33+
* var topsort =
34+
* require('path-to-algorithms/src/graphs/' +
35+
* 'others/topological-sort').topologicalSort;
3436
* var graph = {
3537
* v1: ['v2', 'v5'],
3638
* v2: [],
@@ -55,4 +57,4 @@
5557

5658
exports.topologicalSort = topologicalSort;
5759

58-
}(typeof exports === 'undefined' ? window : exports));
60+
}(typeof exports === 'undefined' ? window : exports));

‎src/graphs/shortest-path/dijkstra.js‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(function (exports) {
22
'use strict';
33

4-
var dijkstra = (function() {
4+
var dijkstra = (function () {
55

66
var Heap = require('../../data-structures/heap.js').Heap,
77
current,
@@ -76,13 +76,15 @@
7676
* @returns {Number} The shortest distance between two nodes.
7777
*
7878
* @example
79-
* var dijkstra = require('path-to-algorithms/src/graphs/shortest-path/dijkstra').dijkstra;
80-
* var distMatrix = [[Infinity, 7, 9, Infinity, Infinity, 16],
81-
* [7, Infinity, 10, 15, Infinity, Infinity],
82-
* [9, 10, Infinity, 11, Infinity, 2],
83-
* [Infinity, 15, 11, Infinity, 6, Infinity],
84-
* [Infinity, Infinity, Infinity, 6, Infinity, 9],
85-
* [16, Infinity, 2, Infinity, 9, Infinity]];
79+
* var dijkstra =
80+
* require('path-to-algorithms/src/graphs/shortest-path/dijkstra').dijkstra;
81+
* var distMatrix =
82+
* [[Infinity, 7, 9, Infinity, Infinity, 16],
83+
* [7, Infinity, 10, 15, Infinity, Infinity],
84+
* [9, 10, Infinity, 11, Infinity, 2],
85+
* [Infinity, 15, 11, Infinity, 6, Infinity],
86+
* [Infinity, Infinity, Infinity, 6, Infinity, 9],
87+
* [16, Infinity, 2, Infinity, 9, Infinity]];
8688
* var shortestDist = dijkstra(0, 2, distMatrix); // 9
8789
*/
8890
return function (src, dest, graph) {
@@ -92,7 +94,8 @@
9294
for (var i = 0; i < graph.length; i += 1) {
9395
if (current.node !== i && //if it's not the current node
9496
!visited[i] && //and if we haven't visited this node
95-
Number.isFinite(graph[i][current.node])) { //and this node is sibling of the current...
97+
//and this node is sibling of the current...
98+
Number.isFinite(graph[i][current.node])) {
9699

97100
tempDistance = current.distance + graph[i][current.node];
98101
if (tempDistance < distance[i].distance) {
@@ -107,7 +110,7 @@
107110
}
108111
return distance[dest].distance;
109112
};
110-
113+
111114
})();
112115

113116
exports.dijkstra = dijkstra;

‎src/others/hanoi.js‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
/*
22
* Hanoi towers
33
*/
4-
function hanoi(count, source, intermediate, goal, result) {
4+
(function (exports) {
55
'use strict';
6-
result = result || [];
7-
if (count === 1) {
8-
result.push([source, goal]);
9-
} else {
10-
hanoi(count - 1, source, goal, intermediate, result);
11-
result.push([source, goal]);
12-
hanoi(count - 1, intermediate, source, goal, result);
6+
7+
function hanoi(count, source, intermediate, goal, result) {
8+
result = result || [];
9+
if (count === 1) {
10+
result.push([source, goal]);
11+
} else {
12+
hanoi(count - 1, source, goal, intermediate, result);
13+
result.push([source, goal]);
14+
hanoi(count - 1, intermediate, source, goal, result);
15+
}
16+
return result;
1317
}
14-
return result;
15-
}
18+
19+
exports.hanoi = hanoi;
20+
21+
})(typeof window === 'undefined' ? module.exports : window);

‎src/searching/binarysearch/binarysearch.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
(function (exports) {
2+
'use strict';
23

34
/**
4-
* Searchs for specific element in given array using the binary search algorithm.
5+
* Searchs for specific element in given array using
6+
* the binary search algorithm.
57
* It's complexity is O(log n)
68
*
79
* @public

0 commit comments

Comments
 (0)