Skip to content

Instantly share code, notes, and snippets.

@tomgullo
Last active December 20, 2015 03:09
Show Gist options
  • Save tomgullo/6061879 to your computer and use it in GitHub Desktop.
Save tomgullo/6061879 to your computer and use it in GitHub Desktop.
traverse_tree_nodes_in_js
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