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

Chp 2 Data Structure

The document provides an overview of data structures, defining data items, their types, and various structures such as arrays, linked lists, and trees. It discusses operations on data structures including traversing, searching, inserting, deleting, and sorting, as well as algorithmic notation and control structures. Additionally, it explains the representation of data in memory and the advantages of different data structures.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Chp 2 Data Structure

The document provides an overview of data structures, defining data items, their types, and various structures such as arrays, linked lists, and trees. It discusses operations on data structures including traversing, searching, inserting, deleting, and sorting, as well as algorithmic notation and control structures. Additionally, it explains the representation of data in memory and the advantages of different data structures.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

Data Structure

Introduction
• Data are simply values or set of values.
• Data item refers to a single unit values.
• There are 2 type of data Data items that are sub divided into sub
items.
1) Group Item. Ex: name = firstname , middlename, last
name.
2) Elementary Item.
Data items that cannot be sub divided.
Ex: pincode , gender.
Emp_no name

Employee1 Gender

ORGANIZATION

Employee2

Employee3
Terms such as:
1. Entity
2. Entity set
3. Attribute
Field’s

Name Address Salary


Abc xyz Mira road 10000
Record
Nita mani Andheri 15000
Data structure
• Data can be organized in different ways.
• The logical or mathematical model of a particular
organization is called a Data Structure.
• Some data structure are
1) Array
2) Linked list
3) Queue
4) Trees
5) Graphs
6) stack
Data structure Operations.

1) Traversing: accessing each record(element) only


once so that it can be processed.
2)Searching: finding the location of record with
given key value or condition.
3) Inserting: adding new record to structure.
4)Deleting: removing records from structure.
5) Sorting: arranging records.
6) Merging: combining the records from different
file.
Algorithmic Notation
• An algorithm, is a finite step-by-step list of
well defined instruction for solving a particular
problem.
Algorithm 1: Purpose of algorithm and input data and variable used.

Step 1: ……………………..
Step 2: …………………….
:
:
:
Step n:
Control structures
• There are three types of flow of controls(or logic)
1) Sequential flow
2) Conditional flow
3) Repetitive flow
1) Sequential flow: it consist of modules. Each module
is a set of steps.
Module A
Module B
Module C….
Conditional flow: one or other module is selected
depending on condition.

1) Single alternative: NO

If condition, then Yes


[Module A] Module A
End if

2)Double Alternative
If condition then NO
[Module A]
YES
ElSE:
Module A MODULE
Module BB
[Module B]
End if
3) Repetitive flow
Repeat for k=R to S by T K=R

[Module ]
Is YES
End of Loop K>s
NO

Module A

K=K+T
Q)Algorithm to find roots of quadratic equation ax2+bx+c=0
where a=0 and roots are
-b + √ b 2 - 4ac
X=
2a

Steps are: Else if D=0 then


Step 1: Read a, b and c (a) set X=-b/2a
(b)write:‘unique solution’
Step 2:set D=b2- 4ac
,x
Step 3: if D>0 then Else:
(a) set x1=(-b + √D )/2a Write: ‘no real roots’
set x2=(-b – √D )/2a [End of If structure]
(b) write : x1, x2 Step 4: Exit
Arrays
• A linear array is a list of finite number ‘n’ of
homogeneous (same data-type)data elements.
• The elements of an array are stored in successive
memory location.
• The number ‘n’ of elements is called length or
size of an array.
Length=UB-LB+1
Ex: length=4-0+1 A[2] = 1 A[0] = 1
A[3] = 2 A[1] = 2
=5 A[4] = 3 A[2] = 3
length=6-2+1 A[5] = 7 A[3] = 7
A[6] = 3 A[4] = 3
=5
Representation of linear array in memory
• The elements of linear are stored in successive
memory cells.
• The number of memory cells required depends on
type of data elements.
• IF LA is the array then starting address of array LA is
Base(LA)
• Using formula , address of any elements can be
found.
LOC(LA[k]) = Base(LA) +W(K-LB)
Where W =number of bytes(words) per cell
LOC(LA[k]) = Base(LA) +W(K-LB)
Memory
address
LOC(LA[1])=base(LA) + 2(1-0) 1000
LA[0]=1
=1000 +2(1) 1001
=1002 1002
LA[1]=7
1003
Where k=1; // to find 1st element 1004

W=2 //integer takes 2


bytes
Traversing linear arrays 1 2 3 5 7

LA[0] LA[4]
• Traversing array means accessing each elements of an array
only once.
• Algorithm:
let LB be lower bound and UB upper bound.
1) Initialize counter
set K=LB 1) K=0 //LB=0
2) Repeat step 3 and 4 while k<=UB 2) While K<=4 //UB=4
3) Visit element 3) cin>>LA[K] OR
Apply Process to LA[K] cout<<LA[K]
4) Increase counter 4) K=K+1
Set K=K+1 end of while loop
5)Exit
[End of step 2 Loop]
5)EXIT
Inserting an element in an array
• Inserting refers to the operation of adding
another elements to the exiting elements.

• Elements can be added


1)End of array
2)Middle of an array

Inserting can be done provided memory space is


available.
But inserting in middle requires to create new space
and moving down other.
NAME NAME K=3
1 Anil 1 Anil J=4 while 4>=3
2 Atul 2 Atul LA[5]=LA[4]
3 Rajesh 3 Milind J=3 while 3>=3
4 Sanjay 4 Rajesh LA[4]=LA[3]
5 5 Sanjay J=2 loop stops.
Addition of name in LA[3]=milind
Original array
middle of array
INSERT(LA,N,K,ITEM)
1)Set J=N //initialize counter j=N
2)While J>=k //repeat step 3 and 4
3)Set LA[J+1]=LA[J] //Move jth element downward
4)Set J=J-1 //Decrease the counter
5)Set LA[K]=ITEM //insert the element
6) N=N+1 //Reset N
7)EXIT
NAME NAME
K=2
Anil Anil J=2 while 2<5-1
Atul Milind LA[2]=LA[3]
Milind Rajesh J=3 while 3<5-1
Rajesh Sanjay LA[3]=LA[4]
Sanjay J=4 while 4<5-1
Original array removing of name in LA[4]=LA[5]
middle of array J=5 while 5>5-1
DELETE(LA,N,K,ITEM) loop stops.
1)Set ITEM=LA[k]
2)For J=K to N-1 //repeat
3) set LA[J]=LA[J+1] //move J+1st element upward
[End of Loop]
4) N=N-1 //Reset N
5)EXIT
Searching Linear search
• Searching refers to the operation of finding
the LOCATION of given element in the list.
• The search is said to be successful if the ITEM
is found.
1 2 3 5 7
Algorithm: Linear search DATA[5]
DATA[1]
LINEAR(DATA,N,ITEM,LOC)
1)Set DATA[N+1]=ITEM //insert ITEM at end
2)Set LOC=1 //Initialize counter
3)Repeat While DATA[LOC]=ITEM //searching for ITEM
set LOC=LOC+1;
[END of Loop]
4)IF LOC=N+1,then
set LOC=null //set LOC =-1 if unsuccessful
else “Item found at” LOC
5)EXIT
Binary Search
• Suppose a list in arranged in ascending order ,
and we have to search for a element.
• We can use more efficient algorithm called
binary search.
• Suppose DATA is an array in increasing order
then
DATA[Beg],Data[Beg+1],…….,DATA[End]
Here ITEM will be Compared with Middle
element. i.e DATA[Mid].
Let MID=INT((BEG + END)/2)
If DATA[MID]=ITEM then search is successful & we set
LOC=MID.
Otherwise there are 2 possibilities
1) ITEM<DATA[MID] then ITEM is on LEFT Half
i.e DATA[BEG],……DATA[MID-1]
so Reset END=MID-1
1) ITEM>DATA[MID] then ITEM is on RIGHT Half
i.e DATA[MID+1],……..,DATA[END]
so Reset BEG=MID+1
If ITEM is not in DATA, then we obtain END<BEG

DATA[BEG] DATA[MID] DATA[END]


BINARY(DATA,LB,UB,ITEM,LOC)
1. Set BEG=LB, END=UB &
MID=INT((BEG+END)/2)
2.While ( BEG<=END and DATA[MID] = ITEM)
{
3. IF ITEM<DATA[MID] then
set END=MID-1 //repeat 3 & 4
ELSE:
set BEG=MID+1
[End of IF]
4. Set MID=INT((BEG+END)/2)
}[End of Step 2 loop]
5. IF DATA[MID]=ITEM then
set LOC=MID
ELSE:
set LOC=NULL //Unsuccessful
[End of IF]
6.EXIT

Here number of comparison required here is


approx to LOG2n which is less than linear
search
Sorting
• Sorting means rearranging elements of an
array in increasing order or decreasing order.
• Most common and famous sorting algorithm
is Bubble sort algorithm.
• Comparing is done with next element.
• i.e a[1]>a[2]
then a[2],a[1]
N-2
comp

N-3
comp
Sort(Data,N) 4 2 3 1
1. Start LA[1] LA[4]

2. Repeat step 3 and 4 For K=1 to N-1


3. Set Ptr=1
4. Repeat while Ptr<=N-K
a) If Data[Ptr]>Data[Ptr+1] then:
interchange Data[Ptr] and Data[Ptr+1]
[end of If structure]
(b) Set Ptr=Ptr+1
[End of inner loop]
[End of outer loop]
5. Exit
Pointer Array
• A variable is called pointer if it points to an
element in list.
• The Pointer Variable contains the address of an
element in the list.
• An Array is called Pointer array if each element of
Member
array is pointer. 1
GROUP 2
3
1 4
5
5 .
14 14
15
16 16
22
22
END
Records 1.New2.Born
Name
2.Gender
• Records is a collection of fields.
2.BirthDay
3.Month
• A Record may contain Non Homogeneous data.
3.Day
3.Year
• The Elements in records can2.be described
by
Father
level numbers. 3.Name
3.Age
• Ex: 2.Mother
3.Name
3.Age

NewBorn.Father.Age

NewBorn[6]
NewBorn.Gender[6]
Representation of records in memory
• To store record in memory we may use Linear
arrays.
• Like: Name, Gender, month, day, year, father
name, father age, mother name, mother age.
• One array for each elements of data item.
• All the array should be parallel that is for
subscript(index) K the elements.
• Eg: Name[K], Gender[K],…. Must belong to
same record.
Name Gender Month Mother age
1
2

Parallel arrays for record


Linked Lists
• A linked list is a collection of data elements,
called nodes, where the linear order is given
by means of pointer.
• Each node is divided into 2 parts
1)Data
2)Link //address of next node in list.
Advantage
• Inserting and deletion is easy.
• Linked list do not require consecutive memory
location.
Representation of linked list in memory
INFOMATION LINK
1
2 B 9
3
5 4
Start
5 A 2
6
7
8
9 C 10
10
Stack
TREES
• Tree is a non linear data structure.
• It is used to represent data containing
hierarchical relationship between elements.
• Most common trees used in computer are
Binary tree.
Binary Trees
T
• A Binary Tree T is defined as a finite
set of elements called nodes.
Such that,
1) T is empty(null tree)
2) T contains a distinguish node R, R

called Root of T and the remaining


nodes of T form an ordered pair of T1 T2
disjoint binary trees T1 & T2
R

T1 T2

T1 T1 T2 T2
1 2 1 2
Root
NODE
A
Right
Left Successor
Successor
B C

EDGE
D E G H

F J K

Terms are:
1. Edge
2. Path L
TERMINAL
3. Branch
NODE/LEAF

The maximum number of nodes of a symmetric binary tree is : 2𝑛 − 1


E=(a-b)/((c*d)+e)
/

- +

a b * e

c d
Representing Binary Trees in memory
• Let T be binary tree.
ROOTS
• T can be represented
A
in memory either by
link or X B X X C X
array(sequential).
• The linked used 3
INFO LEFT RIGHT
parallel arrays,
1
INFO,LEFT, & RIGHT ROOTS
2 A 4 5
2
and a pointer 3
variable ROOT. 4 B 0 0
5 c 0 0
Sequential array:
1 A
Here a linear array is used.
2 B
The root R is stored in TREE[1]. C
3
If a node n occupies TREE[k], 4 0
Then left child TREE[2*k] 5 0

& right child in TREE[2*k+1] 6 D


7 E
Ex:
K=1 tree[1]=A A
Then left tree[2*1]=tree[2]=B
right tree[2*1+1]=tree[3]=C B C

K=2 tree[2]=B
Then left tree[2*2]=tree[4]=0 D E
right tree[2*2+1]=tree[5]=0
K=3 tree3]=C
Then left tree[2*3]=tree[6]=D
right tree[2*3+1]=tree[7]=E
1) Pincode number is ______ item
A)Group B) Elementary c) subitem
2)A record is collection of ______
a) Fields b) files c) array
3) The element is array are ______ type.
a) Homogenous b) non homogenous
4) Pointer variable contains _____
a) Character b) number c) address.
Thank u

You might also like