Download elc312_day25&26

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

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

Document related concepts
no text concepts found
Transcript
ELC 310
Day 24
Agenda
• Questions?
• Problem set 5 Parts A & B Corrected
 Good results
 2 A’s, 1 B and 1 non-submit
• Problem set 6 posted
 Due Dec 13
• Last Capstone Progress Report Due
 Capstone project & presentations Due December 16
• Exam # 4
 December 13
 Chap 10, 11, & 12, 25 M/C
 45 min
• Discussion on Collections
© 2004 Pearson Addison-Wesley. All rights reserved
12-2
Chapter 12
Collections
Collections
• A collection is an object that helps us organize and
manage other objects
• Chapter 12 focuses on:




the concept of a collection
separating the interface from the implementation
dynamic data structures
Classic linear data structures
• Queues and stacks
 Classic nonlinear data structures
• Trees and graphs
 Java Collections API
 Generic types in collections classes
© 2004 Pearson Addison-Wesley. All rights reserved
12-4
Outline
Data Structures
Dynamic Representations
Queues and Stacks
Trees and Graphs
The Java Collections API
© 2004 Pearson Addison-Wesley. All rights reserved
12-5
Collections and Data Structures
• A collection is an object that serves as a
repository for other objects
• A collection usually provides services such as
adding, removing, and otherwise managing the
elements it contains
 ArrayList represents a collection
• Sometimes the elements in a collection are
ordered, sometimes they are not
• Sometimes collections are homogeneous,
sometimes the are heterogeneous
 ArrayList is heterogeneous since can contain objects of
various types
© 2004 Pearson Addison-Wesley. All rights reserved
12-6
Separating Interface from
Implementations
• Collections can be implemented in many different ways
• An abstract data type (ADT) is an organized collection of
information and a set of operations used to manage that
information
 Has a Name, domain of values, set of operations
• The set of operations defines the interface to the ADT
• As long as the ADT fulfills the promises of the interface, it
doesn't really matter how the ADT is implemented
• Objects are a perfect programming mechanism to create
ADTs because their internal details are encapsulated
© 2004 Pearson Addison-Wesley. All rights reserved
12-7
Dynamic Representations
• Array is one way a list can be represented, there
are other ways
 Arrays are static (fixed length)
• Dynamic data structures are implemented using
Links to other objects
Class Node
{
int info;
Node next;
}
• Allows object to be “chained together” to created
a linked list
© 2004 Pearson Addison-Wesley. All rights reserved
12-8
Static vs. Dynamic Structures
• A static data structure has a fixed size
• This meaning is different from the meaning of the
static modifier
• Arrays are static; once you define the number of
elements it can hold, the number doesn’t change
• A dynamic data structure grows and shrinks at
execution time as required by its contents
• A dynamic data structure is implemented using
links
© 2004 Pearson Addison-Wesley. All rights reserved
12-9
Object References
• Recall that an object reference is a variable that
stores the address of an object
• A reference also can be called a pointer
• References often are depicted graphically:
student
John Smith
40725
3.58
© 2004 Pearson Addison-Wesley. All rights reserved
12-10
References as Links
• Object references can be used to create links
between objects
• Suppose a Student class contains a reference to
another Student object
John Smith
40725
3.57
© 2004 Pearson Addison-Wesley. All rights reserved
Jane Jones
58821
3.72
12-11
References as Links
• References can be used to create a variety of
linked structures, such as a linked list:
studentList
• Linked list has a reference variable to start to the
list and ends with a null pointer
© 2004 Pearson Addison-Wesley. All rights reserved
12-12
Intermediate Nodes
• The objects being stored should not be concerned
with the details of the data structure in which they
may be stored
• For example, the Student class should not have
to store a link to the next Student object in the list
• Instead, we can use a separate node class with
two parts:
 1) a reference to an independent object
 2) a link to the next node in the list
• The internal representation becomes a linked list
of nodes
Storage
Storage
Storage
Start
© 2004 Pearson Addison-Wesley. All rights reserved
Object
Object
Object
Stored
Object
Stored
Object
Stored
Object
NULL
12-13
A dynamically Linked List
• Let’s explore an example of a collection of Magazine objects
• The collection is managed by the MagazineList class,
which has an private inner class called MagazineNode
• Because the MagazineNode is private to MagazineList, the
MagazineList methods can directly access MagazineNode
data without violating encapsulation
• See MagazineRack.java (page 615)
• See MagazineList.java (page 616)
• See Magazine.java (page 618)
© 2004 Pearson Addison-Wesley. All rights reserved
12-14
Magazine Collection
• A method called insert could be defined to add a
node anywhere in the list, to keep it sorted, for
example
Magazine Rack
New
magazine
© 2004 Pearson Addison-Wesley. All rights reserved
12-15
Magazine Collection
• A method called delete could be defined to
remove a node from the list
Magazine Rack
© 2004 Pearson Addison-Wesley. All rights reserved
12-16
Other Dynamic List representations
• It may be convenient to implement as list as a
doubly linked list, with next and previous
references
list
© 2004 Pearson Addison-Wesley. All rights reserved
12-17
Other Dynamic List
Implementations
• It may be convenient to use a separate header
node, with a count and references to both the front
and rear of the list
list
count: 4
front
rear
© 2004 Pearson Addison-Wesley. All rights reserved
12-18
Other Dynamic List
implementations
• A linked list can be circularly linked in which case
the last node in the list points to the first node in
the list
• If the linked list is doubly linked, the first node in
the list also points to the last node in the list
• The representation should facilitate the intended
operations and should make them easy to
implement
© 2004 Pearson Addison-Wesley. All rights reserved
12-19
Classic Data Structures
• Classic linear data structures include queues and
stacks
• Classic nonlinear data structures include trees,
binary trees, graphs, and digraphs
© 2004 Pearson Addison-Wesley. All rights reserved
12-20
Queues
• A queue is similar to a list but adds items only to
the rear of the list and removes them only from the
front
• It is called a FIFO data structure: First-In, First-Out
• Analogy: a line of people at a bank teller’s window
enqueue
© 2004 Pearson Addison-Wesley. All rights reserved
dequeue
12-21
Queues
• We can define the operations for a queue
 enqueue - add an item to the rear of the queue
 dequeue (or serve) - remove an item from the front of the
queue
 empty - returns true if the queue is empty
• As with our linked list example, by storing generic
Object references, any object can be stored in the
queue
• Queues often are helpful in simulations or any
situation in which items get “backed up” while
awaiting processing
© 2004 Pearson Addison-Wesley. All rights reserved
12-22
Stacks
• A stack ADT is also linear, like a list or a queue
• Items are added and removed from only one end of
a stack
• It is therefore LIFO: Last-In, First-Out
• Analogies: a stack of plates in a cupboard, a stack
of bills to be paid, or a stack of hay bales in a barn
© 2004 Pearson Addison-Wesley. All rights reserved
12-23
Stacks
• Stacks often are drawn vertically:
push
© 2004 Pearson Addison-Wesley. All rights reserved
pop
12-24
Stacks
• Some stack operations:




push - add an item to the top of the stack
pop - remove an item from the top of the stack
peek (or top) - retrieves the top item without removing it
empty - returns true if the stack is empty
• A stack can be represented by a singly-linked list;
it doesn’t matter whether the references point from
the top toward the bottom or vice versa
• A stack can be represented by an array, but the
new item should be placed in the next available
place in the array rather than at the end of the
array
© 2004 Pearson Addison-Wesley. All rights reserved
12-25
Stacks
• The java.util package contains a Stack class
 http://java.sun.com/j2se/1.5.0/docs/api/java/util/Stack.html
 Has search method
• Like ArrayList operations, the Stack operations
operate on Object references
• See Decode.java (page 623)
© 2004 Pearson Addison-Wesley. All rights reserved
12-26
ELC 310
Day 25
Agenda
• Questions?
• Problem set 6 posted
 Due Dec 13
• Capstone project & presentations
 Due December 16
• Exam # 4
 December 13
 Chap 10, 11, & 12, 25 M/C
 45 min
• Discussion on Non-linear Data Structures
© 2004 Pearson Addison-Wesley. All rights reserved
12-28
Trees and Binary Trees
• A tree is a non-linear data structure that consists
of a root node and potentially many levels of
additional nodes that form a hierarchy
• Nodes that have no children are called leaf nodes
• Nodes except for the root and leaf nodes are called
internal nodes
Root
Internal node
Leaf nodes
© 2004 Pearson Addison-Wesley. All rights reserved
12-29
Trees and Binary Trees
• A binary tree is defined recursively. Either it is
empty (the base case) or it consists of a root and
two subtrees, each of which is a binary tree
• Binary trees and trees typically are represented
using references as dynamic links, though it is
possible to use fixed representations like arrays
• Binary tree example
 http://www.cs.umd.edu/~egolub/Java/BinaryTree.html
© 2004 Pearson Addison-Wesley. All rights reserved
12-30
Graphs and Digraphs
• A graph is a non-linear structure
• Unlike a tree or binary tree, a graph does not have
a root
• Any node in a graph can be connected to any
other node by an edge
• Analogy: the highway system connecting cities on
a map
© 2004 Pearson Addison-Wesley. All rights reserved
12-31
Graphs and Digraphs
• In a directed graph or digraph, each edges has a
specific direction.
• Edges with direction sometimes are called arcs
• Analogy: airline flights between airports
© 2004 Pearson Addison-Wesley. All rights reserved
12-32
Graphs and Digraphs
• Both graphs and digraphs can be represented
using dynamic links or using arrays.
• As always, the representation should facilitate the
intended operations and make them convenient to
implement
• Example
 http://www.jibble.org/applets/montygraph/
© 2004 Pearson Addison-Wesley. All rights reserved
12-33
Collection Classes
• The Java standard library contains several classes
that represent collections, often referred to as the
Java Collections API
• Their underlying implementation is implied in the
class names such as ArrayList and LinkedList
• Several interfaces are used to define operations
on the collections, such as List, Set, SortedSet,
Map, and SortedMap
• Collections Tutorial
© 2004 Pearson Addison-Wesley. All rights reserved
12-34
Generics
• The classes implemented in the JAVA collections
API are generic types
 The programmer established the type of object the
collections manages when the collection type is
instantiated
 LinkedList<String> aName= new LinkedList<String> ();
 LinkedList<Book> anotherName= new LinkedList<Book>
();
• Specifying type enables two things
 Limits the collections to only objects of the correct type
(don’t forget about polymorphism)
 No need to cast objects when they are removed from the
collection
• If not object type is specified than the collection
can store any object
© 2004 Pearson Addison-Wesley. All rights reserved
12-35
Summary
• Chapter 12 focuses on:




the concept of a collection
separating the interface from the implementation
dynamic data structures
Classic linear data structures
• Queues and stacks
 Classic nonlinear data structures
• Trees and graphs
 Java Collections API
 Generic types in collections classes
© 2004 Pearson Addison-Wesley. All rights reserved
12-36