3.1
Types
Data
Using
.
Objects
Objects
holds
:
data type
a
the variable name
has its
own set of
the
to
ways to hide
called
abstract data types
value
refers
often
are
:
complexity
Abts
object
leg Strings
properties
.
concatenate
:
.
.
.
)
Data
examples of objects
values
set
of
on
those
type :
and
operations
values
V
Data
of
set
type
color
Operations
values
bits
24
Picture
2D
string
sequence
array
red
get
primitive
brighten
component,
of colours
get / set color of
pixel
of character
length
,
,
substring
>
Ciij )
types
these
:
boolean ,
values
double
int
,
directly map to
,
.
.
.
machine
representation / follow machine
instructions
compare
(
building blocks of objects)
✓
construct a
new
keyword
new
•
name of
◦
data
object
creates
( invokes
type
constructor
specify
to
space
for
an
method )
which
•
•
name
use
dot
use
name
string
the
ADT
An
:
•
basis for text
•
set
•
has an API
of
values
>
>
e.
g.
API
Gene
:
a
tasks
(
they
types
perform specific
to
process other
types
data)
of
,
( specify)
method )
( invoke
operator
of
combine
not
initialized
object
of
use
objects
type of object
Apply an operation
•
object
method
Object )
Cto
for
^
specify operation )
dot
operator
processing
text
processing
Application
way
specification
of
sequence
=
:
for
2
Programming
or
more
describes how
finding algorithm
Unicode
to
characters
Interface
computer
use
such
programs
an
to
interface
communicate
with each
other
service to
coffers
a
other
pieces
software )
of
•
Image
processing
color
Data
set
of values
type
quantify
( RGB
amount
representation )
of
red,
2563
:
blue
and
green
values
on
of
scale
a
0
-
255
instance
(
•
•
•
luminance
determines
difference
in
grayscale
picture
◦
2D
-
Data
array
luminance
when
:
of
the
all
readibility
between
three
R,
2
G
of
text
colours
and
B
a
on
should
values
are
screen
be
≥
the
( effective
methods
modify objects to
brightness
of
a
something )
do
colour )
128
same
type
colour
objects
(
pixels )
OOP :
Natural
vehicle
studying
models
world
.
for
abstract
of the
real
scaling filter
>
shrink
strategy
scale
◦
•
scale
•
colour
In
and
Ws
-
specific size
by
-
hs
wt
to
-
by
-
ht
Ws
we
hs
ht
ix. up
in
target
image
colour
to
of
pixel
lxx
It
,
y ✗
input from different streams
•
standard
In
pixel
from
a
out
Read
"
of
to
by
by
index
row
set
"
index
column
image
an
convert
to
:
①
•
enlarge
or
data
input
Read from
:
type
:
Read
terminal
text
from
window
I
Stalin
file
,
a
05
use
,
a
to
redirect
website
or
from
one
network
file
%-)
in
source
image
properties of reference types
• Aliasing. An assignment statement with a reference type creates a second copy of the
reference. The assignment statement does not create a new object, just another
reference to an existing object. This situation is known as aliasing: both variables refer to
the same object.
• Pass by value. When you call a method with arguments, the effect in Java is as if each
argument were to appear on the right-hand side of an assignment statement with the
corresponding argument name on the left-hand side. That is, Java passes a copy of the
argument value from the caller to the method. If the argument value is a primitive type,
Java passes a copy of that value; if the argument value is an object reference, Java
passes a copy of the object reference. This arrangement is known as pass by value.
• Arrays are objects. In Java, arrays are objects. As with strings, special language support
is provided for certain operations on arrays: declarations, initialization, and indexing. As
with any other object, when we pass an array to a method or use an array variable on
the right-hand side of an assignment statement, we are making a copy of the array
reference, not a copy of the array.
• Arrays of objects. When we create an array of objects, we do so in two steps:
◦ Create the array by using new and the square bracket syntax for array creation.
◦ Create each object in the array, by using new to call a constructor.
• For example, the following code creates an array of two Color objects:
• Safe pointers. In Java, there is only one way to create a reference (with new) and only
one way to manipulate that reference (with an assignment statement). Java references
are known as safe pointers, because Java can guarantee that each reference points to
an object of the specified type (and not to an arbitrary memory address).
• Orphaned objects. The ability to assign different objects to a reference variable creates
the possibility that a program may have created an object that it can no longer reference.
Such
After the final assignment statement, not only do a and b refer to the same Color object
(sienna), but also there is no longer a reference to the Color object that was created and
usedto initialize b (yellow).
• Garbage collection. One of Java’s most significant features is its ability to automatically
manage memory. The idea is to free the programmer from the responsibility of managing
memory by keeping track of orphaned objects and returning the memory they use to a
pool of free memory. Reclaiming memory in this way is known as garbage collection,
and Java s safe pointer policy enables it to do this efficiently
#
3.2
define
To
•
a
Creating
.
Defines
those
on
data
Instance
•
by specifying
defined
and
( create
:
of values )
( set
operations
those
on
initialize
new
values )
objects )
variables
specifies
the
outside
always
use
modifier
values
of
set
declare
use
values
type
variables
Constructors
•
:
values
methods
◦
specify
type
,
a
instance
>
Types
class
Java
-
data
a
of
set
operations
•
Data
any method
modifier
access
final
with
what
happens
private
variables
instance
that
never
change
constructor
>
specifies
when
you
create
a
object
new
combination
constructor
of
type
will
be
and
invoked ;
Invoking a constructor
Use
e.
g.
new
operator
Charge
[ I
to
=
create
new
a
new
object
Charge C. 51
,
.
63
,
21.3 )
;
<
invoke
argument
constructor
e. g.
number
scnr
.
determines
has Next C)
which
Data
>
type method
operations
Invoking
>
a
double
instance
on
method
v2
=
use
:
C2
.
variables
dot
operator
potential
invoke
method
↓
Atac Y ) ;
,
↑
object name
Using
Anatomy of a
class
Java DOC
:
documentation that
Java
all
pulls
your
methods
out
public
on
variables /
Turtle
graphics
create
data type to
a
location
values :
+
manipulate
orientation
a
turtle
moving
can
a
create
draw
•
~
~
a
new
turtle
square
go forward
:
( 1,0 )
turn Left 190.0 )
at
;
i
}
✗ ↳
x
8
cxo
,
a
plane
of turtle
The code
•
in
yo )
be used
draw
graphics
and
shapes
to
g
complex numbers
Goal :
set
data
a
values :
of
real
:
and
imaginary parts
:
form
the
in
numbers
real
2
complex number
number
numbers
type to manipulate complex
✗
where
iy
+
x
and
y
are
real
numbers
•
•
Plot
ix. y )
the
Let
z
black
if
square
if
z=
✗ +
in
the
is
yi
in
the
set
and
white
otherwise
:
complex
no
.
20
set ?
.
t
-
=
iterate
the
set
Implementation
Is
is
complex numbers
set of
A
i
API
operations defined on complex numbers
Mandelbrot
and
21-+1
lztl
otherwise
=
( Zt )
diverges
zo
is
≥
+
to
20
infinity
in the
set
,
then
20
is
not
in
the
set
root
of
-1
.
3.3
Designing Data Types
.
Encapsulation
data
Encapsulated
Hide
internal
class
provides
•
client
uses
API
specifies
representation and code for operations
data
data
as
type
API
to
black box
between
contract
keep data hidden
expose
:
representation of data type
•
•
type
'
with
clients
client
private
'
'
using
and
modifier
access
public
'
class
access
modifier
Immutability
data
Immutable
object 's
value
type
:
cannot
change
once
( when
same
constructed
Advantages
•
Avoid
aliasing bugs
•
makes
debugging easier
•
Limits
scope of code that
•
Pass
objects around
can
without
memory
change
location
is
accessed
through
more
than
it a
value
only once
one
reference
values
worrying about modification
Disadvantage
New
Final
object must
access
be created
.
every value
.
modifier
Declaring an instance
constructor
for
variable
to
be
final
means
you
can
assign
,
in
initialize r
or
Advantages
helps
◦
enforce
easier
•
to
spatial
vector
this
•
a
'
changes
debug
vectors
data
values
'
immutability
accidental
prevents
•
:
type
:
of
sequence
real
no
.
keyword
reference
the
to
invoking object
[ the
helps us differentiate between
object being called )
instance
variables
and local variables
e.
g.
When
invoke
you
a.
magnitude C)
'
,
this '
is
alias
an
for
'
a'
the
Interfaces and
•
•
•
•
◦
inheritance
a
abstract
types
used
contains
static
constants
a
class
declared
can
specify
and
the
declare
"
interface
APIs
and
the
behaviour of
abstract methods
implement multiple
using
Interfaces
to
"
interfaces
keyword
implement
APIs
a
class
interface
message
to
allows
an
sending
object
concerning which class
it
without
belongs to
.
Inheritance
arranges
•
each
classes
in
subclass has
subclasses
a
superclass /
exactly
extend
the
more
instance variables
more
methods
more
complicated
one
subclass
superclass
superclasses
invariants
:
relation
3- 4
N
.
-
body
simulation
⊕ determine the
:
e. g.
planet
motion
orbits the
Body data type
to
of
N
particles
sun
represent
a
particle
,
moving
under their
mutual
Newtonian
gravitational
forces
performance
4.1.
analysis of algorithms
-
Algorithm:
for
method
a
Scientific
method
observe
·
as
a
(3-step approach
feature of the
some
hypothesize
·
solving a problem that is suitable for implementation
natural
world
model that is consistent with these
a
validate
·
the
until
by repeating
principle:
pay
attention
observations
study cost,
①
Predict
program
when
will
·
will
·
hypothesis and
aswell
observations
·
⑤
·
Al
my program finish?
agree
analysis
change
make
this
how
can
I
make
develop
a
basis for
·
·
experiments
must
be reproducible
cable
as
to
understanding the problem
new
technology
enables
new
research
of
cost
to
be
must
we
be
design
and
falsifiable
proven false)
and
for
designing
new
algorithms
porithmic successes
NE
Rearrange array of h-items
databases,
scheduling,
used
in
Brute
force:N" Steps
·merpesort:
in
NogN steps
>
ascending order
stats,
enables
apply
derive
my program faster?
enables
of
To
of
mathematical
my program faster?
sorting
·
The
hypotheses
algorithms and implementations
will
To
·
behaviour
my program finish?
&
compare
·
we
analyze algorithms
to
cost
Study
observations
models
Reasons
to
scientific methods
predictions by making further
the
verify
·
Basic
running programs.
predict events using this hypothesis
·
computer program
genomics,
new
...
technology
NIOPN
N
concise
Discrete
Fourier
down
·break
Transform
waveform
·
used
in
·
Brute
force:N" Steps
fFT
algorithm: Nogn
·
of
periodic
into
astrophysics,
MRI,
JPC6,
Dup,
samples
N
steps
>
running
time
enables
components
...
technology
new
Observations
the
measuring
first
program
a
run
exact
various
on
the problem size characterizes the
normally,
the
size of
the
value
·
·
the problem size
the
of
the
running
e.g.
Threesum
input
time
the
use
stopwatch data type
is
a
"problem size"
the
computational
task
is either:
or
should increase
java
program:
difficulty of
command-line
a
a
inputs
there
observation:
qualitative
of
counts
argument
with
the
the number
problem
of
(but, by
size
how much?)
of
in
an
array
create
an
accurate
triples
integers
that
sums
to
0
Hypotheses
·
Daniel
Knuth
showed
that
it is
possible
to
running-time.
·
This
~
requires detailed understanding
the
~the
and
of:
program
system
advanced
and
tools
computer
of
mathematical
analysis
model
that
can
predict program
Log-log plot
Lop-logplot
hypothesis:
Initial
Running time approximately obeys a power
Data
analysis:
Plot
running time
Input
size
yields straight
line.
vs.
N
law
on
a
TCN)
=
and
standard
plot
log-log scale
consequence
·
law
power
slope b
=
hypothesis
Refined
Running time
slope
-
as
grows
/log-logplot
cube
of
input
size:
straight line
with
and
b
is
a
is
about
aNe
slope
3)
TIN =
Hypothesis:
Running
time
with
=
19(
Doubling hypothesis
Quick
Run
way
the
to
estimate b in
program,
a
power law hypothesis
doubling the size of the input.
xI
↑
E
↑
I
2
x
and
based
verifiable
or
on
observation,
not
by
theory
Empirical analysis
Run
for various
input sizes.
small,
will
the program
is
N
If
*
too
you
mainly noise.
measure
Primary challenge
is
cost of
·
executing each
·frequency
If
*
we
of
execution
know
both
by
2
(property
of each statement
cost
all
of state
primary factors:
statement
the
for
and sum
together
determine
frequency
is determined
time
Running
to
the
of execution
mathematical Analysis
and
of
the
(property
system)
of the
algorithm)
frequency for all instructions in the program,
instructions to
-
ments
get the running
time."
(cost,
x
we
then
can
frequency)
multiply
(cost2x
-
them
frequency2)....
Tilde notation
used
·
to
develop approximate expressions for run-time.
with
·work
leading term of mathematical expressions
~gin) represents any quantity that
g(n) -fin)
ignores
·
indicates
that
lower order terms
florsa gas
means
when divided
g(n) / fine
/small
by find approaches
approaches
I
as
·
·
Running
b
time
depends
of
on
(imagine)
grows
are
the
(n-2)
typical program:
algorithm
<use
=
M3 -m +
instructions executed
law
a
grows
himas for:
these
power
a
values
nCM-1
constants in
a
as
aNb
doubling hypothesis
to
estimate
most
frequently
leading
·
depends
constant a
algorithm,
~
input data,
experiments to
run
on:
caching, machine, compiler, garbage collection,
predicting,
model useful for
·
mathematical
·
·
estimate
may
require advanced
maths
model
useful
order
of
<N
·
for
and
·
for
fit curve
e.g.
for
growth
<
many
find
Threesum,
fn = 4
3
Doubling:
TC2M)
=
a
(2n)3
g
=
cam3)
T (n)
but not
to
independent of a
·
by other apps
explaining
A
analysis
analyze algorithm
·
plot and
In
perform experiments
to
easy
·
e.g.
running times,
measure
use
estimate
emperical analysis
·
or
N2
operations
function of input-size
a
as
predicting and explaining
particular machine
or
compiler;
applies
to
machines
not
built yet
classifications
>
N3
<
programs,
a
of
number
function
typical programs,
N4, 2N
running time satisfies
known
find is
as
a
the
order of
function such
the
relationship
growth
as
of
log2W,
Tin)-cf(n)
where
running time.
N,
NOgN, NY,
N3,
etc.
c
is
a
constant
search
Binary
sequential search
examine each
·
time
binary
search
·
examine
·
if
entry
array
finding
a
match
(or
length of array
to
search
left half
if
small,
search
right half (recursively)
array
of size
an
after
N
<
each
N,
it
3-way compare,
N/2
worst
N14
->
->
(recursively)
takes
at
most
If
<..
.
·
·
Bit:
0/
Byte:
8
until
you reach
I?
a
factor of
memory of
(OB):
an
1
million
bytes-2 bytes
billion
bytes
2
-
bytes
object
memory for each
overhead
I
instance
8
=
bytes
variable
on
a
+
32-bit
2
(
bits
megabyte (MB):
object
can
type
·gigabyte
·
many times
logzN3-way compares
problem size decreases by
N18
How
1042 N
memory
·
case
index
large,
in
in
entry
return
if too
too
end
ordered array
in
middle
equal,
until
proportional
takes
·
unordered
in
machine
bytes
boolean
I
byte
I
char
Z
int
4
float
L
long
S
double
S
you divide
N
by
2
memory of
an
array
memory for each array entry
·array overhead:
How
·
·
·
to
evaluate
16
bytes
performance
Computational
experiments
mathematical
experiments
scientific
method
on
32-bit
of
a
machine
program:
and
Sorting
4. 2.
searching
Java
Arrays, binary Search();
sorting
Rearrange
statistics,
databases,
order
data
compression,
bioinformatics,
computer graphics,
...
sor t
Insertion
·
ascending
Binary search
Bubble
·
in
items
N
Applications:
·
library implementation:
sort
mergesort
·
searching
sequential search
scan
through array,
looking
key:
array
Hits
·search
e.g.
return
miss:
each
examine
takes
time
Binary
search
·
main
·
·
idea:
·
examine
·
if
entry
the
middle
to
length
match
a
of
(or
in
array
return
left half
if
small,
search
right half (recursively)
search
one
compare
do
then
binary
running
reaching
the
worst
the
case
in
search
an
in
of
size
N:
array
of
size
array
an
(recursively)
Running
time
N12
v
N
>
N12
end,
index
search
binary
>
N14
->
2
. . .
N18...
I
3 log2N
times
1
until
brute
=>
algorithm
much
entry
large,
to
not
array
if too
too
0, 1,
array
finding
until
proportional
sort
equal,
unordered
in
Guess
force
index
re t u r n -
search
logarithm chasm
hitting the key
key.
whitelists
sequential
·
for
Linear-
regard to cost
time
can
be
as
much
as
n
Insertion sort
sorting
solution
left-to-right
through
Brute-force
·
move
·
Exchange
·
with
element
n ex t
array
larger
elements
to
its
Emperical analysis
Number of
sort
·
(N)
depends on input family
order:
Ascending
N
Random
N2/L
·
·
comparisons
(quickest
left,
public static void sort(String[] a) {
int n = a.length;
for (int i = 1; i < n; i++) {
for (int j = i; j > 0; j--) {
if (a[j-1].compareTo(a[j]) > 0)
exch(a, j, j-1);
else break;
}
}
}
N2 / 2
Descending
mergesort
①
Divide
②
Recursively
③
merge
MERGINO:
A
use
an
array
two
into
sort
each
halves
combining
halves
2
to
2
half
make
sorted
pre-sorted
auxilary array!
whole
lists
into
a
sorted
one-by-one
whole
·
·
Best
Reduction
step:
recursively
sort
a
smid, his
Running
worst
-
subaricy is length
case:
otherwise,
the
and
2
0
compute
subarrays
merge
to
produce
calready sorted)
1
or
mid=Io+(hi-low),
also, mid]
a
sorted
and
result
time
case:
between
NogN
and
Nogh
compares
running
=>
time
is
linearithmic
Application:
~
A
frequency counts
Ei
~Longest repeated substring
>Ip:
~
longest
search
common
prefix
only adjacent
suffixes
-
4.4.
Symbol
Tables
SYMBOL
TABLE:
data
insert
-
given
key
a
with
key,
a
~
a
specified
a
search
used
type
for the
associate
to
value
values
with
keys
(collection
>
sput)
corresponding
Types
value
of
of
key-value pairs)
choices:
ST's
(get)
>JPVD
libraries:
Hashmap
Treemap,
types of symbol tables
ordered
unordered array,
array,
binary search tree (BST),
Hash
red-black,
tables,
etc.
Hoshinp:
operation
which
transforms
keys
array indices
into
that
we
can
use
to
access
values
The
asterisk
&
*
indicate
is
a
multiple
placeholder
to
implementations
might be considered e.g.
HashST
and
BST.
Applications:
The
API
reflects
Immutable
·
~
most
frequency counter, dictionary, phone book,
keys:
commonly
several
design decisions:
keys do
not
used:
linguistic analysis
their values while in the
change
String, Integer,
Double
call
(word
frequencies in piece of text
symbol table
immutable)
Replace-the-old-value policy:
·
if
a
we
·
Not
key-value pair inserted
adopt
found:
the convention
get()
into the
that the
returns
symbol table
new
"hull"
if
value
no
that
replaces the
value
is
already associates another
value
old one
associated with the
specified key
with
a
given key,
keys and
Null
·
clients
Remove:
·
remove (key
to
hull
use
(string key:
Hashable
equalss)
method
key
iterate
over
key
and
or
its
value
associated
value
key-value pairs in the
data
structure
whether
tests
examined
I
keys are
the same
later)
keys:
symbol table
·
the
a
3
hashCodel) method
I
either
st.keys() S
keys:
·comparable
as
removes
1)
Iterating over key-value pairs:
for
·
values:
permitted
not
are
·
null
clients
prototypical examples
of
symbol
Dictionary look up:
client
builds
table
with
a
·
symbol
successive
tables:
"put"
operations to support "get" requests.
Indexing:
st
and
standard
the
that
client
get()
Index.java
uses
puts):
an
reads
intermixed
in
input and prints a
positions
where each
a
sequence
sequence
sorted
of
list
of
string appeared
in
of calls
to
strings from
integers specifying
the input
values
the
a re
indexes (
locations
Elementary implementations
basic
·
·
①
implementations
resizing arrays
on
and
used
by symbol tables are based
in
unordered
lists)
linked
basic
2
on
data
resizing arrays
lists
linked
key-value pairs
store
sequential
an
search:
examine
each
(not
feasible,
because
list/array
model
linked
list
element in
a
and
array
use
until
sequence
sequential
we
hit
unsorted
·
·
the
search.
specific key or exhaust
the
linear time)
takes
gets)
or
linear
running
time
array
key to end
put:
add
set:
scan
&
Binary
(if
not
through all keys
already there)
to
desired
find
value
search
Alternatively,
·
structures:
sequential search
·
·
(based
might
we
use
a
sorted
for the
cresizing) array
keys
and
a
parallel array
for
the values
the
since
·
keys are
fast
getc
is
put()
takes
*
=>
in
sorted
order,
time,
can
to
binary
each
time
due
(logarithmic,
linear
we
because
for
search
key
a
and its
value
using binary
search
search)
a
new
key
is
inserted, larger keys must be shifted one up in
the array
sorted
·
·
array
and
shift all
larger keys right
put:
find insertion
set:
binary search to find desired key
To
*
point
implement clients
Index.java,
such
as
the
hash
we
need
more
flexible data structures than
arrays.
we
n ow
consider:
table
and
binary search
tree.
linked
lists/resizing
#ash
a
data
to
be
TableS
structure
in which
equal
size
in
for each
group,
use
we
hash function
keys in an
keep the
we
a
unordered
keys into
the
divide
to
linked
list and
which
groups,
me
expect
we
sequential search
use
Representation:
maintain
·
value
·
of
i
is
(along
with
hash function
a
the
·
array
an
linked
me
with element;
lists,
their associated
values)
possible key
maps each
into
si
hash
a
symbol table is then an array of linked lists,
desired
containing a linked
value
can
where the
hash
list
integer between
value
is
the
of
all
keys
0
and
m-1)
array index
to
hash
whose
access
the
list
i
n =
12
strings
m =
5
groups
v
the
To convert
hash code
into
hash value
a
between
private int hash(Key key) {
return Math.abs(key.hashCode() % m);
}
Hash
and
0
M-1:
value
search
to
·
·
·
key:
search
for
a
compute
its
hash value
to
identify
iterate
over
the
in
that linked
if the
search
nodes
key
is
in the
linked
its
linked
checking for the search key
list,
list,
list
return
the
value,
otherwise
return
null
Insert
to
·
·
·
insert
key-value pair:
a
compute
its
iterate
over
if the
create
hash value
of
the
in
nodes
the
key
that linked
key
is
in the
linked
a
new
mode
with
to
list,
list, replace
the
identify
its
linked
list
checking for the key
the
specified key
value
and
associated
value
and
with
insert
the
it at
key with
the
the
beginning
new
of
value, otherwise,
the linked
list.
primary advantage
of hash
they support
and
do
a
order
of
the
in
constant
tree
empty
time
keys
thus,
and
cannot
performance
provide keys
bigger
~
Nodes
-
top:
than
↑
to
disjoint binary trees
symmetric
max
·keys
in
right subtree
Implementation:
·
·
both
hull:
leaf
of
links
on
no.
is
are
smaller
I
than
any path
from the
links
per
parent
mode.
(
A
node
a
key
private
key
key;
a
value
private
val
val;
a
reference
to
the
private
Node
left;
reference
to
the right subtree
private
Node
right;
·a
mode
root
parent
larger than
are
use
comprised
symmetric order
nodes
$
left subtree
in
left subtree
link:
key above
in
·
left
order
keys
·
are
tree
by its
links are
whose
Height of tree:
-
of
root
referenced
Node
order
OR
at
-
in sorted
parent
keys
Node
for both put and
is either:
key-value pair (model containing links
·
(achieves
binary tree is defined recursively
Binary
·
effectively
search trees
Binary
~
advantage
take
not
get operations
hash tables
of
Disadvantage
they
put
tables:
of:
left subtree
private
Node
class
7
to
a
leaf
node
gets
search
for
search
·
·
·
with
a
key
given
in
a
BST:
search
as
unsuccessful
algorithm:
Recursive
if tree
·
mode
a
is
if search
terminate
empty,
key
key=
terminate search
mode:
in
as
successful
value
search
key
smaller
than
key
in
mode:
search
(recursively)
in
left subtree
search
key
greater
than
key
in
mode:
search
(recursively)
in
right subtree
a
new
mode
into the
one
link
must
be
Inser t
Insert
only
*
~
the
link
that
unsuccessful
performance
·running
·
(by returning
tree
is
times
BST:
changed
point
to
would
be found
to
search
for that
key
characteristics
of
be null
the
in
new
mode
an
BST
ultimately dependent
dependent on
to
the order in
on
which
shape
of
tree
keys are inserted
associated
with
key)
Analysis
Running
*
Best case:
·
each
node has
exactly
cost
of
every
put
Average
case:
-
-
inserted
keys
-
balanced depth at
perfectly
tree
-
·
proportional to depth of node.
cost
non-null
I
and
most
logN
children
logo
is
get operation
or
less
*
N
random
in
order,
average depth
is
is
the
number
of
keys
LInN
Worst case;
·
tree
-
each
-
unbalanced, depth is N
is
node has
exactly one
put and get requests
-
this
*
arises
scenario
Red-black
when
Bst
(so
is
basically a linked
list with
depth
->21pN
an
extra
wasted link)
linear time
take
clever
tree:
*
null-link
we
Bst
order
put keys
in
variant
that
guarantees
a
Iteration
-Traversing
Tree
~
·
recursion
·
has
to
be
used,
process every
because trees
mode
have
in
the
I
links to
tree
follow
visit
left
subtree
prints keys
in
right
subtree
ascending order
subtree
⑥
⑱
left subtree
recursively visit right
visit
⑧
⑤
recursively vist
node
Recursively
⑧
@
traversal
recursively
visit
systematically
traversal:
norder
I
·
BST-
a
&
⑧
&
M
returns
Enhanced
for
=>nables
client
39.
symbol
y
)
loop
to
iterate
Integer S t
a
4
C
(strings:
Stout.
To
·
·
·
support
in
a
collection
=
new
STcstring, Integers ();)
create
a
new
string,
Integer
symbol
table
value
key
for
items
table
string,
...
over
st) <<
loops through every string sitem)
Printin (st.get(s)+" "+s);3
enhanced
for
loop:
import
java.util. Iterator;
import
java.util. No such Element exception;
public
class
BST. . .
implements
⑧
⑧
Iterable
skeys
in
the
symbol table
(collection)
set
unordered
SET:
Efficient
collection
ent
of
implementation:
same
duplicates from
an
as
keys
but
symbol table,
ignore
value
bedup
Remove
input.
↳
if
the
set
does
contain the
Inverted
list of pages,
a
e.g.
book
index,
symbol
value
preprocess
web
search
table:
query word
key:
·
the
that
you
key
index
Given
~
add
not
already
specific key,
=
set
of pages
no duplicates
them
engine index, file
can
index
quickly
find
all
pages
containing
a
given
query
word.
4.5
>
~
idea of
encapsulates the
being delivered to
message
a
message delivered
outcome :
Phenomenon
small world
:
away from anyone
handshakes
six
~
study
case
.
with
average
of
5
someone
you
•
Data
Graph
vertex
•
•
vertices
of
Degree
•
the
represents
pairwise
between
connections
a
if
neighbours
are
its
vertex :
vertices
2
no
.
they
Of
basic
idea
chain of
that
,
even
connected
by an edge
known
the
as
small
-
world
,
us
from
one
another
API
Graph Representation
•
•
•
use
key
symbol
a
=
value
table
name
of
set
of
=
3
>
vertex
neighbors
important
phenomenon
though each of us has relatively few acquaintances
acquaintances separating
Graph
are
neighbours
graphs exhibit a specific property
some
*
2
neighbours
type that
connection
=
two
=
element
=
Edge
•
type
data
:
first name
intermediaries
Degree (A)
Graph
by
know
properties :
through graph vertices
through a vertex 's neighbours
•
clients
can
iterate
•
clients
can
iterate
•
space usage is proportional
to
the
no
.
of edges
:
there
is
a
relatively short
Date
set
Type
Identical
Kevin
Bacon
same:
find
This
brings
path
finder
Its
·
performer
to
Kevin
every other
vertex
Bacon.
API.
paths
shortest
paths:
distance
neighbours
are
at
distance
O
1.
remaining neighbours are
at
Their
remaining neighbours
Breadth
search
·
first
Goal:
given
a
vertex
s,
find
its
from
a
source
vertex
s
②
a
API
at
&
P u ts
values.
connecting
finder
is
Their
·
path
the
to
us
vertex
source
ignore
shortest chain of movies
compute shortest
To
but
came
computing
·
symbol table,
to
keys.
of distinct
unordered collection
onto
flo
a
distance
are
shortest
quene
I.
at distance
path
to
3.
v.
(first-in-first-out
Repeat until the quene is empty:
·
·
dequence
add each
:
least recently added
of
his
unvisited
Vertices
are
visited
Applications:
extends
the
to:
particle tracking,
Google
maps
ver tex
unvisited
in
neighbours
to
the
increasing order
of
image processing,
crawling
queue,
distance
from
the web
and mark
5,
them
because
we
as
visited.
use
a
f150
queue.
conclusions
·
·
·
Linked
list:
hierarchical
Binary tree:
Graph: pairwise
Data
structure
connections
of elements
between elements
structures
Quene:
set:
of elements
ordering
linked
list
binary tree
symbol table:
binary tree
oraph:
symbol table
of sets
Breadth
first searcher
(Bts):
Importance
·
Enables
·
Enables
us
us
graph
of
data
structures
to
build
and
to
solve
-
quene
+
symbol
debug large programs
large problems efficiently
table
0
You can add this document to your study collection(s)
Sign in Available only to authorized usersYou can add this document to your saved list
Sign in Available only to authorized users(For complaints, use another form )