Outline of Class 40: Implementing Trees with Nodes
Held: Tuesday, April 14, 1998
- Any questions on
Don't forget that it's due this Friday.
- Today and tomorrow, we'll be spending most of the class on
lab 14. You should make sure
to read over (and think about) the last part of the lab before
- A reminder: on Friday, we'll be starting to discuss the history
of computing in conjunction with the Evolution of Technology
class in Anthropology (and Technology Studies).
- In preparation for the discussion of the history of computing, you
should take a
survey on the history of
computing sometime after class and before midnight on Wednesday.
- As with lists, it is possible to implement trees directly with a
TreeNode class and without a wrapper class.
TreeNodes will have many similarities to
ListNodes, there will be some differences:
- We need to support more than one next element (child). We could do that
by creating a separate variable for each child or by building an array
or vector of children.
- We need to keep track of how many children we have. We can use the
size of the array or vector, but it may be useful to have separate
access to that information.
- We may need to consider the arity of our objects in the constructor.
- We also have to make some of the same decisions we made for list
- Do we want unidirectional or bidirectional links? Bidirectional
links support a
parent() method, but unidirectional
links (parent to child) permit reuse of the same subtree in multiple
trees and may be easier to support.
- Should each node keep track of its depth? This makes computation
easier, but modification harder.
- We also have a number of questions to ask ourselves in the design,
such as whether the arity of a node can change. If so, how do you
determine the current arity of a node?
- If we allow variable-arity nodes, it becomes important to consider
the appropriate preconditions for
setChild(). For example,
can you set the fifth child of a node with no children?
- If we want to implement binary trees and don't want to support a
parent link, we can simply use the
we've already developed.