Download DataStructures-Abstr..

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Linked list wikipedia , lookup

Java ConcurrentMap wikipedia , lookup

Transcript
Data Structures and Abstract Data Types
"Get your data structures correct
first, and the rest of the program will
write itself."
- David Jones
Abstract Data Types
Abstract Data Types (aka ADTs) are
descriptions of how a data type will work
without implementation details
Description can be a formal, mathematical
description
Java interfaces are a form of ADTs
– some implementation details start to creep in
CS 221 - Computer Science II
Data Structures
A Data Structure is:
– an implementation of an abstract data type and
– "An organization of information, usually in
computer memory", for better algorithm
efficiency."
List Object
aList
5
size
myElements
0 1 2 3 4
A C E B A
CS 221 - Computer Science II
5 6
7
8 9 10
Data Structure Concepts
Data Structures are containers:
– they hold other data
– arrays are a data structure
– ... so are lists
Other types of data structures:
– stack, queue, tree,
binary search tree, hash table,
dictionary or map, set, and on and on
– en.wikipedia.org/wiki/List_of_data_structures
Different types of data structures are optimized for
certain types of operations
CS 221 - Computer Science II
Core Operations
Data Structures have three core operations
– a way to add things
– a way to remove things
– a way to access things
Details of these operations depend on the
data structure
– Example: List, add at the end, access by
location, remove by location
More operations added depending on what
data structure is designed to do
CS 221 - Computer Science II
Implementing ADTs
When implementing an ADT, the operations
and behaviors are already specified
– think Java interface
But what use as the internal storage
container for the concrete data type?
– the internal storage container is used to hold the
items in the collection
– initially slim pickings for choice of storage
containers: arrays anyone?
– later add linked structures
– now often an implementation of an ADT (which
use arrays or linked structures)
CS 221 - Computer Science II
Bags and Sets
Simplest ADT is a Bag
– items can be added, removed, accessed
– no implied order to the items
– duplicates allowed
Set
– same as a bag, except duplicate elements not
allowed
– union, intersection, difference, subset
CS 221 - Computer Science II
Lists
Items have a position in this Collection
– Random access or not?
Array Lists
– internal storage container is native array
Linked Lists
public class Node
{ private Object data;
private Node next;
first
}
CS 221 - Computer Science II
last
Stacks
Collection with access only to the last
element inserted
– Last in first out
– push (insert)
– pop (remove)
– peek (top item)
– make empty
Data4
Data3
Data2
Data1
CS 221 - Computer Science II
Top
Queues
Collection with access only to the item that
has been present the longest
– first in,first out or last in, last out
– enqueue (insert)
– dequeue (remove)
– front
Front
Back
Data1
Data2
Data3
CS 221 - Computer Science II
Data4
Stacks and Queues in the
Java Collection API
No queue in the Java collections ADT
Stack extends Vector (which is almost
exactly like ArrayList)
– Hmmm?
One reason the Java Collections Library is
often said to be broken
no Queue in Collection API
CS 221 - Computer Science II
Trees
Similar to a linked list
public class TreeNode
{ private Object data;
private TreeNode left;
private TreeNode right;
}
CS 221 - Computer Science II
Root
Other Types of Trees
Binary Search Trees
– sorted values
Heaps
– sorted via a different algorithm
AVL and Red-Black Trees
– binary search trees that stay balanced
Splay Trees
B Trees
CS 221 - Computer Science II
HashTables
Take a key, apply function
f(key) = hash value
store data or object based on hash value
Sorting O(N), access O(1) if a perfect hash
function and enough memory for table
How deal with collisions?
CS 221 - Computer Science II
Other ADTs
Maps
– a.k.a. Dictionary
– Collection of items with a key and associated
values
– similar to hash tables, and hash tables often
used to implement Maps
Graphs
– Nodes with unlimited connections between other
nodes
Sparse vectors and sparse matrices
CS 221 - Computer Science II
Generic Containers
ADTs or Collection classes should be
generic
– only write them once, hold lots or all types of
data
– Java achieves genericity through inheritance and
polymorphism
ADTs have an internal storage container
– What is storing the stuff,
– implementation vs. abstraction
– in Java, usually holds Objects. Why?
CS 221 - Computer Science II
CS 221 - Computer Science II
ADTs and Data Structures in
Programming Languages
Modern programming languages usually
have a library of data structures
– Java collections framework
– C++ standard template library
– .Net framework (small portion of VERY large
library)
– Python lists and tuples
– Lisp lists
CS 221 - Computer Science II
Data Structures in Java
Part of the Java Standard Library is the
Collections Framework
– In class we will create our own data structures
and discuss the data structures that exist in Java
A library of data structures
Built on two interfaces
– Collection
– Iterator
http://java.sun.com/j2se/1.5.0/docs/guide/coll
ections/index.html
CS 221 - Computer Science II
The Java Collection Interface
A generic collection
Can hold any object data type
Which type a particular collection will hold is
specified when declaring an instance of a
class that implements the Collection interface
Helps guarantee type safety at compile time
CS 221 - Computer Science II
Methods in the Collection interface
public interface Collection<E>
{
public boolean add(E o)
public boolean addAll(Collection<? extends E> c)
public void clear()
public boolean contains(Object o)
public boolean containsAll(Collection<?> c)
public boolean equals(Object o)
public int hashCode()
public boolean isEmpty()
public Iterator<E> iterator()
public boolean remove(Object o)
public boolean removeAll(Collection<?> c)
public boolean retainAll(Collection<?> c)
public int size()
public Object[] toArray()
public <T> T[] toArray(T[] a)
}
CS 221 - Computer Science II
The Java ArrayList Class
Implements the List interface and uses an
array as its internal storage container
It is a list, not an array
The array that actual stores the elements of
the list is hidden, not visible outside of the
ArrayList class
All actions on ArrayList objects are via the
methods
ArrayLists are generic.
– They can hold objects of any type!
CS 221 - Computer Science II
ArrayList's (Partial)
Class Diagram
Iterable
Object
Collection
AbstractCollection
List
AbstractList
ArrayList
CS 221 - Computer Science II
java.util
ArrayList Specification
Class ArrayList
java.lang.Object
|
+--java.util.AbstractCollection
|
+--java.util.AbstractList
|
+--java.util.ArrayList
All Implemented Interfaces:
Cloneable, Collection, List, Serializable
void add(int index, Object element)
Inserts the specified element at the specified position in this list.
boolean add(Object o)
Appends the specified element to the end of this list.
void clear()
Removes all of the elements from this list.
boolean contains(Object elem)
Returns true if this list
contains the specified element.
int indexOf(Object elem)
Searches for the first occurence of the given argument, testing for
equality using the equals method.
boolean isEmpty()
Tests if this list has no elements.
Object set(int index, Object element)
Replaces the element at the specified position in this list with the
specified element.
int size()
Returns the number of elements in this list.