[Instructions] [Search] [Current] [Changes] [Syllabus] [Handouts] [Outlines] [Labs] [Assignments] [Examples] [Bailey Docs] [SamR Docs] [Tutorial] [API]
Held: Wednesday, April 15, 1998
for each move in legalMoves strategy, which we would
then need to implement more precisely for each type of puzzle.
/**
* 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
Enumerations and Iterators 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.
hasMoreElements() which returns true if there are
more elements to enumerate.
nextElement() which returns the next element in the list.
/**
* 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
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();
addElements(temp);
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
if they are already there.
*/
private void addElements(Linear stuff) {
stuff.add(value);
for (int i = 0; i < children.length; ++i) {
if (children[i] != null)
children[i].addElements(stuff);
} // for
} // addElements()
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
[Instructions] [Search] [Current] [Changes] [Syllabus] [Handouts] [Outlines] [Labs] [Assignments] [Examples] [Bailey Docs] [SamR Docs] [Tutorial] [API]
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.
This page may be found at http://www.math.grin.edu/~rebelsky/Courses/CS152/98S/home/rebelsky/public_html/Courses/CS152/98S/Outlines/outline.41.html
Source text last modified Tue Jan 12 11:52:29 1999.
This page generated on Mon Jan 25 09:49:50 1999 by SiteWeaver. Validate this page.
Contact our webmaster at rebelsky@math.grin.edu