Java review
part 1: classes & objects
Objects
Data structures literally, storage containers
for data constitute object knowledge
Operations an object can perform on data are
its actions
Object knowledge and actions are specified by
its class
In a clas
Graphs II:
Trailblazing
Paths
In an undirected graph, a path of length n
from u to v, where n is a positive integer, is a
sequence of edges e1, , en of the graph such
that f(e1)=cfw_x0,x1, f(e2)=cfw_x1,x2, , f(en)=cfw_x n-1,xn
where x0 = u and xn = v
In
Graphs
The ultimate data structure
graphs
1
Definition of graph
Non-linear data structure consisting of
nodes & links between them (like trees in
this sense)
Unlike trees, graph nodes may be
completely unordered, or may be linked in
any order suited to th
B-trees:
Theyre not just binary anymore!
btrees
1
The balance problem
Binary search trees provide efficient search
mechanism only if theyre balanced
Balance depends on the order in which
nodes are added to a tree
This tree is balanced
because data
arriv
Binary Trees
trees
1
Basic terminology
Finite set of nodes
(may be empty - 0
nodes), which
contain data
First node in tree is
called the root
trees
2
Basic terminology
Each node may be
linked to 0, 1 or 2
child nodes (or
children)
A node with children
Queues II:
Applications
queues2
1
Palindrome recognition
Palindrome: collection of characters that
reads the same backwards and forwards
Examples:
otto is a palindrome
34543 is a palindrome
snub no man; nice cinnamon buns! is a
palindrome, if you ign
Queues
Data structures that wait their turn
queues
1
Queue characteristics
FIFO: first in, first out
insertion of items occurs at one end,
removal occurs at the other end
first item inserted is the first item removed;
second inserted is second removed,
Stacks II
Adventures in Notation
stacks2
1
The trouble with infix .
Rules for expression evaluation seem
simple - evaluate expression left to right,
results of each sub-expression becoming
operands to larger expression - but
All operators are not creat
Stacks
The unorganized persons data
structure
stacks
1
Stack characteristics
Entries are ordered in terms of access -both insertion and removal take place at
same spot (top of stack)
Specialized type of container class; defining
characteristic is insert
Generic Programming
*Really* reusable code
First, a bit of history
Since Java version 5.0, Java has borrowed a page
from C+ and offers a template mechanism,
allowing programmers to create data structures
without specifying an underlying data type
Until
Linked Lists II
The rest of the story
linklist2
1
Adding/removing in the middle
of a list, part deux
Up to now, we have performed operations
on nodes in the middle of a list with a
crucial assumption: we have an external
reference to a node preceding the
Linked Lists
Its a conspiracy!
1
Linked list: a data structure used
to represent an ordered list
Consists of a sequence of nodes
A node consists of a data item and a
reference to the next node - the connecting
reference is called a link
Links keep node
Container Classes
Bags & Sequences
containers
1
Operations on a Bag
Constructors
default: creates bag with capacity of 10
with int argument: creates bag with specified
capacity
containers
2
Operations on a Bag
Modifiers:
add- place an item in a bag
ad
Hashing II:
The leftovers
hashing2
1
Hash functions
Choice of hash function can be important
factor in reducing the likelihood of
collisions
Division hashing: key % CAPACITY
Certain table sizes are more conducive to
collision avoidance with this method
Recursion
just in case you didnt love
loops enough
Recursion
A recursive method is a method that
contains a call to itself
Often used as an alternative to iteration
when iteration is awkward or inelegant
Each recursive call is given a smaller
portion
Hashing
Its not just for breakfast anymore!
hashing
1
Hashing: the facts
Approach that involves both storing and
searching for values
Behavior is linear in the worst case, but
strong competitor with binary searching in
the average case
Hashing makes it
Algorithm Analysis & Program
Testing
An introduction
bigointro
1
Time Analysis: reasoning about
speed of algorithm
Need to decide how time will be measured
actual performance time: not always accurate
different system
same system, different time
numb
Time & Date Representation in
Java
Date class
An object of type Date represents an instance in
time
Part of java.util.* (requires import statement)
A new Date object is automatically set to the time
of its creation, to the millisecond
For example:
Dat
Class design guidelines
Most of this material comes from
Horstmann, Cay: Object-Oriented
Design & Patterns (chapter 3)
1
Encapsulation
Classes can be implemented many different
ways
each has advantages & disadvantages
improvement/revision always a poss
Arrays in Java
data in bulk
Array
Homogeneous collection of elements
all same data type
can be simple type or object type
Each element is accessible via its index
(random access)
Arrays are, loosely speaking, objects
require initialization with the
Sample Final Exam
1) Convert each of the following expressions into its equivalent prefix, postfix or infix expressions (in
other words, provide the alternatives to the one given):
a) prefix expression:
/ - + 9 9 + * 2 3 4 2
infix:
postfix:
a b % c - d +
Sample Exam 2
1. Consider the array below, in its initial state. Arrays A and B represent the same array after four data items
have been sorted using one of the sorting algorithms discussed in class. For each partially sorted array:
determine which algor