Documente Academic
Documente Profesional
Documente Cultură
Lectures 11 & 12
Abstract data types (ADTs):
Object-oriented design & implementation
Outline
● Overview
Lecture
Lecture 11
11
● Tree
● List
Lecture
Lecture 12
12
Duc M. L. PR2 2
Overview
Duc M. L. PR2 4
1
List and Tree 2
2.1
1 1.1
2 1.2
1.3
3
1.4
4 1.5
1.6
2.2
2.3
2.4
2.5
2.6
3
4
Duc M. L. PR2 5
Relationship between List and Tree
● Both are recursive structures
● A list is a kind of binary tree
● A tree is a kind of recursive list:
● a list whose elements can themselves be lists
● More details shortly
Duc M. L. PR2 6
Java support
● List is provided as part of the Java's collection
API
● package: java.util
● classes: ArrayList, LinkedList
● Java does not provide an implementation for
Tree
● third-party library is needed
Duc M. L. PR2 7
DEMO
Java List
● Demonstrate how to use ArrayList or
LinkedList
Duc M. L. PR2 8
Guiding principle
● Design and implement Tree and List without
using any libraries
● Focus on the most basic type of tree and list
● Use recursion as the guiding principle
Duc M. L. PR2 9
Tree
Duc M. L. PR2 11
Example
root
1
edge
2 3 4
parent
5 6 7
child
Duc M. L. PR2 12
Tree-related terms
● Sibling
● Path, ancestor, descendant
● Subtree
● Leaf and interior nodes
● Height and depth
● [0] Ordered tree
● [0] Labelled tree
● [0] Branching factor
Duc M. L. PR2 13
Sibling
nodes of 1
same parent
2 3 4
5 6 7
Duc M. L. PR2 14
Path, ancestor, descendant
path =
sequence of 1 ancestors
(path.length >= 1)
(>= 0) edges
2 3 4
descendants
(path.length >= 0)
5 6 7
Duc M. L. PR2 15
a node and Subtree
all proper
descendants
(if any) 1
2 3 4
5 6 7
Duc M. L. PR2 16
Leaf and interior nodes
interior 1
nodes
(incl. root)
2 3 4
5 6 7
leaf nodes
Duc M. L. PR2 17
Height and depth (level)
height(n) = length
of longest path 1
from n to a leaf
2 depth(n)3= length of 4 2
path from n to root
1
5 6 7
Duc M. L. PR2 18
Recursive definition
● We can recursively define a tree in two ways:
● bottom-up: tree is formed from the sub-trees
● top-down: tree is formed by extending out from the root
● Each definition gives rise to a different tree design
Duc M. L. PR2 19
Preliminaries
● Tree is a tuple <r,nodes,edges>, where:
● nodes: set of nodes, edges: set of edges,
● r is the root (r in nodes)
● Terms:
Given tree T = <r,nodes,edges>
T.root := r
T.nodes := nodes
T.edges := edges
edge(p,n): edge connecting parent p to child n
Duc M. L. PR2 20
Bottom-up definition
Basis
For any node r, T = <r,{r},{}> is a tree.
Induction
"1 £ k ÎN, "k trees T1, T2,...,Tk, and "node r Ï
T1,...,Tk:
T = <r, T1.nodes ⋃ ... ⋃ Tk.nodes ⋃ {r},
{edge(r,T1.root),...,edge(r,Tk.root)} ⋃
T1.edges ⋃ ... ⋃ Tk.edges>
is a tree.
Duc M. L. PR2 21
Example (1)
Basis 3
r=5,6,3,7
5 6 7
Induction (1)
2 3 4
r=2,4
5 6 7
Duc M. L. PR2 22
Example (2)
Induction (2) 1
r=1
2 3 4
5 6 7
Duc M. L. PR2 23
Top-down definition
Basis
For any node r, T = <r,{r},{}> is a tree.
Induction
"node n, "tree T' (n ÏT'), for some node p ÎT':
T = <T'.root, T'.nodes ⋃ {n}, T'.edges ⋃ {edge(p,n)}>
is a tree.
Duc M. L. PR2 24
Example (1)
Basis
1
r=1
Induction (1-3)
n=2,3,4
p=1,1,1
1 1 1
2 2 3 2 3 4
Duc M. L. PR2 25
Example (2)
Induction (4-6)
n=5,6,7
p=2,2,4
1 1 1
2 3 4 2 3 4 2 3 4
5 5 6 5 6 7
Duc M. L. PR2 26
Abstract properties
● Tree is an acyclic, connected graph, i.e.:
PT: there exists exactly one path that
connects any two nodes of a tree.
which also means:
● connectedness: there is a path that connects any
two nodes, and
● acyclicity: there is exactly one such path
● In addition, rooted tree must have at least one
node (the root)
Duc M. L. PR2 27
Abstract properties (design)
● root must be specified
● root in nodes
● nodes and edges are set
● The following properties are equivalent to (yet
easier to check than) PT:
● every non-root node has exactly one parent
● exists a path from root to every node (i.e.
connectedness)
Duc M. L. PR2 28
Design
● Two designs:
● Bottom-up design
● Top-down design
● Common design features:
● node and edge are both represented by Object
● set (of nodes and edges) is represented by Vector
● operations are recursive in nature:
– operate on a node and, recursively, on its children and
descendants (if any)
Duc M. L. PR2 29
How to realise recursive definition
● Recursive definition is placed in the OBJECT
section of header spec.
● Basis rule is realised by one constructor
● Bottom-up differs from top-down in how the
inductive rule is realised:
● bottom-up: another constructor is used
● top-down: operation addNode is used
– more efficient because does not create new Trees during
construction
– a 'pseudo' recursive design (smaller Trees are integral to
larger ones)
Duc M. L. PR2 30
SPEC
Basic bottom-up design
Tree
- root: Object
- nodes: Vector
basis - edges: Vector
+ Tree(Object)
induction + Tree(Object, Tree...)
+ count(Object)
+ toString()
- toString(StringBuffer, Object, int)
+ repOK(): boolean
Duc M. L. PR2 31
SPEC
Basic top-down design
Tree
- root: Object
- nodes: Vector
basis - edges: Vector
+ Tree(Object)
induction + addNode(Object, Object)
+ count(Object)
+ toString()
- toString(StringBuffer, Object, int)
+ repOK(): boolean
Duc M. L. PR2 32
count
Recursive definition:
child(c,n) = true if c is a child of n, false if otherwise
1 if n is leaf
count (n) =
1+ ∑ count (c i ) if n is not leaf
child(c i , n)
Duc M. L. PR2 33
toString
Recursive definition:
● NIL: empty string
● NL: next line character
● “|” : vertical bar character
nodeLine (n , l ) if n is leaf
toString(n , l ) = nodeLine (n , l )+toString(c 1 , l +1)+…+ toString(c k , l +1)
if n is not leaf, child(c i , n)∀ i =1…k
1 1
|-2
|--3
2 10 |---4
|---5
|--6
3 6 11 12
|---7
|----8
4 5 7 |----9
|-10
|--11
8 9 |--12
Duc M. L. PR2 35
EXer
cise
2 10
1(2(3(4 5) 6(7(8 9))) 10(11 12))
3 6 11 12
4 5 7
8 9
Duc M. L. PR2 36
CODE
Implementation
chap6.tree.
bottomup.Tree
topdown.Tree
Duc M. L. PR2 37
DEMO
Application examples
chap6.test.tree
BottomUpTreeTest
TopDownTreeTest
Duc M. L. PR2 38
List
Abstract concept
Design
Implementation
Abstract concept
● List is a finite sequence of elements.
● list is empty if the number of elements is zero
● List of T is a list whose elements are all of type
T
● Notation: (a1,a2,...,an)
● empty list: () or e
● Examples:
● (2, 3, 5, 7, 11, 13, 17, 19)
● (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
● a line of text, a document, a point in Rn, ...
Duc M. L. PR2 40
List-related terms
● Length
● Head, tail
● Sublist, subsequence
● [0] Prefix, suffix
● [!] Element position
Duc M. L. PR2 41
Length
● Number of occurrences of elements
2 3 5 7 11 13 17 19
length = 8
Duc M. L. PR2 42
Sublist, subsequence
● Sublist: a list that contains ⩾0 consecutive
elements of another list
● Subsequence: a sequence of ⩾0 (not
necessarily) consecutive elements of another
list
2 3 5 7 11 13 17 19
2 3 5 2 5 11 13 19
sublist subsequence
Duc M. L. PR2 43
Head, tail
● Head: first element
● Tail: remainder of the elements or empty (a sub-
list)
2 3 5 7 11 13 17 19
head tail
Duc M. L. PR2 44
Recursive definition
● List can be defined recursively in terms of head,
tail:
Basis
() is a list
Induction
"x , "list L: (x, L.elements) is a list
(L.elements is the sequence of elements of L)
Duc M. L. PR2 45
Abstract properties
● list is a sequence of elements
● a non-empty list must have a head
● tail may be empty
Duc M. L. PR2 46
Design
● Two recursive designs:
● “pure” list: list as a recursive class
● “linked” list: list as a chain of element cells
● Common design features:
● element type is Object
● derived attribute: list's length
Duc M. L. PR2 47
“Pure” list
● Head is an element a1 a2 ... an
● Tail is another list
a1 a2 ... an
a2 ... an
...
an
Duc M. L. PR2 48
Linked list design
● Both head and tail are cells
● List points to first cell
● Head of a cell contains the element
● Tail of a cell points to next cell (last tail = null)
L a1 a2 ... an
Duc M. L. PR2 49
How to realise recursive definition
● Recursive definition is placed in the OBJECT
section of header spec.
● Basis rule is realised by the default constructor
● Inductive rule is realised by two operations:
● one other constructor, and
● operation push
Duc M. L. PR2 50
SPEC “Pure” List
+ LinkedList()
basis - LinkedList(Cell)
+ push(Object)
+ remove(Object)
+ lookUp(Object): boolean
induction + head(): Object
+ tail(): List
+ retrieve(int): Object
+ length(): int
+ isEmpty(): boolean
+ toString(): String
+ repOK(): boolean
Cell
- head: Object
- tail: Cell
+ Cell(Object, Cell)
+ Cell(Object)
+ clone(): Cell
+ repOK(): boolean
Duc M. L. PR2 52
EXer
cise push
Duc M. L. PR2 53
EXer
cise remove
Duc M. L. PR2 54
EXer
cise lookUp
Duc M. L. PR2 55
length
● use the derived attribute length
Duc M. L. PR2 56
EXer
cise What about other operations?
● retrieve
● toString
Duc M. L. PR2 57
CODE
Implementation
chap6.list.
List
LinkedList
Duc M. L. PR2 58
DEMO
Application examples
chap6.test.list.
ListTest
LinkedListTest
Duc M. L. PR2 59
EXer
cise
Duc M. L. PR2 60