-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhelpers.js
45 lines (30 loc) · 943 Bytes
/
helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const BinarySearchTree = require('./data-structures/trees/binary-search');
exports.swap = function (array, a, b) {
var temp = array[a];
array[b] = (array[a] = array[b], temp);
}
function makeSortedArray() {
var sorted = [];
for (var i = 65; i < 91; ++i) sorted.push(i);
return sorted;
}
var sortedArray = makeSortedArray();
exports.sorted = sortedArray;
// Fisher-Yates shuffle
function makeUnsortedArray() {
var arrayCopy = sortedArray.slice(); // copy
var currentIndex = arrayCopy.length;
while (currentIndex) {
var randomIndex = Math.floor(Math.random() * currentIndex);
exports.swap(arrayCopy, --currentIndex, randomIndex);
}
return arrayCopy;
}
var unsortedArray = makeUnsortedArray();
exports.unsorted = unsortedArray;
exports.initTree = function (array) {
if (!array) array = unsortedArray;
var tree = new BinarySearchTree();
array.forEach((value) => tree.add(value));
return tree;
};