Transcript (ppt)

CS100A, Fall 1997
Lecture 13, Thursday, 16 October
More about Arrays
Maintaining a Sorted List
An Automatic Expanding List Class
Concepts:
• Review of List class (integer lists)
• Extending List to create sorted and
dynamically expanding lists.
Reading in Lewis/Loftus (review)
6.1 (208-219), 8 (285-329)
CS100A, Fall 1997, Lecture 13
1
Review
In the last lecture we designed a class List to
maintain unsorted lists of integers with the
following operations.
• Constructors: List( ) creates an empty list
with space for 100 integers; List(n) creates
an empty list with space for n integers.
• makeEmpty( ) — set the list to empty.
• add(n) — add n to the list if n is not already
in the list
• delete(n) — delete n from the list
• isMember(n) — yield true if n is in the list,
otherwise false
• size( ) — yield number of items in the list
• toString( ) — yield a string representation
of the list.
Numbers in the list are stored in no
particular order. No duplicate numbers
appear in the list.
CS100A, Fall 1997, Lecture 13
2
Representation
class List
{private int nItems; // # of integers in the List
private int [ ] items; // integers in the List
// are stored in
// items[0..nItems-1];
A List object:
nItems
items
0
1
2
3
…
methods: List, add, delete, …
CS100A, Fall 1997, Lecture 13
3
New Problems — Sorted List
Extend the List class to create a new class that
keeps the array contents in ascending order.
class SortedList extends List
{
?
}
Interface changes: None, except the result of
toString( ) will appear sorted.
Implementation changes:
• Representation?
• Operations?
What can we use (inherit) from class List?
What needs to be overridden (redefined)?
CS100A, Fall 1997, Lecture 13
4
Representation
• No change in actual data structures, but
items and nItems must be changed to
protected in class List so SortedList
operations can access them.
• Operations must preserve original
restrictions: nItems <= items.length data
stored in items[0..nItems-1], and no
duplicate values.
• In addition, must preserve
items[0] < items[1] < … < items[nItems-1].
nItems
items
0
1
2
3
…
methods: List, add, delete, …
CS100A, Fall 1997, Lecture 13
5
Searching the List — Find
The original Find still works, but there are
two worthwhile changes.
• To add a new integer, we need to know the
appropriate location, so we change find to
report a suitable place.
• If the array is sorted, we can stop searching
as soon as a number >= n is reached.
// Yield smallest k such that items[k] >= n,
// unless n is not in the list and is larger than
// all items in the list, then yield nItems.
private int find(int n) {
int k = 0;
// inv: n > items[0..k-1]
while (k < nItems && items[k] < n)
k = k + 1;
return k;
}
CS100A, Fall 1997, Lecture 13
6
Test
nItems
3
items 17 35 42
0
1
2
3
…
methods: List, add, delete, find, …
CS100A, Fall 1997, Lecture 13
7
Add
When an item is added, it must be inserted in
the right place, which requires moving all
larger items to the right.
public void add(int n) {
int loc, k;
loc = find(n);
if ( (loc == nItems || items[loc] != n) &&
nItems < items.length ) {
// add n to the list at items[loc]
// shift items[loc..nItems-1] right to
// items[loc+1..nItems]
k = nItems;
// inv: items[k..nItems-1] shifted to
//
items[k+1..nItems]
while (k > loc) {
items[k] = items[k-1];
k = k - 1;
}
CS100A, Fall 1997, Lecture 13
8
Add (cont.)
// add n to list at items[loc]
items[loc] = n;
nItems = nItems + 1;
}
}
}
Test
nItems
3
items 17 35 42
0
1
2
3
…
methods: List, add, delete, find, …
CS100A, Fall 1997, Lecture 13
9
Another Test
nItems
3
items 17 35 42
0
1
2
3
…
methods: List, add, delete, find, …
CS100A, Fall 1997, Lecture 13
10
Yet Another Test
nItems
3
items 17 35 42
0
1
2
3
…
methods: List, add, delete, find, …
CS100A, Fall 1997, Lecture 13
11
Delete
To delete an item that’s in the list, we slide all
larger values to the left to keep the numbers
in items[0..nItems-1] sorted.
// remove n from the List if it is present,
// otherwise do nothing.
public void delete(int n) {
int loc; // If n is in the list, loc < nItems
// and items[loc] = n.
int k;
loc = find(n);
CS100A, Fall 1997, Lecture 13
12
Delete (cont.)
if (loc < nItems && items[loc] == n) {
// delete n from the list
// shift items[loc+1..nItems-1] left to
// items[loc..nItems-2])
k = loc;
// inv: items[loc+1..k] shifted to
//
items[loc..k-1]
while (k < nItems -1) {
items[k] = items[k+1];
k = k + 1;
}
// reduce size of list by 1
nItems = nItems - 1;
}
}
CS100A, Fall 1997, Lecture 13
13
Test
nItems
5
items 17 18 31 35
0
1
2
3
42
…
methods: List, add, delete, find, …
CS100A, Fall 1997, Lecture 13
14
isMember
Membership testing is still easy, but the
previous test (find(n) < nItems) is not
sufficient by itself.
// yield true if n is a member of the List,
// otherwise yield false.
public boolean isMember(int n) {
int loc = find(n);
return loc < nItems &&
items[loc] == n;
}
Notice that this is only safe because && is a
short-circuit operation. We rely on that
property in many places in the code.
CS100A, Fall 1997, Lecture 13
15
A Self-Expanding List
Lists and SortedLists can become full. It’s
quite easy in Java to define a list data
structure that expands as needed.
The basic idea is that if an attempt is made to
add a new number and there’s no more
room, we allocate a new array twice the size
of the old one, copy the old value to it, and
use the new array in place of the old items
array.
class ExpandableSortedList
extends SortedList
{
…
}
CS100A, Fall 1997, Lecture 13
16
Add
The only method that needs to be overridden
is add, which allocates a new list if needed,
then calls the superclass add method to
actually search for and add the new number.
public void add(int n) {
// double size of list if no free space
if (nItems == items.length) {
// allocate new array
int[ ] newItems =
new int[2*items.length];
// copy old values to new array
int k = 0;
// inv: items[0..k-1] copied to
//
newItems[0..k-1]
while (k < nItems) {
newItems[k] = items[k];
k = k + 1;
}
CS100A, Fall 1997, Lecture 13
17
Add (cont.)
public void add(int n) {
// double size of list if no free space
if (nItems == items.length) {
// allocate new array
…
// copy old values to new array
…
// replace old array with new one
items = newItems;
}
// add n to the list if not already present
super.add(n);
}
Test
nItem 3
s
items 17 35 42
0 1 2
CS100A, Fall 1997, Lecture 13
18