Chapter 2 Data Structure
Chapter 2 Data Structure
2. Elementary item :
A data item which can not divided into sub items called as elementary
item.
Eg. Roll No. : 2019101562
Entity : Entity is something (an object) that has certain attributes or properties.
These attributes may assigned values.
The values can be numeric, non-numeric or alphanumeric.
Entity set : Collection of entities called entity set.
Information : A meaningful data is called information.
Name : Vivan Name : Dexter Name : Hannah
12th
Roll No. 101 Roll No. 102 Roll No. 103
Gender : Male Gender : Male Gender : Female
DOB: 1-1-05 DOB : 2-3-04 DOB : 14-9-05
Module N
2. Conditional control flow ( selection logic)
• In this modules are selected or executed depending on condition.
• There are three types of conditional flow :
i. Single alternative
ii. Double alternative
iii. Multiple alternative
i. Single alternative
It has only one condition and having only one alternate path.
Syntax / form Diagram / Flowchart
if(condition) then :
[Module A]
Condn NO
[End of if statement] ?
YES
Module A
ii. Double alternative
It has two alternative paths., one is true and another is false.
if(condition) then :
[Module A]
Condn NO
else ?
Module B
[Module B]
YES
[End of if statement]
Module A
iii. Multiple alternative
It has multiple conditions and having multiple alternative paths.
Syntax / form Diagram / Flowchart
if(condition 1) then :
Condn YES
[Module A] 1 Module A
else if(condition 2) ?
[Module B] NO
else if(condition 3)
Condn YES
[Module C] 2 Module B
else ?
Module N NO
[End of if statement] Condn YES
3 Module C
?
NO
Module N
3. Repetitive control flow ( iteration logic)
• In this flow certain modules executed repeatedly until condition satisfies.
Syntax : while
Diagram / Flowchart
Initialization of loop variable
Repeat i) & ii) while (condition)
i) [Module A] Set loop variable
ii) Increment
[End of while Loop]
Condn NO
?
Increment YES
Module A
Syntax : for Diagram / Flowchart
Repeat for K = R to S by T
[Module A] K=R
[End of for loop]
Module A
Marks[1] = 80
Marks[2] = 95
Marks[3] = 75
Marks[4] = 62
Representation of linear Arrays in memory :
• Elements of linear array are stored in successive memory cells / blocks.
• The number of cells required depends upon on type of data elements.
Eg. • Computer keep track of address of the first elements
LA 14 78 67 23 36 of array.
• Here LA is linear array and address of first element is
1 2 3 4 5 denoted by Base(LA) and called base address of LA.
Memory Addresses • Using Base address computer calculates the address
C000
1000H 14 of any elements of LA by using formula :
1001 78 LOC( LA[K] ) = Base(LA) + W(K - LB)
1002 67 LOC(LA[3]) = 1000 + 1 (3 – 1 )
1003 23 = 1000 + 2
1004 36 = 1002
1005
1006 Memory locations
W = number of words (bytes) per memory cells
Fig. Memory representation of Array
Traversing Linear Array
• Accessing each elements of array only once so that it can be processed.
Algorithm : Traversing Linear Array
Let LA is a linear array with lower bound LB and upper bound UB.
This algorithm will apply PROCESS to each element of LA.
Steps : 1) Start
2) Initialize counter LA 14 78 67 23
set K = LB
1 2 3 4
3) Repeat steps 4th and 5th while (K <= UB)
4) Visit Element 1 <= 4 ? YES K = LB UB
Apply PROCESS to LA[K] 2 <= 4 ? YES K=1
3 <= 4 ? YES K = 2 LA[1] 14 Display LA[1] OR
5) Increment Counter
4 <= 4 ? YES LA[2]
K=K+1 LA[1] = LA[1] * 2
5 <= 4 ? NO LA[3]
6) Stop LA[4]
Sum of all the elements of Array : N =4
Let LA is a linear array of size N with lower bound LB LA 10 70 60 20
and upper bound UB. 1 2 3 4
This algorithm will find SUM of all the element of LA. LB UB
1) Start
SUM = 0 + LA[1] + LA[2] + LA[3] + LA[4]
2) Initialize SUM = 0 ? YES = 0 + 10 = 10
K = 1 = (1
SUM <= 4)
SUM + LA[1]
3) Initialize Counter
SUM == SUM
SUM SUM ++ LA[2]
LA[K] =0+10
10 +=70 10= 80
Set K = LB
4) Repeat Steps 5th and 6th while (K <= UB) SUM
K = K =+ SUM
1 = 2 + LA[3] = 80 + 60 = 140
5) SUM = SUM + LA[K] SUM
(2 <= =4)SUM
? YES+ LA[4] = 140 + 20 = 160
6) Increment Counter SUM = SUM + LA[K] 10+70 = 80
K=K+1 K=K+1=3
7) Display SUM (3 <= 4) ? YES
K=K+1=5
8) Stop (5 <= 4) ? No SUM = SUM + LA[K] 80+60 = 140
K=K+1=4
(4 <= 4) ? YES
SUM = SUM + LA[K] 140+20 = 160
Average of all the elements of Array :
Let LA is a linear array of size N with lower bound LB and upper bound UB.
This algorithm will find average AVG of all the element of LA.
1) Start
2) Initialize SUM = 0
3) Initialize Counter
K = LB
4) Repeat Steps 5th and 6th While(K <= UB)
5) Calculate SUM = SUM + LA[K]
6) Increment Counter
K=K+1
7) Calculate AVG = SUM / N
8) Display AVG
9) Stop
N =4
Inserting a new element into Array :
INSERT (LA, N, ITEM K) LA 10 70 60
60
30 20
20
60 20
30
20
Let LA is a linear array of size N, This algorithm will 1 2 3 4 5
insert ITEM at the Kth position, Where K is any
positive integer such that K <= N+1
ITEM
ITEM == 30
30 K K= =3 5
1) Start
2) Initialize Counter LA[K]
LA[5]
LA[4 ==LA[4]
+ 1] =ITEM
LA[4]
Set J = N LA[4]
LA[5]
N=N ==LA[4]
+LA[3]
1
3) Repeat steps 4th and 5th While (J >= K) J =NN==54
4) Move Jth element downward LA[J+1] = LA[J]
LA[J+1] = LA[J] J=J–1 =3
5) Decrement counter LA[J+1] = LA[J]
J=J–1 LA[4] = LA[3]
6) Insert ITEM
LA[K] = ITEM
7) Reset N
N=N+1
8) Stop
Deleting a element from Array : N =4
LA 10 70
60 20
60 20
DELETE (LA, N, ITEM, K)
Let LA is a linear array of size N, This algorithm will 1 2 3 4
delete ITEM from the Kth position, Where K is any
K = 42 ITEM
ITEM == LA[K]
20 ITEM = LA[K]
ITEM = 70
positive integer such that K <= N
N=N–1
1) Start LA[2]
N = 3 = LA[3]
2) Set ITEM = LA[K] LA[2] = LA[2 + 1]
3) Initialize counter
J=K=2
Set J = K
LA[J] = LA[J+1]
4) Repeat steps 5th and 6th while (J <= N – 1)
5) Move J + 1th element upwards J=J+1=3
LA[J] = LA[J + 1] LA[J] = LA[J+1]
6) Increment Counter LA[3] = LA[4]
J=J+1 N =N–1
7) Reset N N=3
N=N–1
8) Stop
Deleting a element from Array :
DELETE (LA, N, ITEM K)
Let LA is a linear array of size N, This algorithm will
delete ITEM from the Kth position, Where K is any
positive integer such that K <= N
1) Start
1) Start
2) Set ITEM = LA[K] 2) Set ITEM = LA[K]
3) Initialize counter 3) Repeat step 4th for J = K to N – 1 by 1
Set J = K 4) Move J + 1th element upwards
4) Repeat steps 5th and 6th while (J <= N – 1) LA[J] = LA[J + 1]
5) Move J + 1th element upwards 5) Reset N
LA[J] = LA[J + 1] N=N–1
6) Increment Counter 6) Stop
J=J+1
7) Reset N
N=N–1
8) Stop
Algorithm : Searching N =4
Finding Location of an element in array is called as searching. DATA 35 87 20 62
1. Linear Search LOC = 1 2 3 4
LINEAR(DATA, N, ITEM, LOC) ITEMITEM==55 20 LOC
LOC==??
Let DATA is a linear array of size N, This algorithm will find LOC = 135 ?1 <=
LOC of ITEM in DATA. If search is unsuccessful then it will 20 == No4 ?
set LOC = NULL. 55 20====35
87? ?NONo
LOC 20 ==
= 220 ?2 <=
YES
4?
1) Start
55 ====87
LOC
LOC 1 NO
LOC +1=3
2) Initialize LOC LOC = 3 3 <= 4 ?
Set LOC = 1 If(ITEM
If(ITEM ==
== DATA[LOC])
DATA[LOC])
55 == 20 NO
3) Repeat Steps 4th and 5th while(LOC <= N) If(20
If(20 ==
== DATA[3])
DATA[1])
LOC = 4 4 <= 4 ?
4) Compare ITEM 20 20====20
35??YES
No
55 == 62 NO
if(ITEM == DATA[LOC]) ITEM is found
5 <= at?LOC
4 NO
LOC =
LOC <= 5
= LOC
i) Display : ITEM is found at LOC LOC N +1=2
ii) goto step (Stop)
7th ifIf(ITEM
LOC > N== DATA[LOC])
6) if(LOC > N) If(20Item
== DATA[2])
is not found
5) Increment LOC
Set LOC:=Item
Display NULLnot found 20 == 87 ? No
LOC = LOC + 1
7) Stop
2. Binary Search
In this search technique data (array) should be sorted.
N =8
DATA 10 20 30 40 50 60 70 80 ITEM = 40 LOC = ?
1 2 3 4 5 6 7 8
BEG = LB MID END = UB
1st halve 2nd halve
Calculate MID = INT(BEG + END) / 2
= INT (1 + 8) / 2
= INT 9 / 2
= INT 4.5
=4
Compare ITEM
If(ITEM == DATA [MID])
If(40 == DATA[4])
If(40 == 40)
Display ITEM is found at LOC = MID
2. Binary Search
In this search technique data (array) should be sorted.
N =8
DATA 10 20 30 40 50 60 70 80 ITEM = 20 LOC = ?
1 2 3 4 5 6 7 8
BEG = LB MID END MID END = UB
1st halve 2nd halve
Calculate MID = INT(BEG + END) / 2 Calculate MID = INT(1 + 3) / 2
= INT (1 + 8) / 2 = INT(4) / 2
= INT 9 / 2 = 2
= INT 4.5 Compare ITEM
=4 If(ITEM == DATA [MID])
Compare ITEM If(20 == DATA[2])
If(ITEM == DATA [MID]) Else If(ITEM < DATA [MID]) If(20 == 20)
If(20 == DATA[4]) If(20 < DATA[4]) Display ITEM is found at LOC = MID
If(20 == 40) NO If(20 < 40) YES
END = MID – 1
2. Binary Search
In this search technique data (array) should be sorted.
N =8
DATA 10 20 30 40 50 60 70 80 ITEM = 60 LOC = ?
1 2 3 4 5 6 7 8
BEG = LB MID BEG MID END = UB
1st halve 2nd halve
Calculate MID = INT(BEG + END) / 2 Calculate MID = INT(5 + 8) / 2
= INT (1 + 8) / 2 = INT(13) / 2
= INT 9 / 2 = 6
= INT 4.5 Compare ITEM
=4 If(ITEM == DATA [MID])
Compare ITEM Else If(ITEM < DATA [MID]) NO If(60 == DATA[6])
If(ITEM == DATA [MID]) Else If(ITEM > DATA [MID]) If(60 == 60)
If(60 == DATA[4]) If(60 > 40) YES Display ITEM is found at LOC = MID
If(60 == 40) NO BEG = MID + 1
2. Binary Search
In this search technique data (array) should be sorted.
N =8
DATA 10 20 30 40 50 60 70
70 80 ITEM = 74 LOC = ?
1 2 3 4 5 6 77 8 UB
BEG = LB END END
MID BEG MID BEG
MID BEG
1st halve 2 halve MID
nd
If(74 == 80) NO
Calculate MID = INT(BEG + END) / 2 If(74 == 60) NO If(74 < 80) YES
= INT (1 + 8) / 2
If(74 > 60) YES END = MID - 1 = 7
= INT 9 / 2 BEG = MID + 1 = 7
= INT 4.5 MID = (7 + 8) / 2 Now here BEG > END
=4 =7
Compare ITEM If(74 < 40) NO i.e. ITEM is not found
If(ITEM == DATA [MID]) If(74 > 40) YES If(74 == 70) NO
If(74 == DATA[4]) BEG = MID + 1 = 5 If(74 > 70) YES
MID = (5 + 8) / 2 BEG = MID + 1 = 8
If(74 == 40) NO
=6 MID = (8 + 8) / 2
=8
BINARY(DATA, N, ITEM, LOC)
Let DATA is a sorted linear array of size N, with lower bound LB and upper bound UB.
BEG denotes beginning, END denotes ending and MID denotes middle segment of the array.
This algorithm will find LOC of ITEM DATA and if search is unsuccessful then it will set LOC = NULL.
1) Start
2) Initialize BEG = LB, END = UB DATA 45 62 98
3) Repeat steps 4th and 5th while (BEG <= END) ITEM = 62
LOC = ?
1 2 3
4) Calculate MID = INT(BEG + ENG) / 2
5) Compare ITEM BEG = 1 END = 3 (1<=3) ? YES
If(ITEM == DATA[MID]) MID = 2 is(62 == DATA[2]) YES
i) Display : ITEM is found at LOC = MID
7th
ii) goto step (stop)
Else if( ITEM < DATA[MID])
Set END = MID – 1
Else
Set BEG = MID + 1
6) If(BEG > END) 7) Stop
Display : ITEM is not found, Set LOC = NULL
Difference between Linear search and Binary search :
Linear search Binary search
1) In linear search array (data) may or may 1) In binary search array (data) should
not be sorted. i.e. it is not necessary that be sorted.
data should be sorted.
2) Item to be searched is compared with 2) Item to be searched is compared
every element of array starting from first with middle value of array.
location
3) Its less efficient than binary search. 3) Its more efficient than linear search.
•
{ Membership
4.5, 9.6, list is to be stored in memory
0.5, 44.2, 78.65 PTR1
The most efficient method is to form two } Real array 3 called
C000 H MB -14
4 H MB -78
as 1 array.
3 integer
2
CS = 3
1 C001 4 IT = 2
arrays. 3 Index Numbers
•
{ One
‘a’,is‘D’, ‘7’, ‘+’ } Character
3 array
C002 H
members consisting of all members
5 5 H CS - 67
C002
6 H CS - 23
C003
1
2 4
ELE =2
8
one after the other. PTR2 7 H CS - 36
C004 3
10 5
8
• { And
ptr1,another
ptr2, ptr3 ….} array C003
pointer group
5 H 12 IT - 1
Fig. Memory locations
9 IT - 2
containing the starting locationsPTR3of
different groups Fig. Pointer array 10 ELE - 1
11 ELE - 1
• Observe that the elements of pointer 12 END
{PTR1, PTR2, PTR3…..}
array are starting addresses of each group.
Pointer Array Fig. Member list
Records : It’s the collection of field.
• Record may contain non-homogeneous data. Eg.
i.e. data item in record may have different data
1. Student
types.
2. Roll No.
• The elements in record are described by level
2. Full Name
numbers.
3. First Name
• In record natural ordering of elements are not
3. Middle Name
possible.
3. Last Name
• The number to left of each is called level number. 2. Gender
• Each group item is followed by its subitem. 2. DOB
• The level of subitem is 1 more than the level of 3. Day
group item. 3. Month
• Any particular field in record can be accessed by 3. Year
using period. (dot) Student.DOB.Year 2. Age
• 1. Student(15) it indicated 15 records in a file.
• Any particular field in file can be accessed by : Student.Age[7]
Representation of record in memory :
Eg. • Record contain non homogeneous data.
1. Student • To store record in memory we need parallel linear arrays.
2. Roll No. • For this example of students record, we requires 5 linear
2. Full Name arrays such as Roll No., First Name, Last Name, Age and Class
3. First Name • One array for each elements of data item.
3. Last Name • Roll No.[K], First Name[K], Last Name[K], Age[K], Class[K]
2. Age must be belong to same record.
2. Class
A 8 B 4 C 2 D 6 E 3 F X
Start / Name
(Head)
5 8 4 2 6 3
A 8 B 74 C 2 D 63 E 3 F X
Start / Name
5 8 4 2 6 3
(Head)
G 4
Linked list 7 Array
1) Definition : It’s the collection of data 1) Definition : It’s the collection of
elements called as node. homogeneous elements.
2) Linear order is given by means of pointer. 2) Linear order is given by index number.
3) Insertion and deletion are easy. 3) Insertion and deletion are
expensive.
4) It doesn’t requires consecutive memory 4) It requires consecutive memory
locations. locations.
5) Example 5) Example
Representation of Linked list in memory: NULL pointer
Eg.
A 8 B 4 C 2 D 6 E 3 F X
Start / Name
(Head)
5 8 4 2 6 3
• Linked list can be represented by two parallel INFO LINK
linear arrays.
• One is INFO containing information part. 1
• And other is LINK containing next pointer field. 2 D 6
• The name of linked list/start contains beginnings 3 F X NULL pointer
of the list. 4 C 2
• START points to 5 elements
th Start A 8
5
• INFO[5] = A and LINK[5] = 8 so next node address is 8
6
• INFO[8] = B and LINK[8] = 4 so next node address is 4
7
• INFO[4] = C and LINK[4] = 2 so next node address is 2
• INFO[2] = D and LINK[2] = 6 so next node address is 6
8 B 4
• INFO[6] = E and LINK[6] = 3 so next node address is 3 9
• INFO[3] = F and LINK[3] = NULL, The list is ended here. Fig. Representation of Linked list in memory:
Tree :
• It’s a non linear hierarchical data structures, which consist of finite set of nodes.
• Its used to represent data containing hierarchical relationship between elements.
Eg. R
Root Root / College
R
A B C A B C
Arts
Science Commerce
Binary Tree :
• A Binary tree T is defined as a finite set of elements, called nodes, such that :
a) T is empty. (null tree or empty tree) OR
b) T contains a distinguished node R, called root of T and remaining nodes of T form
an ordered pair of disjoint binary trees T1 and T2.
Eg. R Root • Here T contains root R and T1 and T2 are subtrees of R.
T • If T1 is non empty, then its root is called as left successor of R.
• And if T2 is non empty, then its root is called as right successor of R.
T1 T2
Eg. R Root • Any node in binary tree has 0, 1 or 2 successor.
Left Right • The node with no successor(child) are called as
T
B C terminal node. It is also called as leaf.
• Root : The node which has no parent.
D E G H • Sibling : Children of the same parent.
F J K
• The two binary tree said be similar is they have
L same structure.
T : Binary Tree A P
B C Q R
D E S T
F G H U V W
= Root [(a+b)*c]/[a*((b–c)+a)]
E /
/
– + * *
+ c a +
a b * e
a b – a
c d
b c
Eg.
a+b a + 2b a + b2
a+2*b a+b*b X
+
+ a+b^2
a b
a * +
2 b a ^
(a + b)2 b 2
a+b*c
(a + b) ^ 2
a+b+c
+
^ (a+b)+c
a * +
+ 2
b c + c
a b
a b
Eg.
C = ( X + Y)3 – ( 5A + B2 ) C = ( X + Y)3 – ( 5A + B2 )
C = (( X + Y) ^ 3 ) – ( 5 * A + B ^ 2 )
C –
^ +
+ 3 * ^
X Y 5 A B 2
Eg.
1) E = (p – q ) / (r * s) + t 13) Y = [ (a – b – c ) + (a + b – c)]3/2 [M - 2020]
2) (2x + y) (a – 7b)3 Y = [ ((a – b) – c ) + ((a + b)– c)] ^ (3/2)
3) Z = (A + B / E) * (C + D / F)
=
4) A = (P + 2Q)2 – (R/3)
5) (2A + B)(5F – D)3 Y ^
6) B = (3R / 5T) – (R – Q )
3
+ /
7) (a2 + 2ab + 5c) / (a + b) – – 3 2
8) E = ((a – b) + (b – c)) / e
– c + c
9) E = ((a + b) – c ) * (e/g)
10) F = (a – b) / ((p * m) + h) a b a b
11) (p *(q – t)) / ((q + r) * s)
12) B = (3R / 5T)2 – (R + Q3) 14) Y = [ (a – b – c ) + (a + b – c)3/2 ]
14) Y = [ (a – b – c ) + (a + b – c)3/2 ]
Y = [ ((a – b) – c ) + ((a + b)– c) ^ (3/2) ]
Y +
– ^
– c – /
a b + c 3 2
a b
Binary Tree : Parent and Child
• Suppose N is node in T with left successor S1 and right successor S2.
• Then N is called as parent(father) of S1 and S2.
• The line drawn from node N to a successor is called as edge.
T N Parent • Sequence of consecutive edges is called as path.
N -> S2 -> D
left right • Terminal node is leaf and path ending in a leaf is called as
child S1 S2 child branch.
• The depth (height) of a tree T is the maximum number of
A B C D nodes in a branch of T.
• Depth = Highest Level + 1
H E F
Complete / Symmetric Binary tree :
• The tree T is said to be complete / symmetric if all its levels except the last, have
maximum number of possible nodes.
• Depth(n) = Highest Level + 1
T Level 0 n=2+1
R
n=3
Level 1 A B • If n = 1, then Total nodes = 1
• If n = 2, then Total nodes = 3
• If n = 3, then Total nodes = 7
Level 2 C D C D • If n = 4, then Total nodes = 15
T : Complete / Symmetric binary tree • Maximum Nodes = 2n – 1
• If n = 5, then Total nodes = ?
= 2n – 1
= 25 – 1
= 32 – 1
= 31
• If n = 6, then Total nodes = 63
• If n = 7, then Total nodes = 127
2 tree / Extended Binary tree :
• The tree T is said to be a 2 tree or extended binary tree if each node has
either 0 or 2 children.
T Level 0 R
• The node with 2 children are called as internal nodes.
Level 1 A • The node with 0 children are called as external nodes.
B
Level 2 C D C D
T : Complete / Symmetric binary tree
Level 3 E F
T : 2 tree / Extended Binary tree
Representation of binary tree in memory :
• It can be represented in memory either by linked representation or by using array called
sequential representation.
• The requirements in any representation is that one should have direct access to root R given
any node of N of T. Root
R 5 R 2 X = Null Pointer
1) Linked representation :
Eg. Let T be a binary tree. 3
A B 4 A 6 X B X
5 2
C E X C X X E X
4 6
• The Linked representation uses three parallel arrays. INFO LEFT RIGHT
• INFO, LEFT, RIGHT and pointer variable ROOT. 1
• Each node N of T will corresponds to a location K 2 B X X
such that : ROOT 3 R 5 2
1. INFO[K] contains the data at node N. 4 C X X
2. LEFT[K] contains the location of left child of node N. 5 A 4 6
3. RIGHT [K] contains the location of right child of node N. 6 E X X
4. ROOT will contain location of root R of T. 7
2) Sequential representation : TREE
1 R Root
Eg. Let T be a binary tree.
2 A
Root B
R 3
4 X X = Null Pointer
A B
5 X
6 C
7 E
C E 8 X
9 X
• In sequential representation a linear array is used.
• The Root R is stored in TREE[1].
• If a node N occupies TREE[K], then B TREE[3] K = 3
• Its left child is stored in TREE[2 * K] and R Left
TREE[1]
child C :K TREE[2
=1 * K]
• Its Right child is stored in TREE[2 * K + 1] Left child A : TREE[2
TREE[2* *K]3] = 6
Left child E TREE[2
: TREE[2* 1]
* K += 1]2
Left child B : TREE[2
TREE[2* *K 3+ +1]1] = 7
TREE[2 * 1 + 1] = 3
Stack and Queue :
Array 85 72 43 64 15
1 2 3 4 5
ITEM = 39
K = 1, 2, 3, 4, 5, 6
• ITEM can be inserted at first position, last position as well as at middle position.
K = 1, 2, 3, 4, 5
ITEM = 72
• ITEM can be deleted from first position, last position as well as middle position.