Praktikum 2: alles grün

Binary Tree
This commit is contained in:
Johannes Theiner 2018-04-12 17:00:06 +02:00
parent fcdeabe55f
commit 7665f9bbb6
5 changed files with 118 additions and 13 deletions

View File

@ -28,11 +28,11 @@ public class CharCollection implements Cloneable{
}
public int count(char c) {
int rueckgabe = 0;
int result = 0;
for(char b:list) {
if(b == c)rueckgabe ++;
if(b == c)result ++;
}
return rueckgabe;
return result;
}
public int different() {
@ -59,7 +59,7 @@ public class CharCollection implements Cloneable{
}
*/
public char top() {
if(list.size() == 0) return 0;
if(list.isEmpty()) return 0;
int max = 0;
char character ='_';
for(char c : list) {
@ -72,12 +72,45 @@ public class CharCollection implements Cloneable{
return character;
}
public CharCollection moreThan(int n) {
CharCollection result = new CharCollection(list);
for (int i = 0; i < result.size(); i++) {
if(result.count(result.list.get(i)) < n) result.list.remove(i);
public CharCollection moreThan(int m) {
ArrayList<Character> list2 = new ArrayList<>();
for(char c:this.list){
if(this.count(c) > m) {
list2.add(c);
}
}
return result;
StringBuilder s = new StringBuilder();
for(char c:list2) s.append(c);
return new CharCollection(s.toString());
}
public CharCollection except(CharCollection cc) {
ArrayList<Character> list= new ArrayList<>(this.list);
boolean removeCharacterOnce = false;
for(int i=0; i<list.size(); i++){
for(int w=0; w<cc.list.size(); w++) {
if(cc.list.get(w)==list.get(i) && !removeCharacterOnce) {
removeCharacterOnce=true;
cc.list.remove(w);
list.remove(i);
}
}
if(removeCharacterOnce) i--;
removeCharacterOnce = false;
}
//System.out.println("list2: "+list2.toString());
StringBuilder s = new StringBuilder();
for(char c:list) s.append(c);
return new CharCollection(s.toString());
}
public boolean isSubset(CharCollection cc) {
ArrayList<Character> list2 = new ArrayList<>(this.list);
for(char c:cc.list) {
if(!list2.contains(c)) return false;
else list2.remove(list2.indexOf(c));
}
return true;
}
@Override

View File

@ -0,0 +1,47 @@
package de.joethei.hs.java2.vorlesungen.binaryTree;
public class Node<T> {
private T value;
private Node<T> left;
private Node<T> right;
public Node() {
}
public Node(T value) {
this.value = value;
}
public Node(T value, Node<T> left, Node<T> right) {
this.value = value;
this.left = left;
this.right = right;
}
public T getValue() {
return value;
}
public Node<T> getLeft() {
return left;
}
public Node<T> getRight() {
return right;
}
@Override
public String toString() {
return super.toString();
}
@Override
public boolean equals(Object obj) {
if(obj instanceof Node) {
Node node = (Node) obj;
return node.value.equals(this.value) && node.left.equals(this.left) && node.right.equals(this.right);
}
return false;
}
}

View File

@ -0,0 +1,14 @@
package de.joethei.hs.java2.vorlesungen.binaryTree;
public class Tree<T> {
private Node<T> root;
public Tree(Node<T> root) {
this.root = root;
}
public Node<T> getRoot() {
return root;
}
}

View File

@ -50,15 +50,14 @@ public class CharCollectionTest {
assertEquals(new CharCollection("llo"), new CharCollection("lol"));
}
/*
@Test
public void except() {
assertEquals(new CharCollection("Hao"), new CharCollection("Hallo").except("l"));
assertEquals(new CharCollection("Halo"), new CharCollection("Hallo").except(new CharCollection("l")));
}
@Test
public void isSubsect() {
assertTrue(new CharCollection("Hallo").isSubset("Hao"));
assertTrue(new CharCollection("Hallo").isSubset(new CharCollection("Hao")));
}
*/
}

View File

@ -1,6 +1,8 @@
package de.joethei.hs.java2.tests;
import de.joethei.hs.java2.vorlesungen.Sieb;
import de.joethei.hs.java2.vorlesungen.binaryTree.Node;
import de.joethei.hs.java2.vorlesungen.binaryTree.Tree;
import org.junit.Test;
import java.util.Arrays;
@ -17,4 +19,14 @@ public class VorlesungsTest {
Set<Integer> set = new TreeSet<>(Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19));
assertEquals(set, new Sieb(20).getPrimzahl());
}
@Test
public void binaryTree() {
Tree<Integer> tree = new Tree<>(new Node<>(45, new Node<>(14), new Node<>(16)));
assertEquals(Integer.valueOf(45), tree.getRoot().getValue());
assertEquals(Integer.valueOf(14), tree.getRoot().getLeft().getValue());
assertEquals(Integer.valueOf(16), tree.getRoot().getRight().getValue());
}
}