Created
April 29, 2017 17:18
-
-
Save cklanac/ae95ee90411db757b663373c9b1b962d to your computer and use it in GitHub Desktop.
Recursion examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const organization = {name: "Zuckerberg", reports: [ | |
{name: "Schroepfer", reports:[ | |
{name:"Bosworth", reports: [ | |
{name:'Steve'}, | |
{name:'Kyle'}, | |
{name:'Andra', reports: [ | |
{name:'Amy'}, | |
{name:'Chuck'}, | |
{name:'Vinni'} | |
]} | |
]}, | |
{name:"Kirkland", reports: [ | |
{name:'Dom'}, | |
{name:'Keefer'}, | |
{name:'Maggie', reports: [ | |
{name:'Bryan'}, | |
{name:'Todd'}, | |
{name:'Dan'} | |
]} | |
]} | |
]}, | |
{name: "Warner", reports:[ | |
{name:"Denver", reports: [ | |
{name:'Dave'}, | |
{name:'Elizabeth'}, | |
{name:'Diane', reports: [ | |
{name:'Frank'}, | |
{name:'Stan'}, | |
{name:'Roy'} | |
]} | |
]}, | |
{name:"Jabobs", reports: [ | |
{name:'Nicholas'}, | |
{name:'Alicia'}, | |
{name:'Alexander', reports: [ | |
{name:'Jon'}, | |
{name:'William'}, | |
{name:'Paul'} | |
]} | |
]} | |
]} | |
]} | |
// ============================== | |
function traverse(indent, level) { | |
level = level || 1; | |
if (isArray(indent)) { | |
traverseArray(indent, level); | |
} else if ((typeof indent === 'object') && (indent !== null)) { | |
traverseObject(indent, level); | |
} else { | |
console.log(" ".repeat(level), indent); | |
} | |
} | |
function isArray(o) { | |
return Object.prototype.toString.call(o) === '[object Array]'; | |
} | |
function traverseArray(arr, level) { | |
arr.forEach(function(indent) { | |
traverse(indent, level + 1); | |
}); | |
} | |
function traverseObject(obj, level) { | |
for (var key in obj) { | |
if (obj.hasOwnProperty(key)) { | |
traverse(obj[key], level + 1); | |
} | |
} | |
} | |
traverse(organization); | |
// ============================== | |
function printMixed (data, tab = 0) => { | |
let spaces = ' '.repeat(tab); | |
let string = spaces + data.name + '\n'; | |
if (!data.reports) { | |
return string; | |
} else { | |
tab++; | |
data.reports.forEach(report => { | |
string += printMixed(report, tab); | |
}); | |
return string; | |
} | |
} | |
console.log(printMixed(organization)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
7! = 7 × 6 × 5 × 4 × 3 × 2 × 1 = 5040 | |
------------------ | |
7! = 7 x 6! = 5040 | |
6! = 6 x 5! = 720 | |
5! = 5 x 4! = 120 | |
4! = 4 x 3! = 24 | |
3! = 3 x 2! = 6 | |
2! = 2 x 1! = 2 | |
1! = 1 x 1 = 1 | |
0! = 1 | |
**/ | |
// Iteration | |
function factorialIter(num){ | |
var result = 1; | |
for (var i = 1; i <= num; i++) { | |
result = result * i; | |
} | |
return result; | |
} | |
// Accumulator - computes during stack push | |
function factorialAccumRecur(num, total = 1) { | |
if (num === 0) { | |
return total; | |
} | |
return factorialAccumRecur(num - 1, total * num); | |
} | |
// Tail Recursion - computes during stack pop | |
function factorialTailRecur(num) { | |
if(num === 0) { | |
return 1 | |
} else { | |
return num * factorialTailRecur(num - 1); | |
} | |
} | |
console.log('factorialIter:', factorialIter(100)); | |
console.log('factorialAccumRecur:', factorialAccumRecur(100)); | |
console.log('factorialTailRecur:', factorialTailRecur(100)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var organization = { | |
"Zuckerberg": { | |
"Schroepfer": { | |
"Bosworth": { | |
"Steve":{}, | |
"Kyle":{}, | |
"Andra":{} | |
}, | |
"Zhao": { | |
"Richie":{}, | |
"Sofia":{}, | |
"Jen":{} | |
}, | |
"Badros": { | |
"John":{}, | |
"Mike":{}, | |
"Pat":{} | |
}, | |
"Parikh": { | |
"Zach":{}, | |
"Ryan":{}, | |
"Tes":{} | |
} | |
}, | |
"Schrage": { | |
"VanDyck": { | |
"Sabrina":{}, | |
"Michelle":{}, | |
"Josh":{} | |
}, | |
"Swain": { | |
"Blanch":{}, | |
"Tom":{}, | |
"Joe":{} | |
}, | |
"Frankovsky": { | |
"Jasee":{}, | |
"Brian":{}, | |
"Margaret":{} | |
} | |
}, | |
"Sandberg": { | |
"Goler": { | |
"Eddie":{}, | |
"Julie":{}, | |
"Annie":{} | |
}, | |
"Hernandez": { | |
"Rowi":{}, | |
"Inga":{}, | |
"Morgan":{} | |
}, | |
"Moissinac": { | |
"Amy":{}, | |
"Chuck":{}, | |
"Vinni":{} | |
}, | |
"Kelley": { | |
"Eric":{}, | |
"Ana":{}, | |
"Wes":{} | |
} | |
}}}; | |
function traverseA(data, depth = 0) { | |
var indent = " ".repeat(depth * 4); | |
Object.keys(data).forEach(key => { | |
console.log(indent + key); | |
traverseA(data[key], depth + 1) | |
}); | |
} | |
traverseA(organization) | |
function traverseB(node, indent) { | |
indent = indent || 0; | |
for (var key in node) { | |
console.log(" ".repeat(indent), key); | |
traverseB(node[key], indent + 4); | |
} | |
} | |
traverseB(organization); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment