LAD Block 5: Design and implementation of data structures

  1. Here is an incomplete impementation of a data structure for finite sets of
    non-negative integers.
    Finish the intersection function. 

    public class IntSet{

    private boolean[] arr = new boolean[0];

    /** Does the set contain x? */
    public boolean contains(int x){
    return x < arr.length && X >= 0 && arr[x];
    }

    /** Alter this set, removing all values not in the other set */
    public void intersection(IntSet other){ // Complete this method
    • for (int i=0; i<arr.length; i++) {
    • arr[i] = arr[i] && other.contains(i);
    • }
    • //ovan sparar true om den finns i båda och annars false. arr[i] som används är den som callar funktionen och alltså inte others array, others används bara till contains.
  2. Here is an incomplete impementation of a set data structure for storing nonnegative
    integers.
    It has two methods, contains and add. Finish the add function such that contains(x) is (and remains) true after add(x) has been executed (for any x >=0).

    public class IntSet{
    boolean[] arr = new boolean[0];
    /** Does the set contain x? */

    public boolean contains(int x){
    return x = 0 && arr[x];
    }

    /** add x>=0 to the set, expanding the array if needed */
    public void add(int x){
    // Complete this method
    • if (x >= arr.length){
    • boolean[] newarr=new boolean[x+1];

    • for (int i=0;i<arr.length;i++){
    • newarr[i]=arr[i];
    • arr = newarr;
    • }

    • arr[x] = true;
    • }
    • }
  3. What is the complexity of the following method?

    if (x >= arr.length){
    boolean[] newarr=new boolean[x+1];

    for (int i=0;i<arr.length;i++){ newarr[i]=arr[i]; arr = newarr;
    }
    arr[x] = true;
    }
    }
    • O(X) worst case, where X is the value being added
    • [O(1) best case]
  4. A hash table has 36 cells and contains 9 elements. What is the load factor of the
    table?
    9/36 = 0.25
  5. Your task is to implement a Map data structure.
    The keys are Strings and values
    are Objects.
    It must have the standard operations: 
    public void put(String key, Object Value)
    public Object get(String key)

    If the key used in put is already mapped, put should do nothing.
    If the key used in
    get does not exist, get should return null. There are no requirements on
    performance.
    For full points it must support any number of keys.
    You are not allowed to import other classes. Hint: Use two arrays.
    public class Map{
    • String[] keys = new String[10];
    • Object[] values = new String[10];

    int size = 0;

    public void put (String key, Object value){

    • if (get(key)!=null) return;
    • if (size >= keys.length) {

    String[] newkeys = new String[keys.length*2];

    Object[] newvalues = new Object[keys.length*2];

    • for(int i = 0;i<size;i++){
    • newkeys[i] = keys[i];
    • newvalues[i] = values[i];
    • }

    • keys = newkeys;
    • values = newvalues;

    • }
    • keys[size]=key;
    • values[size]=value;
    • size++;
    • }

    • public Object get(String key) {
    • for(int i = 0;i<size;i++)

    • if(keys[i].equals(key)) {return values[i];} 
    • return null;
    • }
    • }
Author
ccc
ID
329516
Card Set
LAD Block 5: Design and implementation of data structures
Description
LAD Block 5: Design and implementation of data structures
Updated