Skip to content

Instantly share code, notes, and snippets.

@joennlae
Created April 25, 2017 07:27
Show Gist options
  • Save joennlae/d7515f2b1e2691e6045e52b8dba6dc07 to your computer and use it in GitHub Desktop.
Save joennlae/d7515f2b1e2691e6045e52b8dba6dc07 to your computer and use it in GitHub Desktop.
package u7a1;
import java.util.ArrayList;
public class Filter implements IFilter{
private ArrayList<Student> finalList;
Filter(){
}
@Override
public ArrayList filterRaw(ArrayList groups) {
finalList = new ArrayList();
int pointsRequired = (int) (IFilter.maxNumberofPoints * (IFilter.criteria/100));
for(int i = 0; i < groups.size(); i++){
ArrayList sublist = (ArrayList) groups.get(i);
for(int j = 0; j < sublist.size(); j++){
Student s = (Student) sublist.get(j);
if(s.getPoints()>=pointsRequired){
finalList.add((Student) sublist.get(j));
}
}
}
return finalList;
}
@Override
public ArrayList<Student> filterGeneric(ArrayList<ArrayList<Student>> groups) {
finalList = new ArrayList<Student>();
int pointsRequired = (int) (IFilter.maxNumberofPoints * (IFilter.criteria/100));
for(int i = 0; i < groups.size(); i++){
ArrayList<Student> sublist = groups.get(i);
for(int j = 0; j < sublist.size(); j++){
if(sublist.get(j).getPoints()>=pointsRequired){
finalList.add(sublist.get(j));
}
}
}
return finalList;
}
}
package u7a1;
/**
* Factory for "Testat" filters
*/
public class FilterFactory {
/**
* Create a "Testat" filter
* @return a "Testat" filter
*/
public static IFilter create()
{
return new Filter();
}
}
package u7a3;
import java.util.ArrayList;
public class BinarySearchTreeUtils<T> implements IBinarySearchTreeUtils<T>{
private ArrayList<T> output = new ArrayList<T>();
BinarySearchTreeUtils(){
}
@Override
public int height(BinarySearchTree<T> tree) {
if(tree != null){
return 1 + Math.max(height(tree.left), height(tree.right));
}
return 0;
}
@Override
public boolean isLeaf(BinarySearchTree<T> tree) {
return (tree.left == null && tree.right == null);
}
@Override
public boolean hasOneChild(BinarySearchTree<T> tree) {
return (tree.left == null && tree.right != null || tree.left != null && tree.right == null);
}
@Override
public ArrayList<T> preOrder(BinarySearchTree<T> tree) {
if(tree!=null){
output.add(tree.thing);
preOrder(tree.left);
preOrder(tree.right);
}
return output;
}
@Override
public ArrayList<T> inOrder(BinarySearchTree<T> tree) {
ArrayList<T> output = new ArrayList<T>();
if(tree!=null){
output.addAll(inOrder(tree.left));
output.add(tree.thing);
output.addAll(inOrder(tree.right));
}
return output;
}
@Override
public ArrayList<T> postOrder(BinarySearchTree<T> tree) {
if(tree!=null){
postOrder(tree.left);
postOrder(tree.right);
output.add(tree.thing);
}
return output;
}
@Override
public BinarySearchTree<T> insert(BinarySearchTree<T> tree, int key, T thing) {
if (tree == null)
return new BinarySearchTree<T>(key, thing);
else if (key == tree.key){
tree.thing = thing;
return tree;
}
else if (key<tree.key){
tree.left = insert(tree.left,key,thing);
}
else{
tree.right = insert(tree.right,key,thing);
}
return tree;
}
@Override
public T find(BinarySearchTree<T> tree, int key) {
if(tree == null) return null;
if(tree.key == key) return tree.thing;
if(key < tree.key){
return find(tree.left,key);
}
else return find(tree.right,key);
}
@Override
public BinarySearchTree<T> remove(BinarySearchTree<T> tree, int key) {
if(find(tree,key) == null) return tree;
else{
if(tree.key == key){
if(isLeaf(tree)){
tree = null;
}
else if(hasOneChild(tree)){
if(tree.left != null){
tree = tree.left;
}
else tree = tree.right;
}
else{ // two children
BinarySearchTree<T> lowest = getLowest(tree);
tree.thing = lowest.thing;
tree.key = lowest.key;
tree.right = remove(tree.right,lowest.key);
}
}
else if(key < tree.key){
tree.left = remove(tree.left,key);
}
else{
tree.right = remove(tree.right,key);
}
}
return tree;
}
public BinarySearchTree<T> getLowest(BinarySearchTree<T> tree){
tree = tree.right;
while(tree.left != null){
tree = tree.left;
}
return tree;
}
}
package u7a3;
public class UtilsFactory {
public static IBinarySearchTreeUtils<String> create()
{
return new BinarySearchTreeUtils<String>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment