1 / 61

Datové struktury a algoritmy Část 5 Ab straktní datové typy

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 ?

lavada
Télécharger la présentation

Datové struktury a algoritmy Část 5 Ab straktní datové typy

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Datové struktury a algoritmyČást 5Abstraktní datové typy Petr Felkel

  2. Data structures and algorithmsPart 5Abstract data types Petr Felkel

  3. 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. 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. Abstraktní datové typy • Fronta (Queue) • Zásobník (Stack) • Pole (Array) • Tabulka (Table) • Seznam (List) • Strom (Tree) • Množina (Set)

  6. Seznam (List) • Posloupnost údajů • Ukazovátko • Pouze v místě ukazovátka lze přidat / zrušit / aktualizovat prvek • Homogenní, lineární, dynamická

  7. List (Seznam) • Sequence of items • Marker • Insert, delete, update only where points the marker • Homogeneous, linear, dynamic

  8. init read Elem List length Nat empty, full Bool insert delete, first,last next,prev List (Seznam)

  9. List (Seznam) • init: -> List • insert(_,_): Elem, List -> List • read(_): List -> Elem • delete(_), • first(_), last(_) • next(_), prev(_): List -> List • length(_): List -> Nat • empty(_), full(_) • atbeg(_), atend(_): List -> Bool

  10. List (Seznam) • Pomocný druh • Seq = posloupnost bez ukazovátka • Pomocné operace: • new: -> Seq ... prázdná posloupnost • cons(_,_): Elem, Seq -> Seq • cons2(_,_): Elem, List -> List • ... vlož do čela • mark(_): Seq -> List ... ukaz. do čela

  11. a b c d List (Seznam) Example: cons2( a, cons2( b, mark( cons( c, cons( d, new )))))

  12. x x x a b c d a b c d a b c d x a b c d a b c d List – insert Where to insert? Where to leave the marker?

  13. a b c d a b d List – delete Where to leave the marker? On the next a b d On the previous

  14. x x x y y x List (Seznam) • var: x,y:Elem, s:Seq, l:List • init = mark( new ) • insert( x, mark(s) ) • = cons2( x, mark(s)) • ... Insert BEFORE the pointer • insert( x, cons2( y, l ))= • cons2( y, insert( x,l))

  15. x x List (Seznam) • 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 x

  16. List (Seznam) • next( init ) = init • next( mark( cons( x, s ) ) ) = • cons2( x, mark( s ) ) • next( cons2( x, l ) ) = • cons2( x, next( l ) ) x x ... pointer move x x ..no change before

  17. 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 by head y x y x ... move inside

  18. x List (Seznam) • first( mark(s) ) = mark(s) • first( cons2( x, l ) ) = • first( prev( cons2( x, l ) ) ) ... no move by head x ... move step by step to front

  19. 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 ... moveback x x …no change before

  20. List (Seznam) • read( init ) = error_elem • read( cons2( x, l ) = read( l ) • read( mark( cons( x, s ))) = x

  21. 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. List (Seznam) • empty( l) = ( length( l ) == 0 ) • full( l) = ( length( l ) == max ) • atbeg( mark( s)) = true • atbeg( cons2( x, l )) = false • atend( mark( new)) = true • atend( mark( cons( x, s )))= false • atend( cons2( x, l )) = atend( l )

  23. 0 1 2 3 4 5 A B C D point tail 0 1 2 3 4 5 C A B C D A B D point (tail) point List implementation In ArrayO(n) insert, delete O(1) first, last, prev, next In Array (head) Stacks in ArrayO(1) insert, delete, prev, next O(n) first, last, … (head)

  24. head tail A B C point len 3 head tail point A B C len 3 A B C 3 List implementation O(1) insert, O(1)..O(n) delete O(1) first, last, prev, next - memory for pointers In Dynamic memory Linked list Double linked list Circular double linked list head tail point len

  25. Linked list List implementation list::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++; } head tail A B C point len 3 help head tail A x B C point len 3

  26. help head tail A B C point len 3 Linked list help help head tail A B C D point 4 len head tail A B D D point len 3 List implementation 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--; } }

  27. Linked list List implementation list::prev() { item *help; if( point != head){//point can move help = head; while( help->next != point ) help = help->next; point = help; } } head tail A B C point len 3 help head tail A x B C point len 4

  28. Double linked list head tail point A B C len 3 List implementation list::prev() { item *help; if( point != head){//point can move point = point->prev; } } Prev is the only place to save!

  29. Double linked list head tail point A B C len 3 List implementation list::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. A List implementation Circular double linked list 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; } head tail B C list point 3 len help

  31. Abstraktní datové typy • Fronta (Queue) • Zásobník (Stack) • Pole (Array) • Tabulka (Table) • Seznam (List) • Strom (Tree) • Množina (Set)

  32. 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. 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. 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. Tree (Strom) • node - more successors • binary tree - 2 successors • successor - also a tree • homogeneous, nonlinear, dynamic

  36. empty info Elem Tree null leaf Bool cons left, right setleft, setright Binární strom (Tree)

  37. Binární strom (Tree) • empty: -> Tree • leaf(_): Elem -> Tree • cons(_,_,_): Elem, Tree, Tree • ->Tree • left(_), right(_): Tree -> Tree • null(_): Tree -> Bool • setleft, setright(_,_): Tree, • Tree -> Tree • setinfo( Tree, Elem ): -> Tree • info(_): Tree -> Elem

  38. Binární strom (Tree) • var x: Elem; a,b,t: tree • leaf( x ) = cons( x, empty, empty) • left( empty ) = error_tree • left( cons( x, a, b)) = a • right( empty ) = error_tree • right( cons( x, a, b)) = b

  39. Binární strom (Tree) • null( empty ) = true • null( cons( x, a, b)) = false • setleft( empty, t ) = error_tree • setleft( cons( x, a, b ), t ) = • cons( x, t, b ) • setright( empty, t ) = error_tree • setright( cons( x, a, b ), t ) = • cons( x, a, t )

  40. Binární strom (Tree) • setinfo( empty, x ) = error_tree • setinfo( cons( x, a, b ), y ) = • cons( y, a, b ) • info( empty ) = error_elem • info( cons( x, a, b ) ) = x

  41. Abstraktní datové typy • Fronta (Queue) • Zásobník (Stack) • Pole (Array) • Tabulka (Table) • Seznam (List) • Strom (Tree) • Množina (Set)

  42. 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ý typ odlišné prvky - parametry

  43. Množina typ: 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. Množina operace: []: 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. Množina bez opakování proměnné: s: Set, x,y: Elem axiomy: 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. 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. 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. Abstraktní datové typy • Fronta (Queue) • Zásobník (Stack) • Pole (Array) • Tabulka (Table) • Seznam (List) • Strom (Tree) • Množina (Set)

  49. Appendix A Bool example

  50. true,false Bool and,eq not Bool data type - signature • Logical value - Bool • Diagram: Symbolic: see next slide

More Related