You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
108 lines
2.6 KiB
108 lines
2.6 KiB
//const numBeats = Math.ceil(Math.random()*9);
|
|
|
|
//let resultString = '';
|
|
//const instructionPosibilities = [ 'n', 'p', 'm', 'b' ]
|
|
|
|
//for(let i = 1; i <= numBeats; i++){
|
|
//const instructionIndex = Math.floor(Math.random()*instructionPosibilities.length);
|
|
|
|
//resultString += instructionPosibilities[instructionIndex] + ' ';
|
|
//}
|
|
|
|
//console.log(resultString);
|
|
|
|
const breakDown = (node)=>{
|
|
|
|
if(node.value < 4){
|
|
return node
|
|
}
|
|
|
|
//2 path
|
|
node.twoPath = {};
|
|
node.twoPath.left = { value: 2 }
|
|
node.twoPath.right = breakDown({value:node.value-2})
|
|
|
|
//3 path
|
|
if(node.value > 4){
|
|
node.threePath = {}
|
|
node.threePath.left = { value: 3 }
|
|
node.threePath.right = breakDown({value:node.value-3})
|
|
}
|
|
|
|
return node;
|
|
}
|
|
|
|
const tree = breakDown({value:10});
|
|
|
|
//console.dir(tree, {depth:null});
|
|
|
|
const getLeaves = (node)=>{
|
|
if(node.twoPath === undefined){
|
|
return node.value;
|
|
}
|
|
|
|
let twoPathResult = [];
|
|
const twoPathLeftValue = getLeaves(node.twoPath.left);
|
|
const twoPathRightValue = getLeaves(node.twoPath.right);
|
|
if(Number.isInteger(twoPathRightValue)){
|
|
twoPathResult[0] = twoPathLeftValue;
|
|
twoPathResult[1] = twoPathRightValue;
|
|
} else {
|
|
if(Number.isInteger(twoPathRightValue[0])){
|
|
twoPathRightValue.unshift(twoPathLeftValue);
|
|
} else {
|
|
for(let subArray of twoPathRightValue){
|
|
subArray.unshift(twoPathLeftValue);
|
|
}
|
|
}
|
|
twoPathResult = twoPathRightValue;
|
|
}
|
|
|
|
let threePathResult = [];
|
|
if(node.threePath !== undefined){
|
|
const threePathLeftValue = getLeaves(node.threePath.left);
|
|
const threePathRightValue = getLeaves(node.threePath.right);
|
|
if(Number.isInteger(threePathRightValue)){
|
|
threePathResult[0] = threePathLeftValue;
|
|
threePathResult[1] = threePathRightValue;
|
|
} else {
|
|
if(Number.isInteger(threePathRightValue[0])){
|
|
threePathRightValue.unshift(threePathLeftValue);
|
|
} else {
|
|
for(let subArray of threePathRightValue){
|
|
subArray.unshift(threePathLeftValue);
|
|
}
|
|
}
|
|
threePathResult = threePathRightValue;
|
|
}
|
|
}
|
|
|
|
if(threePathResult.length === 0){
|
|
return twoPathResult;
|
|
} else {
|
|
if(Number.isInteger(twoPathResult[0])){
|
|
if(Number.isInteger(threePathResult[0])){
|
|
return [twoPathResult, threePathResult]
|
|
} else {
|
|
const result = [twoPathResult];
|
|
result.push(threePathResult[0]);
|
|
return result;
|
|
}
|
|
} else {
|
|
if(Number.isInteger(threePathResult)){
|
|
twoPathResult.push(threePathResult)
|
|
} else {
|
|
if(Number.isInteger(threePathResult[0])){
|
|
twoPathResult.push(threePathResult)
|
|
} else {
|
|
twoPathResult = twoPathResult.concat(threePathResult);
|
|
}
|
|
}
|
|
return twoPathResult;
|
|
}
|
|
}
|
|
}
|
|
const leaves = getLeaves(tree);
|
|
|
|
console.dir(leaves, {depth:null});
|