This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Computer Science 136: David R. Cheriton School of Computer Science Elementary Algorithm Design and Data Abstraction Winter 2012 Lecture 4 – Abstract Data Types (ADTs) An Abstract Data Type is a mathematical description of a collection of data. ADTs are an important approach to separating the specification of a type of data from its implementation. They are beneficial both for the design and implementation of algorithms and data structures. CS 136 – Winter 2012 04 – Abstract Data Types (ADTs) 1/28 Topics from Last Time “Separation of concerns” The Simon game CS 136 – Winter 2012 04 – Abstract Data Types (ADTs) 2/28 Abstract Data Types (ADTs) An ADT is a mathematical specification of a collection of data. Specification is given though the set of available operations one can perform on the data Useful in separating specification from implementation One ADT specification can have several implementations Description uses mathematical notation (sets, sequences) Abstract : Implentation is not specified Data : Operations are performed on a collection of data Type : Description of a kind of data (e.g. string) not a description of a specific instance (e.g. "Hello, world" ). Many instances of an objects conforming to an ADT may be created; each can contain different data. CS 136 – Winter 2012 04 – Abstract Data Types (ADTs) 3/28 Specification Specification of the ADT is just a list of the specifications for each operation The specification of an operation for an ADT includes: List and brief description of parameters A precondition : A logical statement that is true if and only if the operation can be applied A postcondition : A logical statement that is true if the precondition is true and the operation is applied CS 136 – Winter 2012 04 – Abstract Data Types (ADTs) 4/28 Example: The queue ADT A queue is a (possibly empty) sequence ( q 1 , q 2 , ..., q n ) . Operations newqueue Takes no parameters Precondition: True (operation can always be done) Postcondition: Produces a queue Q that is the empty sequence () queueempty? Q One parameter, a queue Q = ( q 1 , q 2 , ..., q n ) Precondition: True Postcondition: produces True if sequence is empty, False otherwise ... CS 136 – Winter 2012 04 – Abstract Data Types (ADTs) 5/28 Queue: More interesting operations enqueue Q e Two parameters, an item e and a queue Q = ( q 1 , q 2 , ..., q n ) Precondition: True Postcondition: Produces Q = ( e , q 1 , q 2 , ..., q n ) head Q One parameter, a queue Q = ( q 1 , q 2 , ..., q n ) . Precondition: n > 1 Postcondition: Produces value q n . dequeue Q One parameter, a queue Q = ( q 1 , q 2 , ..., q n ) Precondition: n > 1 Postcondition: Produces Q = ( q 1 , ..., q n 1 ) ....
View
Full
Document
 Winter '10
 cormack
 Computer Science

Click to edit the document details