In this recitation, you will practice building a tree and traversing it to count the number of leaves in the tree. Building a tree is not as straight forward as adding a value to the other data structures we've seen so far, so working through how to build (or add to) trees will be useful to practice. Tree traversal is a very common action since so many tree operations depend on it so once your tree is built, you will traverse it to count the number of leaves.
We haven't seen a complete Node
class for trees yet. Use the Node
class below to represent your tree. Note: this is for a generic tree, not binary trees (which we're looking at in lecture this week). Do not modify the class below.
Use the Node
class above to construct the tree shown below (which comes from our lecture notes):
Implement the operation below on the tree described below.
int leafCount(Node tree)
Is is possible for a node to be both a root and a leaf in the same tree? Explain your answer.
This is for your practice. You should be able to complete some of the functions by the end of recitation. If you complete a function, call the TA over to look over your function to make sure it is correct. If you do not have time to complete the programs before the end of recitation, you have the option of uploading it to CourseWeb once you are finished to receive feedback on it from the TA. If you upload it after Monday, November 23, please email the TA so he knows to look at it.