cs 367 introduction to data structures lecture 2 audio for lecture 1 is available homework 1 due...

60
CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

Upload: cecil-lane

Post on 19-Jan-2016

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

CS 367

Introduction to Data Structures

Lecture 2

• Audio for Lecture 1 is available

• Homework 1 due Friday, September 18

Page 2: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

Let’s build a BagAST using an array of Objects.

Arrays are simple to use but also have a fixed size.

Page 3: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

public class ArrayBag implements BagADT {

/* Local data to implement a Bag */

/* One or more constructors */

/* Implementations for add, remove, isEmpty and clone */

}

Page 4: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

public class ArrayBag implements BagADT {

private Object[] items;

private int itemCount;

private final int INIT_SIZE;

/* One or more constructors */

/* Implementations for add, remove, isEmpty and clone */

}

Page 5: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

public class ArrayBag implements BagADT {

private Object[] items;private int itemCount;private final int INIT_SIZE;

public ArrayBag() {itemCount = 0;INIT_SIZE = 100;items = new Object[INIT_SIZE];

} /* Implementations for add, remove, isEmpty and clone */}

Page 6: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

public class ArrayBag implements BagADT {

private Object[] items;private int itemCount;private final int INIT_SIZE;

public ArrayBag() { … }

public boolean isEmpty() {return (itemCount == 0);

}}

Page 7: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

public class ArrayBag implements BagADT {

private Object[] items;private int itemCount;private final int INIT_SIZE;

public ArrayBag() { … }public boolean isEmpty() { … }

public void add(Object item) {if (item == null)

throw new NullPointerException();

if (itemCount >= INIT_SIZE)throw new Error();

items[itemCount] = item;itemCount++;

}}

Page 8: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

public class ArrayBag implements BagADT {

private Object[] items;private int itemCount;private final int INIT_SIZE;

public ArrayBag() { … }public boolean isEmpty() { … }

public void add(Object item) {…} public Object remove() throws NoSuchElementException {

if (itemCount == 0)throw new NoSuchElementException();

else {itemCount--;return items[itemCount];

}}

Page 9: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

public class ArrayBag implements BagADT {

private Object[] items;private int itemCount;private final int INIT_SIZE;

public ArrayBag() { … }public boolean isEmpty() { … }

public void add(Object item) {…} public Object remove() throws NoSuchElementException {…}

public ArrayBag clone() {ArrayBag copy = new ArrayBag();copy.itemCount = itemCount;copy.items = items.clone();return copy;

}}

Page 10: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

Examples of using ArrayBag: ArrayBag bag = new ArrayBag();

bag.add(1); bag.add(2); bag.add(3); printBag(bag); int item = (int) bag.remove(); System.out.println(item);

Output is:3213

Page 11: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

Using the Object class in BagADT can be problematic

• You have to type-cast all objects returned by remove() (Why?)

• It is hard to enforce a uniform type in a bag.

• Bag declarations are uninformative. (All bags are essentially the same)

Page 12: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

Java Generics

Generics allow you to add a type parameter to an interface or class:

BagADT<E> or ArrayBag<E>

Page 13: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

When a type is declared, a class namereplaces the type parameter:

ArrayBag<Integer> or ArrayBag<String>

Only the declared type can be inserted.

Removed items need not be type-cast.

Page 14: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

public interface BagADT<E> {

void add(E item);

E remove () throws NoSuchElementException;

boolean isEmpty();

BagADT<E> clone();}

Page 15: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

public class ArrayBag<E> implements BagADT<E> {

/* Local data to implement a Bag */

/* One or more constructors */

/* Implementations for add, remove, isEmpty and clone */

}

Page 16: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

public class ArrayBag<E> implements BagADT<E> {

private E[] items;

private int itemCount;

private final int INIT_SIZE;

/* One or more constructors */

/* Implementations for add, remove, isEmpty and clone */

}

Page 17: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

public class ArrayBag<E> implements BagADT<E> {

private E[] items;private int itemCount;private final int INIT_SIZE;

public ArrayBag() {itemCount = 0;INIT_SIZE = 100;

// Kludge alert! items = (E[]) new Object[INIT_SIZE];

} /* Implementations for add, remove, isEmpty and clone */}

Page 18: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

public class ArrayBag<E> implements BagADT<E> {

private E[] items;private int itemCount;private final int INIT_SIZE;

public ArrayBag() { … }

public boolean isEmpty() {return (itemCount == 0);

}}

Page 19: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

public class ArrayBag<E> implements BagADT<E> {

private E[] items;private int itemCount;private final int INIT_SIZE;

public ArrayBag() { … }public boolean isEmpty() { … }

public void add(E item) {if (item == null)

throw new NullPointerException();

if (itemCount >= INIT_SIZE)throw new Error();

items[itemCount] = item;itemCount++;

}}

Page 20: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

public class ArrayBag<E> implements BagADT<E> {

private E[] items;private int itemCount;private final int INIT_SIZE;

public ArrayBag() { … }public boolean isEmpty() { … }

public void add(Object item) {…} public E remove() throws NoSuchElementException {

if (itemCount == 0)throw new NoSuchElementException();

else {itemCount--;return items[itemCount];

}}

Page 21: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

public class ArrayBag<E> implements BagADT<E> {

private E[] items;private int itemCount;private final int INIT_SIZE;

public ArrayBag() { … }public boolean isEmpty() { … }

public void add(Object item) {…} public Object remove() throws NoSuchElementException {…}

public ArrayBag<E> clone() {ArrayBag<E> copy = new ArrayBag<E>();copy.itemCount = itemCount;copy.items = items.clone();return copy;

}}

Page 22: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

printBag becomes:

public void printBag(BagADT<E> myBag){ BagADT<E> temp = myBag.clone();

while(! temp.isEmpty()) { System.out.println(temp.remove()); }}

Page 23: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

Examples of using ArrayBag in Generic form:

ArrayBag<Integer> bag = new ArrayBag<Integer>();bag.add(1); bag.add(2); bag.add(33);bag.printBag(bag);int item = bag.remove(); // No casting!System.out.println(item);

Output is:332133

Page 24: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

List ADT

A List is an ordered collection of items.Each item has a position, starting at 0.

Item: “a” “b” “c” “d” “e”

Position: 0 1 2 3 4

Page 25: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

Like an array, a list can be indexed.

But, a list can grow or shrink in size.

A size of zero (an empty list) is allowed.

Page 26: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

Operations in a ListADT

void add(E item)Add where? At right end of list.

void add(int pos, E item)add does not overwrite items, so list size grows.Valid values for pos are 0 <= pos <= size()-1

Page 27: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

boolean contains(E item) Is E already in the list? Use equals(item) to test membership.

int size() Zero size is OK.

boolean isEmpty() Same as size() == 0

Page 28: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

E get(int pos) Return value at pos. Non-destructive. Requires 0 <= pos <= size()-1

E remove(int pos) Remove and return value at pos. Is destructive. Requires 0 <= pos <= size()-1

Page 29: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

Error ConditionsCan null be added? We’ll ignore adds of null. contains must handle null correctly.

Bad pos values will throw IndexOutOfBounds

get or remove on empty list is really a bad pos error

Page 30: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18
Page 31: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

Interface definition for ListADT

public interface ListADT<E> {void add(E item);void add(int pos, E

item);boolean contains(E item);int size( );boolean isEmpty( );E get(int pos);E remove(int pos);

}

Page 32: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

Using the ListADT

Write a method that reverses the contents of a list.

Thus (1,2,3,4) becomes (4,3,2,1).

Choose the approach you will take before writing code.

Page 33: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

One approach:

Move 2nd from right to very end.Then 3rd from right to very end.…Finally, farthest from right (leftmost)To very end.(11,22,33,44) (11,22,44,33)(11,44,33,22) (44,33,22,11)

Page 34: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

Java code to reverse a List

void reverse(){for (int i = size() - 2; i >= 0;

i--)add(remove(i));

}

Why start i at size() - 2?

Are “corner cases” (lists of size 0 or 1) handledproperly?

Page 35: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

Let’s build a ListAST using an array.

Arrays are simple to use but also have a fixed size. We’ll expand the list as necessary when the list grows.

Page 36: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

public class SimpleArrayList<E> implements ListADT<E>{

/* Local data to implement a list*/

/* One or more constructors */

/* Implementations for add, remove, isEmpty, size, get and contains */

}

Page 37: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

public class SimpleArrayList<E> implements ListADT<E>{

private E[] items;

private int itemCount;

private final int INIT_SIZE;

/* One or more constructors */

/* Implementations for interface methods */

}

Page 38: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

public class SimpleArrayList<E> implements ListADT<E>{

private E[] items;private int itemCount;private final int INIT_SIZE;

public SimpleArrayList() {itemCount = 0;INIT_SIZE = 100;items = (E[]) new Object[INIT_SIZE];

} /* Implementations for interface methods */}

Page 39: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

public class SimpleArrayList<E> implements ListADT<E>{

private E[] items;private int itemCount;private final int INIT_SIZE;

public SimpleArrayList() {. . . }. . .

public boolean isEmpty() {return (itemCount == 0);

}}

Page 40: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

public class SimpleArrayList<E> implements ListADT<E>{

private E[] items;private int itemCount;private final int INIT_SIZE;

public SimpleArrayList() {. . . }. . .

public int size() {return itemCount;

}}

Page 41: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

public class SimpleArrayList<E> implements ListADT<E>{

. . .

public void add(E item) {if (item == null)

throw new NullPointerException();

if (itemCount == items.length)expandArray();

items[itemCount] = item;itemCount++;

}}

Page 42: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

public class SimpleArrayList<E> implements ListADT<E>{

. . .

private void expandArray() { E[] newArray =

(E[]) new Object[itemCount*2]; for (int k = 0; k < itemCount; k+

+) { newArray[k] = items[k]; } items = newArray;} }}

Page 43: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

public class SimpleArrayList<E> implements ListADT<E>{

private Object[] items;private int itemCount;private final int INIT_SIZE;

public ArrayBag() { … }public boolean isEmpty() { … }

public void add(Object item) {…} public Object remove() throws NoSuchElementException {

if (itemCount == 0)throw new NoSuchElementException();

else {itemCount--;return items[itemCount];

}}

Page 44: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

public class SimpleArrayList<E> implements ListADT<E>{

. . .

public void add(int pos, E item){if (item == null)

throw new NullPointerException();if (itemCount == items.length)

expandArray();if (pos < 0 || pos > itemCount) throw new

IndexOutOfBoundsException();// move items over and insert new itemfor (int k = itemCount; k > pos; k--) items[k] = items[k-1];items[pos] = item;itemCount++;

}}

Page 45: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

public class SimpleArrayList<E> implements ListADT<E>{

. . .

public E get(int pos) { // check for bad pos if (pos < 0 || pos >= itemCount) { throw new

IndexOutOfBoundsException(); } // return the item at pos return items[pos];};

}

Page 46: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

public class SimpleArrayList<E> implements ListADT<E>{

. . .

public E remove(int pos) { // check for bad pos if (pos < 0 || pos >= itemCount) throw new IndexOutOfBoundsException(); // get the item to be removed from pos E ob = items[pos]; // move items over to fill removed pos for (int k = pos; k < itemCount-1; k++) items[k] = items[k+1]; // decrease the number of items itemCount--; // return the removed item return ob;}}

Page 47: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

public class SimpleArrayList<E> implements ListADT<E>{

. . .

public boolean contains(E item){// null values are not allowed in

the listif (item == null)

return false;for (int i=0; i < itemCount; i++){

if (items[i].equals(item))return true;

}return false;

} }

Page 48: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

A Very Useful Auxiliary Method

In testing and debugging ADTs, it is very useful to see their contents.In Java, you may add a “toString” method to a class. When Java tries to print an object, it calls toString to obtain a string representation of the object, and then prints that string.

Page 49: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

toString methods need not be very complex to be useful:public String toString() {

String result = "(";for (int k = 0; k < itemCount; k++)

{if (k==0)

result += items[0].toString() ;

else result += ("," + items[k].toString());

}return result + ")";

}

Page 50: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

Are lists of lists allowed?

They are, and can be very useful.Consider:

SimpleArrayList<SimpleArrayList<Integer>> LL = new SimpleArrayList<SimpleArrayList<Integer>>();

SimpleArrayList<Integer> L3 = new SimpleArrayList<Integer>();SimpleArrayList<Integer> L4 = new SimpleArrayList<Integer>();

Page 51: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

L3.add(123); L3.add(234); L3.add(345);System.out.println(L3);L4.add(456); L4.add(567); System.out.println(L4);LL.add(L3); LL.add(L4);System.out.println(LL);

What is printed?(123,234,345)(456,567)((123,234,345),(456,567))

Page 52: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

Iterators

Most ADTs in Java can provide an iterator object, used to traverse all the data in an ADT.

Page 53: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

Iterator Interfacepublic interface Iterator<E>{

boolean hasNext();

E next(); void remove(); // Optional }

Page 54: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

Getting an Iterator

You get an iterator from an ADT by calling the method iterator();

Iterator<Integer> iter = myList.iterator();

Page 55: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

Now a simple while loop can process each data value in the ADT:

while(iter.hasNext()) {process iter.next()

}

Page 56: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

A Simple Print method for Listsvoid printArrayList(){

Iterator<E> iter = this.iterator();

while(iter.hasNext()){ System.out.print(iter.next()+"

");}System.out.println();

}

Page 57: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

Adding Iterators to SimpleArrayList is easy

First, we add the iterator() method to SimpleArrayList:public Iterator<E> iterator(){

return new ArrayListIterator<E>(this);}

Page 58: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

Then we implement the iterator class for Lists:

import java.util.*;public class ArrayListIterator<E> implements Iterator<E> {

// *** fields *** private SimpleArrayList<E> list; private int curPos; public ArrayListIterator( SimpleArrayList<E> list) { this.list = list; curPos = 0; }

Page 59: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

public boolean hasNext() { return curPos < list.size(); } public E next() { if (!hasNext()) throw new NoSuchElementException();

E result = list.get(curPos); curPos++; return result; } public void remove() { throw new UnsupportedOperationException(); }

}

Page 60: CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

Empty vs. Null

In Java all objects are accessed through a reference. A reference may be null. This is not the same as a data object that has nothing in it.

Consider String str1 = “”; String str2 = null;str1 references something (the empty string)str2 references nothing.