Created
January 9, 2013 02:15
-
-
Save apiv/4489972 to your computer and use it in GitHub Desktop.
A simple process to measure the depth of a binary tree, using the recursive method
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
### | |
Here is a representation of a simple binary tree, | |
to illustrate the idea of using a self-calling function | |
to measure the length of a binary tree. | |
### | |
nodeMap = [ | |
[ | |
[ | |
[ | |
null, | |
[ | |
null, | |
[ | |
null, | |
[ | |
null, | |
null | |
] | |
] | |
] | |
], | |
[ | |
null, | |
null | |
] | |
], | |
[ | |
null, | |
null | |
] | |
], | |
[ | |
null, | |
[ | |
[ | |
null, | |
null | |
], | |
null | |
] | |
] | |
] | |
evaluate = (node) -> | |
if node == null then return 0 | |
left = evaluate node[0] | |
right = evaluate node[1] | |
if left > right then left+1 else right+1 | |
depth = evaluate nodeMap | |
console.log depth |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment