Chp 2 Data Structure
Chp 2 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
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
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
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.
N-3
comp
Sort(Data,N) 4 2 3 1
1. Start LA[1] LA[4]
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
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
- +
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
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