Due: Monday 5/5 10:00am
Submission name: w19_treeStats
For this, you will need the TreeNode
and Tree
classes, which you can get from thesource
Make the following modifications to your Tree
class (reminder to keep your base cases to null
to make your code simpler):
- Change
makeTree
so that left and right subtrees are not always made.- This means that
makeTree will
no longer definitely make trees with a particular height. That is ok. - You can decided how likely your program is to generate a subtree. If you’re going to use the same percentage each time, I suggest something at least as large as 60%, otherwise you won’t get very interesting trees. If you want to try something fancier, you could tie the percentage to the current level, where levels closer to the root are more likely to have subtrees than lower levels.
- This means that
countNodes(TreeNode current)
- Return the total number of nodes in the tree rooted at
current
. - Also include the wrapper function
countNodes()
- Return the total number of nodes in the tree rooted at
getHeight(TreeNode current)
- Return the height of the tree rooted at
current
. - Remember that the height may not be the same as the value of
numLevels
. - Also include the wrapper function
getHeight()
- Return the height of the tree rooted at
- Display the number of nodes and height in
setup()