3 2 1 4
.
In-order | Pre-order |
---|---|
11, 3, 6, 7, 4, 10, 12 | 7, 11, 6, 3, 12, 10, 4 |
2, 10, 3, 9, 8, 5, 7, 1, 6 | 8, 10, 2, 3, 9, 7, 5, 6, 1 |
2, 9, 3, 5 | 5, 9, 2, 3 |
Modify the delete()
method of the IntBST class so that if
a node has two children, it is replaced with its successor, not with
its predecessor. The successor of a node with the data value d is the
smallest node in the tree with the data greater than d. Use in-order
and pre-order traversals to check your
method (this way you can reconstruct the tree and check which node
replaced the one you have deleted).
The code for deleting a node (replacing it with its predecessor if
it has two children) is available here.
public class StringTest {
public static void main(String [] args) {
String [] words = {"red", "BLUE", "grEEn", "Yellow"};
// change the words:
change(words);
// print the words:
for (String s : words) {
System.out.println(s);
}
}
// write the method "change" here:
}
Write the method change
that takes an array of strings as
a parameter and changes it so that each word has alternating upper
case and lower case letters, starting from upper case. For instance,
the output for the above example is:
ReD
BlUe
GrEeN
YeLlOw
To change a character to upper case, use the class method
toUpperCase
of the class Character
. For
instance,
char c = 'a';
c = Character.toUpperCase(c);
changes c to 'A'. For other helpful methods see String
API and Character
API
Your task will be to implement the behavior of these three pieces given the following abstract class:
abstract class ChessPiece {
protected String name;
protected char color; // black or white piece
protected int row;
protected char col;
public ChessPiece(String n, char c, char cl, int r) {
name = n;
color = c;
row = r;
col = cl;
}
public String toString() {
return color + " " + name + " at " + col + row;
}
abstract public boolean moveTo(char c, int r);
// a helper method for converting columns into numbers
// returns 1 for a, 2 for b, etc.
// returns 0 for an illegal character
protected int toNumber(char c) {
String cols = "abcdefgh";
int i = cols.indexOf(c);
if (i != -1) return i+1;
System.out.println("Illegal position " + c);
return 0;
}
}
You cannot modify the abstract class.
You need to write the classes King, Rook, and Pawn. Each of these classes extends the ChessPiece class. The abstract class provides a constructor and a toString() method. It also provides a protected toNumber() method which converts a character (a column name, such as a or d) to a number from 1 to 8.
The abstract class requires that every class that inherits from it has a moveTo() method. The method takes the new position of the piece. It checks whether the move is legal (based on the specific rules of how this piece moves and on the current position). If the move is legal, then the piece is moved and the method returns 'true', otherwise the method returns 'false' and the position is unchanged.
Below is the sample of a test program for a King:
King whiteking = new King('W','e',4);
King blackking = new King('B','a',8);
if (!whiteking.moveTo('d',4)) {
System.out.println("can't move the king to d4");
}
System.out.println(whiteking);
if (!blackking.moveTo('b',7)) {
System.out.println("can't move the king to b7");
}
System.out.println(blackking);
if (!blackking.moveTo('b',7)) {
System.out.println("can't move the king to b7");
}
if (!blackking.moveTo('b',5)) {
System.out.println("can't move the king to b5");
}
The white king is initially at e4, the black one is at a8.
The first two moves are legal, so the white king gets moved to d4,
and the black king to b7. The next move is illegal because the king
remains at the same position (an error message gets printed). The last
move is illegal because b5 is not next to d4, and another error
message gets printed.
For this problem you need to: