Lab 21 - part deux :: BST / Map - due. Fri. Mar. 7

Implement the APMap interface with your BST implementation (see below)

Assignment Sheet: Lab21_CharCount_part2.pdf

Create a new class in your CharCount project and copy and paste the APMap interface into it. This is the interface you should implement with your BST.

 public interface APMap{
	public boolean containsKey(Object key);
	public Object get(Object key);
	public Object put(Object key, Object val);
	public Set keySet();
	public int size();
	public Object remove(Object key);
}

And here are the three helper methods for the remove.  You should go through the exercise of writing the move method yourself.

 private TreeNode<Comparable> findLeftMost(TreeNode<Comparable> t){
        TreeNode temp = t;
        while(temp.getLeft()!=null) temp = temp.getLeft();

        return temp;
    }

    private TreeNode<Comparable> findParent(TreeNode<Comparable> t){

        TreeNode p = root;
        if(t==root) return null;

        int compVal;
        while(p.getLeft()!=t && p.getRight()!=t){

           compVal = t.getValue().compareTo(p.getValue());
           if(compVal < 0) p = p.getLeft();
           else p = p.getRight();
        }
        return p;

    }

    private void replaceNode(TreeNode a, TreeNode b){

        TreeNode p = findParent(a);
        if(p==null) root = b;
        else if(p.getLeft()==a) p.setLeft(b);
        else p.setRight(b);
    }

Lab 21 :: Char Count

Assignment Sheet: Lab21_CharCount.pdf

Starting Files: Lab21_BST_stuDistro.zip

Sample Text Doc: decl.txt (the Declaration of Independence)

BELOW: code that uses a class called BufferedReader  You should be able to copy/paste this to wherever you need to process the document.  in-line comments explain what the code is doing.

  BufferedReader in=null; //A more primitive character reader
try{

in = new BufferedReader(new FileReader(”decl.txt”));

int nextChar = in.read(); //reads the next byte out of the file as an int

while(nextChar!=-1){    //read() will return -1 if it reaches end of file
System.out.println((char)nextChar); //process the char
nextChar = in.read(); // read the next one.
}
}
catch(Exception e){}

Lab 20 :: Heap PQ - Due MEOM Mon., Feb 25

Assignment:

  1. implement the APPriorityQueue interface with a heap.
  2. Write a main method that demonstrates it works properly (it doesn’t have to interact with the user. You can just hard code the addition of the numbers to the Queue and then print out removeMin until it’s empty.

Starting Files:
Lab20_PQ_Heap.zip

Lab 19 :: QueueNet - Due MEOM Wed. Feb 20

Starting project: Lab19_QueueNet_stuDistro.zip

Assingment Sheet: Lab19_QueueNet_assgnSheet.pdf

Lab 18 :: Stack/Undo - Due MEOM Sunday. Feb 10

Two part assignment: 1. implement a Stack 2. Use it to enable “undo” behavior in a graphical application.

Assignment sheet: Lab18_Stack_undo.pdf

Starting files: Lab18_StackUndo_stuDistro.zip

Lab 17 :: Merge/Quicksort Due. MEOM Wed. Feb. 6

There is no assignment sheet for this one…

ASSIGNMENT:

Implement either Mergesort or Quicksort and provide a testing class (main method) that shows how it works.

Your merge/quicksort must work on an ArrayList<Comparable>.  The method signature should be:

public void merge(quick)sort(ArrayList<Comparable> A)

That is, the method SHOULD NOT RETURN anything, it should modify the argument array.  Even if you’re doing mergesort, which isn’t in place, you should copy elements into the argument array.

NOTE: the methods we’ve written in class have all been recursive and accept indecies that define a “chunk” of the array to sort.  You should still write these, but make them private helper methods.  This way, the public version is very clear.  All it would do is make the initial call to the private recursive helper method.

TURN IT IN:
username.lab17.zip

Lab 16 :: n^2 sorts - due MEOM Friday

Assignment: Implement each of the n^2 sorting algorithms we have learned: Selection- , Insertion- , Bubble-sort.

The starting files provide a testing structure for writing your code.

Starting files: Lab16_n2Sorts.zip

Lab 15 :: Linked List - Due. MEOM Monday, Jan. 28

There is no assignment sheet for this assignment, instead here it is:

1. Download the starting project. It contains a class called MyLinkedList which we started in class, that is the beginning of an implementation of the APList interface. The javadoc comments above each method should give you enough information to work with.

2. Minimum requirement is to implement the following methods:

  • add(Object o) — already done
  • get(int index)
  • set(int index, Object o)
  • remove(int index)

3. MyLinkedList has a testing class attached. Your code must pass all of the tests for the minimum set of methods. NOTE: there are 4 tests for remove.

4. Additionally you my write: add(int index, Object o) but there is no test for it.

5. Extra: implement an Iterator (LLIterator) for your LinkedList.

Grading: Doing the minimun with clear, concise code will earn a B+

Turning it in: The usual. Rename the project username.lab15, zip it up and dropbox it.

Quiz Friday, Jan. 25

We will have a brief in-class Quiz on Friday. Material you are responsible for:

  • 2D arrays
  • ArrayLists, including the generic form ArrayList<T>
  • Iterators - usage only, and what they’re good for
  • Sequential Search (of both Arrays and 2D arrays)
  • Binary Search concepts - not expected to reproduce code
  • General understanding of running time.

EXAMPLE Questions:

1. Write the method below that tries to find the given object in the given 2D array. Return true if it’s found, false otherwise.

public boolean search(Object[][] grid, Object toFind)

2. Given an unsorted ArrayList of Comparables, some object will be the min value in the list. Find the min object and return it using an Iterator. No credit will be given for code that does not use an iterator.

public Comparable findMin(ArrayList<Comparable> L)

3. Assume that the given array is in order (from small to large) and that all elements are unique. Write the method below which returns the index in the list where the given object is actually located, or where it would be located if it were in the list. You may do this search sequentially.

public int findIndex(Comparable[] L, Comparable toFind)

4. Write a method that adds an Object to an ordered list in its correct location. You will have to shift elements “up” in the array to make room for the object you’re inserting. You should ASSUME that there is enough room in the array for the new element - no need to do bounds checking. AND you MAY ASSUME that the findIndex method from problem 3 works as described regardless of what you wrote.

public void addInOrder(Comparable[] L, Comparable toAdd)

5. In the worst case, how many operations will be performed by addInOrder if the array contains n elements? Is it proportional to n? Much less than n? Much more than n? Explain your reasoning.

5.a. what if findIndex(…) was implemented with binary search? would that affect the general running time of addInOrder? Yes, or no, and explain why.

Reading for Tues. Jan. 22

From “Java Software Structures” by Lewis, Chase, Sudol

pp.14-19 - analysis of algorithms
pp. 103-110 - Linked Structures, linked list

Be prepared to answer conceptual questions about the reading in class.

You are encouraged to ask questions about the reading on the email list.