# Outline of Class 41: Iterating Trees

Held: Wednesday, April 15, 1998

• Any questions on assignment six? Don't forget that it's due this Friday.
• Today we'll continue to work on lab 14. I've made some modifications to the lab, so be sure to read it over again.
• 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. If you took it before today, take it again.
• On Tuesday, April 21 at 11 a.m. in Science 1023, Robert Cadmus, professor of Physics, will present a lecture about electronic imaging technology. It's likely we'll use class time to visit that lecture (it's probably related to something we have talked about or will talk about).
• Tomorrow at 4:15, Dick Randell of the University of Iowa will be giving a Great Math Talk! on Knot Theory. Anyone who's had linear should be able to understand it (I think), so I encourage you all to attend. Since I've never taken linear :-), I won't be there.

## Iterators and Enumerations

• Many algorithms have portions that rely on looking at all the portions of a source structure.
• In our generic puzzle solving method, we used a `for each move in legalMoves` strategy, which we would then need to implement more precisely for each type of puzzle.
• Similarly, I could write a generic minimum routine as follows ( again, this is not real Java):
```/**
* Compute the minimum in a group of integers.
* pre: The group is nonempty.
* post: The group is unaffected.
*/
public int min(Group g) {
int guess;	// Current guess as to the minimum
int val;		// The next value;
// Get a start guess
guess = g.someElement();
// Compare to each other element, reducing our guess whenever
// possible.
foreach val in g {
if val.lessThan(guess)
guess = val;
}
// We're done
return val;
} // min
```
• However, rather than rewriting my routine for each different structure, I might instead provide a standard way of getting all the elements in a structure. * `Enumeration`s and `Iterator`s are structures that give you the elements of some structure, one by one, and thereby support these types of algorithms. They maintain a state that helps them keep track of which elements they have and have not returned.
• `Enumeration` is a standard Java interface, defined in `java.util`.
• `Iterator` is Bailey's refinement of `Enumeration`.
• Both provide
• `hasMoreElements()` which returns true if there are more elements to enumerate.
• `nextElement()` which returns the next element in the list.
• Using these two functions, one might write minimum as follows:
```/**
* Compute the smallest element in an enumeration.
* pre: The enumeration is nonempty.
* pre: No elements have been removed from the enumeration.
* pre: All elements in the enumeration are comparable.
* post: The smallest element is returned.
*/
public Object min(Enumeration stuff) {
Object guess;	// Current guess as to the minimum
Object val;	// The next value in the enumeration
// Get a start guess
guess = stuff.nextElement();
// Compare to each other element, reducing our guess whenever
// possible.
while (stuff.hasMoreElements()) {
val = stuff.nextElement();
if (val.lessthan(guess))
guess = val;
} // while
// We're done
return val;
} // min
```

## Tree Iterators

• Can we build an iterator based on these simple tree nodes? Certainly, and there are a number of ways to do so.
• We could traverse the tree and shove all of the nodes into a linear structure and then return the iterator for that structure.
• We could build the iterator for the current node in terms of the iterators of its children. To get the next element, you determine which child iterator is "current" and get the next element from that iterator.
• We could keep track of where we've been and haven't been in the tree.
• Which is best? The first is certainly the easiest, although it requires the most computation to create the iterator.
• Here's a simple implementation (which should be included in the `TreeNode` class)
```/**
Build an interator for the tree rooted at the current node.
post: The iterator contains all the nodes currently in the tree.
*/
public Iterator elements() {
// We wouldn't really use a Linear here, but it makes a good
// generic exmaple.
Linear temp = new Linear();
return temp.elements();
} // elements()

/**
Add all the elements at or below the current node to a linear
structure.  Note that there are many ways to do this, and the
way in which you do it (as well as the linear structure used)
affects the order in which elements appear.
pre: The linear structure is initialized.
pre: The lienar structure has room for the elements.
pre: The node is used in a tree (in particular, there is no path
from this node back to this node based only on child edges).
post: All the elements are added to the linear structure, even
*/
for (int i = 0; i < children.length; ++i) {
if (children[i] != null)
} // for
```

On to History of Computing I
Back to Implementing Trees with Nodes
Outlines: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
Current position in syllabus

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.