datové struktury a algoritmy Část 5 ab straktní datové typy
DESCRIPTION
Datové struktury a algoritmy Část 5 Ab straktní datové typy. Petr Felkel. Dat a stru ctures and algorithms Part 5 A bstra ct dat a typ es. Petr Felkel. Abstraktní datové typy. Abstrahují od jak ? Jak jsou data zobrazena v paměti … Jak jsou prováděny operace … Zdůrazňují co ? - PowerPoint PPT PresentationTRANSCRIPT
Datové struktury a algoritmy
Část 5
Abstraktní datové typy
Petr Felkel
Data structures and algorithms
Part 5
Abstract data types
Petr Felkel
3 / 61 DSA
Abstraktní datové typy• Abstrahují od jak?
• Jak jsou data zobrazena v paměti…
• Jak jsou prováděny operace…
• Zdůrazňují co? • Co s nimi operace s daty provádějí
nezávisle na konkrétní implementaci
4 / 61 DSA
Abstract data types• Abstract from: How?
• How is the data stored in memory?
• How are the operations executed?
• Emphasize: What? • What do the operations do with the data
(independent of any particular implementation)?
5 / 61 DSA
Abstraktní datové typy Fronta (Queue)
Zásobník (Stack)
Pole (Array)
Tabulka (Table)
• Seznam (List)
• Strom (Tree)
• Množina (Set)
6 / 61 DSA
Seznam (List)• Posloupnost údajů
• Ukazovátko
• Pouze v místě ukazovátka lze přidat / zrušit / aktualizovat prvek
• Homogenní, lineární, dynamická
7 / 61 DSA
List (Seznam)• Sequence of items
• Marker
• Insert, delete, update only where points the marker
• Homogeneous, linear, dynamic
8 / 61 DSA
List (Seznam)
ListElem
readinit
Bool
Nat
insertempty,full delete,
first,lastnext,prev
length
9 / 61 DSA
List (Seznam)init: -> Listinsert(_,_): Elem, List -> Listread(_): List -> Elemdelete(_), first(_), last(_)next(_), prev(_): List -> Listlength(_): List -> Natempty(_), full(_)atbeg(_), atend(_): List -> Bool
10 / 61 DSA
List (Seznam)
Pomocný druh Seq = posloupnost bez ukazovátka
Pomocné operace:new: -> Seq ... prázdná posloupnostcons(_,_): Elem, Seq -> Seqcons2(_,_): Elem, List -> List
... vlož do čelamark(_): Seq -> List ... ukaz. do čela
11 / 61 DSA
List (Seznam)
cons2( a, cons2( b, mark( cons( c, cons( d, new )))))
a b c d
Example:
12 / 61 DSA
List – insert
Where to insert?
Where to leave the marker?
a b c d
x
a b c d
a b c d
x
a b c d
x
a b c d
x
13 / 61 DSA
List – delete
Where to leave the marker?
a b d
a b c d
a b d
On the next
On the previous
14 / 61 DSA
var: x,y:Elem, s:Seq, l:Listinit = mark( new )
insert( x, mark(s) ) = cons2( x, mark(s))
... Insert BEFORE the pointer
insert( x, cons2( y, l ))= cons2( y, insert( x, l ))
List (Seznam)
x
x
x
x
y
y
15 / 61 DSA
delete( init ) = init
delete( mark( cons( x, s))) = mark( s )
... delete by the pointer
delete( cons2( x, l )) = cons2( x, delete( l ))
... delete by the pointer
List (Seznam)
x
x
x
16 / 61 DSA
List (Seznam)next( init ) = init
next( mark( cons( x, s ) ) ) = cons2( x, mark( s ) )
next( cons2( x, l ) ) = cons2( x, next( l ) )
x x
x x
... pointer move
..no change before
17 / 61 DSA
List (Seznam)prev( mark(s) ) = mark(s)
prev( cons2( x, mark( s ) ) ) = mark( cons( x, s ))
prev( cons2( x, cons2( y, l ))) = cons2( x, prev( cons2( y, l )))
x x ... move inside y
x x ... move by head
y
18 / 61 DSA
List (Seznam)first( mark(s) ) = mark(s)
first( cons2( x, l ) ) = first( prev( cons2( x, l ) ) )
x x ... move step by step to front
... no move by head
19 / 61 DSA
List (Seznam)last( init ) = init
last( mark( cons( x, s ) ) ) = cons2( x, last( mark( s ) ) )
last( cons2( x, l ) ) = cons2( x, last( l ) )
x x
x x
... move back
…no change before
20 / 61 DSA
List (Seznam)read( init ) = error_elem
read( cons2( x, l ) = read( l )
read( mark( cons( x, s ))) = x
21 / 61 DSA
List (Seznam)length( init ) = 0
length( cons2( x, l )) = succ( length( l ) )
length( mark( cons( x, s ))) = succ( length( mark( s )))
... elements before the pointer
... elements after the pointer
22 / 61 DSA
List (Seznam)empty( l ) = ( length( l ) == 0 )
full( l ) = ( length( l ) == max )
atbeg( mark( s )) = trueatbeg( cons2( x, l )) = false
atend( mark( new)) = trueatend( mark( cons( x, s )))= falseatend( cons2( x, l )) = atend( l )
23 / 61 DSA
List implementation In Array O(n) insert, delete
O(1) first, last, prev, next In Array
(head)
1 2 30 4 5
BA C D
tailpoint
1 2 30 4 5
BA C D
(tail)point(head)
Stacks in Array O(1) insert, delete, prev, next
O(n) first, last, …
BA C D
point
24 / 61 DSA
List implementation O(1) insert, O(1)..O(n) delete
O(1) first, last, prev, next
- memory for pointers
In Dynamic memory
tailhead
B CA point len 3
tailhead
B CA point len 3
tailhead
point len B CA3
Linked list
Double linked list
Circular double linked list
25 / 61 DSA
List implementation
tailhead
B CA point len 3
Linked listlist::insert( elem x ) { item *help = new item; if( point == NULL ){ //point behind help->next = NULL; help->val = x; if( tail == NULL ) //empty list head = help; else //add at end tail->next = help; tail = help; } //point still points behind list! else { //point in the list - trick help->val = point->val; help->next = point->next; point->next = help; point->val = x; point = help; } len++;}
tailhead
x CA point len 3
B
help
26 / 61 DSA
List implementationLinked list
list::delete( ) { item *help; if( point != NULL ){ //behind ignore if( point->next == NULL ) { //last help = head; //find predecessor while( help->next != point ) help = help->next;
} help->next = NULL; point = NULL; delete( tail ); tail = help; } else {// trick: skip predec.search help = point->next; *point = *help; if( help == tail ) tail = point; delete( help ); } len--; }}
tailhead
B CA point len 3
help
tailhead
B DA point len
C
help
4
B DA D
help
3
tailhead
point len
27 / 61 DSA
List implementation
tailhead
B CA point len 3
Linked listlist::prev( ) { item *help; if( point != head){//point can move help = head; while( help->next != point ) help = help->next; point = help; }}
tailhead
x CA point len 4
B
help
28 / 61 DSA
List implementation
tailhead
B CA point len 3
Double linked listlist::prev( ) { item *help; if( point != head){//point can move point = point->prev; }}
Prev is the only place to save!
29 / 61 DSA
List implementation
tailhead
B CA point len 3
Double linked listlist::delete( ) { item *help; if( point != NULL ){ //behind ignore help = point->next ; if( head == point ) //first elem head = help; if( tail == point ) //last elem tail = tail->prev; if( help != NULL ) //update prev help->prev = point->prev; //update next if( point->prev != NULL ); point->prev->next = help;
delete( point ); point = help;
len--; }}
30 / 61 DSA
List implementation
point len
B CA
3
Circular double linked list
tailhead
list::delete( ) { item *help; if( point != list ){ //not at end point->prev->next = point->next; point->next->prev = point->prev;
help = point; point = point->next;
delete( help );
len--; }}
list::prev( ) { if( !atBegin() ) //point!=list->head point = point->prev;}
list
help
31 / 61 DSA
Abstraktní datové typy Fronta (Queue)
Zásobník (Stack)
Pole (Array)
Tabulka (Table)
Seznam (List)
• Strom (Tree)
• Množina (Set)
32 / 61 DSA
Strom (Tree)• Kdy?
• řazení, vyhledávání, vyhodnocování výrazů, ...
• acyklický souvislý graf
• kořenový strom • orientovaný strom, zvláštní uzel - kořen• kořen spojen se všemi uzly orient. cestou• binární strom - má 0, (1), 2 následníky
• uspořádaný binární strom - dvojice <u, u1> a <u, u2> jsou uspořádány
33 / 61 DSA
Tree (Strom)• When?
• sorting, searching, evaluation of expressions, ...
• acyclic connected graph
• root tree• oriented tree, special node - root• root connected to all nodes by oriented path• binary tree - has 0, (1), 2 successors
• ordered binary tree - pairs <u, u1> and <u, u2> are ordered
34 / 61 DSA
Strom (Tree)• uzel - více následníků
• binární strom - 2 následníci
• následník - opět strom
• homogenní, nelineární, dynamická
35 / 61 DSA
Tree (Strom)• node - more successors
• binary tree - 2 successors
• successor - also a tree
• homogeneous, nonlinear, dynamic
36 / 61 DSA
Binární strom (Tree)
TreeElem
infoempty
Bool
cons
null
left,right
leaf
setleft,setright
37 / 61 DSA
Binární strom (Tree)empty: -> Treeleaf(_): Elem -> Treecons(_,_,_): Elem, Tree, Tree
->Treeleft(_), right(_): Tree -> Treenull(_): Tree -> Bool setleft, setright(_,_): Tree, Tree -> Treesetinfo( Tree, Elem ): -> Treeinfo(_): Tree -> Elem
38 / 61 DSA
Binární strom (Tree)var x: Elem; a,b,t: tree
leaf( x ) = cons( x, empty, empty)
left( empty ) = error_treeleft( cons( x, a, b)) = a
right( empty ) = error_treeright( cons( x, a, b)) = b
39 / 61 DSA
Binární strom (Tree)null( empty ) = truenull( cons( x, a, b)) = false
setleft( empty, t ) = error_treesetleft( cons( x, a, b ), t ) = cons( x, t, b )
setright( empty, t ) = error_treesetright( cons( x, a, b ), t ) = cons( x, a, t )
40 / 61 DSA
Binární strom (Tree)setinfo( empty, x ) = error_treesetinfo( cons( x, a, b ), y ) = cons( y, a, b )
info( empty ) = error_eleminfo( cons( x, a, b ) ) = x
41 / 61 DSA
Abstraktní datové typy Fronta (Queue)
Zásobník (Stack)
Pole (Array)
Tabulka (Table)
Seznam (List)
Strom (Tree)
• Množina (Set)
42 / 61 DSA
Parametrické datové typy• Nové typy
• obohacováním původních o druhy a operace
• často stejné, jen nad jinými základy
• liší se jen některými prvky
=> parametrický datový typodlišné prvky - parametry
43 / 61 DSA
Množinatyp: MNOŽINA( ELEMENT )
parametr: typ prvků ELEMENT
požadavky na parametr:
druhy: Elem
operace: eq(_,_): Elem, Elem -> Bool
použité typy: ELEMENT, přirozená čísla, logické hodnoty
druhy: Elem, Bool, Set, Nat
44 / 61 DSA
Množinaoperace:
[]: Set (prázdná množina)
ins(_,_): Elem,Set ->Set (vložení prvku)
del(_,_): Elem,Set ->Set (zrušení -“- )
in(_,_) : Elem,Set ->Bool(test přísluš.)
card(_) : Set->Nat (počet prvků)
45 / 61 DSA
Množina bez opakováníproměnné:
s: Set, x,y: Elemaxiomy:
ins(x,s) = if in(x,s) then s del(x,[])=[]
del(x,ins(y,s))= if eq(x,y) then del(x,s)else
ins(y,del(x,s))
bez opakování prvků
nezáleží na pořadí vkládání
46 / 61 DSA
Množina bez opakováníaxiomy: (pokračování)
in(x,[]) = false
in(x,ins(y,s))= if eq(x,y)then true
else in(x,s)
card([]) = 0
card(ins(x,s)) = if(in(x,s)) card(s) else succ(card(s))
47 / 61 DSA
Množina bez opakováníaxiomy: (pokračování)
eq([],[]) = true eq([], ins(x, t)) = false
eq(s,t) = eq(t,s)
eq(ins(x,s),t) = if in(x,s) then eq(s,t) else if in(x,t)
then eq(s,del(x,t))
else false
bez opakování prvků
48 / 61 DSA
Abstraktní datové typy Fronta (Queue)
Zásobník (Stack)
Pole (Array)
Tabulka (Table)
Seznam (List)
Strom (Tree)
Množina (Set)
49 / 61 DSA
Appendix A
Bool example
50 / 61 DSA
Bool data type - signature• Logical value - Bool
• Diagram:
Symbolic: see next slide
true,false
Bool and,eq
not
51 / 61 DSA
Bool data type - signatureLogical value - BoolSymbolic:
druhy: Bool
operace: true,false: Bool (constants, nulary) not: Bool -> Bool (unary operation)
and: Bool,Bool -> Bool
(binary)
eq: Bool,Bool -> Bool
52 / 61 DSA
Bool data type - signaturePrefix notation
and(_,_): Bool,Bool -> Bool
Infixový notation
_&_: Bool,Bool -> Bool
priority: not,and ... Must be stated explicitly
53 / 61 DSA
Bool data type - axiomsvar x,y: Bool
not(true) = false negation
not(false) = true
and(x, true) = x
and(x,false) = false logical AND
and(x,y) = and(y,x)
or(x, true) = true
or(x,false) = x logical OR
or(x,y) = or(y,x)
54 / 61 DSA
Bool data type - axiomsEquality - ver. 1eq(true, true) = true
eq(true,false) = false
eq(false, true) = false
eq(false,false) = true
55 / 61 DSA
Bool data type - axiomsEquality - ver. 2eq(x, true) = x
eq(x, false) = not(x)
eq(x, y) = eq(y, x)
56 / 61 DSA
Appendix B - Expressionsa) constant: f
(where f is nulary operation declared: f:d)b) variable: x
(where f is a variable declared var x:d)c) operation – prefix notation: f(t1,t2, ... tn) (where f is n-ary operation declared:
f: d1, d2, ..., dn -> d and t1, t2, ..., tn are expressions of sorts d1, d2, ..., dn)d) operace – infix notation : t1 f1 t2 f2 ...fn tn+1 (where f (n+1)-ary operation declared _f1_f2_..._fn : d1, d2, ..., dn+1 -> d and t1, t2, ..., tn+1 expressions of sorts d1, d2, ..., dn+1)e) brackets: (t)
(where t is an expression of sort d)f) other notations are not expressions.
57 / 61 DSA
Expressions (cont.):Herbrand’s universum for a given signature
= set of all expressions on given signature
Example: {true, false, not(true), not(false), not(not(true)),...}
Two classes of equivalence (describe the same value)• {true, not(false), not(not(true)),...}
... [true]
• {false, not(true), not(not(false)),...} ...
[false]
58 / 61 DSA
Bool - třídy ekvivalence• Množina všech výrazů nad signaturou
(Herbrandovo univerzum) {true, false, not(true), not(false), not(not(true)),...}
• Bool: lze rozdělit na 2 třídy ekvivalence:• {true, not(false), not(not(true)),...} ... [true]• {false, not(true), not(not(false)),...}... [false]
• Výrazy třídy popisují různý způsob konstrukce stejné hodnoty
59 / 61 DSA
Bool - třídy ekvivalenceDoplníme axiomy
true = [true] false = [false]not([x])=[not(x)]and([x],[y]) = [and(x, y)]
Axiomy s třídami ekvivalence=> tvoří model datového typu
60 / 61 DSA
Prameny / References• Jan Honzík: Programovací techniky, skripta,
VUT Brno, 19xx
• Karel Richta: Datové struktury, skripta pro postgraduální studium, ČVUT Praha, 1990
• Bohuslav Hudec: Programovací techniky, skripta, ČVUT Praha, 1993
61 / 61 DSA
Prameny / References• Steven Skiena: The Algorithm Design Manual,
Springer-Verlag New York, 1998 http://www.cs.sunysb.edu/~algorith
• Gang of four (Cormen, Leiserson, Rivest, Stein): Introduction to Algorithms, MIT Press, 1990
• Code exapmples: M.A.Weiss: Data Structures and Problem Solving using JAVA, Addison Wesley, 2001, code web page: http://www.cs.fiu.edu/~weiss/dsj2/code/code.html