Consider the separate sheet with Java code, starting with the definition of the Tnode class. I have removed most of the comments from this code. The listing consists of two executable classes: SearchTree and TraverseTree. Each of these classes make use of two other classes, also listed: Tnode and GetChar. (GetChar just fetches the next input character.) In class we considered an example similar to this, written in C.
Here is an interactive session of the execution of these two programs. (Boldface is user input. % is the Unix/Linux prompt. The first input is just the first letters of the days of the week, and I then added the first letters of the months.)
% java SearchTree mtwtfss ( m ( f () () ) ( t ( s () () ) ( w () () ) ) )# % java TraverseTree ( m ( f () () ) ( t ( s () () ) ( w () () ) ) )# fmstw % java SearchTree | java TraverseTree mtwtfss fmstw % java SearchTree mtwtfssjfmamjjasond ( m ( f ( a () ( d () () ) ) ( j () () ) ) ( t ( s ( o ( n () () ) () ) () ) ( w () () ) ) )# % java TraverseTree ( m ( f ( a () ( d () () ) ) ( j () () ) ) ( t ( s ( o ( n () () ) () ) () ) ( w () () ) ) )# adfjmnostw % java SearchTree | java TraverseTree mtwtfssjfmamjjasond adfjmnostwAnswer the following questions:
This output represents a tree. What kind of tree? Draw a diagram of the tree.
What does this output mean? Where does it come from?
What changes would be needed to both the programs SearchTree and TraverseTree to implement this change? (Give changes by line number or by marking on the listing.)
This question concerns the series of recitations used to translate programs from the "Tiny" language to MIPS assembler code. The portion of the grammar for Tiny that handles the if-else statements is:
I ---> '[' E '?' { S } ':' { S } ']' | '[' E '?' { S } ']'
Suppose you have the following actual Tiny code that is an if-else statement:
[ 10 - n ? n = n - 1; : a = b; ]
Here is a table showing the Tiny code at the left, the MIPS assembler code in the middle (where the commented code starting with "code to" is assumed to be generated by other parts of the compiler), and on the right a simple parser, with "// Do something" comments where additional MIPS needs to be generated. (In this parser, the function L() parses a list of statements.)
| Tiny Code | MIPS Code | Parser for if-else in Java |
|---|---|---|
[ 10 - n ? n = n - 1; : a = b; ] | # start of if-else # code to evaluate 10 - n # return location (Insert A) (Insert B) # start of then part # code to do n = n - 1 # end of then part; start of else (Insert C) (Insert D) # code to do a = b; (Insert E) # end of if-then |
private void I() {
int res;
if (next == '[') {
scan();
res = E();
}
// Do something A
// Do something B
if (next == '?') {
scan();
L();
}
if (next == ':') {
// Do something C
}
// do something D
if (next == ':') {
scan();
L();
if (next == ']') scan();
// Do something E
}
else if (next == ']') scan();
}
|
1 + (1/2) + (1/3) + . . . + (1/(n-1)) + (1/n).
Your function can calculate the answer either as a double or as a fraction.
((a b) (c d) e)
This part will work with the first and third pictures shown below. (The middle one is just for fun.)
| Cross 1: cross1.ps | Cross 2: cross2.ps | Cross 3: cross3.ps |
|---|---|---|
![]() |
![]() |
![]() |
The following Postscript code produces Cross 1 with its center at the origin. The variable r below is half the narrow width of a "blade", s is half the wide width of a blade, and t is the length of a blade.
% cross1.ps
/r 10 def
/s 30 def
/t 100 def
/blade {
t s lineto
t s neg lineto
r r neg lineto
} def
/cross {
newpath
r r moveto
1 1 4 {
pop
blade
-90 rotate
} for
closepath
} def
2 setlinewidth
cross
stroke
showpage
| r | s | t | gray |
|---|---|---|---|
| 12 | 40 | 80 | 0 (black) |
| 8 | 35 | 90 | 0.4 (dark gray) |
| 4 | 30 | 100 | 0.8 (light gray) |
Jane Austen, "Emma" (Aug 1994) Jane Austen, "Mansfield Park" (Jun 1994) Charles Dickens, "Doctor Marigold" (Aug 1998) Charles Dickens, "George Silverman's Explanation" (Feb 1997) William Faulkner, "Light in August" (Aug 2005) Ernest Hemingway, "A Farewell to Arms" (Aug 2005)We want to rewrite these in the following form. This assumes that the book title is always enclosed in quotes, that the author's first and last names are separated by a blank and followed by an comma (with no middle name or initial), and that the date is enclosed in parentheses
Emma by Austen, Jane. Released: Aug 1994. Mansfield Park by Austen, Jane. Released: Jun 1994. Doctor Marigold by Dickens, Charles. Released: Aug 1998. George Silverman's Explanation by Dickens, Charles. Released: Feb 1997. Sir Nigel by Faulkner, William. Released: Aug 2005. A Farewell to Arms by Hemingway, Ernest. Released: Aug 2005.
Write a Ruby program using Ruby code and regular expressions to do this translation. If you have forgotten various items, just fake it as best you can, but you are not supposed to be using Perl for this problem. If you didn't do Recitation 13 the way I suggested but used some method of your own, then you should mention this.