Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 98 additions & 19 deletions src/main/java/NumberTriangle.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,17 @@ public boolean isLeaf() {
*
*/
public int retrieve(String path) {
// TODO implement this method
return -1;
NumberTriangle current = this;

for (char direction : path.toCharArray()) {
if (direction == 'l') {
current = current.left;
} else if (direction == 'r') {
current = current.right;
}
}

return current.getRoot();
}

/** Read in the NumberTriangle structure from a file.
Expand All @@ -104,40 +113,110 @@ public int retrieve(String path) {
* @throws IOException may naturally occur if an issue reading the file occurs
*/
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));
InputStream inputStream = null;

inputStream = NumberTriangle.class.getClassLoader().getResourceAsStream(fname);

if (inputStream == null) {
inputStream = NumberTriangle.class.getClassLoader().getResourceAsStream("resources/" + fname);
}

if (inputStream == null) {
try {
inputStream = new FileInputStream(fname);
} catch (FileNotFoundException e) {
// Ignore and try next approach
}
}

if (inputStream == null) {
try {
inputStream = new FileInputStream("resources/" + fname);
} catch (FileNotFoundException e) {
// Ignore and try next approach
}
}

// TODO define any variables that you want to use to store things
if (inputStream == null) {
throw new FileNotFoundException("Could not find file: " + fname +
" (tried as resource, resources/" + fname + ", and as file)");
}

// will need to return the top of the NumberTriangle,
// so might want a variable for that.
NumberTriangle top = null;
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));

int lineCount = 0;
String line = br.readLine();
while (line != null) {
lineCount++;
line = br.readLine();
}

br.close();

inputStream = NumberTriangle.class.getClassLoader().getResourceAsStream(fname);
if (inputStream == null) {
inputStream = NumberTriangle.class.getClassLoader().getResourceAsStream("resources/" + fname);
}
if (inputStream == null) {
try {
inputStream = new FileInputStream(fname);
} catch (FileNotFoundException e) {
try {
inputStream = new FileInputStream("resources/" + fname);
} catch (FileNotFoundException e2) {
throw new FileNotFoundException("File not found after reset: " + fname);
}
}
}

// remove when done; this line is included so running starter code prints the contents of the file
System.out.println(line);
br = new BufferedReader(new InputStreamReader(inputStream));

// TODO process the line
NumberTriangle[][] triangle = new NumberTriangle[lineCount][];

//read the next line
for (int i = 0; i < lineCount; i++) {
line = br.readLine();
if (line == null) {
throw new IOException("Unexpected end of file at line " + i);
}
String[] numbers = line.trim().split("\\s+");
triangle[i] = new NumberTriangle[numbers.length];

for (int j = 0; j < numbers.length; j++) {
int num = Integer.parseInt(numbers[j]);
triangle[i][j] = new NumberTriangle(num);
}
}
br.close();
return top;

for (int i = 0; i < lineCount - 1; i++) {
for (int j = 0; j < triangle[i].length; j++) {
triangle[i][j].setLeft(triangle[i + 1][j]);
triangle[i][j].setRight(triangle[i + 1][j + 1]);
}
}

return triangle[0][0];
}

public static void main(String[] args) throws IOException {
NumberTriangle mt = null;
try {
mt = NumberTriangle.loadTriangle("input_tree.txt");
} catch (FileNotFoundException e) {
System.out.println("Failed to load input_tree.txt: " + e.getMessage());
return;
}

if (mt == null) {
System.out.println("Triangle is null after loading");
return;
}

NumberTriangle mt = NumberTriangle.loadTriangle("input_tree.txt");
System.out.println("Root value: " + mt.getRoot());
System.out.println("Left child: " + mt.retrieve("l"));
System.out.println("Right child: " + mt.retrieve("r"));
System.out.println("Left-left grandchild: " + mt.retrieve("ll"));

// [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());
}
Expand Down