Professional Documents
Culture Documents
Data Structures
Data structure is a representation of data and the operations allowed on that data.
Specify the operations of the data structure and leave implementation details to later
in Java use an interface to specify operations
Why Abstract?
Many different ways to implement these items each with associated costs and benefits
CS 307 Fundamentals of Computer Science
Implementing ADTs
when implementing an ADT the operations and behaviors are already specified
think Java interface
Implementers first choice is what to use as the internal storage container for the concrete data type
the internal storage container is used to hold the items in the collection often an implementation of an ADT initially slim pickings for choice of storage containers: arrays anyone?
CS 307 Fundamentals of Computer Science
Set
same as a bag, except duplicate elements not allowed union, intersection, difference, subset
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 }
last
Stacks
Collection with access only to the last element inserted Data4 Last in first out Data3 insert/push Data2 remove/pop Data1 top make empty
Top
10
Queues
Collection with access only to the item that has been present the longest Last in last out or first in first out enqueue, dequeue, front priority queues and deque
Front Back
Data1
CS 307 Fundamentals of Computer Science
Data2
Data3
Data4
11
One reason the Java Collections Library is often said to be broken no Queue in Collection API
CS 307 Fundamentals of Computer Science
12
Trees
Similar to a linked list public class TreeNode { private Object data; private TreeNode left; private TreeNode right; }
Root
13
Heaps
sorted via a different algorithm
14
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?
15
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
16
boolean isEmpty() int size() boolean add(Object x) boolean contains(Object x) boolean remove(Object x) void clear() Object[] toArray() Iterator iterator()
CS 307 Fundamentals of Computer Science
17
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
18
19
Searches for the first occurence of the given argument, testing for equality using the equals method.
boolean isEmpty()
Replaces the element at the specified position in this list with the specified element.
size() int CS 307 Fundamentals of Returns the number of elements in this list. Computer Science
20