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
43 changes: 38 additions & 5 deletions src/main/java/NumberTriangle.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,15 @@ public boolean isLeaf() {
*
*/
public int retrieve(String path) {
// TODO implement this method
return -1;
NumberTriangle current = this;
for (int i = 0; i < path.length(); i++) {
if (path.charAt(i) == 'l') {
current = current.left;
} else if (path.charAt(i) == 'r') {
current = current.right;
}
}
return current.getRoot();
}

/** Read in the NumberTriangle structure from a file.
Expand All @@ -110,19 +117,45 @@ public static NumberTriangle loadTriangle(String fname) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));


// TODO define any variables that you want to use to store things
List<List<NumberTriangle>> rows = new ArrayList<>();

// will need to return the top of the NumberTriangle,
// so might want a variable for that.
NumberTriangle top = null;

String line = br.readLine();
int rowNum = 0;

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
line = line.trim();
if (!line.isEmpty()) {
String[] numbers = line.split("\\s+");
List<NumberTriangle> currentRow = new ArrayList<>();

for (String numStr : numbers) {
int value = Integer.parseInt(numStr);
currentRow.add(new NumberTriangle(value));
}
rows.add(currentRow);

if (rowNum == 0) {
top = currentRow.get(0);
}

if (rowNum > 0) {
List<NumberTriangle> prevRow = rows.get(rowNum - 1);
for (int i = 0; i < prevRow.size(); i++) {
NumberTriangle parent = prevRow.get(i);
parent.setLeft(currentRow.get(i));
parent.setRight(currentRow.get(i + 1));
}
}

rowNum++;
}

//read the next line
line = br.readLine();
Expand Down