14
Data Structure Lecture 3 Abstract Data Type Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1

Lecture 3 Abstract Data Type Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1

Embed Size (px)

Citation preview

Page 1: Lecture 3 Abstract Data Type Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1

Data Structure

Lecture 3Abstract Data Type

Sandy Ardianto & Erick Pranata© Sekolah Tinggi Teknik Surabaya

1

Page 2: Lecture 3 Abstract Data Type Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1

Motives» Problem Solving is different from coding˃ Algorithm˃ Documentation˃ Code

» Algorithm and Documentation is Abstract Layer

» Code is Implementation Layer

2

© Sekolah Tinggi Teknik Surabaya

Page 3: Lecture 3 Abstract Data Type Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1

Motives» Abstraction is important to˃ Improve Readablity˃ Organize Data through Encapsulation

» Abstract Data Type (ADT)˃ an object with a generic description

independent of implementation details˃ we think about what can be done with the

data, not how it is done

3

© Sekolah Tinggi Teknik Surabaya

Page 4: Lecture 3 Abstract Data Type Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1

ADT is A Set of Operation» ADT abstracts away from a specific representation to

focus on the semantic meaning of the data » In what sense are the following two definitions

different?

» Although the representations differ, the client should instead consider a Point as a set of operations to create and manipulate 2D points on the plane

» By restricting the client to only call operations to access data, the potential for modifying the representation (and supporting algorithms) remains

4

© Sekolah Tinggi Teknik Surabaya

Page 5: Lecture 3 Abstract Data Type Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1

ADT = Objects + Operations» The only operations on objects of the

type are those provided by the abstraction

» The implementation is hidden

5

© Sekolah Tinggi Teknik Surabaya

Page 6: Lecture 3 Abstract Data Type Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1

ADT Example

6

© Sekolah Tinggi Teknik Surabaya

Page 7: Lecture 3 Abstract Data Type Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1

Implementation of ADT» An implementation of ADT consists of

storage structures to store the data items and algorithms for basic operation

7

© Sekolah Tinggi Teknik Surabaya

Page 8: Lecture 3 Abstract Data Type Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1

Another Example» Consider example of an airplane flight with 10

seats to be assigned» Tasks

˃ List available seats˃ Reserve a seat

» How to store, access data?

Page 9: Lecture 3 Abstract Data Type Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1

Another Example» Consider example of an airplane flight with 10

seats to be assigned» Tasks

˃ List available seats˃ Reserve a seat

» How to store, access data?˃ 10 individual variables

Page 10: Lecture 3 Abstract Data Type Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1

Use 10 individual variables

» Algorithm to List available seats

1. If seat1 == ‘ ’:display 1

2. If seat2 == ‘ ’: display 2

.

.

.10. If seat10 == ‘ ’:

display 10

» Algorithm to Reserve a seat

1. Set DONE to false2. If seat1 ==‘ ’:

print “do you want seat #1??”Get answerif answer==‘Y’:

set seat1 to ‘X’set Done to True

3. If seat2 ==‘ ’:print “do you want seat #2??”Get answerif answer==‘Y’:

set seat2 to ‘X’set Done to True

.

.

.

Page 11: Lecture 3 Abstract Data Type Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1

Another Example» Consider example of an airplane flight with 10

seats to be assigned» Tasks

˃ List available seats˃ Reserve a seat

» How to store, access data?˃ 10 individual variables˃ An array of variables

Page 12: Lecture 3 Abstract Data Type Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1

Use Array» Algorithm to List available seats

For number ranging from 0 to max_seats-1, do:If seat[number] == ‘ ’:

Display number

» Algorithm to Reserve a seat

Read in number of seat to be reservedIf seat[number] is equal to ‘ ’:

set seat[number] to ‘X’Else

Display a message that the seat having this number is occupied

Page 13: Lecture 3 Abstract Data Type Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1

Exercise» Develop a USD ADT˃ Properties

+ dollar: int+ cent: int

˃Methods+ Add+ Subtract+ Multiply+ Divide+ ToString

13

© Sekolah Tinggi Teknik Surabaya

Page 14: Lecture 3 Abstract Data Type Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1

Reference» Abstract Data Type, http://

c2.com/cgi/wiki?AbstractDataType, accessed on March 24th 2014.

» Abstract Data Types, http://cse.iitkgp.ac.in/pds/notes/ADT.html, accessed on March 24th 2014.

» Dr. Bernard Chen Ph.D., Data Structures and Abstract Data Type, 2008

14

© Sekolah Tinggi Teknik Surabaya