Having just finished week 9, I am now entering the final stretch of the course, with just one more major assignment left and few new concepts to learn. Overall, the midterm was easier than I had anticipated and involved even a few questions that were more 'analytical' in nature such as finding the min height of a binary tree. If we define the height to be just the number of 'levels', so that a Tree with just a root has height 1, then for n nodes in a binary tree, the minimum height is the ceiling of log_2_(n) (where log is in base 2).
We make a simple observation to support this claim:
Level # Number of Nodes
1 1 = 2^0
2 2 = 2^1
3 4 = 2^2
4 8 = 2^3
...
n 2^(n-1)
Since each node has a max of 2 children, the number of nodes in successive levels is doubled.
As such, for n = 20 nodes, we would have a minimum height of 5 since:
sum(levels 1 through 4) = 15 < 20 < 31 = sum(levels 1 through 5). As you can see, we take the ceiling of log_2_(n) since the remaining nodes will go into the next level giving the final height of the tree.
For me, this type of analysis was quite interesting and I am looking forward to learning about more intuitive and analytical ways to make my code more efficient by factoring in data structures, runtime, etc. As well, I feel that mathematics is best integrated into the more analytical side of computer science in making code more efficient by breaking down its components into digestible parts.
Overall, I am looking forward to the last 3 weeks and hope to finish off strong.
No comments:
Post a Comment