Test Methods Flashcards
Row Lengths Decrease
int[][] t
public static boolean rowLengthsDecrease(int[][] t){ //start with single for loop to target each row for (int i = 0; i < t.length -1; i++){ if (t[i].length < t[i+1].length){ return false; } } return true; }
Row Values Increase
int[][] t
public static boolean rowValuesIncrease(int[][] t){ for (int i=0; i < t.length-1; i++){ for (int j=0; j < t[i].length-1; j++){ if (t[i][j] >= t[i][j+1]){ return false; } } } return true; }
Column Values Increase
int[][] t
public static boolean columnValuesIncrease(int[][] t){ for (int i=1; i < t.length; i++){ for (int j= 0; j < t[i].length; j++){ if (t[i-1][j] >= t[i][j]){ return false; } } } return true }
Is Set of 1 to N
int[][] t
public static boolean isSetOf1toN(int[][] t){ //3 key stages //stage 1: get cell count of t int cellCount = 0; for (int i = 0; i < t.length; i++){ cellCount += t[i].length; } // stage 2: create array of booleans to check if // any cell values are > no. of cells (they can't be) boolean[] s = new boolean[cellCount]; for (int i=0; i < t.length; i++){ for (int j=0; j < t[i].length; j++){ int cellValue = t[i][j]; if (cellValue > s.length){ return false; } s[cellValue -1] = true; } } for (Boolean value : s){ if (!value){ return false; } } return true; }
Add Value (Week 8) (Integer value)
public void addValue(Integer value){ if (smallest == null){ smallest = new Cell(value); return; }
Cell bump = smallest; value = addToRow(smallest, value);
while (value != null){ if (bump.below == null){ bump.below = new Cell(value); bump.below.above = bump; return; } bump = bump.below; value = addToRow(bump, value); } }
Add to Row (Week 8)
Cell curr, int value
public Integer addToRow(Cell curr, int value){
int current = value;
while (current > curr.value && curr.right != null){
curr = curr.right;
}
while (curr.value > current){ current = curr.value; curr.value = value; return current; }
if (curr.right == null){ curr.right = new Cell(value); curr.right.left = curr; if (curr.above != null){ curr.right.above = curr.above.right; } } return null; }
Size (Week 10)
public int size(){ if (nodeValue == null){ return 0; } int countNodes = 1; for (Tree child : children){ countNodes += child.size(); } return countNodes; }
Max Degree (Week 10)
public int maxDegree(){
if (children.isEmpty()){
return 0;
}
int[] widths = new int[children.size() + 1];
widths[children.size()] = children.size();
for (int i = 0; i < children.size(); i++){ widths[i] = children.get(i).maxDegree(); }
int maxWidth = 0; for (int width : widths){ if (width > maxWidth){ maxWidth = width; } } return maxWidth; }
Post Order (Week 10) Returns a List
public List postOrder(){ ArrayList nodes = new ArrayList(); if (children.isEmpty()){ nodes.add(rootValue); return nodes; } for (Tree child : children){ nodes.addAll(child.postOrder()); } nodes.add(rootValue); return nodes; }
To Indented String (Week 10)
public String toIndentedString(){ String indentTree = rootValue + "\n"; if (children.isEmpty()){ return indentTree; } String childStr = ""; for (Tree child : children){ childStr += child.toIndentedString(); } String[] childArr = childStr.split("\n");
for (String child : childArr){ indentTree += " " + child + "\n"; } return indentTree; }
Add (Week 11)
T element
public void add(T element){
if (root == null){
root = element;
}
int comparison = root.compareTo(element);
if (comparison > 0){ if (left == null){ left = new LinkedBST(element); } else { left.add(element); } } else if (comparison < 0) { if (right == null){ right = new LinkedBST(element); } else { right.add(element); } } }
Search (Week 11)
T element
public boolean search(T target){
if (root == null){
return false;
}
int comparison = root.compareTo(target); if (comparison == 0){ return true; } else if (comparison > 0 && left != null){ return left.search(target); } else if (comparison < 0 && right != null){ return right.search(target); } return false; }
Height (Week 11)
public int height(){
if (root == null){
return 0;
}
int lTotal = 0; int rTotal = 0; if (left != null){ lTotal += 1 + left.height(); } if (right != null){ rTotal = 1 + right.height(); }
return Math.max(lTotal, rTotal); }
Size (Week 11)
public int size(){
if (root == null){
return 0;
}
int total = 1; if (left != null){ total += left.size(); } if (right != null){ total += right.size(); }
return total; }
Size Above (Week 11) (T low)
public int sizeAbove(T low){
if (root == null){
return 0;
}
int count = 0; int comparison = root.compareTo(low); if (comparison >= 0){ count++; }
if (right != null){ count += right.sizeAbove(low); } if (left != null){ count += left.sizeAbove(low); }
return count; }