binary-type-tree
Version:
Binary Trees written in TypeScript
185 lines • 4.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Shuffle numbers in array
* @param nums
* @returns {number[]}
*/
exports.shuffleNumbersArray = (nums) => {
let tmp;
let current;
let top = nums.length;
if (top) {
while (--top) {
current = Math.floor(Math.random() * (top + 1));
tmp = nums[current];
nums[current] = nums[top];
nums[top] = tmp;
}
}
return nums;
};
/**
* Return an array with the numbers from 0 to n-1, in a random order.
* @param n
* @returns {number[]}
*/
exports.getRandomArray = (n) => {
const b = [];
for (let i = 0; i < n; ++i) {
b.push(i);
}
return exports.shuffleNumbersArray(b);
};
/**
* Default compareKeys function
* @param a
* @param b
* @returns {number}
* if a < b then return -1
* if a > b then return 1
* if a === b then return 0
* else throw could not compare error.
*/
exports.defaultCompareKeysFunction = (a, b) => {
if (a < b) {
return -1;
}
else if (a > b) {
return 1;
}
else if (a === b) {
return 0;
}
else {
throw new Error(`Could not compare keys, please check types. ${a}:${typeof a}, ${b}:${typeof b}`);
}
};
/**
* Default compareValues function
* @param a
* @param b
* @returns {number}
* if a < b then return -1
* if a > b then return 1
* if a === b then return 0
* else throw could not compare error.
*/
exports.defaultCompareValues = (a, b) => {
if (a.length !== b.length) {
throw new Error(`Cannot compare values of different length. a:${a.length},b:${b.length}, a:${a},b:${b}`);
}
else {
const returnNum = [];
for (let i = a.length - 1; i >= 0; i--) {
if (a[i] < b[i]) {
returnNum.push(-1);
}
else if (a[i] > b[i]) {
returnNum.push(1);
}
else if (a[i] === b[i]) {
returnNum.push(0);
}
else {
throw new Error(`Could not compare values, please check types. ${a[i]}:${typeof a[i]}, ${b[i]}:${typeof b[i]}`);
}
}
return returnNum;
}
};
/**
* Check whether two keys are equal with '==='
* @param a
* @param b
* @returns {boolean}
*/
exports.defaultCheckKeyEquality = (a, b) => {
return a === b;
};
/**
* Check whether to values are equal
* @param a
* @param b
* @returns {boolean}
*/
exports.defaultCheckValueEquality = (a, b) => {
return a === b;
};
/**
* get array with number of items in each row. [1,2,4,8,16] depending
* on the height of the btree
* @param height
* @returns {number[]}
*/
exports.getRowsArrayFromHeight = (height) => {
const returnArr = [];
for (let i = 0; i <= height; i++) {
returnArr.push(Math.pow(2, i));
}
return returnArr;
};
/**
* create an array based on the height of the bTee to insert nodes key
* and value. example: height 3, [ [], [], [], [] ]. 3-0
* @param height
* @returns {any}
*/
exports.createRefArrayFromTreeHeight = (height) => {
const returnArr = [];
for (let i = 0; i <= height; i++) {
returnArr.push(new Array(0));
}
return returnArr;
};
const fillRefA = (prev, current) => {
const a = [];
try {
const prefAOdds = prev.filter((val) => (val % 2) !== 0);
const prefAEvens = prev.filter((val) => (val % 2) === 0);
const currentAOdds = current.filter((val) => (val % 2) !== 0);
const currentAEvens = current.filter((val) => (val % 2) === 0);
const newAOdds = currentAOdds.filter((val) => !prefAOdds.includes(val));
const newAEvens = currentAEvens.filter((val) => !prefAEvens.includes(val));
prefAOdds.forEach((v, i) => {
a.push(newAOdds[i]);
a.push(prefAOdds[i]);
});
prefAOdds.forEach((v, i) => {
a.push(newAEvens[i]);
a.push(prefAEvens[i]);
});
}
catch (e) {
throw new Error(e);
}
return a;
};
/**
* Create the reference index master array. This tells another method
* where the nodes need to be inserted in the final array that is turned
* into JSON.
* @param refArray
* @returns {any[]}
*/
exports.createRandomSortedIndex = (refArray) => {
return new Promise((resolve, reject) => {
const returnArray = [];
try {
refArray.forEach((val, ind) => {
returnArray.push([]);
refArray[ind].forEach((v, i) => {
returnArray[ind].push(i);
});
});
for (let i = 2; i < returnArray.length; i++) {
returnArray[i] = fillRefA(returnArray[i - 1], returnArray[i]);
}
}
catch (e) {
reject(e);
}
resolve(returnArray);
});
};
//# sourceMappingURL=bTreeUtils.js.map