Skip to content
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 63 additions & 19 deletions src/main/java/NumberTriangle.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,19 @@ public boolean isLeaf() {
*
*/
public int retrieve(String path) {
// TODO implement this method
return -1;
// Start the traversal from the current node (`this`).
NumberTriangle currentNode = this;

// Iterate through each character in the path string.
for (char direction : path.toCharArray()) {
if (direction == 'l') {
currentNode = currentNode.left;
} else if (direction == 'r') {
currentNode = currentNode.right;
}
}
// After following the path, return the root value of the final node.
return currentNode.getRoot();
}

/** Read in the NumberTriangle structure from a file.
Expand All @@ -104,27 +115,62 @@ 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));
// Use a list to keep track of the nodes in the previous row we processed.
java.util.List<NumberTriangle> previousRow = new java.util.ArrayList<>();
NumberTriangle top = null;

// open the file and get a BufferedReader object
InputStream inputStream = NumberTriangle.class.getClassLoader().getResourceAsStream(fname);

// TODO define any variables that you want to use to store things
// --- START OF THE FIX ---
// Add a check to ensure the file was actually found.
if (inputStream == null) {
// If not found, throw an exception with a helpful message.
throw new FileNotFoundException("Resource file not found: " + fname);
}
// --- END OF THE FIX ---

// 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));

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
// A list to store the nodes for the current row we are building.
java.util.List<NumberTriangle> currentRow = new java.util.ArrayList<>();
String[] numbers = line.trim().split("\\s+");

// Create NumberTriangle objects for each number in the current line.
for (String numStr : numbers) {
if (!numStr.isEmpty()) { // Avoid errors on empty strings
int value = Integer.parseInt(numStr);
currentRow.add(new NumberTriangle(value));
}
}

// This logic prevents a crash if a line is blank
if (currentRow.isEmpty()) {
line = br.readLine();
continue; // Skip to the next iteration
}

if (top == null) {
// This is the first line of the file, so this is the top of our triangle.
top = currentRow.get(0);
} else {
// Link the nodes from the previous row to the nodes in the current row.
for (int i = 0; i < previousRow.size(); i++) {
NumberTriangle parent = previousRow.get(i);
NumberTriangle leftChild = currentRow.get(i);
NumberTriangle rightChild = currentRow.get(i + 1);

parent.setLeft(leftChild);
parent.setRight(rightChild);
}
}

// The current row now becomes the previous row for the next iteration.
previousRow = currentRow;

// Read the next line
line = br.readLine();
}
br.close();
Expand All @@ -138,7 +184,5 @@ public static void main(String[] args) throws IOException {
// [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());
}
}