Nothing Special   »   [go: up one dir, main page]

Introduction To Data Structure

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

Data Structure and

Algorithm
Chapter Four

Introduction to Data
Structures
Introduction
• The first thing that we need to consider when writing programs is the
problem.
• However, the problems that we asked to solve in real life are often
nebulous or complicated. Thus we need to distill such as system down
to its most fundamental parts and describe these parts in a simple,
precise language.
• This process is called abstraction. Problem
• Abstraction is a process of classifying characteristics
as relevant and irrelevant for the particular purpose
at hand and ignoring the irrelevant ones. Abstraction

Model
Cont.

• Through abstraction, we can generate a model of


the problem, which defines an abstract view of the problem.
• Normally, the model includes the data that are affected
and the operations that are required to access or modify. Problem

• Applying abstraction correctly is the essence of successful


programming. Abstraction

Model
Cont.
• As an example, consider a program that manages the student
records, a program which allows to administer the students in
a class.
• Well, this is too vague a problem. We need to think about, for
example, what student information is needed for the record?
What tasks should be allowed?
• There are many properties we could think of about a student,
such as name, ID, major, email, mailing address, transcripts,
hair color, hobbies, etc.
• Not all these properties are necessary to solve the problem.
Cont.
• To keep it simple, we assume that a student's record includes
the following fields: (1) the student's name and (2) ID.
• The three simplest operations performed by this program
include
• (1) adding a new student to the class,
• (2) searching the class for a student, given some
information of the student, and
• (3) deleting a student who has dropped the class.
These three operations can be furthermore defined as below:
Cont.
• ADD (stu_record): This operation adds the given student record to
the collection of student records.
• SEARCH (stu_record_id): This operation searches the collection
of student records for the student whose ID has been given.
• DELETE (stu_record_id): This operation deletes the student
record with the given ID from the collection.
Cont.
• Now, we have modeled the problem in its most abstract form:
 listing the types of data we are interested in and
 the operations that we would like to perform on the data.
• We have not discussed anything about how these student
records will be stored in memory and how these operations
will be implemented.

 This kind of abstraction defines an abstract data type (ADT).


Abstract Data Types (ADTs)
• An abstract data type (ADT) is a set of objects together with a set
of operations.
 ADTs are like user defined data types which defines operations
on values using functions without specifying what is there inside
the function and how the operation are performed.
• Abstract data types are mathematical abstractions; nowhere in an
ADT’s definition is there any mention of how the set of operations is
implemented.
• Objects such as lists, sets, and graphs, along with their operations,
can be viewed as ADTs, just as integers, reals, and booleans are data
types.
Cont.
• Integers, reals, and booleans have operations associated with them,
and so do ADTs. For the set ADT, we might have such operations as
add, remove, size, and contains.
• Alternatively, we might only want the two operations union and
find, which would define a different ADT on the set.
• The C++ class allows for the implementation of ADTs, with
appropriate hiding of implementation details. Thus, any other part of
the program that needs to perform an operation on the ADT can do
so by calling the appropriate function.
Data Structure
• Data structure is a method of organizing large amount of data more
efficiently so that any operation on that data becomes easy.
• A data structure is also a language construct that the programmer
has defined in order to implement an abstract data type.
• Every data structure is used to organize the large amount of data.

• Every data structure follows a particular principle.

• The operations in a data structure should not violate the basic


principle of that data structure.
Cont...d
• Data structures are used in almost every program or
software
system.
• Specific data structures are essential ingredients of many efficient
algorithms, and make possible the management of huge amounts of
data , such as large databases and internet indexing services.
• Some formal design methods and programming languages
emphasize data structures, rather than algorithms, as the key
organizing factors in software design.
Cont...d
• An example of several common data structures are arrays,
linked lists , queues, stacks, binary trees, and hash tables.
 Different kinds of data structures are suited to
different kinds of applications.
 Some data structures are highly specialized to
specific tasks.
• No single data structure works well for ALL purposes.
Cont...d
• For example
– Printer requires a queue data structure to
hold jobs which are send by the different users.
– B-trees are particularly well-suited for
implementations of databases.
– Compiler implementations usually use hash tables to
look up identifiers.
Cont.

 How do data structures model the world or some part of


the world?

• The value held by a data structure represents some specific


characteristic of the world.
• The characteristic being modeled restricts the possible values held
by a data structure.
• The characteristic being modeled restricts the possible operations
to be performed on the data structure.
Cont.
• An algorithm is a well-defined computational procedure that
takes some value or a set of values as input and produces
some value or a set of values as output.
• Data structures model the static part of the world. They are
unchanging while the world is changing. In order to model the
dynamic part of the world we need to work with algorithms.
• Algorithms are the dynamic part of a program's world model.

• An algorithm transforms data structures from one state to another


state by changing the value held by a data structure.
Cont.
• The quality of a data structure is related to its ability to successfully
model the characteristics of the world.
• Similarly, the quality of an algorithm is related to its ability to
successfully simulate the changes in the world.
• However, independent of any particular world model, the quality of
data structure and algorithms is determined by their ability to work
together well.
• Generally speaking, correct data structures lead to simple and
efficient algorithms and correct algorithms lead to accurate and
efficient data structures.
Importance of Data Structures

• Efficiency: proper choice of data structures make program efficient


in terms of space and time.

• Reusability: one implementation can be used by many client


program.

• Abstraction: Data structure is specified by an ADT which provides


a level of abstraction. The client program doesn’t have to worry
about the implementation details.
Types of Data Structures

• Data structures can be classified into two categories: Linear and non-
linear data structures.
• A data structure that maintains a linear relationship between its
elements, it is called linear data structure. For example, an array
holds the linear relationship between its elements , it is linear data
structure.
• In case of non-linear data structure, they maintain hierarchical
relationship between their elements. Consider a tree structure, you can
not define linear relationship between the elements.
Primitive Data Structure Data Structure

Linear Data Structure


Character None Linear Data Structure

Arrays
Tree
Linked List
Graphs
Stacks

Queue
Linear Data Structures
In linear data structure the elements are stored in sequential order.

The linear data structures are:


– Array: Array is a collection of data of same data type stored in
consecutive memory location and is referred by common
name.
– Linked List: - linked list is a collection of data of same data
type but the data items need not be stored in consecutive
memory locations.
Cont...d
– Stacks: A stack is a last-In-First-Out linear data structure in
which insertion and deletion takes pace at only one end called
the top of the stack.
– Queue: A queue is a first-in-first-out linear data structure in
which insertions takes place one end called the rear and the
deletions takes pace at one end called the Front
Non-Linear Data Structures
• The following are some of the Non-Linear data structure:

– Trees:Trees are used to represent data that has


some
hierarchical relationship among the data elements.
– Graph: Graph is used to represent data that has relationship
between pair of elements not necessarily hierarchical in nature.
Operations on data
structures
• The different operations that can be performed on
the various data structures are:
 Traversing
 Searching
 Inserting
 Deleting
 Sorting
 Merging

You might also like