Download Data Structures: Abstract Data Types (ADTs) and Interfaces in Java and more Papers Data Structures and Algorithms in PDF only on Docsity!
CS 261 – Data Structures
Abstract Data Types (ADTs)
Types
What is a type?
1. Set of possible values
2. Operations on those values
3. Properties
Example: Integer (int) type
Values : -2147483648 to 2147483647
Operations : +, - , *, /, ++, --, etc.
Properties : Closed under +, - , *, /, ++, -- (i.e., 3 + 5 = 8)
Abstract Data Type (ADT)
An Abstract Data Type is:
A behavior-level description of the operations on and properties of
some data/values
Values (or data): Instance(s) of some type/class
Operations: Services provided by the ADT on the data
Properties: What is known about the data
- As the definition indicates, an ADT is mainly concerned
with behavior (i.e., operations/services)
What OOP (and Java in particular) mechanism(s) should we use to
describe behavior?
Example: Points
Goal: create an ADT that defines what you can do with a point.
- Each point has a position in some space (2-D, 3-D, n -D)
- Each point knows how to compute the distance from itself to
another point of the same type
- Each point also knows how many dimensions it has
- Create an abstraction of Point using the concepts we’ve
discussed.
- Also, create various implementations of a point:
- Pnt2DInt: 2-D integer (for screen coordinates)
- Pnt2DFlt: 2-D floating point (for 2-D graphics)
- Pnt3DFlt: 3-D floating point (for 3-D graphics)
ADTs: Interfaces & Classes
- An ADT provides a high level description of the behavior
only:
- ADTs enforce encapsulation
- In Java: use an interface to describe ADTs
- Use a class to create a data structure that implements
an interface (an ADT)
- A class can implement ANY number of interfaces
- Multiple Inheritance
- It must provide a method body for all methods described in each
interface inherited
- Classes can only inherit (extend) one base class
- Gets data fields and method implementations
ADTs: Abstract vs. Interface
- What are the advantages and disadvantage of using an
abstract class instead of an interface:
- An abstract class can include data fields and implementation
- Other classes can only inherit from one abstract class (or any other class)
- Forces subclasses to adhere to the abstraction and none other
Common ADTs
- We will bounce between abstractions from high-level
interfaces to low-level implementations
- Remember: an interface describes the behavior (the “what”), a
class defines the implementation (the “how”)
Collection
Bag
Set
Sorted
Comparator
Comparable
Stack
Queue
Deque
FindMin
FindNth
Indexed
SortAlgorithm
Map
Matrix
ADT: Collection
public interface Collection extends Serializable { public Enumeration elements(); public boolean isEmpty(); public int size(); }
- Has elements that we can enumerate and count:
- No method to affect any permanent change
- Conceptually, we don’t care how elements are stored or
counted
- Interfaces are often used to emphasize this generality
Serializable means that an instance of this class can be stored in a binary format to files
ADT: Set
public interface Set extends Collection { // Single element operations. public void addElement(Object elem); public boolean containsElement(Object elem); public Object findElement(Object elem); public void removeElement(Object elem); public void unionWith(Set set); // Set-Set operations. public void intersectWith(Set set); public void differenceWith(Set set); public boolean subsetOf(Set set); }
- Corresponds to mathematical notion of a set:
- Question: Why does redefine the Bag interface instead of extending Bag?
- Answer: addElement only adds the element if it is not already in Set
- Includes standard set-set operations
ADT: Sorted
public interface Sorted extends Collection { // NO NEW METHODS. }
- Adds NO NEW METHODS.
- In this case the ADT specifies a property and not an
operation:
- If your class supports Sorted, you’re implying that certain
properties hold and that any class implementing Sorted will be
expected to ensure that those properties are true … i.e., the data
is sorted
ADT: Comparator & Comparable
Defines objects that can be compared (less than, equal to,
greater than):
public interface Comparator extends Serializable { int compare(Object left, Object right); }
Returns –1 (if left < right), 0 (if left == right), or 1 (if left > right)
public interface Comparable extends Serializable { public int compareTo(Object value); }
If your class implements the Comparable interface, then you can
use the standard Comparator class (DefaultComparator) to
compare instances of your class
ADT: Stack
public interface Stack extends Collection { public void addLast(Object elem); // Push public Object getLast(); public void removeLast(); // Pop }
- Specifies a LIFO (Last-In, First-Out) interface
- Sometimes called a “Push-down” stack:
- Similar in concept to the spring-loaded, push-down stack of
plates at a buffet-style restaurant
- A plate is added to the stack by placing it on top (pushing down
the other plates)
- A plate is removed from the stack by taking it off the stack (and
the plates underneath it pop up one position)
ADT: FindMin
public interface FindMin extends Collection { public void addElement(Object elem); public Object getFirst(); public void removeFirst(); }
- Like Bag, allows you to add an element
- Unlike Bag, searches on a property rather than a value:
- Not “is this value in the collection”, but “get me (or remove) the
smallest value in the collection”
- Like Collection and Bag, does NOT assume that
elements are sorted
ADT: FindNth
public interface FindNth { public Object findNth(int index); }
- Allows you to find the n th^ smallest value in a group:
- Example: find the median – i.e., find the ( n /2)th^ element
- Also assumes no order
- Does not extend any previous ADT:
- Does not allow for elements to be added or removed
- Usually combined with other interfaces
- Implemented by SortedVector and Partition (Chap. 7)
ADT: Indexed Collection
public interface Indexed extends Collection{ public void setSize(int size); public Object elementAt(int idx); public void setElementAt(Object val, int idx); public void addElementAt(Object val, int idx); public void removeElementAt(int index); }
- Allows access to elements via an index: Conceptually
similar to array indexing
- Enumeration will always result in same order – first to last
according to index
- Implemented by the Vector class
ADT: Sorting Algorithm
public interface SortAlgorithm { public void sort(Indexed data); }
- Important task for an indexed collection is to rearrange
elements in nondecreasing sequence – i.e., sorted order
- All sorting is done by a class that implements
SortAlgorithm