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
45 changes: 38 additions & 7 deletions src/main/java/NumberTriangle.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
* This is the provided NumberTriangle class to be used in this coding task.
Expand Down Expand Up @@ -88,7 +90,18 @@ public boolean isLeaf() {
*
*/
public int retrieve(String path) {
// TODO implement this method
if (path.isEmpty()) {
return this.root;
}
char direction = path.charAt(0);
if ( direction == 'l' && this.left != null) {
return this.left.retrieve(path.substring(1));
}

else if (direction == 'r' && this.right != null) {
return this.right.retrieve(path.substring(1));
}

return -1;
}

Expand All @@ -110,19 +123,37 @@ 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

// will need to return the top of the NumberTriangle,
// so might want a variable for that.
NumberTriangle top = null;
List<NumberTriangle> prevRow = new ArrayList<NumberTriangle>();

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
line = line.trim();

if (!line.isEmpty()) {
String[] nums = line.split("\\s+");
List<NumberTriangle> curRow = new ArrayList<>();

for (int i = 0; i < nums.length; i++) {
if (!nums[i].equals("")) {
int value = Integer.parseInt(nums[i]);
curRow.add(new NumberTriangle(value));
}
}

if (top == null) {
top = curRow.get(0);
} else {
for (int j = 0; j < prevRow.size(); j++) {
prevRow.get(j).setLeft(curRow.get(j));
prevRow.get(j).setRight(curRow.get(j + 1));
}
}
prevRow = curRow;
}

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