From 5fdb8b9dd409f0796797526b07ecd26fb9df083b Mon Sep 17 00:00:00 2001 From: Meruemon Date: Sun, 3 Jul 2022 16:20:55 +0200 Subject: [PATCH] 0006_Sixth push added[some stuff...] --- MyFunctionalDatastructures/MyTrie.java | 50 +++++++ MyHashing/MyHashfunctions.java | 20 +++ MyList/MyList.java | 15 +- MyList/MyPair.java | 10 +- MyMath/MyMath.java | 7 + ...lgorithms.java => MySearchAlgorithms.java} | 4 +- ..._algorithms.java => MySortAlgorithms.java} | 2 +- MyTest/MyTest.java | 77 ---------- MyTest/MyTestBinarySearchTree.java | 30 ++++ MyTest/MyTestFunctionalDatastructures.java | 15 ++ MyTest/MyTestList.java | 23 +++ MyTest/MyTestSearch.java | 47 +++++++ MyTest/MyTestSort.java | 49 +++++++ MyTrees/MyBinarySearchTree.java | 132 ++++++++++++++++++ MyTrees/MyBinaryTree.java | 47 ++++++- MyTrees/MyMinHeapTree.java | 2 +- MyTrees/MyTreeNode.java | 34 ++--- 17 files changed, 459 insertions(+), 105 deletions(-) create mode 100644 MyFunctionalDatastructures/MyTrie.java create mode 100644 MyHashing/MyHashfunctions.java create mode 100644 MyMath/MyMath.java rename MySearch/{search_algorithms.java => MySearchAlgorithms.java} (94%) rename MySort/{sort_algorithms.java => MySortAlgorithms.java} (99%) delete mode 100644 MyTest/MyTest.java create mode 100644 MyTest/MyTestBinarySearchTree.java create mode 100644 MyTest/MyTestFunctionalDatastructures.java create mode 100644 MyTest/MyTestList.java create mode 100644 MyTest/MyTestSearch.java create mode 100644 MyTest/MyTestSort.java create mode 100644 MyTrees/MyBinarySearchTree.java diff --git a/MyFunctionalDatastructures/MyTrie.java b/MyFunctionalDatastructures/MyTrie.java new file mode 100644 index 0000000..995d485 --- /dev/null +++ b/MyFunctionalDatastructures/MyTrie.java @@ -0,0 +1,50 @@ +package MyFunctionalDatastructures; + +import MyList.MyPair; +import MyMath.MyMath; +import MyTrees.MyTreeNode; + +public class MyTrie { + private MyTreeNode root; + private int size; + + public MyTrie() { + root = null; + size = 0; + } + + public MyTrie(MyTreeNode root) { + this.root = root; + size = 1; + } + + public MyPair get(int i) { + MyTreeNode node = this.root; + if (i < size) { + String iAsString = Integer.toBinaryString(i); + for (int j = 0; j < iAsString.length() - 1; j++) { + if (iAsString.substring(j, j + 1).equals("0")) { + if (node.getLeftChild() != null) { + node = node.getLeftChild(); + } + } else { + if (node.getRightChild() != null) { + node = node.getRightChild(); + } + } + } + } + return node.getPair(); + } + + public MyTrie add(MyPair pair) { + MyTrie newTrie = new MyTrie(new MyTreeNode()); + //If tree is full (size is power of two) + if ((MyMath.myLog2(size) % 1) == 0) { + + } else { + + } + return newTrie; + } +} diff --git a/MyHashing/MyHashfunctions.java b/MyHashing/MyHashfunctions.java new file mode 100644 index 0000000..4f2f0b6 --- /dev/null +++ b/MyHashing/MyHashfunctions.java @@ -0,0 +1,20 @@ +package MyHashing; + +public class MyHashfunctions { + + public static double goldenCut = 0.6180339887; + + public static int myDivisionRestRemainderMethod(int k) { + return k % 43; + } + + public static int myMultiplicativeMethod(int k) { + return (int)(32 * ((goldenCut * k) - (int)(goldenCut * k))); + } + + //(int)(Math.random() * (iMax - iMin + 1) + iMin); + public static int myRandomHashMethod(int k) { + double A = ((Math.random() * 1.9) + 0.1); + return (int)(32 * ((A * k) - (int)(A * k))); + } +} diff --git a/MyList/MyList.java b/MyList/MyList.java index 4018e92..46896ae 100644 --- a/MyList/MyList.java +++ b/MyList/MyList.java @@ -42,7 +42,7 @@ public class MyList { } //Removes element with key from list. - public void remove(Object key) { + public void remove(int key) { //Case 1: Element to remove is first if (this.first.data.key == key) { this.first = this.first.next; @@ -73,6 +73,19 @@ public class MyList { return listAsString; } + 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; + i++; + } + listAsArray[i] = node.data; + return listAsArray; + } + public int getSize() { return this.size; } diff --git a/MyList/MyPair.java b/MyList/MyPair.java index 40e1ca4..599b60a 100644 --- a/MyList/MyPair.java +++ b/MyList/MyPair.java @@ -1,21 +1,21 @@ package MyList; public class MyPair { - public Object key; + public int key; public Object data; public MyPair() { - key = null; - data = null; + key = -1; + data = new Object(); } - public MyPair(Object key, Object data) { + public MyPair(int key, Object data) { this.key = key; this.data = data; } @Override public String toString() { - return ("(" + this.key.toString() + "," + this.data.toString() + ")"); + return ("(" + Integer.toString(this.key) + "," + this.data.toString() + ")"); } public int getKeyAsInt() { diff --git a/MyMath/MyMath.java b/MyMath/MyMath.java new file mode 100644 index 0000000..2b7645d --- /dev/null +++ b/MyMath/MyMath.java @@ -0,0 +1,7 @@ +package MyMath; + +public class MyMath { + public static int myLog2(int i) { + return (int)(Math.log(i) / Math.log(2)); + } +} diff --git a/MySearch/search_algorithms.java b/MySearch/MySearchAlgorithms.java similarity index 94% rename from MySearch/search_algorithms.java rename to MySearch/MySearchAlgorithms.java index b8fe14f..67035ef 100644 --- a/MySearch/search_algorithms.java +++ b/MySearch/MySearchAlgorithms.java @@ -5,10 +5,10 @@ import MyList.MyList; import MyList.MyListElement; import MyList.MyPair; -public class search_algorithms { +public class MySearchAlgorithms { //Data doesnt have to be sorted. - public static MyPair sequentielSearchList(Object oKey, MyList oList) { + public static MyPair sequentielSearchList(int oKey, MyList oList) { MyPair oData = null; MyListElement elem = oList.first; while ((elem.next != null) && (elem.data.key != oKey)) { diff --git a/MySort/sort_algorithms.java b/MySort/MySortAlgorithms.java similarity index 99% rename from MySort/sort_algorithms.java rename to MySort/MySortAlgorithms.java index b8b8c98..d92c393 100644 --- a/MySort/sort_algorithms.java +++ b/MySort/MySortAlgorithms.java @@ -1,7 +1,7 @@ package MySort; import MyList.MyPair; -public class sort_algorithms { +public class MySortAlgorithms { /** * BubbleSort in-situ diff --git a/MyTest/MyTest.java b/MyTest/MyTest.java deleted file mode 100644 index 3c5b5d8..0000000 --- a/MyTest/MyTest.java +++ /dev/null @@ -1,77 +0,0 @@ -package MyTest; -import static org.junit.Assert.assertEquals; - -import org.junit.Test; - -import MyList.MyList; -import MyList.MyPair; -import MySearch.search_algorithms; -import MySort.sort_algorithms; - -public class MyTest { - @Test - public void testSequentialSearch() { - MyList list = new MyList(); - list.add(new MyPair(1,3)); - list.add(new MyPair(2,4)); - - assertEquals(3, search_algorithms.sequentielSearchList(1, list).data); - assertEquals(4, search_algorithms.sequentielSearchList(2, list).data); - } - - @Test - public void testBinarySearch() { - MyPair[] pairs = {new MyPair(1,3), new MyPair(2,4)}; - - assertEquals(3, search_algorithms.binarySearch(1, pairs).data); - assertEquals(4, search_algorithms.binarySearch(2, pairs).data); - } - - @Test - public void testExponentialSearch() { - MyPair[] pairs = {new MyPair(1,3), new MyPair(2,4)}; - - assertEquals(3, search_algorithms.exponentialSearch(1, pairs).data); - assertEquals(4, search_algorithms.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)}; - sort_algorithms.myBubbleSortInSitu(arrPairs0); - for (int i = 0; i < arrPairs0.length; i++) { - assertEquals(i, arrPairs0[i].getKeyAsInt()); - } - } - - @Test - public void testBubbleSortNotInSitu() { - //Test BubbleSortNotInSitu - MyPair[] arrPairs0 = {new MyPair(2,2), new MyPair(0,0), new MyPair(1,1), new MyPair(3,3)}; - MyPair[] arrPairs1 = sort_algorithms.myBubbleSortNotInSitu(arrPairs0); - for (int i = 0; i < arrPairs1.length; i++) { - assertEquals(i, arrPairs1[i].getKeyAsInt()); - } - } - - @Test - public void testSelectionSortInSitu() { - //Test SelectionSortInSitu - MyPair[] arrPairs0 = {new MyPair(2,2), new MyPair(0,0), new MyPair(1,1), new MyPair(3,3)}; - sort_algorithms.mySelectionSortInSitu(arrPairs0); - for (int i = 0; i < arrPairs0.length; i++) { - assertEquals(i, arrPairs0[i].getKeyAsInt()); - } - } - - @Test - public void testSelectionSortNotInSitu() { - //Test SelectionSortNotInSitu - MyPair[] arrPairs0 = {new MyPair(2,2), new MyPair(0,0), new MyPair(1,1), new MyPair(3,3)}; - MyPair[] arrPairs1 = sort_algorithms.mySelectionSortNotInSitu(arrPairs0); - for (int i = 0; i < arrPairs0.length; i++) { - assertEquals(i, arrPairs1[i].getKeyAsInt()); - } - } -} \ No newline at end of file diff --git a/MyTest/MyTestBinarySearchTree.java b/MyTest/MyTestBinarySearchTree.java new file mode 100644 index 0000000..7101d47 --- /dev/null +++ b/MyTest/MyTestBinarySearchTree.java @@ -0,0 +1,30 @@ +package MyTest; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; + +import MyList.MyList; +import MyList.MyPair; +import MyTrees.MyBinarySearchTree; + +public class MyTestBinarySearchTree { + + @Test + public void testPreorder() { + MyBinarySearchTree tree = new MyBinarySearchTree(); + tree.insert(tree.root, new MyPair(0,0)); + tree.insert(tree.root, new MyPair(1,1)); + tree.insert(tree.root, new MyPair(2,2)); + tree.insert(tree.root, new MyPair(3,3)); + tree.insert(tree.root, new MyPair(4,4)); + MyList treeAsList = tree.preorder(tree.root); + MyPair[] treeListAsArray = treeAsList.toArray(); + //for (int i = 0; i < treeListAsArray.length; i++) { + // assertEquals(, actual); + //} + for (int i = 0; i < treeListAsArray.length; i++) { + System.out.println(treeListAsArray[i].toString()); + } + System.out.println(treeListAsArray.length); + } +} diff --git a/MyTest/MyTestFunctionalDatastructures.java b/MyTest/MyTestFunctionalDatastructures.java new file mode 100644 index 0000000..695ce16 --- /dev/null +++ b/MyTest/MyTestFunctionalDatastructures.java @@ -0,0 +1,15 @@ +package MyTest; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; + +import MyFunctionalDatastructures.MyTrie; +import MyTrees.MyTreeNode; + +public class MyTestFunctionalDatastructures { + @Test + public void testTries() { + MyTrie trie = new MyTrie(new MyTreeNode()); + trie.get(4);//TODO + } +} diff --git a/MyTest/MyTestList.java b/MyTest/MyTestList.java new file mode 100644 index 0000000..e3c8827 --- /dev/null +++ b/MyTest/MyTestList.java @@ -0,0 +1,23 @@ +package MyTest; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; + +import MyList.MyList; +import MyList.MyPair; + +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(); + for (int i = 0; i < listAsArray.length; i++) { + assertEquals(i, listAsArray[i].key); + } + } +} diff --git a/MyTest/MyTestSearch.java b/MyTest/MyTestSearch.java new file mode 100644 index 0000000..8a86091 --- /dev/null +++ b/MyTest/MyTestSearch.java @@ -0,0 +1,47 @@ +package MyTest; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; + +import MyList.MyList; +import MyList.MyPair; +import MySearch.MySearchAlgorithms; +import MySort.MySortAlgorithms; + +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); + } + + @Test + public void testBinarySearch() { + MyPair[] pairs = {new MyPair(1,3), new MyPair(2,4)}; + + assertEquals(3, MySearchAlgorithms.binarySearch(1, pairs).data); + assertEquals(4, MySearchAlgorithms.binarySearch(2, pairs).data); + } + + @Test + public void testExponentialSearch() { + 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()); + } + } +} diff --git a/MyTest/MyTestSort.java b/MyTest/MyTestSort.java new file mode 100644 index 0000000..c812bf4 --- /dev/null +++ b/MyTest/MyTestSort.java @@ -0,0 +1,49 @@ +package MyTest; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; + +import MyFunctionalDatastructures.MyTrie; +import MyList.MyList; +import MyList.MyPair; +import MySearch.MySearchAlgorithms; +import MySort.MySortAlgorithms; +import MyTrees.MyTreeNode; + +public class MyTestSort { + + + @Test + public void testBubbleSortNotInSitu() { + //Test BubbleSortNotInSitu + 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()); + } + } + + @Test + public void testSelectionSortInSitu() { + //Test SelectionSortInSitu + 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()); + } + } + + @Test + public void testSelectionSortNotInSitu() { + //Test SelectionSortNotInSitu + 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()); + } + } + + + + +} \ No newline at end of file diff --git a/MyTrees/MyBinarySearchTree.java b/MyTrees/MyBinarySearchTree.java new file mode 100644 index 0000000..f4ca918 --- /dev/null +++ b/MyTrees/MyBinarySearchTree.java @@ -0,0 +1,132 @@ +package MyTrees; + +import MyList.MyPair; + +public class MyBinarySearchTree extends MyBinaryTree { + public MyBinarySearchTree() { + super(); + } + + public MyBinarySearchTree(MyTreeNode root) { + this.root = root; + } + + /** + * + * @param node + * @param key + * @return + */ + public Object lookup(MyTreeNode node, Object key) { + Object value = null; + if (node != null) { + if ((int)key < node.getKeyAsInt()) { + value = lookup(node.getLeftChild(), key); + } else if ((int)key > node.getKeyAsInt()) { + value = lookup(node.getRightChild(), key); + } else { + value = node.getPair().data; + } + } + return value; + } + + /** + * 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); + } else { + if (node.getKeyAsInt() == pair.getKeyAsInt()) { + node.setPair(pair); + } else if (node.getKeyAsInt() < pair.getKeyAsInt()) { + insert(node.getLeftChild(), pair); + } else { + insert(node.getRightChild(), pair); + } + } + } + + /** + * Removes node specified by key. + * @param node starting point of search for node to remove. + * @param key specifies node to remove. + */ + public void remove(MyTreeNode node, Object key) { + if (node != null) { + if (node.getKeyAsInt() < (int)key) { + remove(node.getLeftChild(), key); + } else if (node.getKeyAsInt() > (int)key) { + remove(node.getRightChild(), key); + } else { + //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 + } else if ((node.getLeftChild() != null) && (node.getRightChild() == null)) { + node = node.getLeftChild(); + //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 + } else { + //Get right parttree + MyTreeNode nnode = node.getRightChild(); + //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 + if (nnode.getLeftChild().getRightChild() != null) { + nnode.setLeftChild(nnode.getLeftChild().getRightChild()); + //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. + node.setPair(symmetricFollowerPair); + } + } + } + } + + /** + * 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; + MyTreeNode nnode = node; + if (node != null) { + while (nnode.getLeftChild() != null) { + nnode = nnode.getLeftChild(); + } + min = nnode.getPair(); + } + return min; + } + + /** + * 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; + MyTreeNode nnode = node; + if (node != null) { + while (nnode.getRightChild() != null) { + nnode = nnode.getRightChild(); + } + max = nnode.getPair(); + } + return max; + } +} diff --git a/MyTrees/MyBinaryTree.java b/MyTrees/MyBinaryTree.java index 811a104..58f1057 100644 --- a/MyTrees/MyBinaryTree.java +++ b/MyTrees/MyBinaryTree.java @@ -1,14 +1,59 @@ package MyTrees; + +import MyList.MyList; + public class MyBinaryTree { public MyTreeNode root; private int size; public MyBinaryTree() { - root = null; + root = new MyTreeNode(); } public int getSize() { return this.size; } + + public MyList preorder(MyTreeNode root) { + MyList treeList = new MyList(); + helpPreorder(root, treeList); + return treeList; + } + + private void helpPreorder(MyTreeNode root, MyList list) { + if (root != null) { + list.add(root.getPair()); + helpPreorder(root.getLeftChild(), list); + helpPreorder(root.getRightChild(), list); + } + } + + public MyList postorder(MyTreeNode root) { + MyList treeList = new MyList(); + helpPostorder(root, treeList); + return treeList; + } + + private void helpPostorder(MyTreeNode root, MyList list) { + if (root != null) { + helpPreorder(root.getLeftChild(), list); + helpPreorder(root.getRightChild(), list); + list.add(root.getPair()); + } + } + + public MyList inorder(MyTreeNode root) { + MyList treeList = new MyList(); + helpInorder(root, treeList); + return treeList; + } + + private void helpInorder(MyTreeNode root, MyList list) { + if (root != null) { + helpInorder(root.getLeftChild(), list); + list.add(root.getPair()); + helpInorder(root.getRightChild(), list); + } + } } diff --git a/MyTrees/MyMinHeapTree.java b/MyTrees/MyMinHeapTree.java index 7bee155..3124430 100644 --- a/MyTrees/MyMinHeapTree.java +++ b/MyTrees/MyMinHeapTree.java @@ -1,5 +1,5 @@ package MyTrees; -public class MyMinHeapTree { +public class MyMinHeapTree extends MyBinaryTree { } diff --git a/MyTrees/MyTreeNode.java b/MyTrees/MyTreeNode.java index 2d7d054..3adc46e 100644 --- a/MyTrees/MyTreeNode.java +++ b/MyTrees/MyTreeNode.java @@ -1,16 +1,24 @@ package MyTrees; +import MyList.MyPair; + public class MyTreeNode { private MyTreeNode leftChild; private MyTreeNode rightChild; - private Object key; - private Object data; + private MyPair pair; public MyTreeNode() { this.leftChild = null; this.rightChild = null; - this.key = null; - this.data = null; + this.pair = new MyPair(); + } + + public MyTreeNode(int key, Object data) { + this.leftChild = null; + this.rightChild = null; + this.pair = new MyPair(); + this.pair.key = key; + this.pair.data = data; } //David Setter Methods @@ -22,12 +30,8 @@ public class MyTreeNode { this.rightChild = rightNode; } - public void setKey(Object key) { - this.key = key; - } - - public void setData(Object data) { - this.data = data; + public void setPair(MyPair pair) { + this.pair = new MyPair(pair.key, pair.data); } //David Getter Methods @@ -39,15 +43,11 @@ public class MyTreeNode { return this.rightChild; } - public Object getKey() { - return this.key; - } - - public Object getData() { - return this.data; + public MyPair getPair() { + return this.pair; } public int getKeyAsInt() { - return (int)this.key; + return this.pair.getKeyAsInt(); } } \ No newline at end of file