66 lines
2.5 KiB
Java
66 lines
2.5 KiB
Java
import java.util.NoSuchElementException;
|
|
|
|
public class Main {
|
|
|
|
public static void main(String[] args) {
|
|
System.out.println();
|
|
System.out.println("=========================================");
|
|
System.out.println("Aufgabe 1 - Baum");
|
|
System.out.println("=========================================");
|
|
System.out.println();
|
|
|
|
System.out.println("Creating tree with 4 nodes: 2, 1, 3, 1");
|
|
Tree tree = new Tree();
|
|
tree.insert(2);
|
|
tree.insert(1);
|
|
tree.insert(3);
|
|
tree.insert(1);
|
|
|
|
TreeVisitor v = new TreeVisitor();
|
|
// post-order traversal
|
|
tree.traverse(v);
|
|
System.out.println("Visiting all nodes in post-order.");
|
|
System.out.println("Expected output: [3.0, 1.0, 1.0, 2.0]");
|
|
System.out.println("Actual output: " + v);
|
|
// count
|
|
System.out.println("Node count of 2 should be 1 and was " + tree.count(2));
|
|
System.out.println("Node count of 1 should be 2 and was " + tree.count(1));
|
|
// height
|
|
System.out.println("Height of the tree should be 3 and was " + tree.height());
|
|
|
|
System.out.println();
|
|
System.out.println("=========================================");
|
|
System.out.println("Aufgabe 2 - Deque");
|
|
System.out.println("=========================================");
|
|
System.out.println();
|
|
|
|
Deque deque = new Deque();
|
|
|
|
// getFirst
|
|
System.out.println("Checking if getFirst on empty deque throws NoSuchElementException");
|
|
try {
|
|
deque.getFirst();
|
|
System.out.println("FAILURE: No exception thrown.");
|
|
} catch (NoSuchElementException e) {
|
|
System.out.println("SUCCESS: NoSuchElementException thrown.");
|
|
} catch (Exception e) {
|
|
System.out.println("FAILURE: Unexpected exception thrown. Exception thrown: " + e.getClass().getName());
|
|
System.out.println(e.getMessage());
|
|
}
|
|
|
|
System.out.println();
|
|
|
|
// addLast
|
|
System.out.println("Checking if addLast(null) throws IllegalArgumentException");
|
|
try {
|
|
deque.addLast(null);
|
|
System.out.println("FAILURE: No exception thrown.");
|
|
} catch (IllegalArgumentException e) {
|
|
System.out.println("SUCCESS: IllegalArgumentException thrown.");
|
|
} catch (Exception e) {
|
|
System.out.println("FAILURE: Unexpected exception thrown. Exception thrown: " + e.getClass().getName());
|
|
System.out.println(e.getMessage());
|
|
}
|
|
}
|
|
|
|
} |