Last active
December 20, 2015 03:09
-
-
Save tomgullo/6061879 to your computer and use it in GitHub Desktop.
traverse_tree_nodes_in_js
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
function node(name, parent) { | |
return { | |
name:name, | |
parent:parent | |
} | |
} | |
root = node('root') | |
n2 = node('left', root) | |
root.left = n2 | |
n3 = node('2left', n2) | |
n2.left = n3 | |
n4 = node('right', root) | |
root.right = n4 | |
n5 = node('3left', n3) | |
n3.left = n5 | |
function size(nodz) { | |
if (!nodz) return 0; | |
return 1 + size(nodz.left) + size(nodz.right); | |
} | |
function max(nodz) { | |
if (!nodz) return -1; | |
return 1 + Math.max(max(nodz.left), max(nodz.right)); | |
} | |
function min(nodz) { | |
if (!nodz) return -1; | |
return 1 + Math.min(min(nodz.left), min(nodz.right)); | |
} | |
console.log('\nstart') | |
console.log(max(root)) | |
if (max(root) > (min(root) + 1)) { | |
console.log('not balanced'); | |
} | |
//console.log(size(root)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment