├── .gitignore ├── Assignments ├── Assignment 1 │ ├── Assignment 1.docx │ ├── Assignment 1.pdf │ ├── Question3.java │ ├── Question4.java │ └── Questions.pdf ├── Assignment 2 │ ├── 1d.pdf │ ├── 1e1.pdf │ ├── 1e2.pdf │ ├── 2a1.pdf │ ├── 2a2.pdf │ ├── 2a3.pdf │ ├── 2a4.pdf │ ├── 2b.pdf │ ├── 2c1.pdf │ ├── 2c2.pdf │ ├── 2c3.pdf │ ├── 31.pdf │ ├── 310.pdf │ ├── 311.pdf │ ├── 312.pdf │ ├── 313.pdf │ ├── 314.pdf │ ├── 315.pdf │ ├── 316.pdf │ ├── 317.pdf │ ├── 318.pdf │ ├── 319.pdf │ ├── 32.pdf │ ├── 320.pdf │ ├── 321.pdf │ ├── 322.pdf │ ├── 323.pdf │ ├── 324.pdf │ ├── 33.pdf │ ├── 34.pdf │ ├── 35.pdf │ ├── 36.pdf │ ├── 37.pdf │ ├── 38.pdf │ ├── 39.pdf │ ├── Assignment 2.pdf │ ├── Assignment 2.tex │ └── Graphs │ │ ├── Q1 │ │ ├── D │ │ │ └── BT1D.gv │ │ └── E │ │ │ ├── BT1E1.gv │ │ │ └── BT1E2.gv │ │ ├── Q2 │ │ ├── A │ │ │ ├── BT2A1.gv │ │ │ ├── BT2A2.gv │ │ │ ├── BT2A3.gv │ │ │ └── BT2A4.gv │ │ ├── B │ │ │ └── BT2B.gv │ │ └── C │ │ │ ├── BT2C1.gv │ │ │ ├── BT2C2.gv │ │ │ └── BT2C3.gv │ │ └── Q3 │ │ ├── BT31.gv │ │ ├── BT310.gv │ │ ├── BT311.gv │ │ ├── BT312.gv │ │ ├── BT313.gv │ │ ├── BT314.gv │ │ ├── BT315.gv │ │ ├── BT316.gv │ │ ├── BT317.gv │ │ ├── BT318.gv │ │ ├── BT319.gv │ │ ├── BT32.gv │ │ ├── BT320.gv │ │ ├── BT321.gv │ │ ├── BT322.gv │ │ ├── BT323.gv │ │ ├── BT324.gv │ │ ├── BT33.gv │ │ ├── BT34.gv │ │ ├── BT35.gv │ │ ├── BT36.gv │ │ ├── BT37.gv │ │ ├── BT38.gv │ │ └── BT39.gv ├── Assignment 3 │ ├── Assignment 3.pdf │ ├── DecisionStump.java │ ├── DecisionStumpTest.java │ ├── DecisionTree.java │ ├── DecisionTreeTest.java │ ├── NodeReplaceException.java │ ├── Printout.txt │ ├── Readme.txt │ └── iris.data.txt ├── Assignment 4 │ ├── 1a1.pdf │ ├── 1b1.pdf │ ├── 1b2.pdf │ ├── 1b3.pdf │ ├── 1b4.pdf │ ├── 1b5.pdf │ ├── 1c1.pdf │ ├── 1c2.pdf │ ├── 1c3.pdf │ ├── 1c4.pdf │ ├── 1c5.pdf │ ├── 3a1.pdf │ ├── Assignment 4.pdf │ ├── Assignment 4.tex │ └── Graphs │ │ ├── Q1 │ │ ├── A │ │ │ └── a1.gv │ │ ├── B │ │ │ ├── b1.gv │ │ │ ├── b2.gv │ │ │ ├── b3.gv │ │ │ ├── b4.gv │ │ │ └── b5.gv │ │ └── C │ │ │ ├── c1.gv │ │ │ ├── c2.gv │ │ │ ├── c3.gv │ │ │ ├── c4.gv │ │ │ └── c5.gv │ │ └── Q3 │ │ └── A │ │ └── a1.gv ├── Assignment 5 │ ├── AdjacencyListGraph.java │ ├── BuildItinerary.java │ ├── BuildItineraryTest.java │ ├── DijkstraAlgorithm.java │ ├── Flight.java │ ├── Integral.java │ ├── Printout.txt │ ├── ReadFlights.java │ ├── Readme.txt │ ├── SimpleGraphDijkstra.java │ ├── Time.java │ ├── flights1.txt │ └── flights2.txt └── resources │ ├── Readme.md │ └── binarytree.gv ├── LICENSE ├── Labs ├── Lab 0 │ ├── GNode.java │ ├── LinkList.java │ └── me │ │ └── matt │ │ ├── DoublyLinkList.java │ │ └── Node.java ├── Lab 1 │ ├── AlgAnalysis.java │ ├── Unique1.java │ └── Unique2.java ├── Lab 10 │ └── SEE LAB 5 ├── Lab 2 │ ├── balance │ │ ├── ArrayStack.java │ │ ├── Stack.java │ │ ├── StackEmptyException.java │ │ ├── StackFullException.java │ │ └── bracketsBalance.java │ ├── dLinkList │ │ ├── DLinkedList.java │ │ ├── ListNode.java │ │ └── TestDLinkedList.java │ ├── index.html │ └── stack │ │ ├── ArrayStack.java │ │ ├── EmptyStackException.java │ │ ├── FullStackException.java │ │ ├── Node.java │ │ ├── NodeStack.java │ │ ├── Stack.java │ │ ├── tryStack1.java │ │ └── tryStack2.java ├── Lab 3 │ ├── NodeList.java │ ├── Sentence.java │ └── TestListImplementation.java ├── Lab 5 │ ├── LinkedBinarySearchTree.java │ └── TestTree.java ├── Lab 6 │ └── Verify.java ├── Lab 7 │ ├── ArrayHashTable.java │ ├── ChainedHashTable.java │ ├── DoubleHashStep.java │ ├── HashFunction.java │ ├── HashTable.java │ ├── HashingTest.java │ ├── LinearProbeStep.java │ ├── QuadraticDoubleHashStep.java │ ├── QuadraticProbeStep.java │ ├── ReverseStringHash.java │ ├── SteppingFunction.java │ ├── StringHash.java │ ├── TheAeneidBook1-3.txt │ ├── TheOdysseyBook1.txt │ └── ToyTest.java ├── Lab 8 │ ├── BubbleSort.java │ ├── HeapSortInPlace.java │ ├── MergeSort.java │ ├── QuickSortInPlace.java │ └── TrySorts.java ├── Lab 9 │ ├── SimpleGraphDFS.java │ ├── graph.txt │ └── graph2.txt └── resources │ └── net │ └── datastructures │ ├── AVLTreeMap.java │ ├── AbstractBinaryTree.java │ ├── AbstractHashMap.java │ ├── AbstractMap.java │ ├── AbstractPriorityQueue.java │ ├── AbstractSortedMap.java │ ├── AbstractTree.java │ ├── AdaptablePriorityQueue.java │ ├── AdjacencyMapGraph.java │ ├── ArrayList.java │ ├── ArrayQueue.java │ ├── ArrayStack.java │ ├── BinaryTree.java │ ├── ChainHashMap.java │ ├── CircularQueue.java │ ├── CircularlyLinkedList.java │ ├── DefaultComparator.java │ ├── Deque.java │ ├── DoublyLinkedList.java │ ├── Edge.java │ ├── Entry.java │ ├── Graph.java │ ├── GraphAlgorithms.java │ ├── GraphExamples.java │ ├── HeapAdaptablePriorityQueue.java │ ├── HeapPriorityQueue.java │ ├── LinkedBinaryTree.java │ ├── LinkedCircularQueue.java │ ├── LinkedPositionalList.java │ ├── LinkedQueue.java │ ├── LinkedStack.java │ ├── List.java │ ├── Map.java │ ├── Partition.java │ ├── Position.java │ ├── PositionalList.java │ ├── PriorityQueue.java │ ├── ProbeHashMap.java │ ├── Queue.java │ ├── RBTreeMap.java │ ├── SinglyLinkedList.java │ ├── SortedMap.java │ ├── SortedPriorityQueue.java │ ├── SortedTableMap.java │ ├── SplayTreeMap.java │ ├── Stack.java │ ├── Tree.java │ ├── TreeMap.java │ ├── UnsortedPriorityQueue.java │ ├── UnsortedTableMap.java │ └── Vertex.java ├── Lectures └── Tests │ └── test.java └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | CSI2110.iml 2 | out/ 3 | -------------------------------------------------------------------------------- /Assignments/Assignment 1/Assignment 1.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 1/Assignment 1.docx -------------------------------------------------------------------------------- /Assignments/Assignment 1/Assignment 1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 1/Assignment 1.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 1/Question3.java: -------------------------------------------------------------------------------- 1 | public class Question3 { 2 | 3 | 4 | public static void main(String[] args) { 5 | int size = 10; 6 | System.out.println(perm(size)); 7 | System.out.println(calculateSwaps(size)); 8 | System.out.println(calculateSwapsString(size)); 9 | } 10 | 11 | //Initial recursive call to perm 12 | private static int perm(int size) { 13 | return perm(size, 0); 14 | } 15 | 16 | //Recursive method to calculate number of swaps, based on the algorithm given in class 17 | private static int perm(int n, int i) { 18 | int swaps = 0; 19 | if (i != n) { 20 | for (int j = i; j < n; j++) { 21 | swaps += perm(n, i + 1); 22 | } 23 | } 24 | return swaps + 2*(n-i); 25 | } 26 | 27 | //Iterative method to calculate number of swaps 28 | private static int calculateSwaps(int n) { 29 | int swaps = 0; 30 | for (int i = 1; i <= n; i++) { 31 | int innerResult = n; 32 | for (int j = 1; j <= (n - i); j++) { 33 | innerResult*= (n - j); 34 | } 35 | swaps += innerResult; 36 | } 37 | return 2 * swaps; // The 2 has been factored out 38 | 39 | //size 3: 2(3+3(3-1)+3(3-1)(3-2)) 40 | } 41 | 42 | //Iterative method to build the string for the number of swaps 43 | static String calculateSwapsString(int n) { 44 | String sum = "2("; 45 | //Case to handle 0 46 | if (n <= 0) { 47 | return sum + "0)"; 48 | } 49 | //Build the string to determine the amount of swaps, not simplified but has 2 factored out 50 | for (int i = n; i > 0; i--) { 51 | String product = String.valueOf(n); 52 | for (int j = 1; j <= (n - i); j++) { 53 | product += "(" + n +"-" +j+")"; 54 | } 55 | sum += product + "+"; 56 | } 57 | return sum.substring(0, sum.length() - 1) + ")"; 58 | } 59 | } -------------------------------------------------------------------------------- /Assignments/Assignment 1/Question4.java: -------------------------------------------------------------------------------- 1 | import java.util.LinkedList; 2 | import java.util.Queue; 3 | import java.util.Stack; 4 | 5 | public class Question4 { 6 | 7 | public static void main(String[] args) { 8 | Stack s = new Stack<>(); 9 | s.push("a"); 10 | s.push("b"); 11 | s.push("c"); 12 | s.push("d"); 13 | s.push("e"); 14 | System.out.println(s); 15 | swap(s, 1, 3); 16 | System.out.println(s); 17 | } 18 | 19 | public static void swap(Stack stack, int i, int j) { 20 | Queue q = new LinkedList<>(); 21 | Stack s = new Stack<>(); 22 | int size = stack.size(); 23 | for (int n = 0; n < size; n++) { 24 | if (n==i || n == j) { 25 | q.add(stack.pop()); 26 | } else { 27 | s.push(stack.pop()); 28 | } 29 | } 30 | for (int n = 0; n < size; n++) { 31 | if (n==i || n == j) { 32 | stack.push(q.poll()); 33 | } else { 34 | stack.push(s.pop()); 35 | } 36 | } 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /Assignments/Assignment 1/Questions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 1/Questions.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 2/1d.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 2/1d.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 2/1e1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 2/1e1.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 2/1e2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 2/1e2.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 2/2a1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 2/2a1.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 2/2a2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 2/2a2.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 2/2a3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 2/2a3.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 2/2a4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 2/2a4.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 2/2b.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 2/2b.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 2/2c1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 2/2c1.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 2/2c2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 2/2c2.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 2/2c3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 2/2c3.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 2/31.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 2/31.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 2/310.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 2/310.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 2/311.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 2/311.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 2/312.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 2/312.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 2/313.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 2/313.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 2/314.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 2/314.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 2/315.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 2/315.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 2/316.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 2/316.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 2/317.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 2/317.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 2/318.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 2/318.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 2/319.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 2/319.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 2/32.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 2/32.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 2/320.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 2/320.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 2/321.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 2/321.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 2/322.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 2/322.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 2/323.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 2/323.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 2/324.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 2/324.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 2/33.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 2/33.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 2/34.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 2/34.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 2/35.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 2/35.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 2/36.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 2/36.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 2/37.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 2/37.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 2/38.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 2/38.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 2/39.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 2/39.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 2/Assignment 2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 2/Assignment 2.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 2/Graphs/Q1/D/BT1D.gv: -------------------------------------------------------------------------------- 1 | digraph BT1D { 2 | nodesep=1; 3 | ranksep=0.1; 4 | margin=0; 5 | graph [ordering="out"] 6 | node [shape=circle]; 7 | edge [arrowsize=0.8, arrowhead=none, arrowtail=none]; 8 | 20 -> 15; 9 | 20 -> 35; 10 | 15 -> 10; 11 | 15 -> 19; 12 | 35 -> 25; 13 | 35 -> 40; 14 | 10 -> 5; 15 | 10 -> 13; 16 | 19 -> 16 [side="left"]; 17 | 25 -> 21 [side="left"]; 18 | 40 -> 38 [side="left"]; 19 | 5 -> 3; 20 | 5 -> 7; 21 | 13 -> 12[ side="left"]; 22 | } -------------------------------------------------------------------------------- /Assignments/Assignment 2/Graphs/Q1/E/BT1E1.gv: -------------------------------------------------------------------------------- 1 | digraph BT1E1 { 2 | nodesep=1; 3 | ranksep=0.1; 4 | margin=0; 5 | graph [ordering="out"] 6 | node [shape=circle]; 7 | edge [arrowsize=0.8, arrowhead=none, arrowtail=none]; 8 | 20 -> 16; 9 | 20 -> 35; 10 | 16 -> 10; 11 | 16 -> 19; 12 | 35 -> 25; 13 | 35 -> 40; 14 | 10 -> 5; 15 | 10 -> 13; 16 | 40 -> 38 [side="left"]; 17 | 5 -> 3; 18 | 5 -> 7; 19 | 13 -> 12[ side="left"]; 20 | } -------------------------------------------------------------------------------- /Assignments/Assignment 2/Graphs/Q1/E/BT1E2.gv: -------------------------------------------------------------------------------- 1 | digraph BT1E2 { 2 | nodesep=1; 3 | ranksep=0.1; 4 | margin=0; 5 | graph [ordering="out"] 6 | node [shape=circle]; 7 | edge [arrowsize=0.8, arrowhead=none, arrowtail=none]; 8 | 20 -> 13; 9 | 20 -> 35; 10 | 13 -> 10; 11 | 13 -> 19; 12 | 35 -> 25; 13 | 35 -> 40; 14 | 10 -> 5; 15 | 10 -> 12; 16 | 19 -> 16 [side="left"]; 17 | 40 -> 38 [side="left"]; 18 | 5 -> 3; 19 | 5 -> 7; 20 | } -------------------------------------------------------------------------------- /Assignments/Assignment 2/Graphs/Q2/A/BT2A1.gv: -------------------------------------------------------------------------------- 1 | digraph BT2A1 { 2 | nodesep=1; 3 | ranksep=0.1; 4 | margin=0; 5 | graph [ordering="out"] 6 | node [shape=circle]; 7 | edge [arrowsize=0.8, arrowhead=none, arrowtail=none]; 8 | 1 -> 14; 9 | 1 -> 10; 10 | 14 -> 8; 11 | 14 -> 7; 12 | 10 -> 9; 13 | 10 -> 3; 14 | 8 -> 2; 15 | 8 -> 4; 16 | } -------------------------------------------------------------------------------- /Assignments/Assignment 2/Graphs/Q2/A/BT2A2.gv: -------------------------------------------------------------------------------- 1 | digraph BT2A2 { 2 | nodesep=1; 3 | ranksep=0.1; 4 | margin=0; 5 | graph [ordering="out"] 6 | node [shape=circle]; 7 | edge [arrowsize=0.8, arrowhead=none, arrowtail=none]; 8 | 14 -> 1 [arrowhead=vee, arrowtail=vee, dir=both]; 9 | 14 -> 10; 10 | 1 -> 8; 11 | 1 -> 7; 12 | 10 -> 9; 13 | 10 -> 3; 14 | 8 -> 2; 15 | 8 -> 4; 16 | } -------------------------------------------------------------------------------- /Assignments/Assignment 2/Graphs/Q2/A/BT2A3.gv: -------------------------------------------------------------------------------- 1 | digraph BT2A3 { 2 | nodesep=1; 3 | ranksep=0.1; 4 | margin=0; 5 | graph [ordering="out"] 6 | node [shape=circle]; 7 | edge [arrowsize=0.8, arrowhead=none, arrowtail=none]; 8 | 14 -> 8; 9 | 14 -> 10; 10 | 8 -> 1 [arrowhead=vee, arrowtail=vee, dir=both]; 11 | 8 -> 7; 12 | 10 -> 9; 13 | 10 -> 3; 14 | 1 -> 2; 15 | 1 -> 4; 16 | } -------------------------------------------------------------------------------- /Assignments/Assignment 2/Graphs/Q2/A/BT2A4.gv: -------------------------------------------------------------------------------- 1 | digraph BT2A4 { 2 | nodesep=1; 3 | ranksep=0.1; 4 | margin=0; 5 | graph [ordering="out"] 6 | node [shape=circle]; 7 | edge [arrowsize=0.8, arrowhead=none, arrowtail=none]; 8 | 14 -> 8; 9 | 14 -> 10; 10 | 8 -> 4; 11 | 8 -> 7; 12 | 10 -> 9; 13 | 10 -> 3; 14 | 4 -> 2; 15 | 4 -> 1 [arrowhead=vee, arrowtail=vee, dir=both]; 16 | } -------------------------------------------------------------------------------- /Assignments/Assignment 2/Graphs/Q2/B/BT2B.gv: -------------------------------------------------------------------------------- 1 | digraph BT2B { 2 | nodesep=1; 3 | ranksep=0.1; 4 | margin=0; 5 | graph [ordering="out"] 6 | node [shape=circle]; 7 | edge [arrowsize=0.8, arrowhead=none, arrowtail=none]; 8 | 16 -> 14; 9 | 16 -> 10; 10 | 14 -> 8; 11 | 14 -> 7; 12 | 10 -> 9; 13 | 3.1 [label="3"]; //Special case to allow duplicates 14 | 10 -> 3.1; 15 | 8 -> 2; 16 | 8 -> 4; 17 | 7 -> 1; 18 | 3.2 [label="3"]; //Special case to allow duplicates 19 | 7 -> 3.2; 20 | } -------------------------------------------------------------------------------- /Assignments/Assignment 2/Graphs/Q2/C/BT2C1.gv: -------------------------------------------------------------------------------- 1 | digraph BT2C1 { 2 | nodesep=1; 3 | ranksep=0.1; 4 | margin=0; 5 | graph [ordering="out"] 6 | node [shape=circle]; 7 | edge [arrowsize=0.8, arrowhead=none, arrowtail=none]; 8 | 16 -> 14; 9 | 16 -> 10; 10 | 14 -> 8; 11 | 14 -> 7; 12 | 10 -> 9; 13 | 10 -> 3; 14 | 8 -> 2; 15 | 8 -> 4; 16 | 7 -> 1; 17 | 7 -> 15; 18 | } -------------------------------------------------------------------------------- /Assignments/Assignment 2/Graphs/Q2/C/BT2C2.gv: -------------------------------------------------------------------------------- 1 | digraph BT2C1 { 2 | nodesep=1; 3 | ranksep=0.1; 4 | margin=0; 5 | graph [ordering="out"] 6 | node [shape=circle]; 7 | edge [arrowsize=0.8, arrowhead=none, arrowtail=none]; 8 | 16 -> 14; 9 | 16 -> 10; 10 | 14 -> 8; 11 | 14 -> 15; 12 | 10 -> 9; 13 | 10 -> 3; 14 | 8 -> 2; 15 | 8 -> 4; 16 | 15 -> 1; 17 | 15 -> 7 [arrowhead=vee, arrowtail=vee, dir=both]; 18 | } -------------------------------------------------------------------------------- /Assignments/Assignment 2/Graphs/Q2/C/BT2C3.gv: -------------------------------------------------------------------------------- 1 | digraph BT2C1 { 2 | nodesep=1; 3 | ranksep=0.1; 4 | margin=0; 5 | graph [ordering="out"] 6 | node [shape=circle]; 7 | edge [arrowsize=0.8, arrowhead=none, arrowtail=none]; 8 | 16 -> 15; 9 | 16 -> 10; 10 | 15 -> 8; 11 | 15 -> 14 [arrowhead=vee, arrowtail=vee, dir=both]; 12 | 10 -> 9; 13 | 10 -> 3; 14 | 8 -> 2; 15 | 8 -> 4; 16 | 14 -> 1; 17 | 14 -> 7; 18 | } -------------------------------------------------------------------------------- /Assignments/Assignment 2/Graphs/Q3/BT31.gv: -------------------------------------------------------------------------------- 1 | digraph BT31 { 2 | nodesep=1; 3 | ranksep=0.1; 4 | margin=0; 5 | graph [ordering="out"] 6 | node [shape=circle]; 7 | edge [arrowsize=0.8, arrowhead=none, arrowtail=none]; 8 | 20 -> 3; 9 | 20 ->17; 10 | 3 -> 5; 11 | 3 -> 9; 12 | 17 -> 21; 13 | 17 -> 8; 14 | 5 -> 12; 15 | 5 -> 32; 16 | 9 -> 2 [side="left"]; 17 | } -------------------------------------------------------------------------------- /Assignments/Assignment 2/Graphs/Q3/BT310.gv: -------------------------------------------------------------------------------- 1 | digraph BT310 { 2 | nodesep=1; 3 | ranksep=0.1; 4 | margin=0; 5 | graph [ordering="out"] 6 | node [shape=circle]; 7 | edge [arrowsize=0.8, arrowhead=none, arrowtail=none]; 8 | 20 -> 12; 9 | 20 -> 17; 10 | 12 -> 5; 11 | 12 -> 9; 12 | 17 -> 2; 13 | 17 -> 8; 14 | 5 -> 3 [side="left"]; 15 | } -------------------------------------------------------------------------------- /Assignments/Assignment 2/Graphs/Q3/BT311.gv: -------------------------------------------------------------------------------- 1 | digraph BT311 { 2 | nodesep=1; 3 | ranksep=0.1; 4 | margin=0; 5 | graph [ordering="out"] 6 | node [shape=circle]; 7 | edge [arrowsize=0.8, arrowhead=none, arrowtail=none]; 8 | 3 -> 12; 9 | 3 -> 17; 10 | 12 -> 5; 11 | 12 -> 9; 12 | 17 -> 2; 13 | 17 -> 8; 14 | } -------------------------------------------------------------------------------- /Assignments/Assignment 2/Graphs/Q3/BT312.gv: -------------------------------------------------------------------------------- 1 | digraph BT312 { 2 | nodesep=1; 3 | ranksep=0.1; 4 | margin=0; 5 | graph [ordering="out"] 6 | node [shape=circle]; 7 | edge [arrowsize=0.8, arrowhead=none, arrowtail=none]; 8 | 17 -> 12; 9 | 17 -> 8; 10 | 12 -> 5; 11 | 12 -> 9; 12 | 8 -> 2; 13 | 8 -> 3; 14 | } -------------------------------------------------------------------------------- /Assignments/Assignment 2/Graphs/Q3/BT313.gv: -------------------------------------------------------------------------------- 1 | digraph BT313 { 2 | nodesep=1; 3 | ranksep=0.1; 4 | margin=0; 5 | graph [ordering="out"] 6 | node [shape=circle]; 7 | edge [arrowsize=0.8, arrowhead=none, arrowtail=none]; 8 | 3 -> 12; 9 | 3 -> 8; 10 | 12 -> 5; 11 | 12 -> 9; 12 | 8 -> 2 [side="left"]; 13 | } -------------------------------------------------------------------------------- /Assignments/Assignment 2/Graphs/Q3/BT314.gv: -------------------------------------------------------------------------------- 1 | digraph BT314 { 2 | nodesep=1; 3 | ranksep=0.1; 4 | margin=0; 5 | graph [ordering="out"] 6 | node [shape=circle]; 7 | edge [arrowsize=0.8, arrowhead=none, arrowtail=none]; 8 | 12 -> 9; 9 | 12 -> 8; 10 | 9 -> 5; 11 | 9 -> 3; 12 | 8 -> 2 [side="left"]; 13 | } -------------------------------------------------------------------------------- /Assignments/Assignment 2/Graphs/Q3/BT315.gv: -------------------------------------------------------------------------------- 1 | digraph BT315 { 2 | nodesep=1; 3 | ranksep=0.1; 4 | margin=0; 5 | graph [ordering="out"] 6 | node [shape=circle]; 7 | edge [arrowsize=0.8, arrowhead=none, arrowtail=none]; 8 | 2 -> 9; 9 | 2 -> 8; 10 | 9 -> 5; 11 | 9 -> 3; 12 | } -------------------------------------------------------------------------------- /Assignments/Assignment 2/Graphs/Q3/BT316.gv: -------------------------------------------------------------------------------- 1 | digraph BT316 { 2 | nodesep=1; 3 | ranksep=0.1; 4 | margin=0; 5 | graph [ordering="out"] 6 | node [shape=circle]; 7 | edge [arrowsize=0.8, arrowhead=none, arrowtail=none]; 8 | 9 -> 5; 9 | 9 -> 8; 10 | 5 -> 2; 11 | 5 -> 3; 12 | } -------------------------------------------------------------------------------- /Assignments/Assignment 2/Graphs/Q3/BT317.gv: -------------------------------------------------------------------------------- 1 | digraph BT317 { 2 | nodesep=1; 3 | ranksep=0.1; 4 | margin=0; 5 | graph [ordering="out"] 6 | node [shape=circle]; 7 | edge [arrowsize=0.8, arrowhead=none, arrowtail=none]; 8 | 3 -> 5; 9 | 3 -> 8; 10 | 5 -> 2 [side="left"]; 11 | } -------------------------------------------------------------------------------- /Assignments/Assignment 2/Graphs/Q3/BT318.gv: -------------------------------------------------------------------------------- 1 | digraph BT318 { 2 | nodesep=1; 3 | ranksep=0.1; 4 | margin=0; 5 | graph [ordering="out"] 6 | node [shape=circle]; 7 | edge [arrowsize=0.8, arrowhead=none, arrowtail=none]; 8 | 8 -> 5; 9 | 8 -> 3; 10 | 5 -> 2 [side="left"]; 11 | } -------------------------------------------------------------------------------- /Assignments/Assignment 2/Graphs/Q3/BT319.gv: -------------------------------------------------------------------------------- 1 | digraph BT319 { 2 | nodesep=1; 3 | ranksep=0.1; 4 | margin=0; 5 | graph [ordering="out"] 6 | node [shape=circle]; 7 | edge [arrowsize=0.8, arrowhead=none, arrowtail=none]; 8 | 2 -> 5; 9 | 2 -> 3; 10 | } -------------------------------------------------------------------------------- /Assignments/Assignment 2/Graphs/Q3/BT32.gv: -------------------------------------------------------------------------------- 1 | digraph BT32 { 2 | nodesep=1; 3 | ranksep=0.1; 4 | margin=0; 5 | graph [ordering="out"] 6 | node [shape=circle]; 7 | edge [arrowsize=0.8, arrowhead=none, arrowtail=none]; 8 | 20 -> 3; 9 | 20 ->17; 10 | 3 -> 32; 11 | 3 -> 9; 12 | 17 -> 21; 13 | 17 -> 8; 14 | 32 -> 12; 15 | 32 -> 5 [arrowhead="vee", arrowtail="vee", dir="both"]; 16 | 9 -> 2 [side="left"]; 17 | } -------------------------------------------------------------------------------- /Assignments/Assignment 2/Graphs/Q3/BT320.gv: -------------------------------------------------------------------------------- 1 | digraph BT320 { 2 | nodesep=1; 3 | ranksep=0.1; 4 | margin=0; 5 | graph [ordering="out"] 6 | node [shape=circle]; 7 | edge [arrowsize=0.8, arrowhead=none, arrowtail=none]; 8 | 5 -> 2; 9 | 5 -> 3; 10 | } -------------------------------------------------------------------------------- /Assignments/Assignment 2/Graphs/Q3/BT321.gv: -------------------------------------------------------------------------------- 1 | digraph BT321 { 2 | nodesep=1; 3 | ranksep=0.1; 4 | margin=0; 5 | graph [ordering="out"] 6 | node [shape=circle]; 7 | edge [arrowsize=0.8, arrowhead=none, arrowtail=none]; 8 | 3 -> 2; 9 | } -------------------------------------------------------------------------------- /Assignments/Assignment 2/Graphs/Q3/BT322.gv: -------------------------------------------------------------------------------- 1 | digraph BT323 { 2 | nodesep=1; 3 | ranksep=0.1; 4 | margin=0; 5 | graph [ordering="out"] 6 | node [shape=circle]; 7 | edge [arrowsize=0.8, arrowhead=none, arrowtail=none]; 8 | 3 -> 2 [side="left"]; 9 | } -------------------------------------------------------------------------------- /Assignments/Assignment 2/Graphs/Q3/BT323.gv: -------------------------------------------------------------------------------- 1 | digraph BT323 { 2 | nodesep=1; 3 | ranksep=0.1; 4 | margin=0; 5 | graph [ordering="out"] 6 | node [shape=circle]; 7 | edge [arrowsize=0.8, arrowhead=none, arrowtail=none]; 8 | 2; 9 | } -------------------------------------------------------------------------------- /Assignments/Assignment 2/Graphs/Q3/BT324.gv: -------------------------------------------------------------------------------- 1 | digraph BT324 { 2 | nodesep=1; 3 | ranksep=0.1; 4 | margin=0; 5 | graph [ordering="out"] 6 | node [shape=circle]; 7 | edge [arrowsize=0.8, arrowhead=none, arrowtail=none]; 8 | 2 -> 3; 9 | 2 -> 5; 10 | 3 -> 8; 11 | 3 -> 9; 12 | 5 -> 12; 13 | 5 -> 17; 14 | 8 -> 20; 15 | 8 -> 21; 16 | 9 -> 32 [side="left"]; 17 | } -------------------------------------------------------------------------------- /Assignments/Assignment 2/Graphs/Q3/BT33.gv: -------------------------------------------------------------------------------- 1 | digraph BT33 { 2 | nodesep=1; 3 | ranksep=0.1; 4 | margin=0; 5 | graph [ordering="out"] 6 | node [shape=circle]; 7 | edge [arrowsize=0.8, arrowhead=none, arrowtail=none]; 8 | 20 -> 3; 9 | 20 -> 21; 10 | 3 -> 32; 11 | 3 -> 9; 12 | 21 -> 17 [arrowhead="vee", arrowtail="vee", dir="both"]; 13 | 21 -> 8; 14 | 32 -> 12; 15 | 32 -> 5; 16 | 9 -> 2 [side="left"]; 17 | } -------------------------------------------------------------------------------- /Assignments/Assignment 2/Graphs/Q3/BT34.gv: -------------------------------------------------------------------------------- 1 | digraph BT34 { 2 | nodesep=1; 3 | ranksep=0.1; 4 | margin=0; 5 | graph [ordering="out"] 6 | node [shape=circle]; 7 | edge [arrowsize=0.8, arrowhead=none, arrowtail=none]; 8 | 20 -> 32; 9 | 20 -> 21; 10 | 32 -> 3 [arrowhead="vee", arrowtail="vee", dir="both"]; 11 | 32 -> 9; 12 | 21 -> 17; 13 | 21 -> 8; 14 | 3 -> 12; 15 | 3 -> 5; 16 | 9 -> 2 [side="left"]; 17 | } -------------------------------------------------------------------------------- /Assignments/Assignment 2/Graphs/Q3/BT35.gv: -------------------------------------------------------------------------------- 1 | digraph BT35 { 2 | nodesep=1; 3 | ranksep=0.1; 4 | margin=0; 5 | graph [ordering="out"] 6 | node [shape=circle]; 7 | edge [arrowsize=0.8, arrowhead=none, arrowtail=none]; 8 | 20 -> 32; 9 | 20 -> 21; 10 | 32 -> 12; 11 | 32 -> 9; 12 | 21 -> 17; 13 | 21 -> 8; 14 | 12 -> 3 [arrowhead="vee", arrowtail="vee", dir="both"]; 15 | 12 -> 5; 16 | 9 -> 2 [side="left"]; 17 | } -------------------------------------------------------------------------------- /Assignments/Assignment 2/Graphs/Q3/BT36.gv: -------------------------------------------------------------------------------- 1 | digraph BT36 { 2 | nodesep=1; 3 | ranksep=0.1; 4 | margin=0; 5 | graph [ordering="out"] 6 | node [shape=circle]; 7 | edge [arrowsize=0.8, arrowhead=none, arrowtail=none]; 8 | 32 -> 20 [arrowhead="vee", arrowtail="vee", dir="both"]; 9 | 32 -> 21; 10 | 20 -> 12; 11 | 20 -> 9; 12 | 21 -> 17; 13 | 21 -> 8; 14 | 12 -> 3; 15 | 12 -> 5; 16 | 9 -> 2 [side="left"]; 17 | } -------------------------------------------------------------------------------- /Assignments/Assignment 2/Graphs/Q3/BT37.gv: -------------------------------------------------------------------------------- 1 | digraph BT37 { 2 | nodesep=1; 3 | ranksep=0.1; 4 | margin=0; 5 | graph [ordering="out"] 6 | node [shape=circle]; 7 | edge [arrowsize=0.8, arrowhead=none, arrowtail=none]; 8 | 2 -> 20; 9 | 2 -> 21; 10 | 20 -> 12; 11 | 20 -> 9; 12 | 21 -> 17; 13 | 21 -> 8; 14 | 12 -> 3; 15 | 12 -> 5; 16 | } -------------------------------------------------------------------------------- /Assignments/Assignment 2/Graphs/Q3/BT38.gv: -------------------------------------------------------------------------------- 1 | digraph BT38 { 2 | nodesep=1; 3 | ranksep=0.1; 4 | margin=0; 5 | graph [ordering="out"] 6 | node [shape=circle]; 7 | edge [arrowsize=0.8, arrowhead=none, arrowtail=none]; 8 | 21 -> 20; 9 | 21 -> 17; 10 | 20 -> 12; 11 | 20 -> 9; 12 | 17 -> 2; 13 | 17 -> 8; 14 | 12 -> 3; 15 | 12 -> 5; 16 | } -------------------------------------------------------------------------------- /Assignments/Assignment 2/Graphs/Q3/BT39.gv: -------------------------------------------------------------------------------- 1 | digraph BT39 { 2 | nodesep=1; 3 | ranksep=0.1; 4 | margin=0; 5 | graph [ordering="out"] 6 | node [shape=circle]; 7 | edge [arrowsize=0.8, arrowhead=none, arrowtail=none]; 8 | 5 -> 20; 9 | 5 -> 17; 10 | 20 -> 12; 11 | 20 -> 9; 12 | 17 -> 2; 13 | 17 -> 8; 14 | 12 -> 3 [side="left"]; 15 | } -------------------------------------------------------------------------------- /Assignments/Assignment 3/Assignment 3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 3/Assignment 3.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 3/DecisionStumpTest.java: -------------------------------------------------------------------------------- 1 | public class DecisionStumpTest { 2 | 3 | public static void main(String[] args) { 4 | 5 | // Build a decision tree 6 | // feature 0 is gender (0: female 1:male) 7 | // feature 1 is age 8 | // class 0 is does not wear glasses 9 | // class 1 is wear glasses 10 | DecisionStump.nClasses = 2; 11 | DecisionStump.dim = 2; 12 | 13 | // is age < 45 14 | DecisionStump s1 = new DecisionStump(1, 45); 15 | DecisionStump s2 = new DecisionStump(); // leaf 16 | s2.setProb(1, 89); // wear glasses at 89% 17 | 18 | // is man? 19 | DecisionStump s3 = new DecisionStump(0, 0.5); 20 | 21 | DecisionStump s4 = new DecisionStump(); // leaf 22 | s4.setProb(0, 61); // does not wear glasses at 61% 23 | 24 | // is age < 24 25 | DecisionStump s5 = new DecisionStump(1, 24); 26 | DecisionStump s6 = new DecisionStump(); // leaf 27 | s6.setProb(0, 62); // does not wear glasses at 62% 28 | DecisionStump s7 = new DecisionStump(); // leaf 29 | s7.setProb(1, 53); // wear glasses at 53% 30 | 31 | s1.setSmallerBranch(s3); 32 | s1.setGreaterBranch(s2); 33 | s3.setSmallerBranch(s5); 34 | s3.setGreaterBranch(s4); 35 | s5.setSmallerBranch(s6); 36 | s5.setGreaterBranch(s7); 37 | 38 | double[] test1 = {1, 55}; 39 | System.out.println("Man age 55 = " + s1.getDecision(test1)); 40 | double[] test2 = {1, 35}; 41 | System.out.println("Man age 35 = " + s1.getDecision(test2)); 42 | double[] test3 = {0, 75}; 43 | System.out.println("Woman age 75 = " + s1.getDecision(test3)); 44 | double[] test4 = {0, 27}; 45 | System.out.println("Woman age 27 = " + s1.getDecision(test4)); 46 | double[] test5 = {1, 15}; 47 | System.out.println("Woman age 15 = " + s1.getDecision(test5)); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /Assignments/Assignment 3/NodeReplaceException.java: -------------------------------------------------------------------------------- 1 | public class NodeReplaceException extends Exception { 2 | 3 | public NodeReplaceException(String message) { 4 | super(message); 5 | } 6 | 7 | } 8 | -------------------------------------------------------------------------------- /Assignments/Assignment 3/Readme.txt: -------------------------------------------------------------------------------- 1 | I have created DecisionTreeTest and a DecisionStumpTest classes to perform tests on each of the datatypes. 2 | 3 | DecisionTreeTest produces the output for answers B and C. 4 | 5 | DecisionTreeTest take the full path to a file on the system to perform the tests. The file must be of the same format as iris.data.txt, with the same number of columns of the same data type. 6 | 7 | I have added a method called getAvgExternalProbibality to DecisionTree which determines the avg max probability of all external decision stumps. This is used to calculate the statistics in part C. -------------------------------------------------------------------------------- /Assignments/Assignment 4/1a1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 4/1a1.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 4/1b1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 4/1b1.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 4/1b2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 4/1b2.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 4/1b3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 4/1b3.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 4/1b4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 4/1b4.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 4/1b5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 4/1b5.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 4/1c1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 4/1c1.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 4/1c2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 4/1c2.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 4/1c3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 4/1c3.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 4/1c4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 4/1c4.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 4/1c5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 4/1c5.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 4/3a1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 4/3a1.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 4/Assignment 4.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Assignments/Assignment 4/Assignment 4.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 4/Graphs/Q1/A/a1.gv: -------------------------------------------------------------------------------- 1 | digraph a1 { 2 | //Global graph settings 3 | graph[splines="false",ordering="out"]; 4 | node [shape = record,height=.1,label="| "]; 5 | 6 | //Root 7 | n0:f0:sw -> n1:f1:ne; 8 | n0 -> n2; 9 | n0:f1:se -> n3:f0:nw; 10 | 11 | //Left child of root 12 | n1:f0:sw -> n4:f1:ne; 13 | n1:f0:se -> n5:f1:nw; 14 | n1:f1:se -> n6:f0:nw; 15 | 16 | //center child of root 17 | n2:f0:sw -> n7:f1:ne; 18 | n2 -> n8; 19 | n2:f1:se -> n9:f0:nw; 20 | 21 | //Right child of root 22 | n3:f0:sw -> n10:f1:ne; 23 | n3:f0:se -> n11:f1:nw; 24 | n3:f1:se -> n12:f0:nw; 25 | } -------------------------------------------------------------------------------- /Assignments/Assignment 4/Graphs/Q1/B/b1.gv: -------------------------------------------------------------------------------- 1 | digraph b1 { 2 | //Global graph settings 3 | graph[splines="false",ordering="out"]; 4 | node [shape = record,height=.1,label="| "]; 5 | 6 | //Special nodes 7 | n4[label="||"] 8 | 9 | //Root node 10 | n0:f0:sw -> n1:f1:ne; 11 | n0 -> n2; 12 | n0:f1:se -> n3:f0:nw; 13 | 14 | //Left child of root 15 | n1:f0:sw -> n4:f2:ne; 16 | n1:f0:se -> n5:f1:nw; 17 | n1:f1:se -> n6:f0:nw; 18 | 19 | //Center child of root 20 | n2:f0:sw -> n7:f1:ne; 21 | n2 -> n8; 22 | n2:f1:se -> n9:f0:nw; 23 | 24 | //Right child of root 25 | n3:f0:sw -> n10:f1:ne; 26 | n3:f0:se -> n11:f1:nw; 27 | n3:f1:se -> n12:f0:nw; 28 | } -------------------------------------------------------------------------------- /Assignments/Assignment 4/Graphs/Q1/B/b2.gv: -------------------------------------------------------------------------------- 1 | digraph b2 { 2 | //Global graph settings 3 | graph[splines="false",ordering="out"]; 4 | node [shape = record,height=.1,label="| "]; 5 | 6 | //Special labels 7 | n1[label="||"]; 8 | nIns[width=.4, label=""]; 9 | 10 | //Root node 11 | n0:f0:sw -> n1:f2:ne; 12 | n0 -> n2; 13 | n0:f1:se -> n3:f0:nw; 14 | 15 | //Left child of root 16 | n1:f0:sw -> n4:f1:ne; 17 | n1:f0:se -> nIns:f0:n; 18 | n1:f1:se -> n5:f0:ne; 19 | n1:f2:se -> n6:f0:nw; 20 | 21 | //Center child of root 22 | n2:f0:sw -> n7:f1:ne; 23 | n2 -> n8; 24 | n2:f1:se -> n9:f0:nw; 25 | 26 | //Right child of root 27 | n3:f0:sw -> n10:f1:ne; 28 | n3:f0:se -> n11:f1:nw; 29 | n3:f1:se -> n12:f0:nw; 30 | } -------------------------------------------------------------------------------- /Assignments/Assignment 4/Graphs/Q1/B/b3.gv: -------------------------------------------------------------------------------- 1 | digraph b3 { 2 | //Global graph settings 3 | graph[splines="false",ordering="out"]; 4 | node [shape = record,height=.1,label="| "]; 5 | 6 | //Special labels 7 | n1[label="||"]; 8 | n4[label="||"]; 9 | nIns[width=.4, label=""]; 10 | 11 | //Root node 12 | n0:f0:sw -> n1:f2:ne; 13 | n0 -> n2; 14 | n0:f1:se -> n3:f0:nw; 15 | 16 | //Left child of root 17 | n1:f0:sw -> n4:f2:ne; 18 | n1:f0:se -> nIns:f0:n; 19 | n1:f1:se -> n5:f0:ne; 20 | n1:f2:se -> n6:f0:nw; 21 | 22 | //Center child of root 23 | n2:f0:sw -> n7:f1:ne; 24 | n2 -> n8; 25 | n2:f1:se -> n9:f0:nw; 26 | 27 | //Right child of root 28 | n3:f0:sw -> n10:f1:ne; 29 | n3:f0:se -> n11:f1:nw; 30 | n3:f1:se -> n12:f0:nw; 31 | } -------------------------------------------------------------------------------- /Assignments/Assignment 4/Graphs/Q1/B/b4.gv: -------------------------------------------------------------------------------- 1 | digraph b4 { 2 | //Global graph settings 3 | graph[splines="false",ordering="out"]; 4 | node [shape = record,height=.1,label="| "]; 5 | 6 | //Special labels 7 | n0[label="||"]; 8 | n2[label="",width=.4]; 9 | n6[label=""width=.4]; 10 | n7[label=""width=.4]; 11 | 12 | //Root node 13 | n0:f0:sw -> n1:f1:ne; 14 | n0:f0:se -> n2:f0:n; 15 | n0:f1:se -> n3:f0:ne; 16 | n0:f2:se -> n4:f0:nw; 17 | 18 | //Left child of root 19 | n1:f0:sw -> n5:f1:ne; 20 | n1:f0:se -> n6:f0:n; 21 | n1:f1:se -> n7:f0:nw; 22 | 23 | //Center left child of root 24 | n2:f0:sw -> n8:f1:ne; 25 | n2:f0:se -> n9:f0:nw; 26 | 27 | //Center right child of root 28 | n3:f0:sw -> n10:f1:ne; 29 | n3:f1:sw -> n11:f0:nw; 30 | n3:f1:se -> n12:f0:nw; 31 | 32 | //Right child of root 33 | n4:f0:sw -> n13:f1:ne; 34 | n4:f1:sw -> n14:f0:nw; 35 | n4:f1:se -> n15:f0:nw; 36 | } -------------------------------------------------------------------------------- /Assignments/Assignment 4/Graphs/Q1/B/b5.gv: -------------------------------------------------------------------------------- 1 | digraph b5 { 2 | //Global graph settings 3 | graph[splines="false",ordering="out"]; 4 | node [shape = record,height=.1,label="| "]; 5 | 6 | //Special labels 7 | n0[label="||"]; 8 | n2[label="",width=.4]; 9 | n5[label="||"]; 10 | n6[label=""width=.4]; 11 | n7[label=""width=.4]; 12 | 13 | //Root node 14 | n0:f0:sw -> n1:f1:ne; 15 | n0:f0:se -> n2:f0:n; 16 | n0:f1:se -> n3:f0:ne; 17 | n0:f2:se -> n4:f0:nw; 18 | 19 | //Left child of root 20 | n1:f0:sw -> n5:f2:ne; 21 | n1:f0:se -> n6:f0:n; 22 | n1:f1:se -> n7:f0:nw; 23 | 24 | //Center left child of root 25 | n2:f0:sw -> n8:f1:ne; 26 | n2:f0:se -> n9:f0:nw; 27 | 28 | //Center right child of root 29 | n3:f0:sw -> n10:f1:ne; 30 | n3:f1:sw -> n11:f0:nw; 31 | n3:f1:se -> n12:f0:nw; 32 | 33 | //Right child of root 34 | n4:f0:sw -> n13:f1:ne; 35 | n4:f1:sw -> n14:f0:nw; 36 | n4:f1:se -> n15:f0:nw; 37 | } -------------------------------------------------------------------------------- /Assignments/Assignment 4/Graphs/Q1/C/c1.gv: -------------------------------------------------------------------------------- 1 | digraph c1 { 2 | //Global graph settings 3 | graph[splines="false",ordering="out"]; 4 | node [shape = record,height=.1,label="| "]; 5 | 6 | //Special nodes 7 | n12[label="",width=.4]; 8 | 9 | //Root 10 | n0:f0:sw -> n1:f1:ne; 11 | n0 -> n2; 12 | n0:f1:se -> n3:f0:nw; 13 | 14 | //Left child of root 15 | n1:f0:sw -> n4:f1:ne; 16 | n1:f0:se -> n5:f1:nw; 17 | n1:f1:se -> n6:f0:nw; 18 | 19 | //center child of root 20 | n2:f0:sw -> n7:f1:ne; 21 | n2 -> n8; 22 | n2:f1:se -> n9:f0:nw; 23 | 24 | //Right child of root 25 | n3:f0:sw -> n10:f1:ne; 26 | n3:f0:se -> n11:f1:nw; 27 | n3:f1:se -> n12:f0:nw; 28 | } -------------------------------------------------------------------------------- /Assignments/Assignment 4/Graphs/Q1/C/c2.gv: -------------------------------------------------------------------------------- 1 | digraph c2 { 2 | //Global graph settings 3 | graph[splines="false",ordering="out"]; 4 | node [shape = record,height=.1,label="| "]; 5 | 6 | //Special nodes 7 | n11[label="",width=.4]; 8 | n12[label="",width=.4]; 9 | 10 | //Root 11 | n0:f0:sw -> n1:f1:ne; 12 | n0 -> n2; 13 | n0:f1:se -> n3:f0:nw; 14 | 15 | //Left child of root 16 | n1:f0:sw -> n4:f1:ne; 17 | n1:f0:se -> n5:f1:nw; 18 | n1:f1:se -> n6:f0:nw; 19 | 20 | //center child of root 21 | n2:f0:sw -> n7:f1:ne; 22 | n2 -> n8; 23 | n2:f1:se -> n9:f0:nw; 24 | 25 | //Right child of root 26 | n3:f0:sw -> n10:f1:ne; 27 | n3:f0:se -> n11:f0:n; 28 | n3:f1:se -> n12:f0:nw; 29 | } -------------------------------------------------------------------------------- /Assignments/Assignment 4/Graphs/Q1/C/c3.gv: -------------------------------------------------------------------------------- 1 | digraph c3 { 2 | //Global graph settings 3 | graph[splines="false",ordering="out"]; 4 | node [shape = record,height=.1,label="| "]; 5 | 6 | //Special nodes 7 | n3[label="",width=.4]; 8 | 9 | //Root 10 | n0:f0:sw -> n1:f1:ne; 11 | n0 -> n2; 12 | n0:f1:se -> n3:f0:nw; 13 | 14 | //Left child of root 15 | n1:f0:sw -> n4:f1:ne; 16 | n1:f0:se -> n5:f1:nw; 17 | n1:f1:se -> n6:f0:nw; 18 | 19 | //center child of root 20 | n2:f0:sw -> n7:f1:ne; 21 | n2 -> n8; 22 | n2:f1:se -> n9:f0:nw; 23 | 24 | //Right child of root 25 | n3:f0:sw -> n10:f1:ne; 26 | n3:f0:se -> n11:f0:nw; 27 | } -------------------------------------------------------------------------------- /Assignments/Assignment 4/Graphs/Q1/C/c4.gv: -------------------------------------------------------------------------------- 1 | digraph c4 { 2 | //Global graph settings 3 | graph[splines="false",ordering="out"]; 4 | node [shape = record,height=.1,label="| "]; 5 | 6 | //Special nodes 7 | n3[label="",width=.4]; 8 | n11[label="",width=.4]; 9 | 10 | //Root 11 | n0:f0:sw -> n1:f1:ne; 12 | n0 -> n2; 13 | n0:f1:se -> n3:f0:nw; 14 | 15 | //Left child of root 16 | n1:f0:sw -> n4:f1:ne; 17 | n1:f0:se -> n5:f1:nw; 18 | n1:f1:se -> n6:f0:nw; 19 | 20 | //center child of root 21 | n2:f0:sw -> n7:f1:ne; 22 | n2 -> n8; 23 | n2:f1:se -> n9:f0:nw; 24 | 25 | //Right child of root 26 | n3:f0:sw -> n10:f1:ne; 27 | n3:f0:se -> n11:f0:nw; 28 | } -------------------------------------------------------------------------------- /Assignments/Assignment 4/Graphs/Q1/C/c5.gv: -------------------------------------------------------------------------------- 1 | digraph c5 { 2 | //Global graph settings 3 | graph[splines="false",ordering="out"]; 4 | node [shape = record,height=.1,label="| "]; 5 | 6 | //Special nodes 7 | n3[label="",width=.4]; 8 | n10[label="",width=.4]; 9 | n11[label="",width=.4]; 10 | 11 | //Root 12 | n0:f0:sw -> n1:f1:ne; 13 | n0 -> n2; 14 | n0:f1:se -> n3:f0:nw; 15 | 16 | //Left child of root 17 | n1:f0:sw -> n4:f1:ne; 18 | n1:f0:se -> n5:f1:nw; 19 | n1:f1:se -> n6:f0:nw; 20 | 21 | //center child of root 22 | n2:f0:sw -> n7:f1:ne; 23 | n2 -> n8; 24 | n2:f1:se -> n9:f0:nw; 25 | 26 | //Right child of root 27 | n3:f0:sw -> n10:f0:ne; 28 | n3:f0:se -> n11:f0:nw; 29 | } -------------------------------------------------------------------------------- /Assignments/Assignment 4/Graphs/Q3/A/a1.gv: -------------------------------------------------------------------------------- 1 | digraph a1 { 2 | node [shape = circle] 3 | S -> U; 4 | S -> P; 5 | U -> X; 6 | U -> V; 7 | P -> Q; 8 | X -> Y; 9 | V -> W; 10 | Y -> R; 11 | R -> T; 12 | } -------------------------------------------------------------------------------- /Assignments/Assignment 5/BuildItineraryTest.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | 4 | public class BuildItineraryTest { 5 | 6 | 7 | public static BuildItinerary readFlightsIntoGraph(String flightFileName, boolean verbose) throws Exception{ 8 | 9 | ReadFlights mine = new ReadFlights(); 10 | ArrayList f = mine.read(flightFileName); 11 | if (verbose) { 12 | System.out.println("\n**** Flights Available ****"); 13 | for (Flight aF : f) { 14 | System.out.println(aF); 15 | } 16 | System.out.println("**** End of Flights Available ****\n"); 17 | } 18 | 19 | BuildItinerary buildItiner= new BuildItinerary(f); 20 | AdjacencyListGraph myGraph = buildItiner.getGraph(); 21 | 22 | if (verbose) { 23 | System.out.println("**** Displaying Complete Graph Information ****"); 24 | myGraph.print(); 25 | System.out.println("**** End of Displaying Complete Graph Information **** \n"); 26 | } 27 | return buildItiner; 28 | 29 | } 30 | 31 | public static void main(String[] args) throws Exception { 32 | 33 | //AdjacencyListGraph myGraph; 34 | 35 | boolean verbose=false; 36 | System.out.println("\n----- Starting Test for file flights1.txt (verbose="+verbose+")\n"); 37 | 38 | BuildItinerary buildItiner= readFlightsIntoGraph("flights1.txt",verbose); 39 | 40 | ArrayList itin; 41 | 42 | itin=buildItiner.cheapestItinerary("YOW","YVR",new Time("8:10"), new Time("22:30"),verbose); 43 | 44 | itin=buildItiner.cheapestItinerary("YOW","YVR",new Time("10:00"), new Time("18:30"),verbose); 45 | 46 | itin=buildItiner.cheapestItinerary("YOW","YVR",new Time("10:00"), new Time("24:00"),verbose); 47 | 48 | itin=buildItiner.cheapestItinerary("YOW","YVR",new Time("13:30"), new Time("15:30"),verbose); 49 | 50 | itin=buildItiner.cheapestItinerary("YUL","YVR",new Time("12:00"), new Time("24:00"),verbose); 51 | 52 | System.out.println("\n----- Ending Test for file flight1.txt\n"); 53 | 54 | 55 | verbose=false; 56 | System.out.println("\n----- Starting Test for file flights2.txt (verbose="+verbose+")\n"); 57 | 58 | buildItiner= readFlightsIntoGraph("flights2.txt",verbose); 59 | 60 | itin=buildItiner.cheapestItinerary("YOW","YVR",new Time("8:10"), new Time("22:30"),verbose); 61 | 62 | itin=buildItiner.cheapestItinerary("YOW","YVR",new Time("10:00"), new Time("18:30"),verbose); 63 | 64 | itin=buildItiner.cheapestItinerary("YOW","YVR",new Time("10:00"), new Time("24:00"),verbose); 65 | 66 | itin=buildItiner.cheapestItinerary("YOW","YVR",new Time("13:30"), new Time("15:30"),verbose); 67 | 68 | itin=buildItiner.cheapestItinerary("YUL","YVR",new Time("12:00"), new Time("24:00"),verbose); 69 | 70 | System.out.println("\n----- Ending Test for file flight2.txt\n"); 71 | 72 | //*************************** part 3 ***************************************************// 73 | System.out.println("\nNow tests for Part 3:"); 74 | 75 | buildItiner= readFlightsIntoGraph("flights1.txt",false); 76 | 77 | verbose=false; 78 | buildItiner.whereMoneyCanGetMe("YOW", new Time("5:00"), 500, verbose); 79 | buildItiner.whereMoneyCanGetMe("YOW", new Time("5:00"), 1000, verbose); 80 | 81 | 82 | } 83 | 84 | } 85 | -------------------------------------------------------------------------------- /Assignments/Assignment 5/Flight.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Store Flight Information 3 | */ 4 | public class Flight implements Integral { 5 | 6 | String flightNumber; 7 | String airportDepart; 8 | String airportDestin; 9 | Time timeDepart; 10 | Time timeDestin; 11 | String cityDepart; 12 | String cityDestin; 13 | int costFlight; 14 | 15 | public Flight(String flightN, String airDep, String airDest, String cityDep,String citDest, 16 | Time timeDep, Time timeDest, int cost) { 17 | flightNumber=flightN; 18 | airportDepart=airDep; 19 | airportDestin=airDest; 20 | timeDepart=timeDep; 21 | timeDestin=timeDest; 22 | cityDepart=cityDep; 23 | cityDestin=citDest; 24 | costFlight=cost; 25 | } 26 | 27 | public String getflightNumber() { 28 | return flightNumber; 29 | } 30 | 31 | public String getAirportDepart() { 32 | return airportDepart; 33 | } 34 | 35 | public String getAirportDestin() { 36 | return airportDestin; 37 | } 38 | 39 | public Time getTimeDepart() { 40 | return timeDepart; 41 | } 42 | 43 | public Time getTimeDestin() { 44 | return timeDestin; 45 | } 46 | 47 | public String getCityDepart() { 48 | return cityDepart; 49 | } 50 | 51 | public String getityDestin() { 52 | return cityDestin; 53 | } 54 | 55 | public int getCost() { 56 | return costFlight; 57 | } 58 | public String toString() { 59 | return flightNumber+" "+ airportDepart+" "+cityDepart+ " " + timeDepart+ 60 | " "+ airportDestin+" "+cityDestin+ " " + timeDestin+ " $"+costFlight; 61 | } 62 | 63 | public int toIntegral() { 64 | return costFlight; 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /Assignments/Assignment 5/Integral.java: -------------------------------------------------------------------------------- 1 | 2 | public interface Integral { 3 | int toIntegral(); 4 | } 5 | -------------------------------------------------------------------------------- /Assignments/Assignment 5/Printout.txt: -------------------------------------------------------------------------------- 1 | ----- Starting Test for file flights1.txt (verbose=false) 2 | 3 | 4 | >>>Customer requested departure from YOW after 8:10 arriving at YVR before 22:30 5 | Official Itinerary: YOW 12:00 to YVR 18:31 6 | WS363 YOW Ottawa 12:00 YYZ Toronto 13:04 $200 7 | AC161 YYZ Toronto 16:15 YVR Vancouver 18:31 $600 8 | Total cost=800 9 | 10 | >>>Customer requested departure from YOW after 10:00 arriving at YVR before 18:30 11 | No itineraries available; please widen your search times. 12 | 13 | >>>Customer requested departure from YOW after 10:00 arriving at YVR before 24:00 14 | Official Itinerary: YOW 12:00 to YVR 23:47 15 | WS363 YOW Ottawa 12:00 YYZ Toronto 13:04 $200 16 | AC161 YYZ Toronto 16:15 YVR Vancouver 18:31 $600 17 | Total cost=800 18 | 19 | >>>Customer requested departure from YOW after 13:30 arriving at YVR before 15:30 20 | No itineraries available; please widen your search times. 21 | 22 | >>>Customer requested departure from YUL after 12:00 arriving at YVR before 24:00 23 | Official Itinerary: YUL 14:05 to YVR 23:47 24 | WJ217 YUL Montreal 19:15 YYC Calgary 21:59 $700 25 | WJ131 YYC Calgary 23:15 YVR Vancouver 23:47 $83 26 | Total cost=783 27 | 28 | ----- Ending Test for file flight1.txt 29 | 30 | 31 | ----- Starting Test for file flights2.txt (verbose=false) 32 | 33 | 34 | >>>Customer requested departure from YOW after 8:10 arriving at YVR before 22:30 35 | Official Itinerary: YOW 8:20 to YVR 22:19 36 | WS363 YOW Ottawa 12:00 YYZ Toronto 13:04 $200 37 | AC33 YYZ Toronto 20:05 YVR Vancouver 22:05 $200 38 | Total cost=400 39 | 40 | >>>Customer requested departure from YOW after 10:00 arriving at YVR before 18:30 41 | Official Itinerary: YOW 12:00 to YVR 18:15 42 | AC104 YOW Ottawa 13:20 YUL Montreal 14:05 $150 43 | AC_195 YUL Montreal 17:50 YVR Vancouver 18:15 $500 44 | Total cost=650 45 | 46 | >>>Customer requested departure from YOW after 10:00 arriving at YVR before 24:00 47 | Official Itinerary: YOW 12:00 to YVR 23:47 48 | WS363 YOW Ottawa 12:00 YYZ Toronto 13:04 $200 49 | AC145 YYZ Toronto 22:55 YYC Calgary 23:12 $51 50 | WJ131 YYC Calgary 23:15 YVR Vancouver 23:47 $83 51 | Total cost=334 52 | 53 | >>>Customer requested departure from YOW after 13:30 arriving at YVR before 15:30 54 | No itineraries available; please widen your search times. 55 | 56 | >>>Customer requested departure from YUL after 12:00 arriving at YVR before 24:00 57 | Official Itinerary: YUL 13:30 to YVR 23:47 58 | AC_183 YUL Montreal 13:30 YVR Vancouver 16:07 $478 59 | Total cost=478 60 | 61 | ----- Ending Test for file flight2.txt 62 | 63 | 64 | Now tests for Part 3: 65 | >>>> Here you will provide info on where to go under $500 66 | ----Itenerary---- 67 | WS363 YOW Ottawa 12:00 YYZ Toronto 13:04 $200 68 | Total cost=200 69 | 70 | ----Itenerary---- 71 | AC104 YOW Ottawa 13:20 YUL Montreal 14:05 $150 72 | Total cost=150 73 | 74 | ----Itenerary---- 75 | AC455 YOW Ottawa 14:00 YYZ Toronto 15:07 $225 76 | Total cost=225 77 | 78 | >>>> Here you will provide info on where to go under $1000 79 | ----Itenerary---- 80 | WS363 YOW Ottawa 12:00 YYZ Toronto 13:04 $200 81 | Total cost=200 82 | 83 | ----Itenerary---- 84 | AC104 YOW Ottawa 13:20 YUL Montreal 14:05 $150 85 | Total cost=150 86 | 87 | ----Itenerary---- 88 | WS363 YOW Ottawa 12:00 YYZ Toronto 13:04 $200 89 | AC161 YYZ Toronto 16:15 YVR Vancouver 18:31 $600 90 | Total cost=800 91 | 92 | ----Itenerary---- 93 | AC104 YOW Ottawa 13:20 YUL Montreal 14:05 $150 94 | WJ217 YUL Montreal 19:15 YYC Calgary 21:59 $700 95 | Total cost=850 96 | 97 | ----Itenerary---- 98 | AC455 YOW Ottawa 14:00 YYZ Toronto 15:07 $225 99 | Total cost=225 100 | 101 | 102 | Process finished with exit code 0 -------------------------------------------------------------------------------- /Assignments/Assignment 5/ReadFlights.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.FileReader; 3 | import java.io.IOException; 4 | import java.util.ArrayList; 5 | import java.util.StringTokenizer; 6 | 7 | 8 | public class ReadFlights { 9 | 10 | ArrayList read( String fileName) throws Exception, IOException { 11 | ArrayList a = new ArrayList(); 12 | Flight fly; 13 | String line; 14 | BufferedReader flightFile = 15 | new BufferedReader( new FileReader(fileName)); 16 | line = flightFile.readLine( ); // read header line 17 | 18 | while( ( line = flightFile.readLine( ) ) != null ) { 19 | StringTokenizer st = new StringTokenizer( line, ","); 20 | String aDp,cDp,tDp,aDs,cDs,tDs,fN, costS; int cost; 21 | aDp=st.nextToken(); cDp=st.nextToken(); aDs=st.nextToken(); cDs=st.nextToken(); 22 | fN=st.nextToken(); tDp=st.nextToken(); tDs=st.nextToken(); costS=st.nextToken(); 23 | Time timeDep=new Time(tDp); 24 | Time timeDes=new Time(tDs); 25 | cost=Integer.parseInt(costS); 26 | fly= new Flight(fN,aDp,aDs,cDp,cDs,timeDep,timeDes,cost); 27 | a.add(fly); 28 | } 29 | return a; 30 | } 31 | 32 | public static void main(String[] args) throws IOException, Exception { 33 | // TODO Auto-generated method stub 34 | ReadFlights mine = new ReadFlights(); 35 | ArrayList f = mine.read("flights.txt"); 36 | for (int i=0; i< f.size(); i++) { 37 | System.out.println(f.get(i)); 38 | } 39 | 40 | 41 | 42 | 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /Assignments/Assignment 5/Readme.txt: -------------------------------------------------------------------------------- 1 | In part 3 I chose to print out the itineraries instead of just the final vertices so that more information would be available to the end user. 2 | 3 | Part 3 also uses the original question from the assignment, showing all flights under the cost, and not just the minimal ones. The prof stated this was okay and to leave it this way. -------------------------------------------------------------------------------- /Assignments/Assignment 5/Time.java: -------------------------------------------------------------------------------- 1 | 2 | public class Time implements Comparable{ 3 | 4 | int hour; 5 | int min; 6 | 7 | public Time(int hour, int min) { 8 | this.hour=hour; 9 | this.min=min; 10 | } 11 | 12 | public Time (String hhmm) throws IllegalArgumentException { // convert string in the form hh:mm 13 | int endofHour=hhmm.indexOf(":"); 14 | if (endofHour==-1) throw new IllegalArgumentException("Invalid Time"); 15 | this.hour=Integer.parseInt(hhmm.substring(0, endofHour)); 16 | this.min=Integer.parseInt(hhmm.substring(hhmm.length()-2,hhmm.length())); 17 | 18 | } 19 | 20 | public int getHour() { return hour;} 21 | public int getMin() {return min;} 22 | 23 | public String toString() { 24 | String s; 25 | if (min<10) s=":0"+min; 26 | else s=":"+min; 27 | if (hour<10) s=" "+hour+s; 28 | else s=hour+s; 29 | return s; 30 | } 31 | 32 | @Override 33 | public int compareTo(Object o) { 34 | Time time = (Time) o; 35 | int compare = this.hour-time.hour; 36 | if (compare==0) 37 | compare = this.min-time.min; 38 | return compare; 39 | } 40 | 41 | 42 | 43 | } 44 | -------------------------------------------------------------------------------- /Assignments/Assignment 5/flights1.txt: -------------------------------------------------------------------------------- 1 | Origin Airport,Origin city,Destination Airport,Destination city,flight number,departure,arrival,price YOW,Ottawa,YYZ,Toronto,WS363,12:00,13:04,200 2 | YOW,Ottawa,YYZ,Toronto,AC455,14:00,15:07,225 3 | YOW,Ottawa,YUL,Montreal,AC104,13:20,14:05,150 4 | YYZ,Toronto,YVR,Vancouver,WJ709,12:55,15:11,324 5 | YYZ,Toronto,YVR,Vancouver,AC161,16:15,18:31,600 6 | YUL,Montreal,YYC,Calgary,WJ217,19:15,21:59,700 7 | YYC,Calgary,YVR,Vancouver,WJ101,13:25,13:57,150 8 | YYC,Calgary,YVR,Vancouver,WJ131,23:15,23:47,83 -------------------------------------------------------------------------------- /Assignments/Assignment 5/flights2.txt: -------------------------------------------------------------------------------- 1 | Origin Airport,Origin city,Destination Airport,Destination city,flight number,departure,arrival,price YOW,Ottawa,YYZ,Toronto,WS363,12:00,13:04,200 YOW,Ottawa,YYZ,Toronto,AC451,12:00,13:07,222 YOW,Ottawa,YYZ,Toronto,AC453,13:00,14:07,224 YOW,Ottawa,YYZ,Toronto,WS3465,13:40,14:59,225 YOW,Ottawa,YYZ,Toronto,AC455,14:00,15:07,225 YOW,Ottawa,YYZ,Toronto,JV503,14:45,15:55,226 YOW,Ottawa,YYZ,Toronto,AC457,15:00,16:07,227 YYZ,Toronto,YVR,Vancouver,AC101,12:10,14:26,300 YYZ,Toronto,YVR,Vancouver,WJ709,12:55,15:11,324 YYZ,Toronto,YVR,Vancouver,WJ713,14:00,16:16,450 YYZ,Toronto,YVR,Vancouver,AC109,14:20,16:36,550 YYZ,Toronto,YVR,Vancouver,AC161,16:15,18:31,600 YYZ,Toronto,YVR,Vancouver,AC137,18:00,20:00,700 YYZ,Toronto,YVR,Vancouver,WJ719,18:30,20:46,560 YYZ,Toronto,YVR,Vancouver,AC149,19:00,21:00,300 YYZ,Toronto,YVR,Vancouver,AC33,20:05,22:05,200 YUL,Montreal,YYC,Calgary,AC165,7:50,10:34,60 YUL,Montreal,YYC,Calgary,AC187,14:15,17:04,400 YUL,Montreal,YYC,Calgary,WJ217,19:15,21:59,700 YUL,Montreal,YYC,Calgary,AC155,19:45,22:29,680 YYC,Calgary,YVR,Vancouver,AC213,12:35,13:09,100 YYC,Calgary,YVR,Vancouver,WJ101,13:25,13:57,150 YYC,Calgary,YVR,Vancouver,AC215,13:35,14:09,250 YYC,Calgary,YVR,Vancouver,AC217,14:15,14:47,333 YYC,Calgary,YVR,Vancouver,WJ311,16:08,16:40,200 YYC,Calgary,YVR,Vancouver,WJ531,17:00,17:32,230 YYC,Calgary,YVR,Vancouver,AC223,17:15,17:49,202 YYC,Calgary,YVR,Vancouver,WJ553,18:20,18:52,222 YYC,Calgary,YVR,Vancouver,AC225,19:15,19:49,220 YYC,Calgary,YVR,Vancouver,AC229,21:45,22:19,200 YYC,Calgary,YVR,Vancouver,WJ557,22:00,22:32,100 YYC,Calgary,YVR,Vancouver,WJ131,23:15,23:47,83 YYZ,Toronto,YYC,Calgary,AC173,12:10,14:30,490 YYZ,Toronto,YYC,Calgary,AC121,14:05,16:25,399 YYZ,Toronto,YYC,Calgary,AC125,16:05,18:25,357 YYZ,Toronto,YYC,Calgary,WJ669,18:00,20:20,400 YYZ,Toronto,YYC,Calgary,AC133,18:10,20:30,250 YYZ,Toronto,YYC,Calgary,WJ671,19:45,22:05,345 YYZ,Toronto,YYC,Calgary,WJ673,20:40,23:00,79 YYZ,Toronto,YYC,Calgary,AC145,22:55,23:12,51 YOW,Ottawa,YUL,Montreal,AC7556,5:45,6:25,60 YOW,Ottawa,YUL,Montreal,AC8976,6:30,7:08,70 YOW,Ottawa,YUL,Montreal,AC8978,8:20,9:02,70 YOW,Ottawa,YUL,Montreal,AC8658,9:15,9:57,88 YOW,Ottawa,YUL,Montreal,AC104,13:20,14:05,150 YOW,Ottawa,YUL,Montreal,AC8980,15:40,16:22,300 YOW,Ottawa,YUL,Montreal,AC8986,18:20,19:00,256 YOW,Ottawa,YUL,Montreal,AC8988,19:20,19:58,66 YOW,Ottawa,YUL,Montreal,AC8660,20:15,20:57,40 YUL,Montreal,YVR,Vancouver,AC111,8:05,10:42,678 YUL,Montreal,YVR,Vancouver,AC_183,13:30,16:07,478 YUL,Montreal,YVR,Vancouver,AC_195,17:50,18:15,500 -------------------------------------------------------------------------------- /Assignments/resources/Readme.md: -------------------------------------------------------------------------------- 1 | # Resources 2 | 3 | 1. binarytree.gv - A simple dot script to generate clean horizontal trees usage: `[sudo] dot MyTree.gv | gvpr -c -f binarytree.gv | neato -n -T pdf -o out.pdf` 4 | 5 | sudo dot ~/Documents/Programming/School/CSI2110/Assignments/Assignment\ 2/Graphs/Q2/B/BT2B.gv | gvpr -c -f /Users/mattlanglois/Documents/Programming/School/CSI2110/Assignments/resources/binarytree.gv | neato -n -T pdf -o ~/Downloads/2B.pdf 6 | -------------------------------------------------------------------------------- /Assignments/resources/binarytree.gv: -------------------------------------------------------------------------------- 1 | // http://stackoverflow.com/questions/10902745/enforcing-horizontal-node-ordering-in-a-dot-tree 2 | 3 | BEGIN { 4 | double tw[node_t]; // width of tree rooted at node 5 | double nw[node_t]; // width of node 6 | double xoff[node_t]; // x offset of root from left side of its tree 7 | double sp = 36; // extra space between left and right subtrees 8 | double wd, w, w1, w2; 9 | double x, y, z; 10 | edge_t e1, e2; 11 | node_t n; 12 | } 13 | BEG_G { 14 | $.bb = ""; 15 | $tvtype=TV_postfwd; // visit root after all children visited 16 | } 17 | N { 18 | sscanf ($.width, "%f", &w); 19 | w *= 72; // convert inches to points 20 | nw[$] = w; 21 | if ($.outdegree == 0) { 22 | tw[$] = w; 23 | xoff[$] = w/2.0; 24 | } 25 | else if ($.outdegree == 1) { 26 | e1 = fstout($); 27 | w1 = tw[e1.head]; 28 | tw[$] = w1 + (sp+w)/2.0; 29 | if (e1.side == "left") 30 | xoff[$] = tw[$] - w/2.0; 31 | else 32 | xoff[$] = w/2.0; 33 | } 34 | else { 35 | e1 = fstout($); 36 | w1 = tw[e1.head]; 37 | e2 = nxtout(e1); 38 | w2 = tw[e2.head]; 39 | wd = w1 + w2 + sp; 40 | if (w > wd) 41 | wd = w; 42 | tw[$] = wd; 43 | xoff[$] = w1 + sp/2.0; 44 | } 45 | } 46 | BEG_G { 47 | $tvtype=TV_fwd; // visit root first, then children 48 | } 49 | N { 50 | if ($.indegree == 0) { 51 | sscanf ($.pos, "%f,%f", &x, &y); 52 | $.pos = sprintf("0,%f", y); 53 | } 54 | if ($.outdegree == 0) return; 55 | sscanf ($.pos, "%f,%f", &x, &y); 56 | wd = tw[$]; 57 | e1 = fstout($); 58 | n = e1.head; 59 | sscanf (n.pos, "%f,%f", &z, &y); 60 | if ($.outdegree == 1) { 61 | if (e1.side == "left") 62 | n.pos = sprintf("%f,%f", x - tw[n] - sp/2.0 + xoff[n], y); 63 | else 64 | n.pos = sprintf("%f,%f", x + sp/2.0 + xoff[n], y); 65 | } 66 | else { 67 | n.pos = sprintf("%f,%f", x - tw[n] - sp/2.0 + xoff[n], y); 68 | e2 = nxtout(e1); 69 | n = e2.head; 70 | sscanf (n.pos, "%f,%f", &z, &y); 71 | n.pos = sprintf("%f,%f", x + sp/2.0 + xoff[n], y); 72 | } 73 | } -------------------------------------------------------------------------------- /Labs/Lab 0/GNode.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Node of a singly linked list, which stores references to its 3 | * element and to the next node in the list. 4 | * 5 | * @author Natasha Gelfand 6 | * @author Roberto Tamassia 7 | * @author Michael Goodrich 8 | */ 9 | //begin#fragment Node 10 | public class GNode { 11 | // Instance variables: 12 | private E element; 13 | private GNode next; 14 | 15 | /** 16 | * Creates a node with null references to its element and next node. 17 | */ 18 | public GNode() { 19 | this(null, null); 20 | } 21 | 22 | /** 23 | * Creates a node with the given element and next node. 24 | */ 25 | public GNode(E e, GNode n) { 26 | element = e; 27 | next = n; 28 | } 29 | 30 | // Accessor methods: 31 | public E getElement() { 32 | return element; 33 | } 34 | 35 | public GNode getNext() { 36 | return next; 37 | } 38 | 39 | // Modifier methods: 40 | public void setElement(E newElem) { 41 | element = newElem; 42 | } 43 | 44 | public void setNext(GNode newNext) { 45 | next = newNext; 46 | } 47 | } 48 | //end#fragment Node 49 | -------------------------------------------------------------------------------- /Labs/Lab 0/LinkList.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Builds a singly linked list of size 5 and prints it to the console. 3 | * 4 | * @author Jochen Lang 5 | */ 6 | 7 | class LinkList { 8 | GNode llist; 9 | 10 | LinkList(int sz) { 11 | if (sz <= 0) { 12 | llist = null; 13 | } else { 14 | // start with list of size 1 15 | llist = new GNode("0", null); 16 | GNode current = llist; // temp GNode for loop 17 | // add further nodes 18 | for (int i = 1; i < sz; ++i) { 19 | // create GNode and attach it to the list 20 | GNode node2Add = new GNode(Integer.toString(i), null); 21 | current.setNext(node2Add); // add first GNode 22 | current = node2Add; 23 | } 24 | } 25 | } 26 | 27 | /** 28 | * Print all the elements of the list assuming that they are Strings 29 | */ 30 | public void print() { 31 | /* Print the list */ 32 | GNode current = llist; // point to the first GNode 33 | while (current != null) { 34 | System.out.print(current.getElement() + " "); 35 | current = current.getNext(); // move to the next 36 | } 37 | System.out.println(); 38 | } 39 | 40 | public void deleteFirst() { 41 | if (llist != null) { 42 | llist = llist.getNext(); 43 | } 44 | } 45 | 46 | public void deleteLast() { 47 | if (llist == null) return; // no GNode 48 | GNode prev = llist; 49 | GNode current = prev.getNext(); 50 | if (current == null) { // only 1 GNode 51 | llist = null; 52 | return; 53 | } 54 | while (current.getNext() != null) { // more than 1 GNode 55 | prev = current; 56 | current = current.getNext(); 57 | } 58 | prev.setNext(null); 59 | } 60 | 61 | // create and display a linked list 62 | public static void main(String[] args) { 63 | /* Create the list */ 64 | LinkList llist = new LinkList(5); 65 | /* Print the list */ 66 | llist.print(); 67 | /* delete first and print */ 68 | llist.deleteFirst(); 69 | llist.print(); 70 | /* delete last and print 5 times */ 71 | for (int i = 0; i < 5; ++i) { 72 | llist.deleteLast(); 73 | llist.print(); 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /Labs/Lab 0/me/matt/DoublyLinkList.java: -------------------------------------------------------------------------------- 1 | package me.matt; 2 | 3 | /** 4 | * Builds a singly linked list of size 5 and prints it to the console. 5 | * 6 | * @author Jochen Lang 7 | */ 8 | 9 | class DoublyLinkList { 10 | Node llist; 11 | 12 | DoublyLinkList(int sz) { 13 | if (sz <= 0) { 14 | llist = null; 15 | } else { 16 | // start with list of size 1 17 | llist = new Node("0", null, null); 18 | Node current = llist; // temp Node for loop 19 | // add further nodes 20 | for (int i = 1; i < sz; ++i) { 21 | // create Node and attach it to the list 22 | Node node2Add = new Node(Integer.toString(i), null, null); 23 | current.setNext(node2Add); // add first Node 24 | current = node2Add; 25 | } 26 | } 27 | } 28 | 29 | /** 30 | * Print all the elements of the list assuming that they are Strings 31 | */ 32 | public void print() { 33 | /* Print the list */ 34 | Node current = llist; // point to the first Node 35 | while (current != null) { 36 | System.out.print(current.getElement() + " "); 37 | current = current.getNext(); // move to the next 38 | } 39 | System.out.println(); 40 | } 41 | 42 | public void deleteFirst() { 43 | if (llist != null) { 44 | llist = llist.getNext(); 45 | } 46 | } 47 | 48 | public void deleteLast() { 49 | if (llist == null) return; // no Node 50 | Node prev = llist; 51 | Node current = prev.getNext(); 52 | if (current == null) { // only 1 Node 53 | llist = null; 54 | return; 55 | } 56 | while (current.getNext() != null) { // more than 1 Node 57 | prev = current; 58 | current = current.getNext(); 59 | } 60 | prev.setNext(null); 61 | } 62 | 63 | // create and display a linked list 64 | public static void main(String[] args) { 65 | /* Create the list */ 66 | DoublyLinkList llist = new DoublyLinkList(5); 67 | /* Print the list */ 68 | llist.print(); 69 | /* delete first and print */ 70 | llist.deleteFirst(); 71 | llist.print(); 72 | /* delete last and print 5 times */ 73 | for (int i = 0; i < 5; ++i) { 74 | llist.deleteLast(); 75 | llist.print(); 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /Labs/Lab 0/me/matt/Node.java: -------------------------------------------------------------------------------- 1 | package me.matt; 2 | /** 3 | * Node of a singly linked list, which stores references to its 4 | * element and to the next node in the list. 5 | * 6 | * @author Natasha Gelfand 7 | * @author Roberto Tamassia 8 | * @author Michael Goodrich 9 | */ 10 | //begin#fragment Node 11 | public class Node { 12 | // Instance variables: 13 | private E element; 14 | private Node prev; 15 | private Node next; 16 | 17 | /** 18 | * Creates a node with null references to its element and next node. 19 | */ 20 | public Node() { 21 | this(null, null, null); 22 | } 23 | 24 | /** 25 | * Creates a node with the given element and next node. 26 | */ 27 | public Node(E e, Node p, Node n) { 28 | element = e; 29 | prev = p; 30 | next = n; 31 | } 32 | 33 | 34 | 35 | // Accessor methods: 36 | public E getElement() { 37 | return element; 38 | } 39 | 40 | public Node getNext() { 41 | return next; 42 | } 43 | 44 | // Modifier methods: 45 | public void setElement(E newElem) { 46 | element = newElem; 47 | } 48 | 49 | public void setNext(Node newNext) { 50 | next = newNext; 51 | } 52 | } 53 | //end#fragment Node 54 | -------------------------------------------------------------------------------- /Labs/Lab 1/Unique1.java: -------------------------------------------------------------------------------- 1 | // CSI2110 Fall 2015 Laboratory 1.5: Algorithm Runtimes 2 | // ========================================================================== 3 | // (C)opyright: 4 | // 5 | // Lachlan Plant 6 | // SITE, University of Ottawa 7 | // 800 King Edward Ave. 8 | // Ottawa, On., K1N 6N5 9 | // Canada. 10 | // http://www.site.uottawa.ca 11 | // 12 | // Creator: lplant (Lachlan Plant) 13 | // Email: lplan053@uottawa.ca 14 | // ========================================================================== 15 | // $Log: Unique1.java,v $ 16 | // Revision 1.0 2015/09/14 01:20:40 lplant 17 | // ========================================================================== 18 | 19 | public class Unique1{ 20 | 21 | public static boolean unique1(int[] input){ 22 | for(int i = 0; i < input.length-1; i++){ 23 | for(int j = i+1; j < input.length; j++){ 24 | if(input[i] == input[j]) 25 | return false; 26 | } 27 | } 28 | return true; 29 | } 30 | 31 | } -------------------------------------------------------------------------------- /Labs/Lab 1/Unique2.java: -------------------------------------------------------------------------------- 1 | // CSI2110 Fall 2015 Laboratory 1.5: Algorithm Runtimes 2 | // ========================================================================== 3 | // (C)opyright: 4 | // 5 | // Lachlan Plant 6 | // SITE, University of Ottawa 7 | // 800 King Edward Ave. 8 | // Ottawa, On., K1N 6N5 9 | // Canada. 10 | // http://www.site.uottawa.ca 11 | // 12 | // Creator: lplant (Lachlan Plant) 13 | // Email: lplan053@uottawa.ca 14 | // ========================================================================== 15 | // $Log: Unique2.java,v $ 16 | // Revision 1.0 2015/09/14 01:20:40 lplant 17 | // ========================================================================== 18 | import java.util.*; 19 | public class Unique2{ 20 | 21 | public static boolean unique2(int[] input){ 22 | Arrays.sort(input); 23 | for(int i = 0; i < input.length-1; i++){ 24 | if(input[i] == input[i+1]) 25 | return false; 26 | } 27 | return true; 28 | } 29 | 30 | 31 | } -------------------------------------------------------------------------------- /Labs/Lab 10/SEE LAB 5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fletchto99/CSI2110/5448a41b425d81ad2cce860599a2936301e22e90/Labs/Lab 10/SEE LAB 5 -------------------------------------------------------------------------------- /Labs/Lab 2/balance/ArrayStack.java: -------------------------------------------------------------------------------- 1 | package balance; 2 | 3 | public class ArrayStack implements Stack{ 4 | 5 | public static final int CAPACITY = 1000; //# default capacity of the stack 6 | private int capacity; // maximum capacity of the stack. 7 | private Object S[]; // S holds the elements of the stack 8 | private int top = -1; // the top element of the stack. 9 | 10 | public ArrayStack() { //# Initialize the stack with default capacity 11 | this(CAPACITY); 12 | } 13 | 14 | public ArrayStack(int cap) { //# Initialize the stack with given capacity 15 | capacity = cap; 16 | S = new Object[capacity]; 17 | } 18 | 19 | public int size() { //# Return the current stack size 20 | return (top + 1); 21 | } 22 | 23 | public boolean isEmpty() { //# Return true iff the stack is empty 24 | return (top < 0); 25 | } 26 | 27 | public void push(Object obj) throws StackFullException { //# Push a new object on the stack 28 | if (size() == capacity) 29 | throw new StackFullException("Stack overflow."); 30 | S[++top] = obj; 31 | } 32 | 33 | public Object top() throws StackEmptyException { //# Return the top stack element 34 | if (isEmpty()) 35 | throw new StackEmptyException("Stack is empty."); 36 | return S[top]; 37 | } 38 | 39 | public Object pop() throws StackEmptyException { //# Pop off the stack element 40 | Object elem; 41 | if (isEmpty()) 42 | throw new StackEmptyException("Stack is Empty."); 43 | elem = S[top]; 44 | S[top--] = null; //# Dereference S[top] and decrement top 45 | return elem; 46 | } 47 | } -------------------------------------------------------------------------------- /Labs/Lab 2/balance/Stack.java: -------------------------------------------------------------------------------- 1 | package balance; 2 | 3 | public interface Stack { 4 | 5 | // Return the number of elements. 6 | public abstract int size(); 7 | 8 | // Tests if the stack is empty. 9 | public abstract boolean isEmpty(); 10 | 11 | // Inspect the top element, without removing it or otherwise modifying the stack. 12 | public abstract Object top() throws StackEmptyException; 13 | 14 | // Insert an element at the top. 15 | public abstract void push(Object element) throws StackFullException; 16 | 17 | // Remove the top element. 18 | public abstract Object pop() throws StackEmptyException; 19 | 20 | } -------------------------------------------------------------------------------- /Labs/Lab 2/balance/StackEmptyException.java: -------------------------------------------------------------------------------- 1 | package balance; 2 | 3 | public class StackEmptyException extends RuntimeException { 4 | public StackEmptyException(String err) { 5 | super(err); 6 | } 7 | } -------------------------------------------------------------------------------- /Labs/Lab 2/balance/StackFullException.java: -------------------------------------------------------------------------------- 1 | package balance; 2 | 3 | public class StackFullException extends RuntimeException { 4 | public StackFullException(String err) { 5 | super(err); 6 | } 7 | } -------------------------------------------------------------------------------- /Labs/Lab 2/balance/bracketsBalance.java: -------------------------------------------------------------------------------- 1 | package balance;/* CSI2114 Lab 3 - lab3.java 2 | * 3 | * Class to check balanced brackets in math expressions 4 | * 5 | * Usage: java bracketsBalance 6 | * 7 | * by Jeff Souza 8 | * 9 | */ 10 | 11 | class bracketsBalance { 12 | 13 | private boolean bBalance(String exp) { 14 | Stack sq = new ArrayStack(); 15 | Stack rd = new ArrayStack(); 16 | Stack o = new ArrayStack(); 17 | for (char c : exp.toCharArray()) { 18 | if (c == '[') { 19 | sq.push('['); 20 | } else if (c == ']') { 21 | sq.pop(); 22 | } else if (c == '(') { 23 | rd.push('('); 24 | } else if (c == ')') { 25 | rd.pop(); 26 | } else if (c == '{') { 27 | o.push('('); 28 | } else if (c == '}') { 29 | o.pop(); 30 | } 31 | } 32 | return rd.isEmpty() && sq.isEmpty() && o.isEmpty(); 33 | 34 | } 35 | 36 | public static void main(String[] args) { 37 | 38 | bracketsBalance b = new bracketsBalance(); 39 | boolean result = b.bBalance("{{(})}"); 40 | 41 | if (result) System.out.println("The expression is balanced."); 42 | else System.out.println("The expression is NOT balanced."); 43 | } 44 | } -------------------------------------------------------------------------------- /Labs/Lab 2/dLinkList/DLinkedList.java: -------------------------------------------------------------------------------- 1 | /* CSI2114 Lab 2 - DLinkedList.java 2 | * 3 | * Class doubly linked list 4 | * 5 | * by Jeff Souza 6 | * 7 | */ 8 | 9 | package dLinkList; 10 | 11 | class DLinkedList { 12 | 13 | ListNode firstNode; 14 | ListNode lastNode; 15 | 16 | // Appends a node to the end of the list 17 | void AppendNode(ListNode nNode) { 18 | InsertNode(nNode, lastNode); 19 | } 20 | 21 | // Inserts a node into the list after pAfter 22 | void InsertNode(ListNode nNode, ListNode pAfter) { 23 | nNode.next = pAfter.next; 24 | nNode.previous = pAfter; 25 | nNode.next.previous = nNode; 26 | pAfter.next = nNode; 27 | } 28 | 29 | // Removes the specified node from the list 30 | void RemoveNode(ListNode nNode) { 31 | nNode.previous.next = nNode.next; 32 | nNode.next.previous = nNode.previous; 33 | } 34 | 35 | // print the content of the list 36 | void print() { 37 | ListNode nNode = null; 38 | System.out.print("Current list: "); 39 | for (nNode = firstNode; nNode != null; nNode = nNode.next) { 40 | System.out.print(nNode.data + " "); 41 | } 42 | System.out.println(""); 43 | } 44 | 45 | } -------------------------------------------------------------------------------- /Labs/Lab 2/dLinkList/ListNode.java: -------------------------------------------------------------------------------- 1 | package dLinkList;/* CSI2114 Lab 2 - ListNode.java 2 | * 3 | * Class node of a doubly linked list 4 | * 5 | * by Jeff Souza 6 | * 7 | */ 8 | 9 | class ListNode { 10 | int data; 11 | ListNode next; 12 | ListNode previous; 13 | } 14 | -------------------------------------------------------------------------------- /Labs/Lab 2/dLinkList/TestDLinkedList.java: -------------------------------------------------------------------------------- 1 | package dLinkList;/* CSI2114 Lab 2 - TestDLinkedList.java 2 | * 3 | * Class to test doubly linked list 4 | * 5 | * by Jeff Souza 6 | * 7 | */ 8 | 9 | class TestDLinkedList { 10 | 11 | public static void main(String[] args) { 12 | 13 | ListNode nNode = new ListNode(); 14 | nNode.data = 1; 15 | DLinkedList list = new DLinkedList(); 16 | list.firstNode = nNode; 17 | list.lastNode = nNode; 18 | 19 | // add items to linked list 20 | for (int i = 2; i < 11; i++) { 21 | nNode = new ListNode(); 22 | nNode.data = i; 23 | list.AppendNode(nNode); 24 | } 25 | 26 | System.out.println(); 27 | 28 | // print the content of the list 29 | list.print(); 30 | 31 | System.out.println("items removed."); 32 | 33 | // remove items from linked list 34 | list.RemoveNode(list.firstNode); 35 | list.RemoveNode(list.firstNode); 36 | list.RemoveNode(list.lastNode); 37 | 38 | // print the content of the list 39 | list.print(); 40 | } 41 | } -------------------------------------------------------------------------------- /Labs/Lab 2/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Lab 2 6 | 7 |

8 |

Lab 2

9 |

10 |

1) Stacks. Array and linked list implementation of stacks. 11 |

12 |

13 |

    14 |
  • 15 | Download the archive Lab2.zip and extract subdirectory stack 16 | with the stack implementation contained in the following files 17 |
      18 |
    • Stack.java
    • 19 |
    • Node.java
    • 20 |
    • NodeStack.java
    • 21 |
    • ArrayStack.java
    • 22 |
    • StackEmptyException.java
    • 23 |
    • FullStackException.java
    • 24 |
    25 |
  • 26 |
  • 27 | The archive also contains the files tryStack1.java 28 | and tryStack2.java 29 |
      30 |
    • Compile tryStack1 and run it
    • 31 |
    • Compile tryStack2 and run it
    • 32 |
    33 |
  • 34 |
35 |

36 |

2) Doubly Linked Lists in Java

37 |

38 |

    39 |
  • 40 | Extract the subdirectory dLinkList from the archive Lab2.zip 41 | with the implementation of the doubly-linked list contained in the following files. 42 |
      43 |
    • ListNode.java
    • 44 |
    • DLinkedList.java
    • 45 |
    46 |
  • 47 |
48 |
    49 |
  • In DLinkedList.java implement the following methods: 50 |
      51 |
    • InsertNode(ListNode nNode, ListNode pAfter) 52 |
        53 |
      • inserts the node nNode after node pAfter in the 54 | current list
      • 55 |
      56 |
    • 57 |
    • RemoveNode(ListNode nNode) 58 |
        59 |
      • removes node nNode from current list
      • 60 |
      61 |
    • 62 |
    63 |
  • 64 |
  • Compile TestDLinkedList.java and run it
  • 65 |
66 |

67 |

3) Checking Balanced Brackets in Expressions with Stacks

68 |

69 |

    70 |
  • 71 | Extract the subdirectory balance from the archive 72 | Lab2.zip with the implementation of the stack ADT with an 73 | array list contained in the following files: 74 |
      75 |
    • Stack.java
    • 76 |
    • ArrayStack.java
    • 77 |
    • StackEmptyException.java
    • 78 |
    • StackFullException.java
    • 79 |
    80 |
  • 81 |
82 |
    83 |
  • 84 | The archive also contains the file bracketsBalance.java in which you should implement the 85 | following method: 86 |
      87 |
    • boolean bBalance (String exp) 88 |
        89 |
      • that evaluates exp for balanced brackets and returns true 90 | (if balanced) and false otherwise
      • 91 |
      • use the stack implementation in ArrayStack.java.
      • 92 |
      93 |
    • 94 |
    95 |
  • 96 |
  • Compile bracketsBalance.java and run it with different expressions
  • 97 |
98 |

99 | 100 | 101 | 102 | 103 | -------------------------------------------------------------------------------- /Labs/Lab 2/stack/ArrayStack.java: -------------------------------------------------------------------------------- 1 | package stack; 2 | 3 | import stack.Stack; 4 | 5 | /** 6 | * Implementation of the Stack interface using a fixed-length array. 7 | * An exception is thrown if a push operation is attempted when the 8 | * size of the stack is equal to the length of the array. 9 | * 10 | * @author Natasha Gelfand 11 | * @author Roberto Tamassia 12 | * @see FullStackException 13 | */ 14 | public class ArrayStack implements Stack { 15 | /** 16 | * Default length of the array used to implement the stack. 17 | */ 18 | public static final int CAPACITY = 1000; 19 | /** 20 | * Length of the array used to implement the stack. 21 | */ 22 | protected int capacity; 23 | /** 24 | * Array used to implement the stack. 25 | */ 26 | protected Object S[]; 27 | /** 28 | * Index of the top element of the stack in the array. 29 | */ 30 | protected int top = -1; 31 | /** 32 | * Initialize the stack to use an array of default length CAPACITY. 33 | */ 34 | public ArrayStack() { 35 | this(CAPACITY); 36 | } 37 | /** 38 | * Initialize the stack to use an array of given length. 39 | * 40 | * @param cap length of the array. 41 | */ 42 | public ArrayStack(int cap) { 43 | capacity = cap; 44 | S = new Object[capacity]; 45 | } 46 | 47 | 48 | /** 49 | * O(1) time. 50 | */ 51 | public int size() { 52 | return (top + 1); 53 | } 54 | /** 55 | * O(1) time. 56 | */ 57 | public boolean isEmpty() { 58 | return (top < 0); 59 | } 60 | /** 61 | * O(1) time. 62 | * @exception FullStackException if the array is full. 63 | */ 64 | public void push(Object obj) throws FullStackException { 65 | if (size() == capacity) 66 | throw new FullStackException("Stack overflow."); 67 | S[++top] = obj; 68 | } 69 | /** 70 | * O(1) time. 71 | */ 72 | public Object top() throws EmptyStackException { 73 | if (isEmpty()) 74 | throw new EmptyStackException("Stack is empty."); 75 | return S[top]; 76 | } 77 | /** 78 | * O(1) time. 79 | */ 80 | public Object pop() throws EmptyStackException { 81 | Object elem; 82 | if (isEmpty()) 83 | throw new EmptyStackException("Stack is Empty."); 84 | elem = S[top]; 85 | S[top--] = null; // dereference S[top] for garbage collection. 86 | return elem; 87 | } 88 | } 89 | 90 | -------------------------------------------------------------------------------- /Labs/Lab 2/stack/EmptyStackException.java: -------------------------------------------------------------------------------- 1 | package stack; 2 | 3 | 4 | /** 5 | * Runtime exception thrown when one tries to perform operation top or 6 | * pop on an empty stack. 7 | */ 8 | 9 | public class EmptyStackException extends RuntimeException { 10 | public EmptyStackException(String err) { 11 | super(err); 12 | } 13 | } 14 | 15 | -------------------------------------------------------------------------------- /Labs/Lab 2/stack/FullStackException.java: -------------------------------------------------------------------------------- 1 | package stack; 2 | 3 | 4 | /** 5 | * Runtime exception thrown when one tries to perform operation push 6 | * on a full stack. 7 | */ 8 | 9 | public class FullStackException extends RuntimeException { 10 | public FullStackException(String err) { 11 | super(err); 12 | } 13 | } 14 | 15 | -------------------------------------------------------------------------------- /Labs/Lab 2/stack/Node.java: -------------------------------------------------------------------------------- 1 | package stack; 2 | 3 | 4 | public class Node { 5 | // Instance variables: 6 | private Object element; 7 | private Node next; 8 | /** Creates a node with null references to its element and next node. */ 9 | public Node() { 10 | this(null, null); 11 | } 12 | /** Creates a node with the given element and next node. */ 13 | public Node(Object e, Node n) { 14 | element = e; 15 | next = n; 16 | } 17 | // Accessor methods: 18 | public Object getElement() { 19 | return element; 20 | } 21 | public Node getNext() { 22 | return next; 23 | } 24 | // Modifier methods: 25 | public void setElement(Object newElem) { 26 | element = newElem; 27 | } 28 | public void setNext(Node newNext) { 29 | next = newNext; 30 | } 31 | } 32 | 33 | -------------------------------------------------------------------------------- /Labs/Lab 2/stack/NodeStack.java: -------------------------------------------------------------------------------- 1 | package stack; 2 | 3 | public class NodeStack implements Stack { 4 | protected Node top; // reference to the head node 5 | protected int size; // number of elements in the stack 6 | public NodeStack() { // constructs an empty stack 7 | top = null; 8 | size = 0; 9 | } 10 | public int size() { 11 | return size; 12 | } 13 | public boolean isEmpty() { 14 | if (top == null) 15 | return true; 16 | return false; 17 | } 18 | public void push(Object elem) { 19 | Node v = new Node(elem, top); // create and link-in a new node 20 | top = v; 21 | size++; 22 | } 23 | public Object top() throws EmptyStackException { 24 | if (isEmpty()) 25 | throw new EmptyStackException("Stack is empty."); 26 | return top.getElement(); 27 | } 28 | public Object pop() throws EmptyStackException { 29 | if (isEmpty()) 30 | throw new EmptyStackException("Stack is empty."); 31 | Object temp = top.getElement(); 32 | top = top.getNext(); // link-out the former top node 33 | size--; 34 | return temp; 35 | } 36 | } 37 | 38 | -------------------------------------------------------------------------------- /Labs/Lab 2/stack/Stack.java: -------------------------------------------------------------------------------- 1 | package stack; 2 | 3 | import java.util.EmptyStackException; 4 | 5 | /** 6 | * Interface for a stack: a collection of objects that are inserted 7 | * and removed according to the last-in first-out principle. 8 | * 9 | * @author Roberto Tamassia 10 | * @author Michael Goodrich 11 | * @see EmptyStackException 12 | */ 13 | 14 | public interface Stack { 15 | /** 16 | * Return the number of elements in the stack. 17 | * @return number of elements in the stack. 18 | */ 19 | public int size(); 20 | /** 21 | * Return whether the stack is empty. 22 | * @return true if the stack is empty, false otherwise. 23 | */ 24 | public boolean isEmpty(); 25 | /** 26 | * Inspect the element at the top of the stack. 27 | * @return top element in the stack. 28 | * @exception EmptyStackException if the stack is empty. 29 | */ 30 | public Object top() 31 | throws EmptyStackException; 32 | /** 33 | * Insert an element at the top of the stack. 34 | * @param element element to be inserted. 35 | */ 36 | public void push (Object element); 37 | /** 38 | * Remove the top element from the stack. 39 | * @return element removed. 40 | * @exception EmptyStackException if the stack is empty. 41 | */ 42 | public Object pop() 43 | throws EmptyStackException; 44 | } 45 | 46 | -------------------------------------------------------------------------------- /Labs/Lab 2/stack/tryStack1.java: -------------------------------------------------------------------------------- 1 | package stack; 2 | 3 | class tryStack1 { 4 | 5 | public static void main(String [] args){ 6 | 7 | Integer[] arr=new Integer[50]; 8 | 9 | for(int i=0; i<50; i++){ 10 | arr[i]=i*2; 11 | } 12 | 13 | printA(arr); 14 | arr = reverse(arr); 15 | printA(arr); 16 | } 17 | public static Integer[] reverse(Integer[] a) { 18 | ArrayStack S = new ArrayStack(a.length); 19 | Integer[] b = new Integer[a.length]; 20 | for (Integer anA : a) S.push(anA); 21 | for (int i=0; i < a.length; i++) 22 | b[i] = (Integer) (S.pop()); 23 | return b; 24 | } 25 | 26 | public static void printA(Integer[] a) { 27 | System.out.println(); 28 | for(int i=0; i<50; i++){ 29 | System.out.print(a[i] +"\t"); 30 | } 31 | System.out.println(); 32 | 33 | } 34 | } 35 | 36 | 37 | -------------------------------------------------------------------------------- /Labs/Lab 2/stack/tryStack2.java: -------------------------------------------------------------------------------- 1 | package stack; 2 | 3 | class tryStack2 { 4 | 5 | public static void main(String [] args){ 6 | 7 | Integer[] arr=new Integer[50]; 8 | 9 | for(int i=0; i<50; i++){ 10 | arr[i]=new Integer(i*2); 11 | } 12 | 13 | printA(arr); 14 | arr = reverse(arr); 15 | printA(arr); 16 | } 17 | public static Integer[] reverse(Integer[] a) { 18 | NodeStack S = new NodeStack(); // This is the only change from tryStack1!!! 19 | Integer[] b = new Integer[a.length]; 20 | for (int i=0; i < a.length; i++) 21 | S.push(a[i]); 22 | for (int i=0; i < a.length; i++) 23 | b[i] = (Integer) (S.pop()); 24 | return b; 25 | } 26 | 27 | public static void printA(Integer[] a) { 28 | System.out.println(); 29 | for(int i=0; i<50; i++){ 30 | System.out.print(a[i].intValue()+"\t"); 31 | } 32 | System.out.println(); 33 | 34 | } 35 | } 36 | 37 | 38 | -------------------------------------------------------------------------------- /Labs/Lab 3/NodeList.java: -------------------------------------------------------------------------------- 1 | import java.util.NoSuchElementException; 2 | import java.util.LinkedList; 3 | 4 | /** 5 | * 6 | */ 7 | public class NodeList { 8 | 9 | protected LinkedList linkedList = new LinkedList<>(); 10 | 11 | public int size() { 12 | return linkedList.size(); 13 | } 14 | 15 | public boolean isEmpty() { 16 | return linkedList.isEmpty(); 17 | } 18 | 19 | public boolean isFirst(E element) { 20 | return element.equals(linkedList.getFirst()); 21 | } 22 | 23 | public boolean isLast(E element) { 24 | return element.equals(linkedList.getLast()); 25 | } 26 | 27 | public E first() throws NoSuchElementException { 28 | return linkedList.element(); 29 | } 30 | 31 | public E last() throws NoSuchElementException { 32 | return linkedList.getLast(); 33 | } 34 | 35 | E prev(E element) throws NoSuchElementException { 36 | int index = linkedList.indexOf(element); 37 | if (element == null || index < 1) { 38 | throw new NullPointerException(); 39 | } 40 | return linkedList.get(index-1); 41 | } 42 | 43 | E next(E element) throws NoSuchElementException { 44 | int index = linkedList.indexOf(element); 45 | if (element == null || index < 0 || index > linkedList.size() - 2) { 46 | throw new NullPointerException(); 47 | } 48 | return linkedList.get(index+1); 49 | } 50 | 51 | public void swapElements(E element1, E element2) throws NoSuchElementException { 52 | if (!linkedList.contains(element1) || !linkedList.contains(element2)) { 53 | throw new NoSuchElementException(); 54 | } 55 | linkedList.set(linkedList.indexOf(element1), element2); 56 | linkedList.set(linkedList.indexOf(element2), element1); 57 | } 58 | 59 | 60 | public void set(E currElement, E repElement) throws NoSuchElementException { 61 | if (!linkedList.contains(currElement)) { 62 | throw new NoSuchElementException(); 63 | } 64 | linkedList.set(linkedList.indexOf(currElement), repElement); 65 | } 66 | 67 | public void addFirst(E element) { 68 | linkedList.addFirst(element); 69 | } 70 | 71 | public void addLast(E element) { 72 | linkedList.addLast(element); 73 | } 74 | 75 | public void addBefore(E currElement, E addElement) throws NoSuchElementException { 76 | if (!linkedList.contains(currElement)) { 77 | throw new NoSuchElementException(); 78 | } 79 | linkedList.add(linkedList.indexOf(currElement), addElement); 80 | } 81 | 82 | public void addAfter(E currElement, E addElement) throws NoSuchElementException { 83 | if (!linkedList.contains(currElement)) { 84 | throw new NoSuchElementException(); 85 | } 86 | if (linkedList.indexOf(currElement) == linkedList.size() - 1) { 87 | linkedList.add(addElement); 88 | } else { 89 | linkedList.add(linkedList.indexOf(currElement) + 1, addElement); 90 | } 91 | 92 | } 93 | 94 | public E remove(E element) throws NoSuchElementException { 95 | if (!linkedList.contains(element)) { 96 | throw new NoSuchElementException(); 97 | } 98 | linkedList.remove(element); 99 | return element; 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /Labs/Lab 3/Sentence.java: -------------------------------------------------------------------------------- 1 | // ========================================================================== 2 | // $Id: addTemplate.cpp,v 1.1 2005/11/02 23:13:32 jlang Exp $ 3 | // CSI2110 Lab code Test driver for node list implementation 4 | // ========================================================================== 5 | // (C)opyright: 6 | // 7 | // Jochen Lang 8 | // SITE, University of Ottawa 9 | // 800 King Edward Ave. 10 | // Ottawa, On., K1N 6N5 11 | // Canada. 12 | // http://www.site.uottawa.ca 13 | // 14 | // Creator: jlang (Jochen Lang) 15 | // Email: jlang@site.uottawa.ca 16 | // ========================================================================== 17 | // $Log: addTemplate.cpp,v $ 18 | // 19 | // ========================================================================== 20 | 21 | public class Sentence { 22 | 23 | public static void print(NodeList stringNodeList) { 24 | String s = stringNodeList.first(); 25 | System.out.print(s); 26 | System.out.print(' '); 27 | for (int cnt = 0; cnt < stringNodeList.size() - 1; ++cnt) { 28 | s = stringNodeList.next(s); 29 | System.out.print(s); 30 | System.out.print(' '); 31 | } 32 | System.out.println(); 33 | } 34 | 35 | 36 | public static void main(String[] argv) { 37 | NodeList stringNodeList = new NodeList<>(); 38 | 39 | argv = "This is a test sentence".split(" "); 40 | 41 | // Add all the words to the NodeList 42 | for (String anArgv : argv) { 43 | stringNodeList.addLast(anArgv); 44 | } 45 | // Print out the sentence 46 | Sentence.print(stringNodeList); 47 | 48 | // Swap the first and last element 49 | String first = stringNodeList.first(); 50 | String last = stringNodeList.last(); 51 | stringNodeList.swapElements(first, last); 52 | // Print out the sentence 53 | Sentence.print(stringNodeList); 54 | 55 | 56 | // Insert a not before and after the center word 57 | String center = argv[stringNodeList.size() / 2]; 58 | System.err.println(center); 59 | stringNodeList.addBefore(center, "not"); 60 | // Print out the sentence 61 | Sentence.print(stringNodeList); 62 | stringNodeList.addAfter(center, "not"); 63 | // Print out the sentence 64 | Sentence.print(stringNodeList); 65 | 66 | 67 | // Remove all original elements from the list 68 | for (String anArgv : argv) { 69 | stringNodeList.remove(anArgv); 70 | } 71 | // Print out the sentence 72 | Sentence.print(stringNodeList); 73 | 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /Labs/Lab 3/TestListImplementation.java: -------------------------------------------------------------------------------- 1 | // ========================================================================== 2 | // $Id: addTemplate.cpp,v 1.1 2005/11/02 23:13:32 jlang Exp $ 3 | // CSI2110 Lab code testing list interfaces 4 | // ========================================================================== 5 | // (C)opyright: 6 | // 7 | // Jochen Lang 8 | // SITE, University of Ottawa 9 | // 800 King Edward Ave. 10 | // Ottawa, On., K1N 6N5 11 | // Canada. 12 | // http://www.site.uottawa.ca 13 | // 14 | // Creator: jlang (Jochen Lang) 15 | // Email: jlang@site.uottawa.ca 16 | // ========================================================================== 17 | // $Log: addTemplate.cpp,v $ 18 | // 19 | // ========================================================================== 20 | 21 | import java.util.List; 22 | import java.util.Iterator; 23 | import java.util.ArrayList; 24 | import java.util.LinkedList; 25 | 26 | 27 | public class TestListImplementation { 28 | 29 | class MilliNanoTimer { 30 | long startTimeMilli; 31 | long startTimeNano; 32 | 33 | void start() { 34 | startTimeMilli = System.currentTimeMillis(); 35 | startTimeNano = System.nanoTime(); 36 | } 37 | 38 | String stop() { 39 | long diffMilli = System.currentTimeMillis() - startTimeMilli; 40 | long diffNano = System.nanoTime() - startTimeNano; 41 | diffNano -= 1000 * (diffNano / 1000); 42 | if (diffNano < 0) { 43 | return String.format("%6d.%03d", diffMilli - 1, 1000 + diffNano); 44 | } else { 45 | return String.format("%6d.%03d", diffMilli, diffNano); 46 | } 47 | } 48 | } 49 | 50 | public void timeOperations(List integerList, int numElem) { 51 | MilliNanoTimer watch = new MilliNanoTimer(); 52 | 53 | // number of Elements 54 | System.out.printf("%10d ", numElem); 55 | // insert N elements 56 | watch.start(); 57 | for (int i = 0; i < numElem; i++) { 58 | integerList.add(0, new Integer(i % 100)); 59 | } 60 | System.out.print(watch.stop() + " "); 61 | // iterate over all elements 62 | int k = 0; 63 | watch.start(); 64 | for (Integer val : integerList) { 65 | k += val; 66 | k -= 50; 67 | } 68 | System.out.print(watch.stop() + " "); 69 | // random access all elements 70 | k = 0; 71 | watch.start(); 72 | for (int i = 0; i < numElem; i++) { 73 | k += integerList.get(i); 74 | k -= 50; 75 | } 76 | System.out.print(watch.stop() + " "); 77 | // remove N elements at the front 78 | watch.start(); 79 | for (int i = 0; i < numElem; i++) { 80 | integerList.remove(0); 81 | } 82 | System.out.print(watch.stop() + "\n"); 83 | return; 84 | } 85 | 86 | 87 | public static void main(String[] argv) { 88 | TestListImplementation test = new TestListImplementation(); 89 | int numElemStart = 512; 90 | int numSteps = 8; 91 | 92 | if (argv.length > 0) numSteps = Integer.valueOf(argv[0]); 93 | 94 | 95 | System.out.println("ArrayList"); 96 | System.out.printf("%10s %10s %10s %10s %10s\n", "N", "insert", 97 | "iterate", "get", "remove"); 98 | int numElem = numElemStart; 99 | for (int i = 0; i < numSteps; i++) { 100 | // create an ArrayList for Integer 101 | ArrayList aList = new ArrayList(); 102 | // call our timing method 103 | test.timeOperations(aList, numElem); 104 | numElem <<= 1; // multiply by 2 105 | } 106 | System.out.println(); 107 | System.out.println("LinkedList"); 108 | System.out.printf("%10s %10s %10s %10s %10s\n", "N", "insert", 109 | "iterate", "get", "remove"); 110 | numElem = numElemStart; 111 | for (int i = 0; i < numSteps; i++) { 112 | // create an LinkedList for Integer 113 | LinkedList lList = new LinkedList(); 114 | // call our timing method 115 | test.timeOperations(lList, numElem); 116 | numElem <<= 1; // multiply by 2 117 | } 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /Labs/Lab 5/TestTree.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | 7 | import java.util.*; 8 | 9 | /** 10 | * Test class for tree traversal lab 11 | * 12 | * @author Lachlan Plant 13 | */ 14 | public class TestTree { 15 | 16 | 17 | public static void testPreorderRecursive(LinkedBinarySearchTree tree, String expected) { 18 | System.out.println("Expected Result: " + expected); 19 | tree.printPreorderRecursive(); 20 | System.out.println(); 21 | } 22 | 23 | public static void testPreorderIterator(LinkedBinarySearchTree tree, String expected) { 24 | System.out.println("Expected Result: " + expected); 25 | System.out.print("Preorder Printing with Iterator: "); 26 | Iterator i = tree.preorderIterator(); 27 | while (i.hasNext()) { 28 | System.out.print(i.next().toString() + ", "); 29 | } 30 | System.out.println(); 31 | System.out.print("Printing First 5 Elements: "); 32 | i = tree.preorderIterator(); 33 | for (int j = 0; j < 5; j++) { 34 | System.out.print(i.next().toString() + ", "); 35 | } 36 | System.out.println(); 37 | } 38 | 39 | public static void testInorderRecursive(LinkedBinarySearchTree tree, String expected) { 40 | System.out.println("Expected Result: " + expected); 41 | tree.printInorderRecursive(); 42 | System.out.println(); 43 | } 44 | 45 | public static void testInorderIterator(LinkedBinarySearchTree tree, String expected) { 46 | System.out.println("Expected Result: " + expected); 47 | System.out.print("Inorder Printing with Iterator: "); 48 | Iterator i = tree.inorderIterator(); 49 | while (i.hasNext()) { 50 | System.out.print(i.next().toString() + ", "); 51 | } 52 | System.out.println(); 53 | System.out.print("Printing First 5 Elements: "); 54 | i = tree.inorderIterator(); 55 | for (int j = 0; j < 5; j++) { 56 | System.out.print(i.next().toString() + ", "); 57 | } 58 | System.out.println(); 59 | } 60 | 61 | public static void testPostorderRecursive(LinkedBinarySearchTree tree, String expected) { 62 | System.out.println("Expected Result: " + expected); 63 | tree.printPostorderRecursive(); 64 | System.out.println(); 65 | } 66 | 67 | public static void main(String[] args) { 68 | System.out.println("Testing binary search tree printing"); 69 | LinkedBinarySearchTree tree = new LinkedBinarySearchTree<>(); 70 | int[] temp = {3, 5, 7, 1, 4, 2, 51, 17, 43, 28, 47, 89, 6}; 71 | for (int aTemp : temp) { 72 | tree.add(aTemp); 73 | } 74 | testPreorderRecursive(tree, "3, 1, 2, 5, 4, 7, 6, 51, 17, 43, 28, 47, 89,"); 75 | testPreorderIterator(tree, "3, 1, 2, 5, 4, 7, 6, 51, 17, 43, 28, 47, 89,"); 76 | testInorderRecursive(tree, "1, 2, 3, 4, 5, 6, 7, 17, 28, 43, 47, 51, 89,"); 77 | testInorderIterator(tree, "1, 2, 3, 4, 5, 6, 7, 17, 28, 43, 47, 51, 89,"); 78 | testPostorderRecursive(tree, "2, 1, 4, 6, 28, 47, 43, 17, 89, 51, 7, 5, 3,"); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /Labs/Lab 7/ArrayHashTable.java: -------------------------------------------------------------------------------- 1 | // ========================================================================== 2 | // CSI2110 Lab code: Hash Tables 3 | // ========================================================================== 4 | // (C)opyright: 5 | // 6 | // Lachlan Plant 7 | // SITE, University of Ottawa 8 | // 800 King Edward Ave. 9 | // Ottawa, On., K1N 6N5 10 | // Canada. 11 | // http://www.site.uottawa.ca 12 | // 13 | // Creator: lplant (Lachlan Plant) 14 | // Email: lplan053@uottawa.ca 15 | // ========================================================================== 16 | import java.util.*; 17 | public class ArrayHashTable implements HashTable { 18 | HashFunction func1; 19 | HashFunction func2; 20 | int size; 21 | int totalNumProbes; 22 | int elements; 23 | private ArrayList> table; 24 | SteppingFunction step; 25 | 26 | 27 | public ArrayHashTable(int s, HashFunction h1, HashFunction h2, SteppingFunction st){ 28 | elements = totalNumProbes =0; 29 | size = s; 30 | table = new ArrayList<>(size); 31 | for(int i=0; i entry = table.get(hash); 63 | if(entry == null) return; 64 | if(entry.key == null){ //Special Case for deleted object 65 | continue; 66 | } 67 | if(entry.key.equals(key)){ 68 | table.set(hash,new Entry(null,null)); 69 | this.elements--; 70 | return; 71 | } 72 | } 73 | return; 74 | } 75 | 76 | /** 77 | * Returns true if the value is in the hash table 78 | * @param key 79 | * @return 80 | */ 81 | public boolean contains(K key){ 82 | long h = func1.Hash(key); 83 | long h2 = (func2==null)? 1 : func2.Hash(key); 84 | for(int i = 0; i< size; i++){ 85 | int hash = (int)((h+step.step(i, h2,size))%size); 86 | Entry entry = table.get(hash); 87 | if(entry == null) return false; 88 | if(entry.key == null){ //Special Case for deleted object 89 | continue; 90 | } 91 | if(entry.key.equals(key)){ 92 | return true; 93 | } 94 | } 95 | return false; 96 | } 97 | 98 | /** 99 | * Puts the key value pair into the hash table, if the key is 100 | * already in the value is updated 101 | * @param key 102 | * @param value 103 | * @return 104 | */ 105 | public boolean put(K key, V value){ 106 | 107 | /*********** complete this method; now returning a dummy false ***********/ 108 | 109 | return false; 110 | } 111 | 112 | public String extraInfo(){return "Total number of Probes: "+ totalNumProbes+", Average Probe Depth: "+(double)totalNumProbes/(double)elements;} 113 | public double loadFactor(){return (double)elements/(double)size;} 114 | public String type(){return step.getType();} 115 | public int elements(){return this.elements;} 116 | private class Entry{ 117 | public K key; 118 | public V value; 119 | public Entry(K k,V v){ 120 | key = k; value = v; 121 | } 122 | } 123 | } 124 | 125 | -------------------------------------------------------------------------------- /Labs/Lab 7/ChainedHashTable.java: -------------------------------------------------------------------------------- 1 | // ========================================================================== 2 | // CSI2110 Lab code: Hash Tables 3 | // ========================================================================== 4 | // (C)opyright: 5 | // 6 | // Lachlan Plant 7 | // SITE, University of Ottawa 8 | // 800 King Edward Ave. 9 | // Ottawa, On., K1N 6N5 10 | // Canada. 11 | // http://www.site.uottawa.ca 12 | // 13 | // Creator: lplant (Lachlan Plant) 14 | // Email: lplan053@uottawa.ca 15 | // ========================================================================== 16 | import java.util.*; 17 | public class ChainedHashTable implements HashTable { 18 | HashFunction func; 19 | int size; 20 | int collisions; 21 | int elements; 22 | private ArrayList>> table; 23 | public ChainedHashTable(int s, HashFunction h){ 24 | elements = collisions =0; 25 | size = s; 26 | table = new ArrayList<>(size); 27 | for(int i=0; i> list = table.get(hash); 42 | if(list == null) return null; 43 | for(Entry e: list){ 44 | if(e.key.equals(key)){ 45 | return e.value; 46 | } 47 | } 48 | return null; 49 | } 50 | 51 | /** 52 | * Removes the key value pair value from the hash table 53 | * @param key 54 | * @return 55 | */ 56 | public void remove(K key){ 57 | int hash = (int)(func.Hash(key)%size); 58 | LinkedList> list = table.get(hash); 59 | if(list == null) return ; 60 | for(int i =0 ; i< list.size(); i++){ 61 | Entry e = list.get(i); 62 | if(e.key.equals(key)){ 63 | list.remove(i); 64 | } 65 | } 66 | return ; 67 | } 68 | 69 | /** 70 | * Returns true if the value is in the hash table 71 | * @param key 72 | * @return 73 | */ 74 | public boolean contains(K key){ 75 | int hash = (int)(func.Hash(key)%size); 76 | LinkedList> list = table.get(hash); 77 | if(list == null) return false; 78 | for(Entry e: list){ 79 | if(e.key.equals(key)){ 80 | return true; 81 | } 82 | } 83 | return false; 84 | } 85 | 86 | /** 87 | * Puts the key value pair into the hash table, if the key is 88 | * already in the value is updated 89 | * @param key 90 | * @param value 91 | * @return 92 | */ 93 | public boolean put(K key, V value){ 94 | int hash = (int)(func.Hash(key)%size); 95 | LinkedList> list = table.get(hash); 96 | if(contains(key)){ 97 | for(Entry e: list){ 98 | if(e.key.equals(key)){ 99 | e.value = value; 100 | return true; 101 | } 102 | } 103 | } 104 | else{ 105 | elements++; 106 | if(list == null){ 107 | list = new LinkedList<>(); 108 | list.add(new Entry(key,value)); 109 | table.set(hash, list); 110 | return true; 111 | } 112 | collisions ++; 113 | list.add(new Entry(key,value)); 114 | return true; 115 | } 116 | return false; 117 | } 118 | 119 | public String extraInfo(){return "Total Collisions: "+collisions;} 120 | public double loadFactor(){return (double)elements/(double)size;} 121 | public String type(){return "Chaining";} 122 | public int elements(){return this.elements;} 123 | 124 | private class Entry{ 125 | public K key; 126 | public V value; 127 | public Entry(K k,V v){ 128 | key = k; value = v; 129 | } 130 | } 131 | } -------------------------------------------------------------------------------- /Labs/Lab 7/DoubleHashStep.java: -------------------------------------------------------------------------------- 1 | // ========================================================================== 2 | // CSI2110 Lab code: Hash Tables 3 | // ========================================================================== 4 | // (C)opyright: 5 | // 6 | // Lachlan Plant 7 | // SITE, University of Ottawa 8 | // 800 King Edward Ave. 9 | // Ottawa, On., K1N 6N5 10 | // Canada. 11 | // http://www.site.uottawa.ca 12 | // 13 | // Creator: lplant (Lachlan Plant) 14 | // Email: lplan053@uottawa.ca 15 | // ========================================================================== 16 | public class DoubleHashStep implements SteppingFunction{ 17 | public long step(int i, long data, int size){ 18 | return 1; 19 | } 20 | public String getType(){return "Double Hashing";} 21 | } 22 | -------------------------------------------------------------------------------- /Labs/Lab 7/HashFunction.java: -------------------------------------------------------------------------------- 1 | // ========================================================================== 2 | // CSI2110 Lab code: Hash Tables 3 | // ========================================================================== 4 | // (C)opyright: 5 | // 6 | // Lachlan Plant 7 | // SITE, University of Ottawa 8 | // 800 King Edward Ave. 9 | // Ottawa, On., K1N 6N5 10 | // Canada. 11 | // http://www.site.uottawa.ca 12 | // 13 | // Creator: lplant (Lachlan Plant) 14 | // Email: lplan053@uottawa.ca 15 | // ========================================================================== 16 | public interface HashFunction { 17 | public long Hash(E item); 18 | } 19 | -------------------------------------------------------------------------------- /Labs/Lab 7/HashTable.java: -------------------------------------------------------------------------------- 1 | // ========================================================================== 2 | // CSI2110 Lab code: Hash Tables 3 | // ========================================================================== 4 | // (C)opyright: 5 | // 6 | // Lachlan Plant 7 | // SITE, University of Ottawa 8 | // 800 King Edward Ave. 9 | // Ottawa, On., K1N 6N5 10 | // Canada. 11 | // http://www.site.uottawa.ca 12 | // 13 | // Creator: lplant (Lachlan Plant) 14 | // Email: lplan053@uottawa.ca 15 | // ========================================================================== 16 | public interface HashTable { 17 | public V get(K key); 18 | public boolean contains(K key); 19 | public boolean put(K key, V value); 20 | public String extraInfo(); 21 | public double loadFactor(); 22 | public void remove(K key); 23 | public int elements(); 24 | public String type(); 25 | } 26 | -------------------------------------------------------------------------------- /Labs/Lab 7/HashingTest.java: -------------------------------------------------------------------------------- 1 | // ========================================================================== 2 | // CSI2110 Lab code: Hash Tables 3 | // ========================================================================== 4 | // (C)opyright: 5 | // 6 | // Lachlan Plant 7 | // SITE, University of Ottawa 8 | // 800 King Edward Ave. 9 | // Ottawa, On., K1N 6N5 10 | // Canada. 11 | // http://www.site.uottawa.ca 12 | // 13 | // Creator: lplant (Lachlan Plant) 14 | // Email: lplan053@uottawa.ca 15 | // ========================================================================== 16 | import java.util.*; 17 | import java.io.*; 18 | public class HashingTest { 19 | 20 | public ArrayList readFile(String fileName){ 21 | ArrayList ret = new ArrayList<>(); 22 | BufferedReader br = null; 23 | try { 24 | 25 | String sCurrentLine; 26 | 27 | br = new BufferedReader(new FileReader(fileName)); 28 | int i = 0; 29 | while ((sCurrentLine = br.readLine()) != null) { 30 | i++; 31 | String[] a = sCurrentLine.split(" "); 32 | for(String x: a){ 33 | ret.add(x.replaceAll("[^\\p{L}\\p{Nd}]+", "").toLowerCase()); 34 | } 35 | } 36 | 37 | } catch (IOException e) { 38 | e.printStackTrace(); 39 | } finally { 40 | try { 41 | if (br != null)br.close(); 42 | } catch (IOException ex) { 43 | ex.printStackTrace(); 44 | } 45 | } 46 | System.out.println("File Size " +ret.size()+ " words"); 47 | return ret; 48 | } 49 | 50 | public void HashAndStats(ArrayList words, HashTable table ){ 51 | int wordCount = 0; 52 | for(String s: words){ 53 | Integer i = table.get(s); 54 | if(i == null){ 55 | table.put(s, 1); 56 | } 57 | else{ 58 | table.put(s, i+1); 59 | } 60 | wordCount++; 61 | } 62 | String temp = String.format("Type: %0$24s, Elements: %d, Load Factor: %f, %s, Count for word \"The\": %d", table.type(),table.elements(),table.loadFactor(),table.extraInfo(),table.get("the")); 63 | System.out.println(temp); 64 | } 65 | 66 | 67 | 68 | public static void main(String[] args){ 69 | int size = 1009; 70 | HashingTest test = new HashingTest(); 71 | ArrayList words = test.readFile("TheOdysseyBook1.txt"); 72 | System.out.println("Table Size: "+size); 73 | HashTable linear = new ArrayHashTable(size, new StringHash(),null, new LinearProbeStep()); 74 | HashTable quadratic = new ArrayHashTable(size, new StringHash(), null,new QuadraticProbeStep()); 75 | HashTable doubleH = new ArrayHashTable(size, new StringHash(), new ReverseStringHash(),new DoubleHashStep()); 76 | HashTable quadDoubleH = new ArrayHashTable(size, new StringHash(), new ReverseStringHash(),new QuadraticDoubleHashStep()); 77 | HashTable chain = new ChainedHashTable(size, new StringHash()); 78 | test.HashAndStats(words,linear); 79 | test.HashAndStats(words,quadratic); 80 | test.HashAndStats(words,doubleH); 81 | test.HashAndStats(words,quadDoubleH); 82 | test.HashAndStats(words,chain); 83 | 84 | System.out.println(); 85 | size = 4283 ; 86 | words = test.readFile("TheAeneidBook1-3.txt"); 87 | System.out.println("Table Size: "+size); 88 | linear = new ArrayHashTable(size, new StringHash(),null, new LinearProbeStep()); 89 | quadratic = new ArrayHashTable(size, new StringHash(), null,new QuadraticProbeStep()); 90 | doubleH = new ArrayHashTable(size, new StringHash(), new ReverseStringHash(),new DoubleHashStep()); 91 | quadDoubleH = new ArrayHashTable(size, new StringHash(), new ReverseStringHash(),new QuadraticDoubleHashStep()); 92 | chain = new ChainedHashTable(size, new StringHash()); 93 | test.HashAndStats(words,linear); 94 | test.HashAndStats(words,quadratic); 95 | test.HashAndStats(words,doubleH); 96 | test.HashAndStats(words,quadDoubleH); 97 | test.HashAndStats(words,chain); 98 | 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /Labs/Lab 7/LinearProbeStep.java: -------------------------------------------------------------------------------- 1 | // ========================================================================== 2 | // CSI2110 Lab code: Hash Tables 3 | // ========================================================================== 4 | // (C)opyright: 5 | // 6 | // Lachlan Plant 7 | // SITE, University of Ottawa 8 | // 800 King Edward Ave. 9 | // Ottawa, On., K1N 6N5 10 | // Canada. 11 | // http://www.site.uottawa.ca 12 | // 13 | // Creator: lplant (Lachlan Plant) 14 | // Email: lplan053@uottawa.ca 15 | // ========================================================================== 16 | public class LinearProbeStep implements SteppingFunction{ 17 | public long step(int i, long data, int size){ 18 | return i; 19 | } 20 | public String getType(){return "Linear Probing";} 21 | } -------------------------------------------------------------------------------- /Labs/Lab 7/QuadraticDoubleHashStep.java: -------------------------------------------------------------------------------- 1 | // ========================================================================== 2 | // CSI2110 Lab code: Hash Tables 3 | // ========================================================================== 4 | // (C)opyright: 5 | // 6 | // Lachlan Plant 7 | // SITE, University of Ottawa 8 | // 800 King Edward Ave. 9 | // Ottawa, On., K1N 6N5 10 | // Canada. 11 | // http://www.site.uottawa.ca 12 | // 13 | // Creator: lplant (Lachlan Plant) 14 | // Email: lplan053@uottawa.ca 15 | // ========================================================================== 16 | public class QuadraticDoubleHashStep implements SteppingFunction{ 17 | public long step(int i, long data, int size){ 18 | return 1; 19 | } 20 | public String getType(){return "Quadratic Double Hashing";} 21 | } 22 | -------------------------------------------------------------------------------- /Labs/Lab 7/QuadraticProbeStep.java: -------------------------------------------------------------------------------- 1 | // ========================================================================== 2 | // CSI2110 Lab code: Hash Tables 3 | // ========================================================================== 4 | // (C)opyright: 5 | // 6 | // Lachlan Plant 7 | // SITE, University of Ottawa 8 | // 800 King Edward Ave. 9 | // Ottawa, On., K1N 6N5 10 | // Canada. 11 | // http://www.site.uottawa.ca 12 | // 13 | // Creator: lplant (Lachlan Plant) 14 | // Email: lplan053@uottawa.ca 15 | // ========================================================================== 16 | public class QuadraticProbeStep implements SteppingFunction{ 17 | public long step(int i, long data, int size){ 18 | return 1; 19 | } 20 | public String getType(){return "Quadratic Probing";} 21 | } 22 | -------------------------------------------------------------------------------- /Labs/Lab 7/ReverseStringHash.java: -------------------------------------------------------------------------------- 1 | // ========================================================================== 2 | // CSI2110 Lab code: Hash Tables 3 | // ========================================================================== 4 | // (C)opyright: 5 | // 6 | // Lachlan Plant 7 | // SITE, University of Ottawa 8 | // 800 King Edward Ave. 9 | // Ottawa, On., K1N 6N5 10 | // Canada. 11 | // http://www.site.uottawa.ca 12 | // 13 | // Creator: lplant (Lachlan Plant) 14 | // Email: lplan053@uottawa.ca 15 | // ========================================================================== 16 | public class ReverseStringHash implements HashFunction{ 17 | String alpha = "abcdefghijklmnopqrstuvwxyz"; 18 | public long Hash(String key){ 19 | long hash = 0; 20 | String s = key.toLowerCase(); 21 | for(int i =key.length()-1; i >= 0; i--){ 22 | hash = hash*26 + alpha.indexOf(s.charAt(i)+""); 23 | } 24 | return Math.abs(hash); 25 | } 26 | } -------------------------------------------------------------------------------- /Labs/Lab 7/SteppingFunction.java: -------------------------------------------------------------------------------- 1 | // ========================================================================== 2 | // CSI2110 Lab code: Hash Tables 3 | // ========================================================================== 4 | // (C)opyright: 5 | // 6 | // Lachlan Plant 7 | // SITE, University of Ottawa 8 | // 800 King Edward Ave. 9 | // Ottawa, On., K1N 6N5 10 | // Canada. 11 | // http://www.site.uottawa.ca 12 | // 13 | // Creator: lplant (Lachlan Plant) 14 | // Email: lplan053@uottawa.ca 15 | // ========================================================================== 16 | public interface SteppingFunction { 17 | public long step(int i, long data,int size); 18 | public String getType(); 19 | } 20 | -------------------------------------------------------------------------------- /Labs/Lab 7/StringHash.java: -------------------------------------------------------------------------------- 1 | // ========================================================================== 2 | // CSI2110 Lab code: Hash Tables 3 | // ========================================================================== 4 | // (C)opyright: 5 | // 6 | // Lachlan Plant 7 | // SITE, University of Ottawa 8 | // 800 King Edward Ave. 9 | // Ottawa, On., K1N 6N5 10 | // Canada. 11 | // http://www.site.uottawa.ca 12 | // 13 | // Creator: lplant (Lachlan Plant) 14 | // Email: lplan053@uottawa.ca 15 | // ========================================================================== 16 | public class StringHash implements HashFunction{ 17 | String alpha = "abcdefghijklmnopqrstuvwxyz"; 18 | public long Hash(String key){ 19 | long hash = 0; 20 | String s = key.toLowerCase(); 21 | for(int i =0; i < key.length(); i++){ 22 | hash = hash*26 + alpha.indexOf(s.charAt(i)+""); 23 | } 24 | //System.out.println(Math.abs(hash)); 25 | return Math.abs(hash); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Labs/Lab 7/ToyTest.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.ArrayList; 3 | 4 | // ========================================================================== 5 | // CSI2110 Lab code: Hash Tables 6 | // ========================================================================== 7 | // (C)opyright: 8 | // 9 | // Lachlan Plant 10 | // SITE, University of Ottawa 11 | // 800 King Edward Ave. 12 | // Ottawa, On., K1N 6N5 13 | // Canada. 14 | // http://www.site.uottawa.ca 15 | // 16 | // Creator: lplant (Lachlan Plant) 17 | // Email: lplan053@uottawa.ca 18 | // ========================================================================== 19 | public class ToyTest { 20 | 21 | public static void testAdd( HashTable h){ 22 | String[] s ={"the","quick","brown","fox","jumps"}; 23 | for(String e: s){ 24 | h.put(e, 1); 25 | } 26 | System.out.println("All Elements In: "+ (h.elements()==5)); 27 | 28 | } 29 | public static void testGet( HashTable h){ 30 | String[] s ={"the","quick","brown","fox","jumps"}; 31 | int i = 0; 32 | for(String e: s){ 33 | h.put(e, i); 34 | i++; 35 | } 36 | System.out.println("Brown Value is 2: "+ (h.get("brown")==2)); 37 | System.out.println("jumps Value is 4: "+ (h.get("jumps")==4)); 38 | 39 | } 40 | public static void testAddOverWtite( HashTable h){ 41 | String[] s ={"the","quick","brown","fox","jumps"}; 42 | int i = 0; 43 | for(String e: s){ 44 | h.put(e, i); 45 | i++; 46 | } 47 | h.put("brown", 25); 48 | System.out.println("Brown Value is 25: "+ (h.get("brown")==25)); 49 | 50 | } 51 | 52 | public static void main(String[] args){ 53 | int size = 11; 54 | ToyTest test = new ToyTest(); 55 | System.out.println("Table Size: "+size); 56 | HashTable linear = new ArrayHashTable(size, new StringHash(),null, new LinearProbeStep()); 57 | HashTable quadratic = new ArrayHashTable(size, new StringHash(), null,new QuadraticProbeStep()); 58 | HashTable doubleH = new ArrayHashTable(size, new StringHash(), new ReverseStringHash(),new DoubleHashStep()); 59 | HashTable quadDoubleH = new ArrayHashTable(size, new StringHash(), new ReverseStringHash(),new QuadraticDoubleHashStep()); 60 | HashTable chain = new ChainedHashTable(size, new StringHash()); 61 | System.out.println("Testing Linear Probing"); 62 | testAdd(linear); 63 | testGet(linear); 64 | testAddOverWtite(linear); 65 | System.out.println(); 66 | System.out.println("Testing Quadratic Probing"); 67 | testAdd(quadratic); 68 | testGet(quadratic); 69 | testAddOverWtite(quadratic); 70 | System.out.println(); 71 | System.out.println("Testing Double Hashing"); 72 | testAdd(doubleH); 73 | testGet(doubleH); 74 | testAddOverWtite(doubleH); 75 | System.out.println(); 76 | System.out.println("Testing Quadratic Double Hashing"); 77 | testAdd(quadDoubleH); 78 | testGet(quadDoubleH); 79 | testAddOverWtite(quadDoubleH); 80 | System.out.println(); 81 | System.out.println("Testing Chaining"); 82 | testAdd(chain); 83 | testGet(chain); 84 | testAddOverWtite(chain); 85 | } 86 | 87 | } 88 | -------------------------------------------------------------------------------- /Labs/Lab 8/BubbleSort.java: -------------------------------------------------------------------------------- 1 | // ========================================================================== 2 | // $Id: BubbleSort.java,v 1.1 2006/11/05 03:27:51 jlang Exp $ 3 | // CSI2110 Lab code; basic bubble sort 4 | // ========================================================================== 5 | // (C)opyright: 6 | // 7 | // SITE, University of Ottawa 8 | // 800 King Edward Ave. 9 | // Ottawa, On., K1N 6N5 10 | // Canada. 11 | // http://www.site.uottawa.ca 12 | // 13 | // Creator: unknown (Lab source without reference), adapted by J.Lang 14 | // Email: jlang@site.uottawa.ca 15 | // ========================================================================== 16 | // $Log: BubbleSort.java,v $ 17 | // Revision 1.1 2006/11/05 03:27:51 jlang 18 | // Added lab8 on sorting. 19 | // 20 | // 21 | // ========================================================================== 22 | /** 23 | * Implements bubble sort. 24 | */ 25 | public class BubbleSort { 26 | 27 | public BubbleSort(T[] _seq ) { 28 | for (int i = 0; i < _seq.length; i++) { 29 | for (int j = 1; j < _seq.length - i; j++) { 30 | // if (_seq[j - 1] > _seq[j]) { 31 | if (_seq[j - 1].compareTo(_seq[j]) > 0 ) { 32 | T temp = _seq[j]; 33 | _seq[j] = _seq[j - 1]; 34 | _seq[j - 1] = temp; 35 | } 36 | } 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Labs/Lab 8/HeapSortInPlace.java: -------------------------------------------------------------------------------- 1 | // ========================================================================== 2 | // $Id: HeapSortInPlace.java,v 1.1 2006/11/05 03:27:51 jlang Exp $ 3 | // CSI2110 Lab code; Heap sort in place 4 | // ========================================================================== 5 | // (C)opyright: 6 | // 7 | // SITE, University of Ottawa 8 | // 800 King Edward Ave. 9 | // Ottawa, On., K1N 6N5 10 | // Canada. 11 | // http://www.site.uottawa.ca 12 | // 13 | // Creator: unknown (Lab source without reference), adapted by J.Lang 14 | // Email: jlang@site.uottawa.ca 15 | // ========================================================================== 16 | // $Log: HeapSortInPlace.java,v $ 17 | // Revision 1.1 2006/11/05 03:27:51 jlang 18 | // Added lab8 on sorting. 19 | // 20 | // 21 | // ========================================================================== 22 | /** 23 | * Implements an in-place array-based heap sort. 24 | */ 25 | public class HeapSortInPlace { 26 | protected T[] d_seq; 27 | 28 | /** 29 | * Construct a HeapSort function object 30 | * _seq will be sorted on construction 31 | */ 32 | public HeapSortInPlace( T[] _seq ) { 33 | d_seq = _seq; 34 | int size = d_seq.length; 35 | // index i is for a heap position; array indices are one less 36 | for ( int i = size/2; i >= 1; i-- ) { 37 | // do bottom-up heap construction on 'd_seq' 38 | heapify(i, size); 39 | } 40 | // iteratively loop, remove max and fill in large elements from 41 | // the end 42 | for ( int i = size; i >= 2; i-- ) { 43 | // move max element to end (where it belongs) 44 | T temp = d_seq[0]; 45 | d_seq[0] = d_seq[i-1]; 46 | d_seq[i-1] = temp; 47 | // rebuild *max* heap: heap has shrunk by 1 element 48 | heapify(1,i-1); 49 | } 50 | } 51 | 52 | 53 | /** 54 | * Ensure a subset of an array between indices i and j has heap property. 55 | * Creates a heap with *max* element at i = 1 (seq[0]) 56 | */ 57 | protected void heapify(int i, int j) 58 | { 59 | // This method creates/restores the heap property over the range 60 | // seq[i-1] to seq[j-1]. The heap is a *maximum* heap, where the 61 | // maximum element is stored at the root, and a parent is greater 62 | // than its children. This works better for heap sort to keep the 63 | // heap at the front of the array for the entire sorting process. 64 | // 65 | // There is some messy left shifting here to use the 0 element as 66 | // the root. While parameters 'i' and 'j' are in the range 1 <= 67 | // ... <= size, and these values are used to find parents and 68 | // children, when a subscript is accessed, 1 is subtracted to be 69 | // in the range 0 <= ... <= size-1. 70 | 71 | // Essentially, this method implements "downheap" from the text, 72 | // where the start node is at i, and j is the last element in the 73 | // heap. 74 | 75 | // Credit: this algorithm adapted from Aho, Hopcroft, Ullman, 76 | // "Design and Analysis of Computer Algorithms" 77 | 78 | if (i <= j / 2) { 79 | int currentIndex = i - 1; 80 | T currentKey = d_seq[currentIndex]; 81 | int leftIndex = 2 * i - 1; 82 | T leftKey = d_seq[leftIndex]; 83 | if ((i < j / 2) || (j % 2 == 1)) { 84 | // ensures both children must exist 85 | int rightIndex = leftIndex + 1; 86 | T rightKey = d_seq[rightIndex]; 87 | // if ((currentKey < leftKey) || (currentKey < rightKey)) { 88 | if ((currentKey.compareTo(leftKey) < 0 ) || 89 | (currentKey.compareTo(rightKey) < 0 )) { 90 | // see if a downheap is necessary 91 | // if (leftKey > rightKey) { 92 | if (leftKey.compareTo(rightKey) > 0 ) { 93 | d_seq[currentIndex] = leftKey; // downheap to left child 94 | d_seq[leftIndex] = currentKey; 95 | heapify(2 * i, j); 96 | } else { 97 | d_seq[currentIndex] = rightKey; // downheap to right child 98 | d_seq[rightIndex] = currentKey; 99 | heapify(2 * i + 1, j); 100 | } 101 | } 102 | } else { 103 | // only left child exists 104 | // if (currentKey < leftKey) { 105 | if (currentKey.compareTo(leftKey)<0) { 106 | // see if downheap is necessary 107 | d_seq[currentIndex] = leftKey; // downheap to left child 108 | d_seq[leftIndex] = currentKey; 109 | heapify(2 * i, j); 110 | } 111 | } 112 | } 113 | } 114 | 115 | } 116 | -------------------------------------------------------------------------------- /Labs/Lab 8/MergeSort.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | /** 4 | * Implements bubble sort. 5 | */ 6 | public class MergeSort> { 7 | 8 | public MergeSort(T[] sequence) { 9 | this.mergeSort(sequence, 0 , sequence.length); 10 | } 11 | 12 | private void mergeSort(T[] sequence, int start, int size) { 13 | if(size < 2) { 14 | return; 15 | } 16 | mergeSort(sequence, start, size/2); 17 | int second = start + (size/2); 18 | int secondsize = size - (size/2); 19 | mergeSort(sequence, second, secondsize); 20 | merge(sequence, start, size/2, second, secondsize); 21 | } 22 | 23 | private void merge(T[] arr, int first, int firstsize, int second, int secondsize) { 24 | ArrayList temp = new ArrayList<>(firstsize+secondsize); 25 | int first_end = first + firstsize; 26 | int second_end = second + secondsize; 27 | int i,j,k; 28 | for(i=first,j=second,k=0; (i { 29 | protected T[] d_seq; 30 | 31 | /** 32 | * Construct a QuickSort object 33 | * _seq will be sorted on construction 34 | */ 35 | public QuickSortInPlace(T[] _seq) { 36 | d_seq = _seq; 37 | // Note: Make sure right = size-1 for initial call! 38 | quickSort(0, d_seq.length-1); 39 | } 40 | 41 | protected void quickSort( int _left, int _right ) 42 | { 43 | // 44 | // Instead of checking to end recursion at the start, we check to 45 | // see if recursion is needed when it arises. It saves one level 46 | // of recursive calls, at the expense of a couple of comparisions. 47 | 48 | // Pivot selection is always d_seq[right]. It ought to be randomized. 49 | Random r = new Random(); 50 | int pivotLoc = _left + r.nextInt((_right - _left) + 1); 51 | T pivot = d_seq[pivotLoc]; 52 | 53 | int pIndex = partition( pivot, _left, _right ); 54 | // The pivot is now at pIndex, so recur on both sides of it. 55 | // This is the point where we also check to stop recursion. 56 | if (_left < pIndex - 1) { 57 | quickSort( _left, pIndex - 1); 58 | } 59 | if (pIndex + 1 < _right) { 60 | quickSort( pIndex + 1, _right); 61 | } 62 | } 63 | 64 | /** 65 | * Partition the array in place After the call all elements smaller 66 | * than the pivot are on the left of the returned index; all 67 | * elements larger than the pivot are on the left of the returned 68 | * index. 69 | */ 70 | protected int partition(T _pivot, int _left, int _right) { 71 | T temp; 72 | int leftIndex = _left; // will scan rightward 73 | int rightIndex = _right - 1; // will scan leftward 74 | while (leftIndex <= rightIndex) { 75 | // Scan rightward to find an element larger than the pivot. 76 | while ((leftIndex <= rightIndex) && 77 | (d_seq[leftIndex].compareTo(_pivot)<0)) { 78 | leftIndex++; 79 | } 80 | // Scan leftward to find an element smaller than the pivot. 81 | while ((rightIndex >= leftIndex) && 82 | (d_seq[rightIndex].compareTo(_pivot)>=0)) { 83 | rightIndex--; 84 | } 85 | if (leftIndex < rightIndex) { 86 | // Both elements were found. 87 | temp = d_seq[leftIndex]; 88 | d_seq[leftIndex] = d_seq[rightIndex]; 89 | d_seq[rightIndex] = temp; 90 | } 91 | } // The loop continues until the indices cross. 92 | // Place the pivot by swapping it with the element at leftIndex. 93 | temp = d_seq[leftIndex]; 94 | d_seq[leftIndex] = d_seq[_right]; 95 | d_seq[_right] = temp; 96 | return leftIndex; 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /Labs/Lab 9/graph.txt: -------------------------------------------------------------------------------- 1 | A D 2 | A B 3 | B C 4 | D E 5 | H I 6 | I F 7 | B F 8 | C G 9 | G J 10 | F J 11 | F A 12 | -------------------------------------------------------------------------------- /Labs/Lab 9/graph2.txt: -------------------------------------------------------------------------------- 1 | A D 2 | A B 3 | A Y 4 | B C 5 | D E 6 | H I 7 | I F 8 | B F 9 | C G 10 | G J 11 | F J 12 | F A 13 | F X 14 | X F 15 | X Y 16 | Y X 17 | -------------------------------------------------------------------------------- /Labs/resources/net/datastructures/AbstractPriorityQueue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser 3 | * 4 | * Developed for use with the book: 5 | * 6 | * Data Structures and Algorithms in Java, Sixth Edition 7 | * Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser 8 | * John Wiley & Sons, 2014 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | package net.datastructures; 24 | 25 | import java.util.Comparator; 26 | 27 | /** 28 | * An abstract base class to ease the implementation of the PriorityQueue interface. 29 | * 30 | * The base class provides four means of support: 31 | * 1) It defines a PQEntry class as a concrete implementation of the 32 | * entry interface 33 | * 2) It provides an instance variable for a general Comparator and a 34 | * protected method, compare(a, b), that makes use of the comparator. 35 | * 3) It provides a boolean checkKey method that verifies that a given key 36 | * is appropriate for use with the comparator 37 | * 4) It provides an isEmpty implementation based upon the abstract size() method. 38 | * 39 | * @author Michael T. Goodrich 40 | * @author Roberto Tamassia 41 | * @author Michael H. Goldwasser 42 | */ 43 | public abstract class AbstractPriorityQueue implements PriorityQueue { 44 | //---------------- nested PQEntry class ---------------- 45 | /** 46 | * A concrete implementation of the Entry interface to be used within 47 | * a PriorityQueue implementation. 48 | */ 49 | protected static class PQEntry implements Entry { 50 | private K k; // key 51 | private V v; // value 52 | 53 | public PQEntry(K key, V value) { 54 | k = key; 55 | v = value; 56 | } 57 | 58 | // methods of the Entry interface 59 | public K getKey() { return k; } 60 | public V getValue() { return v; } 61 | 62 | // utilities not exposed as part of the Entry interface 63 | protected void setKey(K key) { k = key; } 64 | protected void setValue(V value) { v = value; } 65 | } //----------- end of nested PQEntry class ----------- 66 | 67 | // instance variable for an AbstractPriorityQueue 68 | /** The comparator defining the ordering of keys in the priority queue. */ 69 | private Comparator comp; 70 | 71 | /** 72 | * Creates an empty priority queue using the given comparator to order keys. 73 | * @param c comparator defining the order of keys in the priority queue 74 | */ 75 | protected AbstractPriorityQueue(Comparator c) { comp = c; } 76 | 77 | /** Creates an empty priority queue based on the natural ordering of its keys. */ 78 | protected AbstractPriorityQueue() { this(new DefaultComparator()); } 79 | 80 | /** Method for comparing two entries according to key */ 81 | protected int compare(Entry a, Entry b) { 82 | return comp.compare(a.getKey(), b.getKey()); 83 | } 84 | 85 | /** Determines whether a key is valid. */ 86 | protected boolean checkKey(K key) throws IllegalArgumentException { 87 | try { 88 | return (comp.compare(key,key) == 0); // see if key can be compared to itself 89 | } catch (ClassCastException e) { 90 | throw new IllegalArgumentException("Incompatible key"); 91 | } 92 | } 93 | 94 | /** 95 | * Tests whether the priority queue is empty. 96 | * @return true if the priority queue is empty, false otherwise 97 | */ 98 | @Override 99 | public boolean isEmpty() { return size() == 0; } 100 | } 101 | -------------------------------------------------------------------------------- /Labs/resources/net/datastructures/AbstractSortedMap.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser 3 | * 4 | * Developed for use with the book: 5 | * 6 | * Data Structures and Algorithms in Java, Sixth Edition 7 | * Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser 8 | * John Wiley & Sons, 2014 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | package net.datastructures; 24 | 25 | import java.util.Comparator; 26 | 27 | /** 28 | * An abstract base class to ease the implementation of the SortedMap interface. 29 | * 30 | * The base class provides four means of support: 31 | * 1) It defines a PQEntry class as a concrete implementation of the 32 | * entry interface 33 | * 2) It provides an instance variable for a general Comparator and 34 | * protected methods, compare(a, b), that can compare either two 35 | * entries or two keys using the comparator. 36 | * 3) It provides a boolean checkKey method that verifies that a given key 37 | * is appropriate for use with the comparator 38 | * 39 | * @author Michael T. Goodrich 40 | * @author Roberto Tamassia 41 | * @author Michael H. Goldwasser 42 | */ 43 | public abstract class AbstractSortedMap 44 | extends AbstractMap implements SortedMap { 45 | 46 | // instance variable for an AbstractSortedMap 47 | /** The comparator defining the ordering of keys in the map. */ 48 | private Comparator comp; 49 | 50 | /** 51 | * Initializes the comparator for the map. 52 | * @param c comparator defining the order of keys in the map 53 | */ 54 | protected AbstractSortedMap(Comparator c) { 55 | comp = c; 56 | } 57 | 58 | /** Initializes the map with a default comparator. */ 59 | protected AbstractSortedMap() { 60 | this(new DefaultComparator()); // default comparator uses natural ordering 61 | } 62 | 63 | /** Method for comparing two entries according to key */ 64 | protected int compare(Entry a, Entry b) { 65 | return comp.compare(a.getKey(), b.getKey()); 66 | } 67 | 68 | /** Method for comparing a key and an entry's key */ 69 | protected int compare(K a, Entry b) { 70 | return comp.compare(a, b.getKey()); 71 | } 72 | 73 | /** Method for comparing a key and an entry's key */ 74 | protected int compare(Entry a, K b) { 75 | return comp.compare(a.getKey(), b); 76 | } 77 | 78 | /** Method for comparing two keys */ 79 | protected int compare(K a, K b) { 80 | return comp.compare(a, b); 81 | } 82 | 83 | /** Determines whether a key is valid. */ 84 | protected boolean checkKey(K key) throws IllegalArgumentException { 85 | try { 86 | return (comp.compare(key,key)==0); // see if key can be compared to itself 87 | } catch (ClassCastException e) { 88 | throw new IllegalArgumentException("Incompatible key"); 89 | } 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /Labs/resources/net/datastructures/AdaptablePriorityQueue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser 3 | * 4 | * Developed for use with the book: 5 | * 6 | * Data Structures and Algorithms in Java, Sixth Edition 7 | * Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser 8 | * John Wiley & Sons, 2014 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | package net.datastructures; 24 | 25 | /** 26 | * Interface for the adaptable priority queue ADT. The Entry instance 27 | * returned by the insert method can later be used to remove the 28 | * entry, or to modify that entry's key or value. 29 | * 30 | * @author Michael T. Goodrich 31 | * @author Roberto Tamassia 32 | * @author Michael H. Goldwasser 33 | */ 34 | public interface AdaptablePriorityQueue extends PriorityQueue { 35 | 36 | /** 37 | * Removes the given entry from the priority queue. 38 | * 39 | * @param entry an entry of this priority queue 40 | * @throws IllegalArgumentException if e is not a valid entry for the priority queue. 41 | */ 42 | void remove(Entry entry) throws IllegalArgumentException; 43 | 44 | /** 45 | * Replaces the key of an entry. 46 | * 47 | * @param entry an entry of this priority queue 48 | * @param key the new key 49 | * @throws IllegalArgumentException if e is not a valid entry for the priority queue. 50 | */ 51 | void replaceKey(Entry entry, K key) throws IllegalArgumentException; 52 | 53 | /** 54 | * Replaces the value of an entry. 55 | * 56 | * @param entry an entry of this priority queue 57 | * @param value the new value 58 | * @throws IllegalArgumentException if e is not a valid entry for the priority queue. 59 | */ 60 | void replaceValue(Entry entry, V value) throws IllegalArgumentException; 61 | } 62 | -------------------------------------------------------------------------------- /Labs/resources/net/datastructures/BinaryTree.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser 3 | * 4 | * Developed for use with the book: 5 | * 6 | * Data Structures and Algorithms in Java, Sixth Edition 7 | * Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser 8 | * John Wiley & Sons, 2014 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | package net.datastructures; 24 | 25 | /** 26 | * An interface for a binary tree, in which each node has at most two children. 27 | * 28 | * @author Michael T. Goodrich 29 | * @author Roberto Tamassia 30 | * @author Michael H. Goldwasser 31 | */ 32 | public interface BinaryTree extends Tree { 33 | 34 | /** 35 | * Returns the Position of p's left child (or null if no child exists). 36 | * 37 | * @param p A valid Position within the tree 38 | * @return the Position of the left child (or null if no child exists) 39 | * @throws IllegalArgumentException if p is not a valid Position for this tree 40 | */ 41 | Position left(Position p) throws IllegalArgumentException; 42 | 43 | /** 44 | * Returns the Position of p's right child (or null if no child exists). 45 | * 46 | * @param p A valid Position within the tree 47 | * @return the Position of the right child (or null if no child exists) 48 | * @throws IllegalArgumentException if p is not a valid Position for this tree 49 | */ 50 | Position right(Position p) throws IllegalArgumentException; 51 | 52 | /** 53 | * Returns the Position of p's sibling (or null if no sibling exists). 54 | * 55 | * @param p A valid Position within the tree 56 | * @return the Position of the sibling (or null if no sibling exists) 57 | * @throws IllegalArgumentException if p is not a valid Position for this tree 58 | */ 59 | Position sibling(Position p) throws IllegalArgumentException; 60 | } 61 | -------------------------------------------------------------------------------- /Labs/resources/net/datastructures/ChainHashMap.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser 3 | * 4 | * Developed for use with the book: 5 | * 6 | * Data Structures and Algorithms in Java, Sixth Edition 7 | * Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser 8 | * John Wiley & Sons, 2014 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | package net.datastructures; 24 | 25 | import java.util.ArrayList; 26 | 27 | /* 28 | * Map implementation using hash table with separate chaining. 29 | * 30 | * @author Michael T. Goodrich 31 | * @author Roberto Tamassia 32 | * @author Michael H. Goldwasser 33 | */ 34 | public class ChainHashMap extends AbstractHashMap { 35 | // a fixed capacity array of UnsortedTableMap that serve as buckets 36 | private UnsortedTableMap[] table; // initialized within createTable 37 | 38 | // provide same constructors as base class 39 | /** Creates a hash table with capacity 11 and prime factor 109345121. */ 40 | public ChainHashMap() { super(); } 41 | 42 | /** Creates a hash table with given capacity and prime factor 109345121. */ 43 | public ChainHashMap(int cap) { super(cap); } 44 | 45 | /** Creates a hash table with the given capacity and prime factor. */ 46 | public ChainHashMap(int cap, int p) { super(cap, p); } 47 | 48 | /** Creates an empty table having length equal to current capacity. */ 49 | @Override 50 | @SuppressWarnings({"unchecked"}) 51 | protected void createTable() { 52 | table = (UnsortedTableMap[]) new UnsortedTableMap[capacity]; 53 | } 54 | 55 | /** 56 | * Returns value associated with key k in bucket with hash value h. 57 | * If no such entry exists, returns null. 58 | * @param h the hash value of the relevant bucket 59 | * @param k the key of interest 60 | * @return associate value (or null, if no such entry) 61 | */ 62 | @Override 63 | protected V bucketGet(int h, K k) { 64 | UnsortedTableMap bucket = table[h]; 65 | if (bucket == null) return null; 66 | return bucket.get(k); 67 | } 68 | 69 | /** 70 | * Associates key k with value v in bucket with hash value h, returning 71 | * the previously associated value, if any. 72 | * @param h the hash value of the relevant bucket 73 | * @param k the key of interest 74 | * @param v the value to be associated 75 | * @return previous value associated with k (or null, if no such entry) 76 | */ 77 | @Override 78 | protected V bucketPut(int h, K k, V v) { 79 | UnsortedTableMap bucket = table[h]; 80 | if (bucket == null) 81 | bucket = table[h] = new UnsortedTableMap<>(); 82 | int oldSize = bucket.size(); 83 | V answer = bucket.put(k,v); 84 | n += (bucket.size() - oldSize); // size may have increased 85 | return answer; 86 | } 87 | 88 | /** 89 | * Removes entry having key k from bucket with hash value h, returning 90 | * the previously associated value, if found. 91 | * @param h the hash value of the relevant bucket 92 | * @param k the key of interest 93 | * @return previous value associated with k (or null, if no such entry) 94 | */ 95 | @Override 96 | protected V bucketRemove(int h, K k) { 97 | UnsortedTableMap bucket = table[h]; 98 | if (bucket == null) return null; 99 | int oldSize = bucket.size(); 100 | V answer = bucket.remove(k); 101 | n -= (oldSize - bucket.size()); // size may have decreased 102 | return answer; 103 | } 104 | 105 | /** 106 | * Returns an iterable collection of all key-value entries of the map. 107 | * 108 | * @return iterable collection of the map's entries 109 | */ 110 | @Override 111 | public Iterable> entrySet() { 112 | ArrayList> buffer = new ArrayList<>(); 113 | for (int h=0; h < capacity; h++) 114 | if (table[h] != null) 115 | for (Entry entry : table[h].entrySet()) 116 | buffer.add(entry); 117 | return buffer; 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /Labs/resources/net/datastructures/CircularQueue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser 3 | * 4 | * Developed for use with the book: 5 | * 6 | * Data Structures and Algorithms in Java, Sixth Edition 7 | * Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser 8 | * John Wiley & Sons, 2014 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | package net.datastructures; 24 | 25 | /** 26 | * This provides one additional method not part of the general Queue interface. 27 | * A call to Q.rotate() is logically equivalent to the combination 28 | * Q.enqueue(Q.dequeue()), yet may be implemented more efficiently in some realizations. 29 | * 30 | * @author Michael T. Goodrich 31 | * @author Roberto Tamassia 32 | * @author Michael H. Goldwasser 33 | */ 34 | public interface CircularQueue extends Queue { 35 | /** 36 | * Rotates the front element of the queue to the back of the queue. 37 | * This does nothing if the queue is empty. 38 | */ 39 | void rotate(); 40 | } 41 | -------------------------------------------------------------------------------- /Labs/resources/net/datastructures/DefaultComparator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser 3 | * 4 | * Developed for use with the book: 5 | * 6 | * Data Structures and Algorithms in Java, Sixth Edition 7 | * Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser 8 | * John Wiley & Sons, 2014 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | package net.datastructures; 24 | 25 | import java.util.Comparator; 26 | 27 | /** 28 | * Comparator based on the compareTo method of a Comparable element type. 29 | * 30 | * @author Michael T. Goodrich 31 | * @author Roberto Tamassia 32 | * @author Michael H. Goldwasser 33 | */ 34 | public class DefaultComparator implements Comparator { 35 | 36 | /** 37 | * Compares two elements. 38 | * 39 | * @return a negative integer if a is less than b, 40 | * zero if a equals b, or a positive integer if 41 | * a is greater than b 42 | */ 43 | @SuppressWarnings({"unchecked"}) 44 | public int compare(E a, E b) throws ClassCastException { 45 | return ((Comparable) a).compareTo(b); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Labs/resources/net/datastructures/Deque.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser 3 | * 4 | * Developed for use with the book: 5 | * 6 | * Data Structures and Algorithms in Java, Sixth Edition 7 | * Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser 8 | * John Wiley & Sons, 2014 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | package net.datastructures; 24 | 25 | /** 26 | * Interface for a double-ended queue: a collection of elements that can be inserted 27 | * and removed at both ends; this interface is a simplified version of java.util.Deque. 28 | * 29 | * @author Michael T. Goodrich 30 | * @author Roberto Tamassia 31 | * @author Michael H. Goldwasser 32 | */ 33 | public interface Deque { 34 | 35 | /** 36 | * Returns the number of elements in the deque. 37 | * @return number of elements in the deque 38 | */ 39 | int size(); 40 | 41 | /** 42 | * Tests whether the deque is empty. 43 | * @return true if the deque is empty, false otherwise 44 | */ 45 | boolean isEmpty(); 46 | 47 | /** 48 | * Returns (but does not remove) the first element of the deque. 49 | * @return first element of the deque (or null if empty) 50 | */ 51 | E first(); 52 | 53 | /** 54 | * Returns (but does not remove) the last element of the deque. 55 | * @return last element of the deque (or null if empty) 56 | */ 57 | E last(); 58 | 59 | /** 60 | * Inserts an element at the front of the deque. 61 | * @param e the new element 62 | */ 63 | void addFirst(E e); 64 | 65 | /** 66 | * Inserts an element at the back of the deque. 67 | * @param e the new element 68 | */ 69 | void addLast(E e); 70 | 71 | /** 72 | * Removes and returns the first element of the deque. 73 | * @return element removed (or null if empty) 74 | */ 75 | E removeFirst(); 76 | 77 | /** 78 | * Removes and returns the last element of the deque. 79 | * @return element removed (or null if empty) 80 | */ 81 | E removeLast(); 82 | } 83 | -------------------------------------------------------------------------------- /Labs/resources/net/datastructures/Edge.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser 3 | * 4 | * Developed for use with the book: 5 | * 6 | * Data Structures and Algorithms in Java, Sixth Edition 7 | * Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser 8 | * John Wiley & Sons, 2014 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | package net.datastructures; 24 | 25 | /** 26 | * An edge of a graph. 27 | * 28 | * @author Michael T. Goodrich 29 | * @author Roberto Tamassia 30 | * @author Michael H. Goldwasser 31 | */ 32 | public interface Edge { 33 | /** Returns the element associated with the edge. */ 34 | E getElement(); 35 | } 36 | -------------------------------------------------------------------------------- /Labs/resources/net/datastructures/Entry.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser 3 | * 4 | * Developed for use with the book: 5 | * 6 | * Data Structures and Algorithms in Java, Sixth Edition 7 | * Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser 8 | * John Wiley & Sons, 2014 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | package net.datastructures; 24 | 25 | /** 26 | * Interface for a key-value pair. 27 | * 28 | * @author Michael T. Goodrich 29 | * @author Roberto Tamassia 30 | * @author Michael H. Goldwasser 31 | */ 32 | public interface Entry { 33 | /** 34 | * Returns the key stored in this entry. 35 | * @return the entry's key 36 | */ 37 | K getKey(); 38 | 39 | /** 40 | * Returns the value stored in this entry. 41 | * @return the entry's value 42 | */ 43 | V getValue(); 44 | } 45 | -------------------------------------------------------------------------------- /Labs/resources/net/datastructures/LinkedCircularQueue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser 3 | * 4 | * Developed for use with the book: 5 | * 6 | * Data Structures and Algorithms in Java, Sixth Edition 7 | * Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser 8 | * John Wiley & Sons, 2014 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | package net.datastructures; 24 | 25 | /** 26 | * Realization of a circular FIFO queue as an adaptation of a CircularlyLinkedList. 27 | * This provides one additional method not part of the general Queue interface. 28 | * A call to rotate() is a more efficient simulation of the combination 29 | * enqueue(dequeue()). All operations are performed in constant time. 30 | * 31 | * @author Michael T. Goodrich 32 | * @author Roberto Tamassia 33 | * @author Michael H. Goldwasser 34 | * @see CircularlyLinkedList 35 | */ 36 | public class LinkedCircularQueue implements CircularQueue { 37 | 38 | /** The primary storage for elements of the queue */ 39 | private CircularlyLinkedList circle = new CircularlyLinkedList<>(); 40 | 41 | /** Creates an empty queue. */ 42 | public LinkedCircularQueue() { } 43 | 44 | /** 45 | * Returns the number of elements in the queue. 46 | * @return number of elements in the queue 47 | */ 48 | @Override 49 | public int size() { 50 | return circle.size(); 51 | } 52 | 53 | /** 54 | * Tests whether the queue is empty. 55 | * @return true if the queue is empty, false otherwise 56 | */ 57 | @Override 58 | public boolean isEmpty() { 59 | return circle.isEmpty(); 60 | } 61 | 62 | /** 63 | * Inserts an element at the rear of the queue. 64 | * @param element the element to be inserted 65 | */ 66 | @Override 67 | public void enqueue(E element) { 68 | circle.addLast(element); 69 | } 70 | 71 | /** 72 | * Returns, but does not remove, the first element of the queue. 73 | * @return the first element of the queue (or null if empty) 74 | */ 75 | @Override 76 | public E first() { 77 | return circle.first(); 78 | } 79 | 80 | /** 81 | * Removes and returns the first element of the queue. 82 | * @return element removed (or null if empty) 83 | */ 84 | @Override 85 | public E dequeue() { 86 | return circle.removeFirst(); 87 | } 88 | 89 | /** 90 | * Rotates the front element of the queue to the back of the queue. 91 | * This is a shorthand for enqueue(dequeue()), except that it does 92 | * nothing if the queue is empty, and it is implemented more efficiently 93 | * so that no nodes are created or destroyed. 94 | */ 95 | @Override 96 | public void rotate() { 97 | circle.rotate(); 98 | } 99 | 100 | /** Produces a string representation of the contents of the queue. 101 | * (from front to back). This exists for debugging purposes only. 102 | */ 103 | public String toString() { 104 | return circle.toString(); 105 | } 106 | 107 | } 108 | -------------------------------------------------------------------------------- /Labs/resources/net/datastructures/LinkedQueue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser 3 | * 4 | * Developed for use with the book: 5 | * 6 | * Data Structures and Algorithms in Java, Sixth Edition 7 | * Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser 8 | * John Wiley & Sons, 2014 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | package net.datastructures; 24 | 25 | /** 26 | * Realization of a FIFO queue as an adaptation of a SinglyLinkedList. 27 | * All operations are performed in constant time. 28 | * 29 | * @author Michael T. Goodrich 30 | * @author Roberto Tamassia 31 | * @author Michael H. Goldwasser 32 | * @see SinglyLinkedList 33 | */ 34 | public class LinkedQueue implements Queue { 35 | 36 | /** The primary storage for elements of the queue */ 37 | private SinglyLinkedList list = new SinglyLinkedList<>(); // an empty list 38 | 39 | /** Constructs an initially empty queue. */ 40 | public LinkedQueue() { } // new queue relies on the initially empty list 41 | 42 | /** 43 | * Returns the number of elements in the queue. 44 | * @return number of elements in the queue 45 | */ 46 | @Override 47 | public int size() { return list.size(); } 48 | 49 | /** 50 | * Tests whether the queue is empty. 51 | * @return true if the queue is empty, false otherwise 52 | */ 53 | @Override 54 | public boolean isEmpty() { return list.isEmpty(); } 55 | 56 | /** 57 | * Inserts an element at the rear of the queue. 58 | * @param element the element to be inserted 59 | */ 60 | @Override 61 | public void enqueue(E element) { list.addLast(element); } 62 | 63 | /** 64 | * Returns, but does not remove, the first element of the queue. 65 | * @return the first element of the queue (or null if empty) 66 | */ 67 | @Override 68 | public E first() { return list.first(); } 69 | 70 | /** 71 | * Removes and returns the first element of the queue. 72 | * @return element removed (or null if empty) 73 | */ 74 | @Override 75 | public E dequeue() { return list.removeFirst(); } 76 | 77 | /** Produces a string representation of the contents of the queue. 78 | * (from front to back). This exists for debugging purposes only. 79 | */ 80 | public String toString() { 81 | return list.toString(); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /Labs/resources/net/datastructures/LinkedStack.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser 3 | * 4 | * Developed for use with the book: 5 | * 6 | * Data Structures and Algorithms in Java, Sixth Edition 7 | * Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser 8 | * John Wiley & Sons, 2014 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | package net.datastructures; 24 | 25 | /** 26 | * Realization of a stack as an adaptation of a SinglyLinkedList. 27 | * All operations are performed in constant time. 28 | * 29 | * @author Michael T. Goodrich 30 | * @author Roberto Tamassia 31 | * @author Michael H. Goldwasser 32 | * @see SinglyLinkedList 33 | */ 34 | public class LinkedStack implements Stack { 35 | 36 | /** The primary storage for elements of the stack */ 37 | private SinglyLinkedList list = new SinglyLinkedList<>(); // an empty list 38 | 39 | /** Constructs an initially empty stack. */ 40 | public LinkedStack() { } // new stack relies on the initially empty list 41 | 42 | /** 43 | * Returns the number of elements in the stack. 44 | * @return number of elements in the stack 45 | */ 46 | @Override 47 | public int size() { return list.size(); } 48 | 49 | /** 50 | * Tests whether the stack is empty. 51 | * @return true if the stack is empty, false otherwise 52 | */ 53 | @Override 54 | public boolean isEmpty() { return list.isEmpty(); } 55 | 56 | /** 57 | * Inserts an element at the top of the stack. 58 | * @param element the element to be inserted 59 | */ 60 | @Override 61 | public void push(E element) { list.addFirst(element); } 62 | 63 | /** 64 | * Returns, but does not remove, the element at the top of the stack. 65 | * @return top element in the stack (or null if empty) 66 | */ 67 | @Override 68 | public E top() { return list.first(); } 69 | 70 | /** 71 | * Removes and returns the top element from the stack. 72 | * @return element removed (or null if empty) 73 | */ 74 | @Override 75 | public E pop() { return list.removeFirst(); } 76 | 77 | /** Produces a string representation of the contents of the stack. 78 | * (ordered from top to bottom) 79 | * 80 | * This exists for debugging purposes only. 81 | * 82 | * @return textual representation of the stack 83 | */ 84 | public String toString() { 85 | return list.toString(); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /Labs/resources/net/datastructures/List.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser 3 | * 4 | * Developed for use with the book: 5 | * 6 | * Data Structures and Algorithms in Java, Sixth Edition 7 | * Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser 8 | * John Wiley & Sons, 2014 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | package net.datastructures; 24 | 25 | import java.util.Iterator; 26 | 27 | /** 28 | * A simplified version of the java.util.List interface. 29 | * 30 | * @author Michael T. Goodrich 31 | * @author Roberto Tamassia 32 | * @author Michael H. Goldwasser 33 | */ 34 | public interface List extends Iterable { 35 | /** 36 | * Returns the number of elements in the list. 37 | * @return number of elements in the list 38 | */ 39 | int size(); 40 | 41 | /** 42 | * Tests whether the list is empty. 43 | * @return true if the list is empty, false otherwise 44 | */ 45 | boolean isEmpty(); 46 | 47 | /** 48 | * Returns (but does not remove) the element at index i. 49 | * @param i the index of the element to return 50 | * @return the element at the specified index 51 | * @throws IndexOutOfBoundsException if the index is negative or greater than size()-1 52 | */ 53 | E get(int i) throws IndexOutOfBoundsException; 54 | 55 | /** 56 | * Replaces the element at the specified index, and returns the element previously stored. 57 | * @param i the index of the element to replace 58 | * @param e the new element to be stored 59 | * @return the previously stored element 60 | * @throws IndexOutOfBoundsException if the index is negative or greater than size()-1 61 | */ 62 | E set(int i, E e) throws IndexOutOfBoundsException; 63 | 64 | /** 65 | * Inserts the given element at the specified index of the list, shifting all 66 | * subsequent elements in the list one position further to make room. 67 | * @param i the index at which the new element should be stored 68 | * @param e the new element to be stored 69 | * @throws IndexOutOfBoundsException if the index is negative or greater than size() 70 | */ 71 | void add(int i, E e) throws IndexOutOfBoundsException; 72 | 73 | /** 74 | * Removes and returns the element at the given index, shifting all subsequent 75 | * elements in the list one position closer to the front. 76 | * @param i the index of the element to be removed 77 | * @return the element that had be stored at the given index 78 | * @throws IndexOutOfBoundsException if the index is negative or greater than size() 79 | */ 80 | E remove(int i) throws IndexOutOfBoundsException; 81 | 82 | /** 83 | * Returns an iterator of the elements stored in the list. 84 | * @return iterator of the list's elements 85 | */ 86 | Iterator iterator(); 87 | } 88 | -------------------------------------------------------------------------------- /Labs/resources/net/datastructures/Map.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser 3 | * 4 | * Developed for use with the book: 5 | * 6 | * Data Structures and Algorithms in Java, Sixth Edition 7 | * Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser 8 | * John Wiley & Sons, 2014 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | package net.datastructures; 24 | 25 | /** 26 | * An interface for an associative map which binds a key uniquely to a value. 27 | * This interface is a simplified version of java.util.Map. 28 | * 29 | * @author Michael T. Goodrich 30 | * @author Roberto Tamassia 31 | * @author Michael H. Goldwasser 32 | */ 33 | public interface Map { 34 | 35 | /** 36 | * Returns the number of entries in the map. 37 | * @return number of entries in the map 38 | */ 39 | int size(); 40 | 41 | /** 42 | * Tests whether the map is empty. 43 | * @return true if the map is empty, false otherwise 44 | */ 45 | boolean isEmpty(); 46 | 47 | /** 48 | * Returns the value associated with the specified key, or null if no such entry exists. 49 | * @param key the key whose associated value is to be returned 50 | * @return the associated value, or null if no such entry exists 51 | */ 52 | V get(K key); 53 | 54 | /** 55 | * Associates the given value with the given key. If an entry with 56 | * the key was already in the map, this replaced the previous value 57 | * with the new one and returns the old value. Otherwise, a new 58 | * entry is added and null is returned. 59 | * @param key key with which the specified value is to be associated 60 | * @param value value to be associated with the specified key 61 | * @return the previous value associated with the key (or null, if no such entry) 62 | */ 63 | V put(K key, V value); 64 | 65 | /** 66 | * Removes the entry with the specified key, if present, and returns 67 | * its associated value. Otherwise does nothing and returns null. 68 | * @param key the key whose entry is to be removed from the map 69 | * @return the previous value associated with the removed key, or null if no such entry exists 70 | */ 71 | V remove(K key); 72 | 73 | /** 74 | * Returns an iterable collection of the keys contained in the map. 75 | * 76 | * @return iterable collection of the map's keys 77 | */ 78 | Iterable keySet(); 79 | 80 | /** 81 | * Returns an iterable collection of the values contained in the map. 82 | * Note that the same value will be given multiple times in the result 83 | * if it is associated with multiple keys. 84 | * 85 | * @return iterable collection of the map's values 86 | */ 87 | Iterable values(); 88 | 89 | /** 90 | * Returns an iterable collection of all key-value entries of the map. 91 | * 92 | * @return iterable collection of the map's entries 93 | */ 94 | Iterable> entrySet(); 95 | } 96 | -------------------------------------------------------------------------------- /Labs/resources/net/datastructures/Partition.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser 3 | * 4 | * Developed for use with the book: 5 | * 6 | * Data Structures and Algorithms in Java, Sixth Edition 7 | * Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser 8 | * John Wiley & Sons, 2014 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | package net.datastructures; 24 | 25 | /** A Union-Find structure for maintaining disjoint sets. */ 26 | public class Partition { 27 | 28 | //--------------- nested Locator class ------------- 29 | private class Locator implements Position { 30 | public E element; 31 | public int size; 32 | public Locator parent; 33 | public Locator(E elem) { 34 | element = elem; 35 | size = 1; 36 | parent = this; // convention for a cluster leader 37 | } 38 | 39 | public E getElement() { return element; } 40 | 41 | private boolean validate(Partition universe) { 42 | return Partition.this == universe; 43 | } 44 | } //--------- end of nested Locator class --------- 45 | 46 | /** Validates a Position and returns it cast as a Locator. */ 47 | private Locator validate(Position pos) { 48 | if (!(pos instanceof Locator)) throw new IllegalArgumentException("Invalid position"); 49 | Locator loc = (Locator) pos; 50 | if (!loc.validate(this)) 51 | throw new IllegalArgumentException("Position does not belong to this structure"); 52 | return loc; 53 | } 54 | 55 | /** Makes a new cluster containing element e and returns its position. */ 56 | public Position makeCluster(E e) { 57 | return new Locator(e); 58 | } 59 | 60 | /** 61 | * Finds the cluster containing the element identified by Position p 62 | * and returns the Position of the cluster's leader. 63 | */ 64 | public Position find(Position p) { 65 | Locator loc = validate(p); 66 | if (loc.parent != loc) 67 | loc.parent = (Locator) find(loc.parent); // overwrite parent after recursion 68 | return loc.parent; 69 | } 70 | 71 | /** Merges the clusters containing elements with positions p and q (if distinct). */ 72 | public void union(Position p, Position q) { 73 | Locator a = (Locator) find(p); 74 | Locator b = (Locator) find(q); 75 | if (a != b) 76 | if (a.size > b.size) { 77 | b.parent = a; 78 | a.size += b.size; 79 | } else { 80 | a.parent = b; 81 | b.size += a.size; 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /Labs/resources/net/datastructures/Position.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser 3 | * 4 | * Developed for use with the book: 5 | * 6 | * Data Structures and Algorithms in Java, Sixth Edition 7 | * Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser 8 | * John Wiley & Sons, 2014 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | package net.datastructures; 24 | 25 | /** 26 | * An interface for a position which is an abstraction for the 27 | * location at which a single element is stored in a positional 28 | * container. 29 | * 30 | * @author Michael T. Goodrich 31 | * @author Roberto Tamassia 32 | * @author Michael H. Goldwasser 33 | */ 34 | public interface Position { 35 | /** 36 | * Returns the element stored at this position. 37 | * 38 | * @return the stored element 39 | * @throws IllegalStateException if position no longer valid 40 | */ 41 | E getElement() throws IllegalStateException; 42 | } 43 | -------------------------------------------------------------------------------- /Labs/resources/net/datastructures/PriorityQueue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser 3 | * 4 | * Developed for use with the book: 5 | * 6 | * Data Structures and Algorithms in Java, Sixth Edition 7 | * Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser 8 | * John Wiley & Sons, 2014 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | package net.datastructures; 24 | 25 | /** 26 | * Interface for the priority queue ADT. 27 | * 28 | * @author Michael T. Goodrich 29 | * @author Roberto Tamassia 30 | * @author Michael H. Goldwasser 31 | */ 32 | public interface PriorityQueue { 33 | 34 | /** 35 | * Returns the number of items in the priority queue. 36 | * @return number of items 37 | */ 38 | int size(); 39 | 40 | /** 41 | * Tests whether the priority queue is empty. 42 | * @return true if the priority queue is empty, false otherwise 43 | */ 44 | boolean isEmpty(); 45 | 46 | /** 47 | * Inserts a key-value pair and returns the entry created. 48 | * @param key the key of the new entry 49 | * @param value the associated value of the new entry 50 | * @return the entry storing the new key-value pair 51 | * @throws IllegalArgumentException if the key is unacceptable for this queue 52 | */ 53 | Entry insert(K key, V value) throws IllegalArgumentException; 54 | 55 | /** 56 | * Returns (but does not remove) an entry with minimal key. 57 | * @return entry having a minimal key (or null if empty) 58 | */ 59 | Entry min(); 60 | 61 | /** 62 | * Removes and returns an entry with minimal key. 63 | * @return the removed entry (or null if empty) 64 | */ 65 | Entry removeMin(); 66 | } 67 | -------------------------------------------------------------------------------- /Labs/resources/net/datastructures/Queue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser 3 | * 4 | * Developed for use with the book: 5 | * 6 | * Data Structures and Algorithms in Java, Sixth Edition 7 | * Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser 8 | * John Wiley & Sons, 2014 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | package net.datastructures; 24 | 25 | /** 26 | * Interface for a queue: a collection of elements that are inserted 27 | * and removed according to the first-in first-out principle. Although 28 | * similar in purpose, this interface differs from java.util.Queue. 29 | * 30 | * @author Michael T. Goodrich 31 | * @author Roberto Tamassia 32 | * @author Michael H. Goldwasser 33 | */ 34 | public interface Queue { 35 | /** 36 | * Returns the number of elements in the queue. 37 | * @return number of elements in the queue 38 | */ 39 | int size(); 40 | 41 | /** 42 | * Tests whether the queue is empty. 43 | * @return true if the queue is empty, false otherwise 44 | */ 45 | boolean isEmpty(); 46 | 47 | /** 48 | * Inserts an element at the rear of the queue. 49 | * @param e the element to be inserted 50 | */ 51 | void enqueue(E e); 52 | 53 | /** 54 | * Returns, but does not remove, the first element of the queue. 55 | * @return the first element of the queue (or null if empty) 56 | */ 57 | E first(); 58 | 59 | /** 60 | * Removes and returns the first element of the queue. 61 | * @return element removed (or null if empty) 62 | */ 63 | E dequeue(); 64 | } 65 | -------------------------------------------------------------------------------- /Labs/resources/net/datastructures/SortedMap.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser 3 | * 4 | * Developed for use with the book: 5 | * 6 | * Data Structures and Algorithms in Java, Sixth Edition 7 | * Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser 8 | * John Wiley & Sons, 2014 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | package net.datastructures; 24 | 25 | /** 26 | * A map with additional support for keys from a total ordering. 27 | * 28 | * The total ordering is the natural ordering of keys, by default, 29 | * or it can be defined by providing an optional Comparator. 30 | * 31 | * All iterations will be in sorted order relative to the keys, 32 | * and additional methods provide for non-exact searches. 33 | * This interface is a simple variant that blends features of 34 | * java.util.SortedMap and java.util.NavigableMap. 35 | * 36 | * @author Michael T. Goodrich 37 | * @author Roberto Tamassia 38 | * @author Michael H. Goldwasser 39 | */ 40 | public interface SortedMap extends Map{ 41 | 42 | /** 43 | * Returns the entry having the least key (or null if map is empty). 44 | * @return entry with least key (or null if map is empty) 45 | */ 46 | Entry firstEntry(); 47 | 48 | /** 49 | * Returns the entry having the greatest key (or null if map is empty). 50 | * @return entry with greatest key (or null if map is empty) 51 | */ 52 | Entry lastEntry(); 53 | 54 | /** 55 | * Returns the entry with least key greater than or equal to given key 56 | * (or null if no such key exists). 57 | * @return entry with least key greater than or equal to given (or null if no such entry) 58 | * @throws IllegalArgumentException if the key is not compatible with the map 59 | */ 60 | Entry ceilingEntry(K key) throws IllegalArgumentException; 61 | 62 | /** 63 | * Returns the entry with greatest key less than or equal to given key 64 | * (or null if no such key exists). 65 | * @return entry with greatest key less than or equal to given (or null if no such entry) 66 | * @throws IllegalArgumentException if the key is not compatible with the map 67 | */ 68 | Entry floorEntry(K key) throws IllegalArgumentException; 69 | 70 | /** 71 | * Returns the entry with greatest key strictly less than given key 72 | * (or null if no such key exists). 73 | * @return entry with greatest key strictly less than given (or null if no such entry) 74 | * @throws IllegalArgumentException if the key is not compatible with the map 75 | */ 76 | Entry lowerEntry(K key) throws IllegalArgumentException; 77 | 78 | /** 79 | * Returns the entry with least key strictly greater than given key 80 | * (or null if no such key exists). 81 | * @return entry with least key strictly greater than given (or null if no such entry) 82 | * @throws IllegalArgumentException if the key is not compatible with the map 83 | */ 84 | Entry higherEntry(K key) throws IllegalArgumentException; 85 | 86 | /** 87 | * Returns an iterable containing all keys in the range from 88 | * fromKey inclusive to toKey exclusive. 89 | * @return iterable with keys in desired range 90 | * @throws IllegalArgumentException if fromKey or toKey is not compatible with the map 91 | */ 92 | Iterable> subMap(K fromKey, K toKey) throws IllegalArgumentException; 93 | } 94 | -------------------------------------------------------------------------------- /Labs/resources/net/datastructures/SortedPriorityQueue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser 3 | * 4 | * Developed for use with the book: 5 | * 6 | * Data Structures and Algorithms in Java, Sixth Edition 7 | * Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser 8 | * John Wiley & Sons, 2014 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | package net.datastructures; 24 | 25 | import java.util.Comparator; 26 | 27 | /** 28 | * An implementation of a priority queue with a sorted list. 29 | * 30 | * @author Michael T. Goodrich 31 | * @author Roberto Tamassia 32 | * @author Michael H. Goldwasser 33 | */ 34 | public class SortedPriorityQueue extends AbstractPriorityQueue { 35 | /** primary collection of priority queue entries */ 36 | private PositionalList> list = new LinkedPositionalList<>(); 37 | 38 | /** Creates an empty priority queue based on the natural ordering of its keys. */ 39 | public SortedPriorityQueue() { super(); } 40 | 41 | /** 42 | * Creates an empty priority queue using the given comparator to order keys. 43 | * @param comp comparator defining the order of keys in the priority queue 44 | */ 45 | public SortedPriorityQueue(Comparator comp) { super(comp); } 46 | 47 | /** 48 | * Inserts a key-value pair and returns the entry created. 49 | * @param key the key of the new entry 50 | * @param value the associated value of the new entry 51 | * @return the entry storing the new key-value pair 52 | * @throws IllegalArgumentException if the key is unacceptable for this queue 53 | */ 54 | @Override 55 | public Entry insert(K key, V value) throws IllegalArgumentException { 56 | checkKey(key); // auxiliary key-checking method (could throw exception) 57 | Entry newest = new PQEntry<>(key, value); 58 | Position> walk = list.last(); 59 | // walk backward, looking for smaller key 60 | while (walk != null && compare(newest, walk.getElement()) < 0) 61 | walk = list.before(walk); 62 | if (walk == null) 63 | list.addFirst(newest); // new key is smallest 64 | else 65 | list.addAfter(walk, newest); // newest goes after walk 66 | return newest; 67 | } 68 | 69 | /** 70 | * Returns (but does not remove) an entry with minimal key. 71 | * @return entry having a minimal key (or null if empty) 72 | */ 73 | @Override 74 | public Entry min() { 75 | if (list.isEmpty()) return null; 76 | return list.first().getElement(); 77 | } 78 | 79 | /** 80 | * Removes and returns an entry with minimal key. 81 | * @return the removed entry (or null if empty) 82 | */ 83 | @Override 84 | public Entry removeMin() { 85 | if (list.isEmpty()) return null; 86 | return list.remove(list.first()); 87 | } 88 | 89 | /** 90 | * Returns the number of items in the priority queue. 91 | * @return number of items 92 | */ 93 | @Override 94 | public int size() { return list.size(); } 95 | } 96 | -------------------------------------------------------------------------------- /Labs/resources/net/datastructures/SplayTreeMap.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser 3 | * 4 | * Developed for use with the book: 5 | * 6 | * Data Structures and Algorithms in Java, Sixth Edition 7 | * Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser 8 | * John Wiley & Sons, 2014 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | package net.datastructures; 24 | 25 | import java.util.Comparator; 26 | 27 | /** 28 | * An implementation of a sorted map using a splay tree. 29 | * 30 | * @author Michael T. Goodrich 31 | * @author Roberto Tamassia 32 | * @author Michael H. Goldwasser 33 | */ 34 | public class SplayTreeMap extends TreeMap { 35 | 36 | /** Constructs an empty map using the natural ordering of keys. */ 37 | public SplayTreeMap() { super(); } 38 | 39 | /** 40 | * Constructs an empty map using the given comparator to order keys. 41 | * @param comp comparator defining the order of keys in the map 42 | */ 43 | public SplayTreeMap(Comparator comp) { super(comp); } 44 | 45 | /** Utility used to rebalance after a map operation. */ 46 | private void splay(Position> p) { 47 | while (!isRoot(p)) { 48 | Position> parent = parent(p); 49 | Position> grand = parent(parent); 50 | if (grand == null) // zig case 51 | rotate(p); 52 | else if ((parent == left(grand)) == (p == left(parent))) { // zig-zig case 53 | rotate(parent); // move PARENT upward 54 | rotate(p); // then move p upward 55 | } else { // zig-zag case 56 | rotate(p); // move p upward 57 | rotate(p); // move p upward again 58 | } 59 | } 60 | } 61 | 62 | /** Overrides the TreeMap rebalancing hook that is called after a node access. */ 63 | @Override 64 | protected void rebalanceAccess(Position> p) { 65 | if (isExternal(p)) p = parent(p); 66 | if (p != null) splay(p); 67 | } 68 | 69 | /** Overrides the TreeMap rebalancing hook that is called after an insertion. */ 70 | @Override 71 | protected void rebalanceInsert(Position> p) { 72 | splay(p); 73 | } 74 | 75 | /** Overrides the TreeMap rebalancing hook that is called after a deletion. */ 76 | @Override 77 | protected void rebalanceDelete(Position> p) { 78 | if (!isRoot(p)) splay(parent(p)); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /Labs/resources/net/datastructures/Stack.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser 3 | * 4 | * Developed for use with the book: 5 | * 6 | * Data Structures and Algorithms in Java, Sixth Edition 7 | * Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser 8 | * John Wiley & Sons, 2014 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | package net.datastructures; 24 | 25 | /** 26 | * A collection of objects that are inserted and removed according to the last-in 27 | * first-out principle. Although similar in purpose, this interface differs from 28 | * java.util.Stack. 29 | * 30 | * @author Michael T. Goodrich 31 | * @author Roberto Tamassia 32 | * @author Michael H. Goldwasser 33 | */ 34 | public interface Stack { 35 | 36 | /** 37 | * Returns the number of elements in the stack. 38 | * @return number of elements in the stack 39 | */ 40 | int size(); 41 | 42 | /** 43 | * Tests whether the stack is empty. 44 | * @return true if the stack is empty, false otherwise 45 | */ 46 | boolean isEmpty(); 47 | 48 | /** 49 | * Inserts an element at the top of the stack. 50 | * @param e the element to be inserted 51 | */ 52 | void push(E e); 53 | 54 | /** 55 | * Returns, but does not remove, the element at the top of the stack. 56 | * @return top element in the stack (or null if empty) 57 | */ 58 | E top(); 59 | 60 | /** 61 | * Removes and returns the top element from the stack. 62 | * @return element removed (or null if empty) 63 | */ 64 | E pop(); 65 | } 66 | -------------------------------------------------------------------------------- /Labs/resources/net/datastructures/Tree.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser 3 | * 4 | * Developed for use with the book: 5 | * 6 | * Data Structures and Algorithms in Java, Sixth Edition 7 | * Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser 8 | * John Wiley & Sons, 2014 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | package net.datastructures; 24 | 25 | import java.util.Iterator; 26 | 27 | /** 28 | * An interface for a tree where nodes can have an arbitrary number of children. 29 | * 30 | * @author Michael T. Goodrich 31 | * @author Roberto Tamassia 32 | * @author Michael H. Goldwasser 33 | */ 34 | public interface Tree extends Iterable { 35 | 36 | /** 37 | * Returns the root Position of the tree (or null if tree is empty). 38 | * @return root Position of the tree (or null if tree is empty) 39 | */ 40 | Position root(); 41 | 42 | /** 43 | * Returns the Position of p's parent (or null if p is root). 44 | * 45 | * @param p A valid Position within the tree 46 | * @return Position of p's parent (or null if p is root) 47 | * @throws IllegalArgumentException if p is not a valid Position for this tree. 48 | */ 49 | Position parent(Position p) throws IllegalArgumentException; 50 | 51 | /** 52 | * Returns an iterable collection of the Positions representing p's children. 53 | * 54 | * @param p A valid Position within the tree 55 | * @return iterable collection of the Positions of p's children 56 | * @throws IllegalArgumentException if p is not a valid Position for this tree. 57 | */ 58 | Iterable> children(Position p) 59 | throws IllegalArgumentException; 60 | 61 | /** 62 | * Returns the number of children of Position p. 63 | * 64 | * @param p A valid Position within the tree 65 | * @return number of children of Position p 66 | * @throws IllegalArgumentException if p is not a valid Position for this tree. 67 | */ 68 | int numChildren(Position p) throws IllegalArgumentException; 69 | 70 | /** 71 | * Returns true if Position p has one or more children. 72 | * 73 | * @param p A valid Position within the tree 74 | * @return true if p has at least one child, false otherwise 75 | * @throws IllegalArgumentException if p is not a valid Position for this tree. 76 | */ 77 | boolean isInternal(Position p) throws IllegalArgumentException; 78 | 79 | /** 80 | * Returns true if Position p does not have any children. 81 | * 82 | * @param p A valid Position within the tree 83 | * @return true if p has zero children, false otherwise 84 | * @throws IllegalArgumentException if p is not a valid Position for this tree. 85 | */ 86 | boolean isExternal(Position p) throws IllegalArgumentException; 87 | 88 | /** 89 | * Returns true if Position p represents the root of the tree. 90 | * 91 | * @param p A valid Position within the tree 92 | * @return true if p is the root of the tree, false otherwise 93 | * @throws IllegalArgumentException if p is not a valid Position for this tree. 94 | */ 95 | boolean isRoot(Position p) throws IllegalArgumentException; 96 | 97 | /** 98 | * Returns the number of nodes in the tree. 99 | * @return number of nodes in the tree 100 | */ 101 | int size(); 102 | 103 | /** 104 | * Tests whether the tree is empty. 105 | * @return true if the tree is empty, false otherwise 106 | */ 107 | boolean isEmpty(); 108 | 109 | /** 110 | * Returns an iterator of the elements stored in the tree. 111 | * @return iterator of the tree's elements 112 | */ 113 | Iterator iterator(); 114 | 115 | /** 116 | * Returns an iterable collection of the positions of the tree. 117 | * @return iterable collection of the tree's positions 118 | */ 119 | Iterable> positions(); 120 | } 121 | -------------------------------------------------------------------------------- /Labs/resources/net/datastructures/UnsortedPriorityQueue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser 3 | * 4 | * Developed for use with the book: 5 | * 6 | * Data Structures and Algorithms in Java, Sixth Edition 7 | * Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser 8 | * John Wiley & Sons, 2014 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | package net.datastructures; 24 | 25 | import java.util.Comparator; 26 | 27 | /** 28 | * An implementation of a priority queue with an unsorted list. 29 | * 30 | * @author Michael T. Goodrich 31 | * @author Roberto Tamassia 32 | * @author Michael H. Goldwasser 33 | */ 34 | public class UnsortedPriorityQueue extends AbstractPriorityQueue { 35 | /** primary collection of priority queue entries */ 36 | private PositionalList> list = new LinkedPositionalList<>(); 37 | 38 | /** Creates an empty priority queue based on the natural ordering of its keys. */ 39 | public UnsortedPriorityQueue() { super(); } 40 | 41 | /** 42 | * Creates an empty priority queue using the given comparator to order keys. 43 | * @param comp comparator defining the order of keys in the priority queue 44 | */ 45 | public UnsortedPriorityQueue(Comparator comp) { super(comp); } 46 | 47 | /** 48 | * Returns the Position of an entry having minimal key. 49 | * This should only be called on a nonempty priority queue 50 | * @return Position of entry with minimal key 51 | */ 52 | private Position> findMin() { // only called when nonempty 53 | Position> small = list.first(); 54 | for (Position> walk : list.positions()) 55 | if (compare(walk.getElement(), small.getElement()) < 0) 56 | small = walk; // found an even smaller key 57 | return small; 58 | } 59 | 60 | /** 61 | * Inserts a key-value pair and returns the entry created. 62 | * @param key the key of the new entry 63 | * @param value the associated value of the new entry 64 | * @return the entry storing the new key-value pair 65 | * @throws IllegalArgumentException if the key is unacceptable for this queue 66 | */ 67 | @Override 68 | public Entry insert(K key, V value) throws IllegalArgumentException { 69 | checkKey(key); // auxiliary key-checking method (could throw exception) 70 | Entry newest = new PQEntry<>(key, value); 71 | list.addLast(newest); 72 | return newest; 73 | } 74 | 75 | /** 76 | * Returns (but does not remove) an entry with minimal key. 77 | * @return entry having a minimal key (or null if empty) 78 | */ 79 | @Override 80 | public Entry min() { 81 | if (list.isEmpty()) return null; 82 | return findMin().getElement(); 83 | } 84 | 85 | /** 86 | * Removes and returns an entry with minimal key. 87 | * @return the removed entry (or null if empty) 88 | */ 89 | @Override 90 | public Entry removeMin() { 91 | if (list.isEmpty()) return null; 92 | return list.remove(findMin()); 93 | } 94 | 95 | /** 96 | * Returns the number of items in the priority queue. 97 | * @return number of items 98 | */ 99 | @Override 100 | public int size() { return list.size(); } 101 | } 102 | -------------------------------------------------------------------------------- /Labs/resources/net/datastructures/Vertex.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014, Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser 3 | * 4 | * Developed for use with the book: 5 | * 6 | * Data Structures and Algorithms in Java, Sixth Edition 7 | * Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser 8 | * John Wiley & Sons, 2014 9 | * 10 | * This program is free software: you can redistribute it and/or modify 11 | * it under the terms of the GNU General Public License as published by 12 | * the Free Software Foundation, either version 3 of the License, or 13 | * (at your option) any later version. 14 | * 15 | * This program is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public License 21 | * along with this program. If not, see . 22 | */ 23 | package net.datastructures; 24 | 25 | /** 26 | * A vertex of a graph. 27 | * 28 | * @author Michael T. Goodrich 29 | * @author Roberto Tamassia 30 | * @author Michael H. Goldwasser 31 | */ 32 | public interface Vertex { 33 | /** Returns the element associated with the vertex. */ 34 | V getElement(); 35 | } 36 | -------------------------------------------------------------------------------- /Lectures/Tests/test.java: -------------------------------------------------------------------------------- 1 | public class test { 2 | 3 | public static void main(String[] args) { 4 | } 5 | 6 | } 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | CSI2110 2 | ====== 3 | 4 | My lab assignments for CSI2110 at the University of Ottawa. Closed source until the end of fall 2015. Please use these assignments for learning purposes only and do not use them to cheat as that is illegal. 5 | 6 | 2015 - Matthew Langlois 7 | --------------------------------------------------------------------------------