CS210- Lecture 8 Jun 16, 2005 

advertisement
CS210- Lecture 8
Jun 16, 2005
Agenda
 Circular Linked List
 Lists
 Doubly linked list implementation of Lists
 Sequences
 Trees
 Tree Traversals
6/30/2016
CS210-Summer 2005, Lecture 8
1
Circular Linked List (Project 2)
tail
current
add(3)
3
tail
current
4
current
4
6/30/2016
add(4)
3
5
tail
3
CS210-Summer 2005, Lecture 8
add(5)
2
Circular Linked List (Project 2)
4
current
tail
6
3
5
current
4
5
tail
remove()
3
current
999
6/30/2016
add(6)
tail
4
5
3
CS210-Summer 2005, Lecture 8
add(999)
3
Circular Linked List (Project 2)
current
4
tail
5
current
4
123
remove()
3
tail
5
current
4
6/30/2016
123
456
add(123)
3
tail
5
3
CS210-Summer 2005, Lecture 8
add(456)
4
Circular Linked List (Project 2)
current
4
tail
123
456
5
3
reset()
current tail
4
123
456
5
3
advance()
4 times
current tail
123
6/30/2016
456
5
4
CS210-Summer 2005, Lecture 8
remove()
5
Josephus Problem
2
3
4
5
6
7
current
8
9
10
tail
1
current tail
Advance once
2
3
4
5
6
7
8
9
10
1
Call advance() countOff – 1 times
remove();
6/30/2016
CS210-Summer 2005, Lecture 8
6
Lists


Using ranks is not the only way of
referring to the place where an element
appears in a sequence.
If the sequence S is implemented with a
singly or doubly linked list, then it is
more natural and efficient to use a node
instead of a rank.
6/30/2016
CS210-Summer 2005, Lecture 8
7
Node-Based Operations


We want to define methods for S (singly
or doubly linked list) that takes nodes
as parameters and provide nodes as
return types.
Such methods provide significant speed
ups over rank-based methods as finding
the rank of the element in a linked list
requires searching through the list
incrementally, from its beginning or
end.
6/30/2016
CS210-Summer 2005, Lecture 8
8
Node Based Operations

Example:

Lets define some hypothetical methods:
 removeAtNode(n): removes the element
of S stored in node n of the list. What is
the running time?
 insertAfterNode(n , e): inserts the new
element e after the node n.
6/30/2016
CS210-Summer 2005, Lecture 8
9
Position ADT


The Position ADT models the notion of
place within a data structure where a
single object is stored
It gives a unified view of diverse ways of
storing data, such as



a cell of an array
a node of a linked list
Just one method:

object element(): returns the element stored at
the position
6/30/2016
CS210-Summer 2005, Lecture 8
10
Position ADT


A position is always defined relatively,
that is, in terms of its neighbors. In a
list, a position p will always be “after”
some position q and “before” some
position s (unless p is first or last
position).
A position p, which is associated with
some element e in a list S, does not
change, even if the rank of e changes in
S, unless we explicitly remove e
(destroy position p).
6/30/2016
CS210-Summer 2005, Lecture 8
11
List ADT



The List ADT models
a sequence of
positions storing
arbitrary objects
It establishes a
before/after relation
between positions
Generic methods:

Accessor methods:



Update methods:



size(), isEmpty()

6/30/2016
first(), last()
prev(p), next(p)
replace(p, e)
insertBefore(p, e),
insertAfter(p, e),
insertFirst(e),
insertLast(e)
remove(p)
CS210-Summer 2005, Lecture 8
12
The List ADT
Operations
insertFirst(8)
Output
p1(8)
S
(8)
insertAfter(p1, 5)
insertBefore(p2, 3)
insertFirst(9)
p2(5)
p3(3)
p4(9)
(8, 5)
(8, 3, 5)
(9, 8, 3, 5)
prev(p3)
last()
remove(p4)
p1(8)
p2(5)
9
(9, 8, 3, 5)
(9, 8, 3, 5)
(8, 3, 5)
replace(p3, 7)
insertAfter(first(), 2)
3
p5(2)
(8, 7, 5)
(8, 2, 7, 5)
6/30/2016
CS210-Summer 2005, Lecture 8
13
Doubly Linked list implementation of List ADT





A doubly linked list provides a natural
implementation of the List ADT
Simply make the nodes of the linked list
implement the position ADT.
Each node implement the position interface
and therefore define a method element().
Nodes themselves acts as positions.
Viewed internally by linked list as nodes, but
from outside they are viewed as generic
positions.
6/30/2016
CS210-Summer 2005, Lecture 8
14
Doubly Linked List

Nodes implement Position and
store:




prev
element
link to the previous node
link to the next node
next
elem
node
Special trailer and header nodes
nodes/positions
header
A
B
C
trailer
D
elements
6/30/2016
CS210-Summer 2005, Lecture 8
15
Insertion

We visualize operation insertAfter(p, X), which returns position q
p
A
B
C
p
A
q
B
C
X
p
A
6/30/2016
q
B
X
CS210-Summer 2005, Lecture 8
C
16
Insertion Algorithm
Algorithm insertAfter(p,e):
Create a new node v
v.setElement(e)
v.setPrev(p)
{link v to its predecessor}
v.setNext(p.getNext()) {link v to its successor}
(p.getNext()).setPrev(v) {link p’s old successor to v}
p.setNext(v)
{link p to its new successor, v}
return v {the position for the element e}
6/30/2016
CS210-Summer 2005, Lecture 8
17
Deletion

We visualize remove(p), where p = last()
A
B
C
A
B
C
p
D
p
D
A
6/30/2016
B
C
CS210-Summer 2005, Lecture 8
18
Deletion Algorithm
Algorithm remove(p):
t = p.element
{a temporary variable to hold the
return value}
(p.getPrev()).setNext(p.getNext())
{linking out p}
(p.getNext()).setPrev(p.getPrev())
p.setPrev(null) {invalidating the position p}
p.setNext(null)
return t
6/30/2016
CS210-Summer 2005, Lecture 8
19
Sequences


The Sequence ADT is the union of the
Vector and List ADTs
Elements accessed by



Rank, or
Position
Bridging methods


atRank(r): Return the position of the
element with rank r.
rankOf(p): Return the rank of the element
at position p.
6/30/2016
CS210-Summer 2005, Lecture 8
20
Sequence ADT

Generic methods:



size(), isEmpty()

Vector-based methods:

List-based methods:
elemAtRank(r),
replaceAtRank(r, o),
insertAtRank(r, o),
removeAtRank(r)

Bridge methods:

6/30/2016
first(), last(), prev(p),
next(p), replace(p, o),
insertBefore(p, o),
insertAfter(p, o),
insertFirst(o),
insertLast(o),
remove(p)
atRank(r), rankOf(p)
CS210-Summer 2005, Lecture 8
21
Doubly Linked List Implementation


A doubly linked list provides a
reasonable implementation of the
Sequence ADT
Nodes implement Position and store:






element
link to the previous node
link to the next node
Position-based methods
run in constant time
Rank-based methods
require searching from
header or trailer while
keeping track of ranks;
hence, run in linear time
Special trailer and header nodes
nodes/positions
header
A
B
C
trailer
D
elements
6/30/2016
CS210-Summer 2005, Lecture 8
22
Iterators


We generally require to march through the
elements of vectors, lists or sequences one at
a time.
Iterator ADT



hasNext: Test whether there are elements left in
the iterator.
next: Return the next element in the iterator.
An iterator for a vector, list or sequence
should return the elements according to their
linear ordering.
6/30/2016
CS210-Summer 2005, Lecture 8
23
Using Iterators in Lists and other ADTs

ADTs storing collection of objects should support
the following method:


elements() / iterator() : Return an iterator of the
elements in the collection.
Usage:
public static void printVector(Vector vec)
{
Iterator it = vec.iterator();
while(it.hasNext())
System.out.println(it.next());
}
6/30/2016
CS210-Summer 2005, Lecture 8
24
Using Iterators in Lists and other ADTs

ADTs that support the notion of position,
such as List and sequence ADT, also
provide the following method:

positions(): return an iterator of the
positions in the collection.
6/30/2016
CS210-Summer 2005, Lecture 8
25
What is a Tree



In computer science, a
tree is an abstract model
of a hierarchical
structure
A tree consists of nodes
with a parent-child
relation
Applications:



Organization charts
File systems
Programming
environments
6/30/2016
A
B
C
E
F
I
J
CS210-Summer 2005, Lecture 8
G
D
H
K
26
Tree Terminology







Root: node without parent (A)

Subtree: tree consisting of
a node and its
Internal node: node with at least
descendants
one child (A, B, C, F)
External node (a.k.a. leaf ): node
A
without children (E, I, J, K, G, H, D)
Ancestors of a node: parent,
grandparent, grand-grandparent,
B
C
D
etc.
Depth of a node: number of
ancestors
E
F
G
H
Height of a tree: maximum depth
of any node (3)
Descendant of a node: child,
I
J
K
subtree
grandchild, grand-grandchild, etc.
6/30/2016
CS210-Summer 2005, Lecture 8
27
Tree ADT (§ 6.1.2)


We use positions to
abstract nodes
Generic methods:





integer size()
boolean isEmpty()
Iterator elements()
Iterator positions()
Accessor methods:



position root()
position parent(p)
positionIterator
children(p)
6/30/2016

Query methods:




Update method:


boolean isInternal(p)
boolean isExternal(p)
boolean isRoot(p)
object replace (p, o)
Additional update methods
may be defined by data
structures implementing the
Tree ADT
CS210-Summer 2005, Lecture 8
28
Preorder Traversal



A traversal visits the nodes of a
tree in a systematic manner
In a preorder traversal, a node is
visited before its descendants
Application: print a structured
document
1
Algorithm preOrder(v)
visit(v)
for each child w of v
preorder (w)
A
2
5
9
B
E
3
4
C
D
6/30/2016
6
7
F
I
8
G
CS210-Summer 2005, Lecture 8
H
29
Postorder Traversal


In a postorder traversal, a
node is visited after its
descendants
Application: compute space
used by files in a directory and
its subdirectories
9
3
Algorithm postOrder(v)
for each child w of v
postOrder (w)
visit(v)
A
8
7
B
E
1
2
C
D
6/30/2016
4
5
F
I
6
G
CS210-Summer 2005, Lecture 8
H
30
Download