0006_Sixth push added[some stuff...]

This commit is contained in:
Meruemon
2022-07-03 16:20:55 +02:00
parent 07409bf7a1
commit 5fdb8b9dd4
17 changed files with 459 additions and 105 deletions

View File

@@ -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;
}
}

View File

@@ -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)));
}
}

View File

@@ -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;
}

View File

@@ -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() {

7
MyMath/MyMath.java Normal file
View File

@@ -0,0 +1,7 @@
package MyMath;
public class MyMath {
public static int myLog2(int i) {
return (int)(Math.log(i) / Math.log(2));
}
}

View File

@@ -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)) {

View File

@@ -1,7 +1,7 @@
package MySort;
import MyList.MyPair;
public class sort_algorithms {
public class MySortAlgorithms {
/**
* BubbleSort in-situ

View File

@@ -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());
}
}
}

View File

@@ -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);
}
}

View File

@@ -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
}
}

23
MyTest/MyTestList.java Normal file
View File

@@ -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);
}
}
}

47
MyTest/MyTestSearch.java Normal file
View File

@@ -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());
}
}
}

49
MyTest/MyTestSort.java Normal file
View File

@@ -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());
}
}
}

View File

@@ -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;
}
}

View File

@@ -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);
}
}
}

View File

@@ -1,5 +1,5 @@
package MyTrees;
public class MyMinHeapTree {
public class MyMinHeapTree extends MyBinaryTree {
}

View File

@@ -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();
}
}