19ELC211 DSA Introduction ADT: Dr. O. V. Ramana Murthy
19ELC211 DSA Introduction ADT: Dr. O. V. Ramana Murthy
Introduction ADT
2
Disclaimer
This material is developed for the course 19ELC211 Data
Structures and Algorithms, Jan – May 2021, Amrita Vishwa
Vidyapeetham. Students registered for the above course can use
this material for the above purpose only. Any other usage
beyond is not authorized by the instructors nor the Institute.
3
Contents
Terminology
Motivation
ADT
Examples
4
Terminology
An entity is something that has certain attributes or properties
which may be assigned values.
Entity : Employee
Attributes : Name Age Gender
City
Values : Rupali 34 F
Mumbai
A field is a single elementary unit of information representing
an attribute of an entity,
a record is the collection of field values of a given entity and
a file is the collection of records of the entities
5
Terminology
Each record in a file may contain many field items, but the value
in a certain field may uniquely determine the record in the file.
Such a field K is called a primary key, and the values k1, k2, … in
such a field are called keys or key values.
Suppose an automobile dealership maintains an inventory file
where each record contains the following data:
Serial Number, Type, Year, Price, Accessories
Suppose an organization maintains a membership file where
each record contains the following data:
Name, Address, Telephone Number, Dues Owed
HW
6
Motivation
The organization of data into fields, records and files may not be
simple enough to maintain and efficiently process certain
collections of data. For this reason, data are also organized into
more complex types of structures. The study of such data
structures includes the following three steps:
(1) Logical or mathematical description of the structure
(2) Implementation of the structure on a computer
(3) Quantitative analysis of the structure, which includes
determining the amount of memory needed to store the
structure and the time required to process the structure.
7
DS vs DBM
The second and third of the steps in the study of data structures
depend on whether the data are stored (a) in the main
(primary) memory of the computer or (b) in a secondary
(external) storage unit.
DS will mainly cover the first case. This means that, given the
address of a memory location, the time required to access the
content of the memory cell does not depend on the particular
cell or upon the previous cell accessed.
The second case is called file management or data base
management (DBM)
8
Data Structure
System defined data types (primitive data types)
For example, “int” . If it takes 2 bytes (16 bits), then the total possible
values are minus 32,768 to plus 32,767 (-215 to 215-1). If it takes 4
bytes (32 bits), then the possible values are between -2,147,483,648
and +2,147,483,647 (-231 to 231-1).
User defined data types. Gives more flexibility and comfort in
dealing with computer memory. E.g. structures in C, classes in Java.
struct newType{
int data1;
float data2;
char data3;
};
9
Data Structures –Array
If we choose the name A for the array, then the elements of A
are denoted by subscript notation
a1, a2, a3, …, an,
or by the parenthesis notation
A(1), A(2), A(3), …, A(N)
or by the bracket notation
A[1], A[2], A[3], …, A[N]
Regardless of the notation, the number K in A[K] is called a
subscript and A[K] is called a subscripted variable.
10
A data structure is a special format for organizing and storing
data. General data structure types include arrays, files, linked
lists, stacks, queues, trees, graphs and so on.
Classified into two types:
1) Linear data structures: Elements are accessed in a sequential
order, but it is not compulsory to store all elements sequentially.
Examples: Linked Lists, Stacks and Queues.
2) Non – linear data structures: Elements of this data structure are
stored/accessed in a non-linear order. Examples: Trees and graphs.
11
Data Structure
12
13
Example
The daily flights of an airline company appear below. CITY lists
the cities, and ORIG[K] and DEST[K] denote the cities of
origin and destination, respectively, of the flight NUMBER[K].
S. No NUMBER ORIG DEST
1 701 2 3
S. No CITY 2 702 3 2
1 Agra 3 705 5 3
2 Bangalore 4 708 3 4
3 Chennai 5 711 2 5
4 Mumbai 6 712 5 2
7 713 5 1
5 Patna
8 715 1 4
9 717 5 4
14 10 718 4 5
FLIGHT 1 2 3 4 5
1 0 0 0 715 0
2 0 0 701 0 711
3 0 702 0 708 0
4 0 0 0 0 718
5 713 712 705 717 0
Flight no ORIG DEST
NUMBER ORIG DEST 701 2 3
701 2 3 702 3 2
702 3 2 703 0 0
S. No CITY
705 5 3 704 0 0
1 Agra
708 3 4 705 5 3
2 Bangalore
711 2 5 706 0 0
3 Chennai 712 5 2 … … …
4 Mumbai 713 5 1 715 1 4
5 Patna 715 1 4 716 0 0
717 5 4 717 5 4
15 718 4 5 718 4 5
Operations on DS
The following four operations play a major role in this subject:
1. Traversing: Accessing (“visiting”) each record exactly once
so that certain items in the record may be processed.
2. Searching: Finding the location of the record with a given
key value or finding the locations of all records which
satisfy one or more conditions.
3. Inserting: Adding a new record to the structure.
4. Deleting: Removing a record from the structure.
16
Additional Operations on DS
Miscellaneous operations:
1. Sorting: Arranging the records in some logical order (e.g.,
alphabetically according to some NAME key, or in
numerical order according to some NUMBER key, such as
AADAHAR or account number)
2. Merging: Combining the records in two different sorted
files into a single sorted file
3. Copying
4. Concatenation
17
Abstract Data Types (ADTs)
18
Inheritance Lab 7, Task 5
from abc import ABC, abstractmethod
class Fruit(ABC):
@abstractmethod
def taste(self):
pass
@abstractmethod
def rich_in(self):
pass
class Mango(Fruit):
def taste(self):
return "sweet"
def rich_in(self):
return "Vitamin A"
class Orange(Fruit):
def taste(self):
return "Sour"
def rich_in(self):
return "Vitamin C"
19
20