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
10 changes: 10 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.

57 changes: 43 additions & 14 deletions src/main/java/NumberTriangle.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,21 @@ public boolean isLeaf() {
*
*/
public int retrieve(String path) {
// TODO implement this method
return -1;
NumberTriangle current = this;
for (char c : path.toCharArray()) {
if (c == 'l') {
current = current.left;
} else if (c == 'r') {
current = current.right;
} else {
return -1;
}
if (current == null) {
return -1;
}
}
return current.root;
}

/** Read in the NumberTriangle structure from a file.
*
* You may assume that it is a valid format with a height of at least 1,
Expand All @@ -103,34 +114,51 @@ public int retrieve(String path) {
* @return the topmost NumberTriangle object in the NumberTriangle structure read from the specified file
* @throws IOException may naturally occur if an issue reading the file occurs
*/
//minor update for PR
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
if (inputStream == null) {
throw new FileNotFoundException("File not found: " + fname);
}

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

String line = br.readLine();
while (line != null) {
while (line != null && !line.trim().isEmpty()) {

// remove when done; this line is included so running starter code prints the contents of the file
System.out.println(line);
String[] parts = line.trim().split("\\s+");

// TODO process the line
java.util.List<NumberTriangle> currentRow = new java.util.ArrayList<>();

//read the next line
for (String p : parts) {
currentRow.add(new NumberTriangle(Integer.parseInt(p)));
}

if (!levels.isEmpty()) {
java.util.List<NumberTriangle> prevRow = levels.get(levels.size() - 1);
for (int i = 0; i < prevRow.size(); i++) {
prevRow.get(i).setLeft(currentRow.get(i));
prevRow.get(i).setRight(currentRow.get(i + 1));
}
}

levels.add(currentRow);
line = br.readLine();
}
br.close();

if (!levels.isEmpty()) {
top = levels.get(0).get(0);
}

return top;
}


public static void main(String[] args) throws IOException {

NumberTriangle mt = NumberTriangle.loadTriangle("input_tree.txt");
Expand All @@ -142,3 +170,4 @@ public static void main(String[] args) throws IOException {
System.out.println(mt.getRoot());
}
}