You are on page 1of 11

INTRODUCTION TO DATA STRUCTURE

CHAPTER 1 (Contd..)

Ankita R Karia

ABSTRACT DATA TYPE

The term abstract data type refers to the basic mathematical concept that defines the data type.

Its definition is not concerned with implementation details. It is an useful tool for specifying the logical properties of a data type.

A data structure is a physical implementation of an ADT. Each ADT operation is defined by its inputs and outputs.
18-Dec-12 Ankita R Karia 2

ABSTRACT DATA TYPE

An ADT consists of TWO parts:1. Value Definition:Defines the collection of values for the ADT. It consists of two parts: Definition Clause. Condition Clause.

2. Method Definition:It defines the various functions. Each method is defined with three parts:-

Header. Optional Precondition.

Optional Post condition.

EXAMPLE OF ADT

Consider the ADT RATIONAL which corresponds to the mathematical concept of rational number. Operations that can be performed on rational number are:

Creation of rational number. Addition of two rational number. Multiplication. Test for equality.

Initial Specification of ADT:abstract class RATIONAL <integer, integer> { Definition Clause condition RATIONAL[1]!=0;
STATES THAT RATIONAL CONSISTS OF TWO integers

Condition Clause
VALUE DEFINITION

USED TO SPECIFY CONDITION ON NEWLY DEFINED TYPE

abstract RATIONAL makeRATIONAL (int a, int b) precondition b!=0;


HEADER METHOD DEFINITION

postcondition makeRATIONAL[0]==a; makeRATIONAL[1]==b;

Definition for Addition:abstract class RATIONAL <integer, integer> { condition RATIONAL[1]!=0;


HEADER

abstract RATIONAL add (RATIONAL a, RATIONAl b) postcondition add[1] = = a[1]*b[1]; add[0] = = a[0]*b[1] + b[0]*a[1];

Definition for Multiplication:abstract class RATIONAL <integer, integer> { condition RATIONAL[1]!=0;

abstract RATIONAL mult (RATIONAL a, RATIONAl b) postcondition mult[0] = = a[0]*b[0]; mult[1] = = a[1]*b[1];

ARRAY as an ADT

Let ARRTYPE (ub,elType) denote the ADT corresponding to the JAVA array of the type elType array[ub];
Any data type

ADT is determined by the values of one or more parameters,

where ub and elType are parameters and not values.

Consider an One-dimensional array. for e.g. ARRTYPE (10,int)

int x[10].

ADT Specification for ARRAY


abstract class <<eltype,ub>> ARRTYPE (ub,elType) { condition type (ub) = = int; abstract elType extract (a, i) ARRTYPE (ub,elType) a; int i; precondition 0 <=i < ub; postcondition extract = = ai.
}

ADT Specification for ARRAY


abstract class <<eltype,ub>> ARRTYPE (ub,elType) { condition type (ub) = = int; abstract store (a, i,elt) ARRTYPE (ub,elType) a; int i; elType elt; precondition 0 <=i < ub; postcondition a[i] = = elt;
}

ADT for Varying-length Characters:STRINGS


abstract STRING <<char>> abstract length(s) STRING s; postcondition length = = len(s); abstract STRING concat(s1,s2) STRING s1,s2; postcondition concat = = s1+s2;

You might also like