diff --git a/src/main/java/NumberTriangle.java b/src/main/java/NumberTriangle.java index 30e10ecb..aaaa31a2 100644 --- a/src/main/java/NumberTriangle.java +++ b/src/main/java/NumberTriangle.java @@ -57,9 +57,9 @@ public int getRoot() { * Set the root of this NumberTriangle to be the max path sum * of this NumberTriangle, as defined in Project Euler problem 18. * After this method is called, this NumberTriangle should be a leaf. - * + *
* Hint: think recursively and use the idea of partial tracing from first year :) - * + *
* Note: a NumberTriangle contains at least one value. */ public void maxSumPath() { @@ -76,27 +76,49 @@ public boolean isLeaf() { * Follow path through this NumberTriangle structure ('l' = left; 'r' = right) and * return the root value at the end of the path. An empty string will return * the root of the NumberTriangle. - * + *
* You can decide if you want to use a recursive or an iterative approach in your solution. - * + *
* You can assume that: - * the length of path is less than the height of this NumberTriangle structure. - * each character in the string is either 'l' or 'r' + * the length of path is less than the height of this NumberTriangle structure. + * each character in the string is either 'l' or 'r' * * @param path the path to follow through this NumberTriangle * @return the root value at the location indicated by path * */ public int retrieve(String path) { - // TODO implement this method + NumberTriangle current = this; + + for (int i = 0; i < path.length(); i++) { + char c = path.charAt(i); + if (c == 'l') { + current = current.left; + } else if (c == 'r') { + current = current.right; + } else { + // invalid character in path + return -1; + } + + if (current == null) { + // path went too far or structure incomplete + return -1; + } + } + + if (current != null) { + return current.getRoot(); + } return -1; } - /** Read in the NumberTriangle structure from a file. - * + /** + * Read in the NumberTriangle structure from a file. + *
* You may assume that it is a valid format with a height of at least 1, * so there is at least one line with a number on it to start the file. - * + *
* See resources/input_tree.txt for an example NumberTriangle format.
*
* @param fname the file to load the NumberTriangle structure from
@@ -107,38 +129,53 @@ public static NumberTriangle loadTriangle(String fname) throws IOException {
// open the file and get a BufferedReader object whose methods
// are more convenient to work with when reading the file contents.
InputStream inputStream = NumberTriangle.class.getClassLoader().getResourceAsStream(fname);
- BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
-
-
- // TODO define any variables that you want to use to store things
-
- // will need to return the top of the NumberTriangle,
- // so might want a variable for that.
- NumberTriangle top = null;
-
- String line = br.readLine();
- while (line != null) {
-
- // remove when done; this line is included so running starter code prints the contents of the file
- System.out.println(line);
-
- // TODO process the line
-
- //read the next line
- line = br.readLine();
+ if (inputStream == null) {
+ throw new FileNotFoundException("Resource not found: " + fname);
}
- br.close();
- return top;
- }
-
- public static void main(String[] args) throws IOException {
- NumberTriangle mt = NumberTriangle.loadTriangle("input_tree.txt");
-
- // [not for credit]
- // you can implement NumberTriangle's maxPathSum method if you want to try to solve
- // Problem 18 from project Euler [not for credit]
- mt.maxSumPath();
- System.out.println(mt.getRoot());
+ try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) {
+ // previous row of nodes (top to bottom left->right)
+ java.util.List