0% found this document useful (0 votes)
3 views

Unit 2-Introduction To Datastructures

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Unit 2-Introduction To Datastructures

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

INTRODUCTION TO DATA

STRUCTURES
Organizing Data
• Write TO-DO Lists
• Queue at ATMs
• Arrange stacks of books
• Look-up words in dictionary
• Organize directories in computers
• Plan road map to go somewhere
• Examples
Primitive data of primitive data structureNon-primitive
structure are integer, character, float
data structure

Primitive data structure is a kind of data Non-primitive data structure is a type of data
structure that stores the data of only one structure that can store the data of more than
type. one type.
Examples of primitive data structure are Examples of non-primitive data structure are
integer, character, float. Array, Linked list, stack.
Primitive data structure will contain some Non-primitive data structure can consist of a
value, i.e., it cannot be NULL. NULL value.
The size depends on the type of the data In case of non-primitive data structure, size is
structure. not fixed.
It starts with a lowercase character. It starts with an uppercase character.
Primitive data structure can be used to call Non-primitive data structure cannot be used
the methods. to call the methods.
Basic Terminologies
• Two types
• Static data structures – fix size such as array and record.
• Dynamic – varies in size such as link-list, queue, stack.
Atomic and Composite Data
• Atomic data are data that consist of a single piece of information;
• It cannot be divided into other meaningful pieces of data.
• For ex, the integer 4562 may be considered a single integer value.
• Of course, we can decompose it into digits, but the decomposed
digits do not have the same characteristics of the original integer;
• They are four single-digit integers ranging from 0 to 9.
• In some languages atomic data are known as scalar data because of
their numeric properties.
• The opposite of atomic data is composite data.
• Composite data can be broken out into subfields that have meaning.
• Telephone number is an example of a composite data.
• It has three different parts.
• First, is the area code, next is the phone number which is actually two
different data items, a prefix consisting of a three-digit exchange and
the number within the exchange
Abstract Data Type
• Computer programs need to organize data, for example using lists, queues, stacks,
etc.
• The ways of organizing data are represented by Abstract Data Types (ADT).
• ADT is a specification that describes a data set and the operation on that data.
• Each ADT specifies what data is stored and what operations on the data
• ADT does not specify how to store or how to implement the operations, so ADT is
independent of any programming language.
• In contrast, a Data Structure is an implementation of an ADT within a
programming language.
• Most of the algorithms require the use of correct data representation for
accessing efficiency.
DATA ABSTRACTION
DATA ABSTRACTION
The process of separating logical properties of data from
implementation details of data.
Data Type
A data type is a collection of objects and a set of operations that
act on those objects.
⚫ For e.g., data type 'int' consists of
→ objects {0,+1,-1,+2,-2. . . . }
→ operations such as arithmetic operators + - * /
Abstract Data Type(ADT)
It’s a data type that is organized in such a way that
⚫ specification of objects is separated from the representation of the
objects
⚫ specification of operations on the objects is separated from
implementation of operations.

CHAPTER 1 18
Before understanding what ADT is let us consider different in-built data types
that are provided to us.
Data types such as int, float, double, long, etc. are considered to be in-built
data types, basic operations performed on them are addition, subtraction,
division, multiplication, etc.
There might be a situation when we need operations for our user-defined data
type which have to be defined.
These operations can be defined only as and when required.
So, in order to simplify the process of solving problems, we can create data
structures along with their operations, and such data structures that are not
in-built are known as Abstract Data Type (ADT).
Abstract Data type (ADT) is a type (or class) for objects whose behavior is defined
by a set of values and a set of operations.
The definition of ADT only mentions what operations are to be performed but not
how these operations will be implemented.
It does not specify how data will be organized in memory and what algorithms will
be used for implementing the operations.
It is called “abstract” because it gives an implementation-independent view.
The process of providing only the essentials and hiding the details is known as
abstraction.
Think of ADT as a black box which hides the inner structure and design of the
data type.
Opposite of ADT is Concrete Data Type (CDT), where it contains an
implementation of ADT.
Real life example: Book is Abstract (Telephone Book is an implementation)
https://stackoverflow.com/questions/10267084/what-is-adt-abstract-data-type
http://homepages.math.uic.edu/~jan/mcs360s03/index.html
ADT is a useful guideline to implementors and a useful tool to programmers
who wish to use the data type correctly.
There are a number of methods for specifying an ADT.
The method that we use is semiformal and borrows heavily from C notation but
extends that notation where necessary.
An ADT consists of two parts:-
1) Value definition-defines the collection of values for the ADT and consists of
two parts:
⚫ 1) Definition Clause
⚫ 2) Condition Clause- Pre-Condition & Post Condition
⚫ The definition clause is required but the condition clause may not be necessary for
every ADT.
2) Operation Definition-Immediately following the value definition comes the
operator definition.
⚫ Each operator is defined as an abstract function with three parts: a header, the optional
pre-conditions, and the postconditions.
⚫ The postcondition specifies what the operation does. It is used to denote the result of
the operation.
SPECIFICATION VS. IMPLEMENTATION
Specification of operations
⚫ function name
⚫ the types of arguments
⚫ the type of the results
⚫ but no information is given about how to implement
Implementation of operations
⚫ detailed algorithm using which we can code (i.e. functions)
using any programming language(C or C++).
ADTs can be implemented in C++ using a concept called
class.
ADT definition contains 2 main sections:
→ Objects
→ Functions
CHAPTER 1 24
Functions of a data type can be classified into
1) Constructor: These functions create a new instance of the
designated type.For ex,
NaturalNumber Zero() ::= 0
2) Transformers: These functions create an instance of the
designated type, generally by using one or more other
instances.For ex,
NaturalNumber Successor(x) ::= if(x==INT_MAX)
return INT_MAX
else
return x+1
3) Reporters: These functions provide information about an
instance of the type, but they do not change the instance. For ex,
Boolean IsZero(x) ::= if(x is zero)
return TRUE
else
return FALSE
*STRUCTURE 1.1:ABSTRACT DATA TYPE NATURAL_NUMBER (P.17)
STRUCTURE NATURAL_NUMBER IS
OBJECTS: AN ORDERED SUBRANGE OF THE INTEGERS STARTING AT ZERO AND
ENDING
AT THE MAXIMUM INTEGER (INT_MAX) ON THE COMPUTER
FUNCTIONS:

CHAPTER 1
FOR ALL X, Y∈ NAT_NUMBER; TRUE, FALSE ∈ BOOLEAN
AND WHERE +, -, <, AND == ARE THE USUAL INTEGER OPERATIONS.
NAT_NO ZERO ( ) ::= 0
BOOLEAN IS_ZERO(X) ::= IF (X) RETURN FALSE
ELSE RETURN TRUE
NAT_NO ADD(X, Y) ::= IF ((X+Y) <= INT_MAX) RETURN X+Y
ELSE RETURN INT_MAX
BOOLEAN EQUAL(X,Y) ::= IF (X== Y) RETURN TRUE
ELSE RETURN FALSE
NAT_NO SUCCESSOR(X) ::= IF (X == INT_MAX) RETURN X
ELSE RETURN X+1
NAT_NO SUBTRACT(X,Y)::= IF (X<Y) RETURN 0
ELSE RETURN X-Y
::= is defined as 26
END NATURAL_NUMBER

You might also like