abstract data types (adts) an adt consists of: –a set of values, and –a set of operations on...

13
Abstract Data Types (ADTs) An ADT consists of: a set of values, and a set of operations on those values. Example: rational numbers some values: 15/7, -3/4, 123/1, 0/1 (but NOT 1/0 !) some operations: addition, multiplication, negation An ADT therefore specifies: what the members are, and what operations are supported.

Upload: flora-morris

Post on 18-Dec-2015

216 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Abstract Data Types (ADTs) An ADT consists of: –a set of values, and –a set of operations on those values. Example: rational numbers –some values: 15/7,

Abstract Data Types (ADTs)• An ADT consists of:

– a set of values, and– a set of operations on those values.

• Example: rational numbers– some values: 15/7, -3/4, 123/1, 0/1 (but NOT 1/0 !)– some operations: addition, multiplication, negation

• An ADT therefore specifies:– what the members are, and– what operations are supported.

Page 2: Abstract Data Types (ADTs) An ADT consists of: –a set of values, and –a set of operations on those values. Example: rational numbers –some values: 15/7,

What an ADT isn’t:

• An ADT does not specify:–how the data is stored/represented, or–how the operations are implemented.

• These details are abstracted away.• An ADT is implementation independent• An ADT is language independent.• In Java, an ADT is typically specified in an

interface.

Page 3: Abstract Data Types (ADTs) An ADT consists of: –a set of values, and –a set of operations on those values. Example: rational numbers –some values: 15/7,

Data Structures (DS)

• A data structure is an implementation of an ADT.• In Java, data structures are classes.• In the Java Collections framework, the List

interface specifies an ADT that is implemented by several data structures:– ArrayList (an array-based structure)– LinkedList (a linked structure)

Page 4: Abstract Data Types (ADTs) An ADT consists of: –a set of values, and –a set of operations on those values. Example: rational numbers –some values: 15/7,

Analogy #1:vending machine

• You can perform only the specific tasks that the machine's interface presents to you.

• You must understand these tasks. E.g. you must know what to do to buy a beverage.

• You cannot see or access the inside of the machine, because a steel shell encapsulates it.

• You can use the machine even though you don't know what happens inside.

• If someone replaced the machine's inner mechanism with an improved version, leaving the interface unchanged, you could still use the machine the same way.

Page 5: Abstract Data Types (ADTs) An ADT consists of: –a set of values, and –a set of operations on those values. Example: rational numbers –some values: 15/7,

Analogy #2:car

• Basic interface is the same:– steering wheel to determine path– gearshift (on an automatic) to determine direction

(drive/reverse)– pedals to determine speed (accelerator/brake)

• Underlying implementation is irrelevant– steering: recirculating ball, rack & pinion– transmission: 3, 4 or 5 speed, CVT– engine: 4, 5, 6 or 8 cylinder, gas, diesel, CNG or hybrid

• Client treats them all the same!

Page 6: Abstract Data Types (ADTs) An ADT consists of: –a set of values, and –a set of operations on those values. Example: rational numbers –some values: 15/7,

What if the interface changes?

• How easy is it for people to switch from driving an automatic to a manual transmission car?

• Automatic:– gear selector: drive/reverse– pedals: accelerator, brake

• Manual:– gear selector: 1, 2, 3, 4, 5, R– pedals: accelerator, brake, clutch

• Client needs to adjust to the new interface!

Page 7: Abstract Data Types (ADTs) An ADT consists of: –a set of values, and –a set of operations on those values. Example: rational numbers –some values: 15/7,

With an ADT

• The client can only perform the operations specific to the ADT.

• The client must adhere to the specifications of the operations that the ADT provides.

• Thus, client must understand how to use the operations.

Page 8: Abstract Data Types (ADTs) An ADT consists of: –a set of values, and –a set of operations on those values. Example: rational numbers –some values: 15/7,

Interacting with a List (ADT)

• The client cannot access the data within the list without using an ADT operation. The principle of encapsulation hides the data within the ADT.

• The client can use the ADT, even though it can't access the data directly, and does not know how it is stored.

• If you vary the implementation, but maintain the interface, the client isn’t affected and doesn’t have to care.

Page 9: Abstract Data Types (ADTs) An ADT consists of: –a set of values, and –a set of operations on those values. Example: rational numbers –some values: 15/7,

Examples of ADTs• Bag – unordered collection, allowing duplicates• Set – unordered collection, no duplicates• List – ordered collection – client determines order• Sorted list – comparator determines ordering• Stack – Last-In First-Out (insert/remove from front)• Queue – First-In First-Out (insert back, remove front)• Tree – hierarchical organization• Binary Search Tree – comparator determines ordering

– a simple BST makes no performance guarantees– more advanced BSTs can make performance guarantees

• Dictionary – collection of key-value pairs• Graph – collection of vertices and arcs

Page 10: Abstract Data Types (ADTs) An ADT consists of: –a set of values, and –a set of operations on those values. Example: rational numbers –some values: 15/7,

Creating an ADT

To define an ADT, we • Describe its data• Specify the operations on that data• Focus on “what”, not “how”.

Page 11: Abstract Data Types (ADTs) An ADT consists of: –a set of values, and –a set of operations on those values. Example: rational numbers –some values: 15/7,

Bag ADT

• Values: object references (unordered).• We use generics to define a parameterized

type.• The type of the members of the ADT is

specified by a type parameter in the definition of the interface.

Page 12: Abstract Data Types (ADTs) An ADT consists of: –a set of values, and –a set of operations on those values. Example: rational numbers –some values: 15/7,

Bag Operations

• Basic operations:– add (client has no control over placement; returns a

boolean, multiple references to same object are permitted)

– remove (based on object identity, i.e. == on object references; returns a boolean)

– contains (membership test, based on object identity; returns a boolean)

– size (how many values are in a given bag)

Page 13: Abstract Data Types (ADTs) An ADT consists of: –a set of values, and –a set of operations on those values. Example: rational numbers –some values: 15/7,

Implementation options• There are two basic approaches:

– array-based– linked

• We will consider array-based implementation first

• Use a TDD approach: first consider what the essential functionality of a given ADT is.