Abstract Data Type (ADT). 2 An Abstract Data Type (ADT) is a data structure with a set of operations...

18
Abstract Data Type (ADT)

Transcript of Abstract Data Type (ADT). 2 An Abstract Data Type (ADT) is a data structure with a set of operations...

Page 1: Abstract Data Type (ADT). 2 An Abstract Data Type (ADT) is a data structure with a set of operations –Operations specify how the ADT behaves, but does.

Abstract Data Type (ADT)

Page 2: Abstract Data Type (ADT). 2 An Abstract Data Type (ADT) is a data structure with a set of operations –Operations specify how the ADT behaves, but does.

2

Abstract Data Type (ADT)• An Abstract Data Type (ADT) is a data

structure with a set of operations– Operations specify how the ADT behaves, but

does not reveal how they are implemented– In many cases, there are more than one way to

implement an ADT

• In this course we will cover lots of ADTs– Lists– Stacks– Queues– Trees & Search Trees– Hash Tables & Tries

Page 3: Abstract Data Type (ADT). 2 An Abstract Data Type (ADT) is a data structure with a set of operations –Operations specify how the ADT behaves, but does.

3

Abstract Data Type (ADT) - more• An Abstract Data Type (ADT) is a data

structure with a set of operations

Attributes (ADT State)……

Operations (ADT Methods)

Operation1(…)Operation2(…)Operation3(…)………

Page 4: Abstract Data Type (ADT). 2 An Abstract Data Type (ADT) is a data structure with a set of operations –Operations specify how the ADT behaves, but does.

4

List ADT• What is a list?

– An ordered sequence of elements A1, A2, …, AN– Elements may be of arbitrary, but the same type

(i.e., all ints, all doubles etc.)

• Common List operations are:– Insert(ElementType E, Position P)– Delete(Position P)– Find(ElementType E)– IsEmpty() – Returns true if the list is empty– First() – Returns the first element– Last() – Returns the last element– Kth(Position K) – Returns the Kth element– Length() – Returns the length of the list

Page 5: Abstract Data Type (ADT). 2 An Abstract Data Type (ADT) is a data structure with a set of operations –Operations specify how the ADT behaves, but does.

5

Array List ADT

#define MAX_SIZE 100;typedef struct ArrayList {int elements[MAX_SIZE];int noOfElements;

//public ArrayList (); // Constructor void Insert(int e, int pos); void Delete(int pos); int Find(int e); bool IsEmpty(); int First(); int Last(); int Kth(int pos); int Length();};

ArrayList ADT

// Constructor: // Make empty listArrayList(){ noOfElements = 0;} // end-ArrayList

Page 6: Abstract Data Type (ADT). 2 An Abstract Data Type (ADT) is a data structure with a set of operations –Operations specify how the ADT behaves, but does.

6

The Abstract Data Type (ADT)

• We know what a data type can do• How it is hidden for the user

The concept of abstraction means:

With an ADT users are not concerned with how the task is done but rather with what it can do.

Page 7: Abstract Data Type (ADT). 2 An Abstract Data Type (ADT) is a data structure with a set of operations –Operations specify how the ADT behaves, but does.

7

ADT: Example

• The program code to read/write some data is ADT. It has a data structure (character, array of characters, array of integers, array of floating-point numbers, etc.) and a set of operations that can be used to read/write that data structure.

Page 8: Abstract Data Type (ADT). 2 An Abstract Data Type (ADT) is a data structure with a set of operations –Operations specify how the ADT behaves, but does.

8

The Abstract Data Type (ADT)

• A data declaration packaged together with the operations that are meaningful for the data type.

• In other words, we encapsulate the data and the operations on the data, and then we hide them from the user.

The Abstract Data Type (ADT) is:

Declaration of data

Declaration of operations

Encapsulation of data and operations

Page 9: Abstract Data Type (ADT). 2 An Abstract Data Type (ADT) is a data structure with a set of operations –Operations specify how the ADT behaves, but does.

9

The Abstract Data Type (ADT)

• All references to and manipulation of the data in a structure must be handled through defined interfaces to the structure.

• Allowing the application program to directly reference the data structure is a common fault in many implementations.

• We must hide the implementation from the user

Page 10: Abstract Data Type (ADT). 2 An Abstract Data Type (ADT) is a data structure with a set of operations –Operations specify how the ADT behaves, but does.

ADT OperationsADT Operations• Several classes of operations are required in Several classes of operations are required in

order to effectively used ADT.order to effectively used ADT. 1.1. ConstructorsConstructors – used to create instances of the – used to create instances of the

data type.data type.2.2. DestructorsDestructors – used to destroy an instance of – used to destroy an instance of

the data typethe data type3.3. AccessorsAccessors – used to access attributes of the – used to access attributes of the

data type. (Such as “get” functions)data type. (Such as “get” functions)4.4. ModifiersModifiers – used to modify attributes of the – used to modify attributes of the

data type. (Such as “set” functions)data type. (Such as “set” functions)

Object-oriented languages provide some of this Object-oriented languages provide some of this functionality within the language. In C you must do functionality within the language. In C you must do it yourself.it yourself.

Page 11: Abstract Data Type (ADT). 2 An Abstract Data Type (ADT) is a data structure with a set of operations –Operations specify how the ADT behaves, but does.

11

Page 12: Abstract Data Type (ADT). 2 An Abstract Data Type (ADT) is a data structure with a set of operations –Operations specify how the ADT behaves, but does.

DesignDesignWhile we’ve defined an ADT While we’ve defined an ADT interfaceinterface, we , we

can’t just start coding quite yet. There can’t just start coding quite yet. There are more are more designdesign decisions to be made decisions to be made before writing code for the before writing code for the implementationimplementation. .

A common error among programmers is A common error among programmers is to not place enough emphasis on the to not place enough emphasis on the design step. This is the difference design step. This is the difference between a “programmer” and an between a “programmer” and an “analyst”.“analyst”.

Page 13: Abstract Data Type (ADT). 2 An Abstract Data Type (ADT) is a data structure with a set of operations –Operations specify how the ADT behaves, but does.

Fraction ADT Design: DataFraction ADT Design: DataAnalysis begins with the idea that all the operations will be acting on Analysis begins with the idea that all the operations will be acting on

Fractions that have some internal representation. The representation Fractions that have some internal representation. The representation shall be shared among all the operations. shall be shared among all the operations.

Each operation is responsible for maintaining your design rules regarding Each operation is responsible for maintaining your design rules regarding the representation of Fraction information.the representation of Fraction information.

typedef enum{ POS, NEG } SignType;typedef enum{ POS, NEG } SignType;

typedef Fraction {typedef Fraction { //private://private: // data members// data members

int numerator; /* numerator and denominator are declared as */int numerator; /* numerator and denominator are declared as */ int denominator; /* int to simplify the algorithms, but they */int denominator; /* int to simplify the algorithms, but they */ SignType sign; /* will always be stored >= 0 */SignType sign; /* will always be stored >= 0 */ // public:// public: // member functions would follow// member functions would follow //private://private: // utility functions would follow// utility functions would follow};};

All operations must preserve these rules.All operations must preserve these rules.

Page 14: Abstract Data Type (ADT). 2 An Abstract Data Type (ADT) is a data structure with a set of operations –Operations specify how the ADT behaves, but does.

Fraction ADT VariablesFraction ADT Variables• Now that we’ve decided how to Now that we’ve decided how to

represent the represent the valuevalue of a fraction, how of a fraction, how are we going to declare are we going to declare variablesvariables whose whose values are fractions?values are fractions?

Fraction f1, f2;Fraction f1, f2;

Page 15: Abstract Data Type (ADT). 2 An Abstract Data Type (ADT) is a data structure with a set of operations –Operations specify how the ADT behaves, but does.

Fraction ADT AlgorithmsFraction ADT Algorithms• You are responsible for designing algorithms You are responsible for designing algorithms

that implement all the operations. What that implement all the operations. What process must you follow to implement the process must you follow to implement the operations?operations?

a c ad + bc + = b d bd

Sum:then, reduce

a c ac * = b d bd

Product: then, reduce

Subtraction involves adding the opposite, and Division involves multiplying by the reciprocal.

Page 16: Abstract Data Type (ADT). 2 An Abstract Data Type (ADT) is a data structure with a set of operations –Operations specify how the ADT behaves, but does.

Fraction ADT AlgorithmsFraction ADT Algorithms• Reducing a fraction involves finding the Reducing a fraction involves finding the

Greatest Common Divisor, and then dividing Greatest Common Divisor, and then dividing all the terms by that amount.all the terms by that amount.

Euclid’s Algorithm:Euclid’s Algorithm:if x < y, then swap x and yif x < y, then swap x and y

While y is not zeroWhile y is not zero

remainder = x mod yremainder = x mod y

x = yx = y

y = remaindery = remainder

When you’re done, x is the GCD.When you’re done, x is the GCD.

Page 17: Abstract Data Type (ADT). 2 An Abstract Data Type (ADT) is a data structure with a set of operations –Operations specify how the ADT behaves, but does.

AccessorsAccessors/*********** Accessors /*********** Accessors **************/**************/

int getNumerator();int getNumerator();

int getDenominator();int getDenominator();

char getSign();char getSign();

Page 18: Abstract Data Type (ADT). 2 An Abstract Data Type (ADT) is a data structure with a set of operations –Operations specify how the ADT behaves, but does.

ModifiersModifiers /********** Modifiers ***********/

void setNumerator(int);

void setDenominator(int);

void setSign(char);

void reduceFract();