Professional Documents
Culture Documents
Chapter 12
Chapter Contents
Chapter Objectives
12.1 Introductory Example: Counting Internet Addresses
12.2 The ArrayList and LinkedList Classes
12.3 Example: A Stack Application and Class
12.4 Example: A Queue Class
12.5 An Introduction to Trees
Part of the Picture: Data Structures
12.6 Graphical/Internet Java: A PolygonSketcher Class
Chapter Objectives
Study
Implement
Review Arrays
An
Drawback:
time
either the program runs out of space or
wastes space
Solution:
collection classes
ArrayList Class
Implements
reference type
cannot directly store primitive types
can indirectly store such values by using
instances of their wrapper types
Consider
the declaration:
size
array
Adding to addressSequence
The
command
addressSequence.add(anAddressCounter);
appends anAddressCounter object to the
sequence
The
Update size
system
will then
attribute of the
ArrayList
AddressSeqeunce
size
array
10
128.159.4.201
[1]
[2] . . .
[m-1]
Updating addressSequence
Consider
the command
((AddressCounter)
addressSequence.get(index)).incrementCount();
// assume index == 1
Cast it as an
AddressCounter
object
AddressSeqeunce
size
Gets this
Increment
object the count
attribute
array
[0]
[1]
[2] . . .
2
128.159.4.2011, 1
123.111.222.333, 12
[m-1]
Enlarging the
AddressSequence Array
When
AddressSeqeunce
size
array
[0]
[1]
[2] . . .
[n-1]
2
128.159.4.2011, 1
123.111.345.444, 1
123.111.222.333, 1
ArrayList Drawback
Problems
ArrayList
to insert a value and "shift" others after it
requires extensive copying of values
similarly, deleting a value requires shifting
We
head
size
tail
Resulting
Resulting object
object
shown
shown at
at left
left
88
77
66
aList
head size
tail
88
77
66
Attributes:
Attributes:
link
to
link
to first
first item
item in
in the
the list
list
Nodes:
Nodes:
size
of
Contain
size
of the
the33list
list
handles
Contain
handles
link
last
item
in
the
to
link
to
item
in
the list
list
link
to
linklast
to next
next node
node
link
link to
to previous
previous node
node
link
link to
to stored
stored object
object
Links
Links to
to next
next and
and
previous
previous make
make itit aa
doubly
doubly linked
linked list
list
only
last item in list has link set to null
Lists
Using a LinkedList
Given
the command
Uses
head
size
tail
addressSequence.add(anAddressCounter);
addressSequence
head
size
tail
Successive adds
create more nodes
and data values
adjust links
123.111.345.444, 1
((AddressCounter)
addresssSequence.get(index)).incrementCount();
Command
starts at
first node, iterates
i times to reach
desired node
get(i)
method
determines limit of loop
counter
size
attribute
3. returns handle saved in step 1, so object pointed to can
be output
We
can say
Which will
algorithm requires
Linear
time
time proportional to n
referred to as O(n), "order n"
Constant
time
expressed as O(1)
Demonstration of Efficiency
Note
Conclusions on Efficiency
If
If
If
reverse order
3
7
Stack Container
A
Implemented
has a LinkedList
contrast the "is-a" relationship
of the extends
Vector contains messages not appropriate
that should not be used in Stack
Examples of queues
checkout at supermarket
vehicles at toll booth
ticket line at movies
Queue
exhibits First-In-First-Out
behavior
Queue
Manipulation Operations
value
as a LinkedList attribute
as an ArrayList attribute
poor choice
adding values at one end, removing at other
elements
Attributes
required
queue
use SinglyLinkedNode class, source code,
Figure 12.8
Queue Structure
aQueue
myHead
mySize myTail
...
value0
value1
...
valuen-1
isEmpty()
front()
return myHead.getValue()
// unless empty
remove()
otherwise
save handle to first object
adjust head to refer to node
update mySize
Note
Notesource
sourcecode
codefor
for
whole
wholeclass,
class,Figure
Figure12.9
12.9
28
35
49
62
66
49
28
13
66
35
62
80
80
Tree Terminology
A
49
28
Leaf nodes
13
66
35
62
Sibling nodes
Parent
and
80
child nodes
Applications of Trees
Genealogical
tree
ancestors
Game
trees
Parse
trees
Each
data
one link to left child, other to the right
myValue
Value
myLeftChild
myRightChild
of BinaryTreeNode class
Visualizing a BinaryTree
aBTree
myRoot
mySize
3
46
63
17
Tree Traversals
A traversal is moving through the binary tree,
visiting each node exactly once
Traverse Algorithm
3.
1.
The "anchor"
Traversal Order
Three possibilities for inductive step
Left subtree, Node, Right subtree
the inorder traversal
Node,
Left
ArrayList
LinkedList
Standard
problems
Wise use of these data structures simply
solutions to many problems
Attention should be given to efficiency
of structure for particular task at hand
Set
Map
Collections
class
collections