Sei sulla pagina 1di 32

Introduction

1

1. Trembley & Sorenson: An Introduction to Data Structures with

Applications, 2/E, TMH -1991.

2. Tanenbaum & Augenstein: Data Structures using C and C++,

2/E,PHI,2000

3. Horowitz and Sahani: Fundamentals of Data Structures, Galgotia
Publications, reprint 2004.

4. T

.H. Cormen, C. E. Leiserson, R. L. Rivest

Introduction to

Algorithms,2/E, PHI.,2001

5. Robert L.Kruse, C.L.Tondo and Brence Leung, Data Structures and

Program Design in C, Pearson Education, 2/E, 2001.

2

Data are represented by data values

Held temporary or recorded permanently on a file.

Often the data value are related to each other

This data value must be in organized form

This organized collection of data is called DATA STRUCTURE.

In a general sense, any data representation is a data structure.
Example: An integer

Data structure:

Conceptual and concrete ways to organize data for efficient storage and
efficient manipulation

3

Programs often deal with collection of items

These collections may be organized in many ways and use many
different program structures to represent them,

from abstract point of view, there will be a few common operations on any
collection

Create

Create a new collection

Add

Add an item to a collection

Delete

Delete an item from a
collection

Find

Find an item matching some

criterion in the collection

Destroy

Destroy the collection

4

The Data Structures deal with the study of

How the data is organized in the memory

How efficiently it can be retrieved and manipulated and

The possible ways in which different data items are logically related

Data Structure = Organized Data + Allowed operations

5

Data structures organize data
more efficient programs.

More powerful computers more complex applications.

More complex applications demand more calculations.

Complex computing tasks are unlike our everyday experience.

6

Computers take on more and more complex tasks
Imagine: index of 8 billion pages ! (Google)

Software implementation and maintenance is difficult

Clean conceptual framework allows for more efficient and more
correct code

7

Requirements for a good software:
Clean Design

Easy maintenance

Reliable

Easy to use

Fast algorithms

Efficient data structures

Efficient algorithms

8

Any organization for a collection of records can be searched,
processed in any order, or modified.

The choice of data structure and algorithm can make the
difference between a program running in a few seconds or many

days.

9

A solution is said to be efficient if it solves the problem within its
resource constraints.

Space

Time

The cost of a solution is the amount of resources that the solution
consumes.

10

A data structure requires a certain amount of:

space for each data item it stores

time to perform a single basic operation

programming effort.

11

A collection of 3,000 texts with avg. of 20 lines each, with avg. 10
words / line

600,000 words

Find all occurrences of the word happy

Suppose it takes 1 sec. to check a word for correct matching

What to do?

12

What to do?

Sol. 1 Sequential matching: 1 sec. x 600,000 words = 166 hours

Sol. 2 Binary searching:

- order the words

- search only half at a time

Ex. Search 25 in

5 8

12

15 15 17 23

25

27

25 ? 15

15 17 23 25 27

25 ? 23

23 25 27

25 ? 25

How many steps?

13

log 2 600000 = 19 sec. vs .166 hours!

Set Stack

Tree

Data structure = representation and operations associated with a

data type

14

What are some of the common data structures

What are some ways to implement them

How to use them to solve practical problems

15

Data Types

Primitive Data

Non-Primitive
Types

Data Types

16

Primitive

The primitive data types are the basic data types provided by a
programming language as a basic building block.

That are directly operated upon by machine level instructions

Built in data type is called as primitive data types

int, real, character, pointer, logical data item (true/false)(boolean)

17

Non-Primitive

The data types that are derived from primary data types are known as non-
Primitive data types.

These data types are used to store group of values.

Arrays

Structure

linked list

Stacks

Queue etc

18

Linear data structure is linear if element is adjacent to each other.

It has exactly two neighbors elements to which it is connected as its
previous and next member.

A linear data structure is one in which, while traversing sequentially, we
can reach only one element directly from another.

Example of Linear Data Structure

Array

Linked List

2

5

8

55

1

Stack

Queue

19

Operations on linear Data Structures

Traversal : Visit every part of the data structure

Search : Traversal through the data structure for a given element

Insertion : Adding new elements to the data structure

Deletion : Removing an element from the data structure.

Sorting : Rearranging the elements in some type of order(e.g Increasing or
Decreasing)

Merging : Combining two similar data structures into one

20

Non-Linear data structure is that if one element can be connected
to more than two adjacent element then it is known as non-linear

data structure.

Example of Linear Data Structure:

Tree

Graph

3

2

5

7

4

21

Two approaches to create Data Structures

Static Data Structures

Dynamic Data Structures

22

The size of the structure is fixed

Static data structures is good for storing a well defined number of
data items

Static data structure is given a fixed area of memory which it can
operate within.

It is not possible to expand this fixed size in the run time. So that,
locations of each element is fixed and known by the program.

23

There are many situations where the number of items to be stored
is not known before hand

Programmer will consider dynamic data structure

Data structure is allowed to grow and shrink as the demand for
storage arises

Size of the area is flexible, not fixed as it was with static data
structures.

It is possible to expand or contract the area as required, by
adding or removing elements from data structure.

24

STATIC

DYNAMIC

Memory is allocated at compile time. Fixed Memory is allocated to the data structure
size

dynamically i.e. as the program executes.

Advantage: The memory allocation is fixed
and so there will be no problem with adding
and removing data items

Disadvantage: Because the memory
allocation is dynamic,

it is possible for the structure to 'overflow'
should it exceed its allowed limit.

It can also 'underflow' should it become
empty

Disadvantage: Can be very inefficient as Advantage: Makes the most efficient use of
the memory for the data structure has beenmemory as the data structure only uses as
set aside regardless of whether it is neededmuch memory as it needs
or not whilst the program is executing

Advantage: Easier to program as there is Disadvantage: Harder to program as the
no need to check on data structure size atsoftware needs to keep track of its size and
any point

data item locations at all times

three categories of "lifetime"

static - start to end of program execution

automatic (stack) - start to end of declaring function's execution

heap (variable declared dynamic at runtime, and also de-allocated
dynamically at runtime.

26

static data

space for global variables

automatic data

run-time stack - activation records

added and removed as program runs

(expands and shrinks in an orderly
LIFO manner)

space for variables allocated at run-time
heap data

(allocation and de-allocation requests
occur in unpredictable order)

27

Space for heap variables is allocated from an area of runtime
memory known as the heap or free store.

Heap variables do not have an explicit name, and are accessed
indirectly via a pointer.

Memory space for heap variables is explicitly allocated at runtime
using malloc( ).

Space occupied by heap variables must be explicitly returned
back to the heap to avoid memory leaks. This is done using the
free( ) function.

28

Static array and Dynamic Linked List

29

Static data structure has a fixed size.

Also, elements of static data structures have fixed locations.

In dynamic structures, elements are added dynamically.

So that, the locations of elements are dynamic and determined at runtime.

30

What is Data structure?

Why we need of Data Structure?

Primitive Data Types

Non-Primitive Data Types

Linear Data Structure

Non-Linear Data Structure

Static and Dynamic implementation of Data Structure

31

32

Potrebbero piacerti anche