The challenge for this assignment was to use only ArrayList and Iterator. I was trying to implement a BinaryTree data structure based on ArrayList and then implement my set operations on top of that. The worst case running time using ArrayList for most of the operations is O(n^2), while by using BinaryTree they can be done in O(nlog n), worst case. Here I put my code using only ArrayList features. I will make another post about my BinaryTree implementation using ArrayList.
Set class:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Author Unknown | |
import java.util.Iterator; | |
import java.lang.Iterable; | |
public interface Set extends Iterable<Integer> | |
{ | |
/** | |
* Returns the size of the set, or 0 if the set is empty. | |
*/ | |
public int size(); | |
/** | |
* Checks whether the set is empty. | |
*/ | |
public boolean isEmpty(); | |
/** | |
* Inserts a values into the set. | |
*/ | |
public void insert(Integer x); | |
/** | |
* Removes a value from the set. | |
*/ | |
public void remove(Integer x); | |
/** | |
* Checks whether a given element is a member of the set. | |
*/ | |
public boolean contains(Integer x); | |
/** | |
* Computes the union of two sets. | |
*/ | |
public void union(Set o); | |
/** | |
* Computes the intersection of two sets. | |
*/ | |
public void intersection(Set o); | |
/** | |
* Computes the difference of two sets, such that elements which appear in o | |
* are removed from this set. | |
*/ | |
public void difference(Set o); | |
/** | |
* Checks whether the current set is a subset of the set o. Return true if we are | |
* a subset, or false otherwise. | |
*/ | |
public boolean subset(Set o); | |
/** | |
* Empties the current set. | |
*/ | |
public void clear(); | |
public Iterator<Integer> iterator(); | |
} | |
Here is the MySet class which implements the Set class:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Author Leo the Pamador | |
import java.util.ArrayList; | |
import java.util.Iterator; | |
public class MySet implements Set | |
{ | |
//Instance variables | |
//holds the data in the set | |
private ArrayList<Integer> mySet; | |
//size of the set | |
private int size; | |
/** | |
* Default constructor for the set object. | |
*/ | |
public MySet() | |
{ | |
mySet = new ArrayList<Integer>(); | |
size = 0; | |
} | |
//Constructor with given array as data | |
public MySet (int[] data) | |
{ | |
mySet = new ArrayList<Integer>(); | |
for (int i=0;i<data.length;i++) | |
insert(data[i]); | |
} | |
/** | |
* Returns the size of the set, or 0 if the set is empty. | |
*/ | |
public int size() | |
{ | |
return mySet.size(); | |
} | |
/** | |
* Checks whether the set is empty. | |
*/ | |
public boolean isEmpty() | |
{ | |
return mySet.isEmpty(); | |
} | |
/** | |
* Inserts a values into the set. | |
*/ | |
public void insert(Integer x) | |
{ | |
//if it 'x' exists in the set, ignore it otherwise it's added to the set | |
if(mySet.contains(x)) | |
return; | |
else | |
{ | |
mySet.add(x); | |
size++; | |
} | |
} | |
/** | |
* Removes a value from the set. | |
*/ | |
public void remove(Integer x) | |
{ | |
//if 'x' is not in the set ignore it, otherwise remove it from the set | |
if(mySet.contains(x)) | |
{ | |
mySet.remove(x); | |
size--; | |
} | |
} | |
/** | |
* Checks if an element is contained in the set. | |
*/ | |
public boolean contains(Integer x) | |
{ | |
return mySet.contains(x) ? true : false; | |
} | |
/** | |
* Computes the union of two sets. | |
*/ | |
public void union(Set o) | |
{ | |
//if passing set is empty just ignore it | |
if (o.isEmpty()) | |
return; | |
//if passing set is not empty but the calling set is empty just copy the passing set over the calling set | |
if (mySet.isEmpty()) | |
{ | |
Iterator<Integer> it = o.iterator(); | |
while(it.hasNext()) | |
mySet.add(it.next()); | |
return; | |
} | |
//both sets have element so iterate over the passed set and insert them into the calling set | |
Iterator<Integer> it = o.iterator(); | |
while(it.hasNext()) | |
{ | |
int j = (Integer)it.next(); | |
insert(j); | |
} | |
} | |
/** | |
* Computes the intersection of two sets. | |
*/ | |
public void intersection(Set o) | |
{ | |
//if either set is empty just clear the calling set and return | |
if (o.isEmpty() | isEmpty()) | |
{ | |
clear(); | |
return; | |
} | |
//if calling set is subset of passed set ignore it | |
if (subset(o)) | |
return; | |
//both sets have elements and are not subset of each other | |
//holds the common elements | |
ArrayList<Integer> newSet = new ArrayList<Integer>(); | |
Iterator<Integer> it = iterator(); | |
//so iterate over the calling set | |
while(it.hasNext()) | |
{ | |
int j = (Integer) it.next(); | |
//if it finds something just add it to the newSet and remove it from the passed set | |
if (o.contains(j)) | |
{ | |
o.remove(j); | |
newSet.add(j); | |
} | |
} | |
//clear the calling set and copy the newSet to that | |
mySet.clear(); | |
mySet.addAll(newSet); | |
} | |
/** | |
* Computes the difference of two sets, such that elements which appear in o | |
* are removed from this set. | |
*/ | |
public void difference(Set o) | |
{ | |
//to hold a copy of the calling set | |
ArrayList<Integer> temp = new ArrayList<Integer>(); | |
temp.addAll(mySet); | |
//find the intersection between these 2 sets | |
intersection(o); | |
Iterator<Integer> it = iterator(); | |
//iterate over the set of intersection | |
while(it.hasNext()) | |
{ | |
int data = (Integer)it.next(); | |
//compare it with the temp set, if finds any just remove it | |
if (temp.contains(data)) | |
temp.remove(temp.indexOf(data)); | |
} | |
//clear the calling set and copy the temp set to it | |
mySet.clear(); | |
mySet.addAll(temp); | |
} | |
/** | |
* Checks whether the current set is a subset of the set o. Return true is this is | |
* a subset, otherwise return false. | |
*/ | |
public boolean subset(Set o) | |
{ | |
//if calling set is bigger so it can't be subset of passed set | |
if (size > o.size()) | |
return false; | |
Iterator<Integer> it = o.iterator(); | |
ArrayList<Integer> subSet = new ArrayList<Integer>(); | |
//iterate over the passed set if it finds just add it to the subSet | |
while(it.hasNext()) | |
{ | |
int j = it.next(); | |
subSet.add(j); | |
} | |
//check to see if all elements of the calling set is in the passed set using ArrayList 'contailsAll' method | |
return subSet.containsAll(mySet) ? true : false; | |
} | |
/** | |
* Iterates over all of the elements in the set. | |
*/ | |
public Iterator<Integer> iterator() | |
{ | |
//return ArrayList iterator | |
return mySet.iterator(); | |
} | |
/** | |
* Empties the current set. | |
*/ | |
public void clear() | |
{ | |
mySet = new ArrayList<Integer>(); | |
size = 0; | |
} | |
} |
There was extra mark for efficient running time which I think I won't get any, since the worst case running time for my implementation is O(n^2).
My code passed all the on line tests, however if anybody finds any problem, please leave a comment.
Leo the Pamador
No comments:
Post a Comment