diff --git a/MyList/MyList.java b/MyList/MyList.java index 46896ae..a0f53d9 100644 --- a/MyList/MyList.java +++ b/MyList/MyList.java @@ -1,4 +1,5 @@ package MyList; + public class MyList { public MyListElement first; private int size = 0; @@ -7,82 +8,82 @@ public class MyList { first = null; } - //Adds elem to list. - public void add(MyPair pair) { + // Adds elem to list. + public void add(MyPair pair) { MyListElement elem = new MyListElement(pair, null); - //Case 1: List is empty + // Case 1: List is empty if (this.first == null) { this.first = elem; - //Case 2: List is not empty + // Case 2: List is not empty } else { MyListElement node = this.first; - while (node.next != null) { - node = node.next; + while (node.getNext() != null) { + node = node.getNext(); } - node.next = elem; + node.setNext(elem); } size++; } - //Inserts elem at index. - public void insert(MyPair pair, int index) { + // Inserts elem at index.TODO only works for 0 atm + public void insert(MyPair pair, int index) { MyListElement node = this.first; MyListElement pre = this.first; if (index == 0) { this.first = new MyListElement(pair, node); } else { int i = 1; - while ((index != i) && (node.next != null)) { + while ((index != i) && (node.getNext() != null)) { pre = node; - node = node.next; + node = node.getNext(); i++; } } size++; } - //Removes element with key from list. + // Removes element with key from list. public void remove(int key) { - //Case 1: Element to remove is first - if (this.first.data.key == key) { - this.first = this.first.next; - //Case 2: Element to remove is not first + // Case 1: Element to remove is first + if (this.first.getData().getKey() == key) { + this.first = this.first.getNext(); + // Case 2: Element to remove is not first } else { MyListElement node = this.first; MyListElement pre = this.first; - while ((node.next != null) && (node.data.key != key)) { + while ((node.getNext() != null) && (node.getData().getKey() != key)) { pre = node; - node = node.next; - if (node.data.key == key) { - pre.next = node.next; + node = node.getNext(); + if (node.getData().getKey() == key) { + pre.setNext(node.getNext()); } } } size--; } - //Return MyList as String. + // Return MyList as String. @Override public String toString() { String listAsString = ""; MyListElement node = this.first; while (node != null) { - listAsString += (node.data.toString() + ", "); - node = node.next; + listAsString += (node.getData().toString() + ", "); + node = node.getNext(); } return listAsString; } - public MyPair[] toArray() { - MyPair[] listAsArray = new MyPair[this.size]; + public MyPair[] toArray() { + MyPair[] listAsArray = new MyPair[this.size]; MyListElement node = this.first; int i = 0; - while (node.next != null) { - listAsArray[i] = node.data; - node = node.next; + while (node.getNext() != null) { + listAsArray[i] = node.getData(); + node = node.getNext(); i++; } - listAsArray[i] = node.data; + listAsArray[i] = node.getData(); return listAsArray; } diff --git a/MyList/MyListElement.java b/MyList/MyListElement.java index c13c10f..cb3f169 100644 --- a/MyList/MyListElement.java +++ b/MyList/MyListElement.java @@ -1,14 +1,15 @@ package MyList; + public class MyListElement { - public MyPair data; - public MyListElement next; + private MyPair data; + private MyListElement next; public MyListElement() { data = null; next = null; } - public MyListElement(MyPair data, MyListElement next) { + public MyListElement(MyPair data, MyListElement next) { this.data = data; this.next = next; } @@ -17,4 +18,20 @@ public class MyListElement { public String toString() { return this.data.toString(); } + + public MyPair getData() { + return this.data; + } + + public MyListElement getNext() { + return this.next; + } + + public void setData(MyPair data) { + this.data = data; + } + + public void setNext(MyListElement next) { + this.next = next; + } } diff --git a/MyList/MyPair.java b/MyList/MyPair.java index 599b60a..8c85f61 100644 --- a/MyList/MyPair.java +++ b/MyList/MyPair.java @@ -1,24 +1,32 @@ package MyList; -public class MyPair { - public int key; - public Object data; - public MyPair() { - key = -1; - data = new Object(); - } +public class MyPair { + private K key; + private V value; - public MyPair(int key, Object data) { + public MyPair(K key, V value) { this.key = key; - this.data = data; + this.value = value; } @Override public String toString() { - return ("(" + Integer.toString(this.key) + "," + this.data.toString() + ")"); + return ("(" + this.key + "," + this.value + ")"); } - public int getKeyAsInt() { - return (int)this.key; + public K getKey() { + return this.key; + } + + public V getValue() { + return this.value; + } + + public void setKey(K key) { + this.key = key; + } + + public void setValue(V value) { + this.value = value; } } diff --git a/MySearch/MySearchAlgorithms.java b/MySearch/MySearchAlgorithms.java index 67035ef..adab734 100644 --- a/MySearch/MySearchAlgorithms.java +++ b/MySearch/MySearchAlgorithms.java @@ -11,11 +11,11 @@ public class MySearchAlgorithms { public static MyPair sequentielSearchList(int oKey, MyList oList) { MyPair oData = null; MyListElement elem = oList.first; - while ((elem.next != null) && (elem.data.key != oKey)) { - elem = elem.next; + while ((elem.getNext() != null) && (elem.getData().getKey() != oKey)) { + elem = elem.getNext(); } - if (elem.data.key == oKey) { - oData = elem.data; + if (elem.getData().getKey() == oKey) { + oData = elem.getData(); } return oData; } @@ -30,9 +30,9 @@ public class MySearchAlgorithms { private static MyPair helpBinarySearch(Object oKey, int iLeft, int iRight, MyPair[] oArray) { MyPair oData = null; int m = (int)(iLeft + iRight) / 2; - if (oArray[m].getKeyAsInt() > (int)oKey) { + if ((int)(oArray[m].getKey()) > (int)oKey) { oData = helpBinarySearch(oKey, iLeft, (m - 1), oArray); - } else if (oArray[m].getKeyAsInt() < (int)oKey) { + } else if ((int)(oArray[m].getKey()) < (int)oKey) { oData = helpBinarySearch(oKey, (m + 1), iRight, oArray); } else { oData = oArray[m]; @@ -48,10 +48,10 @@ public class MySearchAlgorithms { int iR = 0; for (int i = 0; i < oArray.length; i++) { int iIndex = ((int)Math.pow(2, i)); - if ((int)oArray[iIndex].key > (int)oKey) { + if ((int)oArray[iIndex].getKey() > (int)oKey) { iR = iIndex; break; - } else if ((int)oArray[iIndex].key == (int)oKey) { + } else if ((int)oArray[iIndex].getKey() == (int)oKey) { oData = oArray[iIndex]; bSuccess = true; break; diff --git a/MySort/MySortAlgorithms.java b/MySort/MySortAlgorithms.java index d92c393..2d21ed1 100644 --- a/MySort/MySortAlgorithms.java +++ b/MySort/MySortAlgorithms.java @@ -20,7 +20,7 @@ public class MySortAlgorithms { public static void myBubbleSortInSitu(MyPair[] arr) { for (int i = 0; i < arr.length; i++) { for (int j = 0; (j + 1) < arr.length - i; j++) { - if (arr[j].getKeyAsInt() > arr[j + 1].getKeyAsInt()) { + if ((int)(arr[j].getKey()) > (int)(arr[j + 1].getKey())) { MyPair store = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = store; @@ -42,7 +42,7 @@ public class MySortAlgorithms { //Sorting starts here for (int i = 0; i < sortedSequence.length; i++) { for (int j = 0; (j + 1) < sortedSequence.length - i; j++) { - if (sortedSequence[j].getKeyAsInt() > sortedSequence[j + 1].getKeyAsInt()) { + if ((int)(sortedSequence[j].getKey()) > (int)(sortedSequence[j + 1].getKey())) { MyPair store = sortedSequence[j]; sortedSequence[j] = sortedSequence[j + 1]; sortedSequence[j + 1] = store; @@ -52,6 +52,32 @@ public class MySortAlgorithms { return sortedSequence; } + /** + * TODO + * @param arr + */ + public static void myBubbleSortRecursiveInSitu(MyPair[] arr, int iStartIndex) { + if (iStartIndex + 1 < arr.length) { + myBubbleSortRecursiveInSitu(arr, iStartIndex + 1); + + if ((int)(arr[iStartIndex].getKey()) > (int)(arr[iStartIndex + 1].getKey())) { + myBubbleSortRecursiveInSituSwap(arr, iStartIndex); + } + } + } + + /** + * + * @param arr + * @param i + * @param j + */ + private static void myBubbleSortRecursiveInSituSwap(MyPair[] arr, int i) { + MyPair store = arr[i]; + arr[i] = arr[i + 1]; + arr[i + 1] = store; + } + /** * * @param arr @@ -59,11 +85,11 @@ public class MySortAlgorithms { public static void mySelectionSortInSitu(MyPair[] arr) { for (int i = 0; i < arr.length; i++) { int smallestIndex = arr.length - 1; - int smallestKey = arr[arr.length - 1].getKeyAsInt(); + int smallestKey = (int)(arr[arr.length - 1].getKey()); for (int j = (arr.length - 1); j >= i; j--) { - if (arr[j].getKeyAsInt() < smallestKey) { + if ((int)(arr[j].getKey()) < smallestKey) { smallestIndex = j; - smallestKey = arr[j].getKeyAsInt(); + smallestKey = (int)(arr[j].getKey()); } } MyPair store = arr[i]; @@ -86,11 +112,11 @@ public class MySortAlgorithms { //Sorting starts here for (int i = 0; i < sortedSequence.length; i++) { int smallestIndex = sortedSequence.length - 1; - int smallestKey = sortedSequence[sortedSequence.length - 1].getKeyAsInt(); + int smallestKey = (int)(sortedSequence[sortedSequence.length - 1].getKey()); for (int j = (sortedSequence.length - 1); j >= i; j--) { - if (sortedSequence[j].getKeyAsInt() < smallestKey) { + if ((int)(sortedSequence[j].getKey()) < smallestKey) { smallestIndex = j; - smallestKey = sortedSequence[j].getKeyAsInt(); + smallestKey = (int)(sortedSequence[j].getKey()); } } MyPair store = sortedSequence[i]; @@ -115,7 +141,7 @@ public class MySortAlgorithms { for (int i = 0; i < arr.length; i++) { int index = 0; for (int j = 0; j <= i; j++) { - if (arr[j].getKeyAsInt() >= arr[i].getKeyAsInt()) { + if ((int)(arr[j].getKey()) >= (int)(arr[i].getKey())) { index = j; } } @@ -153,7 +179,7 @@ public class MySortAlgorithms { int unpartitionedAreaLeft = left; int pivot = right; for (int k = 0; k < arr.length; k++) { - if (arr[unpartitionedAreaLeft].getKeyAsInt() <= arr[pivot].getKeyAsInt()) { + if ((int)(arr[unpartitionedAreaLeft].getKey()) <= (int)(arr[pivot].getKey())) { MyPair store = arr[unpartitionedAreaLeft]; arr[unpartitionedAreaLeft] = arr[smallerPivotIndexRight + 1]; arr[smallerPivotIndexRight + 1] = store; diff --git a/MyTest/MyTestList.java b/MyTest/MyTestList.java index e3c8827..22cade6 100644 --- a/MyTest/MyTestList.java +++ b/MyTest/MyTestList.java @@ -10,14 +10,14 @@ public class MyTestList { @Test public void testToArray() { MyList list = new MyList(); - list.add(new MyPair(0,0)); - list.add(new MyPair(1,1)); - list.add(new MyPair(2,2)); - list.add(new MyPair(3,3)); - list.add(new MyPair(4,4)); - MyPair[] listAsArray = list.toArray(); + list.add(new MyPair(0, 0)); + list.add(new MyPair(1, 1)); + list.add(new MyPair(2, 2)); + list.add(new MyPair(3, 3)); + list.add(new MyPair(4, 4)); + MyPair[] listAsArray = list.toArray(); for (int i = 0; i < listAsArray.length; i++) { - assertEquals(i, listAsArray[i].key); + assertEquals(i, (int) listAsArray[i].getKey()); } } } diff --git a/MyTest/MyTestSearch.java b/MyTest/MyTestSearch.java index 8a86091..798f16b 100644 --- a/MyTest/MyTestSearch.java +++ b/MyTest/MyTestSearch.java @@ -12,36 +12,26 @@ public class MyTestSearch { @Test public void testSequentialSearch() { MyList list = new MyList(); - list.add(new MyPair(1,3)); - list.add(new MyPair(2,4)); - - assertEquals(3, MySearchAlgorithms.sequentielSearchList(1, list).data); - assertEquals(4, MySearchAlgorithms.sequentielSearchList(2, list).data); + list.add(new MyPair(1, 3)); + list.add(new MyPair(2, 4)); + + assertEquals(3, MySearchAlgorithms.sequentielSearchList(1, list).getValue()); + assertEquals(4, MySearchAlgorithms.sequentielSearchList(2, list).getValue()); } - + @Test public void testBinarySearch() { - MyPair[] pairs = {new MyPair(1,3), new MyPair(2,4)}; + MyPair[] pairs = { new MyPair(1, 3), new MyPair(2, 4) }; - assertEquals(3, MySearchAlgorithms.binarySearch(1, pairs).data); - assertEquals(4, MySearchAlgorithms.binarySearch(2, pairs).data); + assertEquals(3, MySearchAlgorithms.binarySearch(1, pairs).getValue()); + assertEquals(4, MySearchAlgorithms.binarySearch(2, pairs).getValue()); } @Test public void testExponentialSearch() { - MyPair[] pairs = {new MyPair(1,3), new MyPair(2,4)}; + MyPair[] pairs = { new MyPair(1, 3), new MyPair(2, 4) }; - assertEquals(3, MySearchAlgorithms.exponentialSearch(1, pairs).data); - assertEquals(4, MySearchAlgorithms.exponentialSearch(2, pairs).data); - } - - @Test - public void testBubbleSortInSitu() { - //Test BubbleSortInSitu - MyPair[] arrPairs0 = {new MyPair(2,2), new MyPair(0,0), new MyPair(1,1), new MyPair(3,3)}; - MySortAlgorithms.myBubbleSortInSitu(arrPairs0); - for (int i = 0; i < arrPairs0.length; i++) { - assertEquals(i, arrPairs0[i].getKeyAsInt()); - } + assertEquals(3, MySearchAlgorithms.exponentialSearch(1, pairs).getValue()); + assertEquals(4, MySearchAlgorithms.exponentialSearch(2, pairs).getValue()); } } diff --git a/MyTest/MyTestSort.java b/MyTest/MyTestSort.java index c812bf4..8b8a62d 100644 --- a/MyTest/MyTestSort.java +++ b/MyTest/MyTestSort.java @@ -12,6 +12,15 @@ import MyTrees.MyTreeNode; public class MyTestSort { + @Test + public void testBubbleSortRecursiveInSitu() { + //Test BubbleSortNotInSitu + MyPair[] arrPairs0 = {new MyPair(2,2), new MyPair(0,0), new MyPair(1,1), new MyPair(3,3)}; + MySortAlgorithms.myBubbleSortRecursiveInSitu(arrPairs0, 0); + for (int i = 0; i < arrPairs0.length; i++) { + assertEquals(i, (int)(arrPairs0[i].getKey())); + } + } @Test public void testBubbleSortNotInSitu() { @@ -19,7 +28,7 @@ public class MyTestSort { MyPair[] arrPairs0 = {new MyPair(2,2), new MyPair(0,0), new MyPair(1,1), new MyPair(3,3)}; MyPair[] arrPairs1 = MySortAlgorithms.myBubbleSortNotInSitu(arrPairs0); for (int i = 0; i < arrPairs1.length; i++) { - assertEquals(i, arrPairs1[i].getKeyAsInt()); + assertEquals(i, (int)(arrPairs1[i].getKey())); } } @@ -29,7 +38,7 @@ public class MyTestSort { MyPair[] arrPairs0 = {new MyPair(2,2), new MyPair(0,0), new MyPair(1,1), new MyPair(3,3)}; MySortAlgorithms.mySelectionSortInSitu(arrPairs0); for (int i = 0; i < arrPairs0.length; i++) { - assertEquals(i, arrPairs0[i].getKeyAsInt()); + assertEquals(i, (int)(arrPairs0[i].getKey())); } } @@ -39,11 +48,18 @@ public class MyTestSort { MyPair[] arrPairs0 = {new MyPair(2,2), new MyPair(0,0), new MyPair(1,1), new MyPair(3,3)}; MyPair[] arrPairs1 = MySortAlgorithms.mySelectionSortNotInSitu(arrPairs0); for (int i = 0; i < arrPairs0.length; i++) { - assertEquals(i, arrPairs1[i].getKeyAsInt()); + assertEquals(i, (int)(arrPairs1[i].getKey())); } } - - - + @Test + public void testBubbleSortInSitu() { + // Test BubbleSortInSitu + MyPair[] arrPairs0 = { new MyPair(2, 2), new MyPair(0, 0), + new MyPair(1, 1), new MyPair(3, 3) }; + MySortAlgorithms.myBubbleSortInSitu(arrPairs0); + for (int i = 0; i < arrPairs0.length; i++) { + assertEquals(i, (int) arrPairs0[i].getKey()); + } + } } \ No newline at end of file diff --git a/MyTrees/MyBinarySearchTree.java b/MyTrees/MyBinarySearchTree.java index f4ca918..87b4955 100644 --- a/MyTrees/MyBinarySearchTree.java +++ b/MyTrees/MyBinarySearchTree.java @@ -20,12 +20,12 @@ public class MyBinarySearchTree extends MyBinaryTree { public Object lookup(MyTreeNode node, Object key) { Object value = null; if (node != null) { - if ((int)key < node.getKeyAsInt()) { + if ((int) key < node.getKey()) { value = lookup(node.getLeftChild(), key); - } else if ((int)key > node.getKeyAsInt()) { + } else if ((int) key > node.getKey()) { value = lookup(node.getRightChild(), key); } else { - value = node.getPair().data; + value = node.getPair().getValue(); } } return value; @@ -34,16 +34,17 @@ public class MyBinarySearchTree extends MyBinaryTree { /** * TODO doesnt work??? * Inserts/replaces new pair into tree specified by root. + * * @param node (root) startingpoint to search for node specified by pair.key * @param pair datatype which contains key and data of new inserted node. */ - public void insert(MyTreeNode node, MyPair pair) { - if (node.getKeyAsInt() == -1) { - node = new MyTreeNode(pair.key, pair.data); + public void insert(MyTreeNode node, MyPair pair) { + if (node.getKey() == -1) { + node = new MyTreeNode(pair.getKey(), pair.getValue()); } else { - if (node.getKeyAsInt() == pair.getKeyAsInt()) { + if (node.getKey() == pair.getKey()) { node.setPair(pair); - } else if (node.getKeyAsInt() < pair.getKeyAsInt()) { + } else if (node.getKey() < pair.getKey()) { insert(node.getLeftChild(), pair); } else { insert(node.getRightChild(), pair); @@ -53,43 +54,46 @@ public class MyBinarySearchTree extends MyBinaryTree { /** * Removes node specified by key. + * * @param node starting point of search for node to remove. - * @param key specifies node to remove. + * @param key specifies node to remove. */ public void remove(MyTreeNode node, Object key) { if (node != null) { - if (node.getKeyAsInt() < (int)key) { + if (node.getKey() < (int) key) { remove(node.getLeftChild(), key); - } else if (node.getKeyAsInt() > (int)key) { + } else if (node.getKey() > (int) key) { remove(node.getRightChild(), key); } else { - //If node has no childs, set node to null + // If node has no childs, set node to null if ((node.getLeftChild() == null) && (node.getRightChild() == null)) { node = null; - //If node has only left child, set node to left child + // If node has only left child, set node to left child } else if ((node.getLeftChild() != null) && (node.getRightChild() == null)) { node = node.getLeftChild(); - //If node has only right child, set node to right child + // If node has only right child, set node to right child } else if ((node.getLeftChild() == null) && (node.getRightChild() != null)) { node = node.getRightChild(); - //If node has two childs + // If node has two childs } else { - //Get right parttree + // Get right parttree MyTreeNode nnode = node.getRightChild(); - //Move left through right parttree till node before symmetric follower. + // Move left through right parttree till node before symmetric follower. while (nnode.getLeftChild().getLeftChild() != null) { nnode = nnode.getLeftChild(); } - //Store data pair of symmetric follower - MyPair symmetricFollowerPair = nnode.getLeftChild().getPair(); - //If symmetric follower has right child: replace symmetric follower with right child + // Store data pair of symmetric follower + MyPair symmetricFollowerPair = nnode.getLeftChild().getPair(); + // If symmetric follower has right child: replace symmetric follower with right + // child if (nnode.getLeftChild().getRightChild() != null) { nnode.setLeftChild(nnode.getLeftChild().getRightChild()); - //If symmetric follower has no childs: set symmetric follower node to null (removed). + // If symmetric follower has no childs: set symmetric follower node to null + // (removed). } else { nnode.setLeftChild(null); } - //Replace data of node with data of symmetric follower. + // Replace data of node with data of symmetric follower. node.setPair(symmetricFollowerPair); } } @@ -98,11 +102,12 @@ public class MyBinarySearchTree extends MyBinaryTree { /** * Searches minimum key. + * * @param node root of tree to search at. * @return pair of most left node of tree. */ - public MyPair minimum(MyTreeNode node) { - MyPair min = null; + public MyPair minimum(MyTreeNode node) { + MyPair min = null; MyTreeNode nnode = node; if (node != null) { while (nnode.getLeftChild() != null) { @@ -115,11 +120,12 @@ public class MyBinarySearchTree extends MyBinaryTree { /** * Searches maximum key. + * * @param node root of tree to search at. * @return pair of most right node of tree. */ - public MyPair maximum(MyTreeNode node) { - MyPair max = null; + public MyPair maximum(MyTreeNode node) { + MyPair max = null; MyTreeNode nnode = node; if (node != null) { while (nnode.getRightChild() != null) { diff --git a/MyTrees/MyTreeNode.java b/MyTrees/MyTreeNode.java index 3adc46e..781ff19 100644 --- a/MyTrees/MyTreeNode.java +++ b/MyTrees/MyTreeNode.java @@ -5,23 +5,21 @@ import MyList.MyPair; public class MyTreeNode { private MyTreeNode leftChild; private MyTreeNode rightChild; - private MyPair pair; + private MyPair pair; public MyTreeNode() { this.leftChild = null; this.rightChild = null; - this.pair = new MyPair(); + this.pair = new MyPair(null, null); } - public MyTreeNode(int key, Object data) { + public MyTreeNode(Integer key, Integer value) { this.leftChild = null; this.rightChild = null; - this.pair = new MyPair(); - this.pair.key = key; - this.pair.data = data; + this.pair = new MyPair(key, value); } - //David Setter Methods + // David Setter Methods public void setLeftChild(MyTreeNode leftNode) { this.leftChild = leftNode; } @@ -30,11 +28,11 @@ public class MyTreeNode { this.rightChild = rightNode; } - public void setPair(MyPair pair) { - this.pair = new MyPair(pair.key, pair.data); + public void setPair(MyPair pair) { + this.pair = new MyPair(pair.getKey(), pair.getValue()); } - //David Getter Methods + // David Getter Methods public MyTreeNode getLeftChild() { return this.leftChild; } @@ -43,11 +41,11 @@ public class MyTreeNode { return this.rightChild; } - public MyPair getPair() { + public MyPair getPair() { return this.pair; } - public int getKeyAsInt() { - return this.pair.getKeyAsInt(); + public Integer getKey() { + return this.pair.getKey(); } } \ No newline at end of file