Commit 1f42cdcb authored by Torstein's avatar Torstein

Fra forelesning torsdag 17. mars

parent c76cfccf
......@@ -21,22 +21,42 @@ public class Tree {
*/
@Override
public String toString() {
// TODO
return null;
String res = "Tree [\n";
res += toString(this.root, 1);
return res + "]";
}
/**
private String toString(TreeNode r, int depth) {
char[] mellomrom = new char[depth*2];
for (int i = 0; i<depth*2; i++) {
mellomrom[i] = ' ';
}
String res = String.copyValueOf(mellomrom) + r.getName() + "\n";
// No children: base case
for (TreeNode child : r.getChildren()) {
// Recursive case
res += toString(child, depth + 1);
}
return res;
}
/**
*
* @return the depth of the tree
*/
public int getDepth() {
// TODO
return 0;
return getDepth(this.getRoot());
}
/**
private int getDepth(TreeNode r) {
int max = 0;
for (TreeNode child : r.getChildren()) {
max = Math.max(1+getDepth(child), max);
}
return max;
}
/**
*
* @return the leaves of the tree
*/
......@@ -50,12 +70,20 @@ public class Tree {
* @return the maximum degree of any node in the tree
*/
public int maxDeg() {
// TODO
return 0;
return maxDeg(this.root);
}
/**
private int maxDeg(TreeNode r) {
int max = r.getChildren().size();
if (r.getParent() != null)
max++;
for (TreeNode child : r.getChildren()) {
max = Math.max(max, maxDeg(child));
}
return max;
}
/**
* Will return a node with the matching name, if found.
* If there is more than one node with the given name,
* an arbitrary (__not__ random) match will be returned.
......
......@@ -11,11 +11,20 @@ public class FileSystem {
public static void listAll(File path) {
// TODO
// Always
System.out.println(path.getAbsolutePath());
// Base case (do nothing)
// Recursive case
if (path.isDirectory()) {
for (File child : path.listFiles()) {
listAll(child);
}
}
}
public static void biggestFile(String path) {
public static void biggestFile(File path) {
// TODO
// Note: use path.length to get filesize
}
}
package inf101v16f12.recursion;
import java.util.HashMap;
import java.util.Map;
public class MathFunctions {
/**
......@@ -11,7 +14,7 @@ public class MathFunctions {
*
* 0, 1, 1, 2, 3, 5, 8, 13, ...
*
* This function will return
* This function will return the n'th number in the sequence, eg. fib(3) = 2
*
* @param n
* non-negative integer
......@@ -24,20 +27,37 @@ public class MathFunctions {
if (n == 1)
return 1;
// Recursive case
return fib(n-1) + fib(n-2);
return fib(n - 1) + fib(n - 2);
}
// The iterative version, MUCH faster
public static int fib2(int n) {
int prev = 0;
int next = 1;
for (int i=0; i<n; i++) {
for (int i = 0; i < n; i++) {
int tmp = next;
next = next + prev;
prev = tmp;
}
return prev;
}
private static Map<Integer, Integer> memfib = new HashMap<Integer, Integer>();
public static int memoizedFib(int n) {
// Normal base case
if (n == 0)
return 0;
if (n == 1)
return 1;
// If n seen before, return now
if (memfib.containsKey(n))
return memfib.get(n);
// Calculate
int res = memoizedFib(n-1) + memoizedFib(n-2);
// Store
memfib.put(n, res);
return res;
}
/**
* Factorial
......@@ -162,4 +182,20 @@ public class MathFunctions {
return 0;
}
/**
* Evaluates a string mathematically. For simplicity, we assume the only
* contain integer numbers and the symbols + and * (addition and
* multiplication)
*
* Example: evaluate("5*3+2*3") yields 21 as output
*
* @param s
* the string
* @return the result
*/
public static int evaluate(String s) {
// TODO
return 0;
}
}
......@@ -91,6 +91,36 @@ public class Sorting {
* @param lst
*/
public static void quickSort(int[] lst) {
// TODO
int[] tmp = new int[lst.length];
quickSort(lst, tmp, 0, lst.length);
}
private static void quickSort(int[] lst, int[] tmp, int left, int right) {
// Base case
if (right - left <= 1)
return;
int kl = left;
int kr = right;
// Pivot number
int pN = lst[left];
for (int i=left+1; i<right; i++) {
if (lst[i] < pN)
tmp[kl++] = lst[i];
else {
tmp[--kr] = lst[i];
}
}
tmp[kl] = pN;
for (int i=left; i<right; i++) {
lst[i] = tmp[i];
}
// Recursive calls
quickSort(lst, tmp, left, kl);
quickSort(lst, tmp, kr, right);
}
}
......@@ -24,4 +24,13 @@ public class TestMathFunctions {
assertEquals(8, MathFunctions.fib2(6));
MathFunctions.fib2(1000);
}
@Test
public void testFibMemoized() {
assertEquals(0, MathFunctions.memoizedFib(0));
assertEquals(1, MathFunctions.memoizedFib(1));
assertEquals(5, MathFunctions.memoizedFib(5));
assertEquals(8, MathFunctions.memoizedFib(6));
MathFunctions.fib2(1000);
}
}
......@@ -11,9 +11,16 @@ import inf101v16f12.recursion.Sorting;
public class TestSorting {
public static final int TESTS = 5;
public static final int LISTLEN = 750*1;
public static final int LISTLEN = 750*2;
private static final Random r = new Random();
@Test
public void testQuickSortSimple() {
int[] lst = { 3, 5, 7, 1, 9, 9, 3, 4, 2 };
Sorting.quickSort(lst);
System.out.println(Arrays.toString(lst));
assertTrue(isSorted(lst));
}
@Test
public void testMergeSortSimple() {
int[] lst = { 3, 5, 7, 1, 9, 9, 3, 4, 2 };
......@@ -53,6 +60,18 @@ public class TestSorting {
}
}
@Test
public void testQuickSortBig() {
for (int i=0; i<TESTS; i++) {
int[] lst = new int[LISTLEN];
for (int j=0; j<LISTLEN; j++) {
lst[j] = r.nextInt(LISTLEN) - (LISTLEN/2);
Sorting.quickSort(lst);
assertTrue(isSorted(lst));
}
}
}
@Test
public void testSelectionSortBig() {
for (int i=0; i<TESTS; i++) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment