Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
13 views56 pages

Chapter 2 Data Structure

The document provides an introduction to data structures, detailing various concepts such as entities, data items, and types of data structures. It explains operations on data structures, including traversing, inserting, deleting, searching, and sorting, along with algorithmic notations and control flow structures. Additionally, it covers linear arrays, their representation in memory, and algorithms for processing arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views56 pages

Chapter 2 Data Structure

The document provides an introduction to data structures, detailing various concepts such as entities, data items, and types of data structures. It explains operations on data structures, including traversing, inserting, deleting, searching, and sorting, along with algorithmic notations and control flow structures. Additionally, it covers linear arrays, their representation in memory, and algorithms for processing arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

Introduction to Data Structures :

Attributes / properties Values / Data


Name : Vivan
Age : 16
DOB : 18 - 09 - 2005
Gender : Male
STD : 12th
College : Disha Jr. College, Warora
Roll No. : 201910156
Mob. No. : 9890xxxxxx
Address : Tilak ward, Warora
Entity : Student
Introduction to Data Structures :
• Data : Data are simply values or set of values.
• Data items: A single unit of value is called data item.
Types of data item:
1. Group item :
A data item which can divided into sub items called as group item.
Eg. Student Name : Vivan Aman Sharma
First Name Middle Name Last Name

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

Field : Field is single elementary unit of information that representing an


attribute of entity. [A single elementary unit is called field (column)]
Record : A record is a collection of field values of a given entity.
[Collection of field is called record (row).]
File : Collection of record is called file.
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

A record in a file may contain several field.


Primary key : There are certain fields having unique value.
Such a field is called as primary key.
[A field which has unique value called primary key.]
Eg. Roll No.
Data Structure : The logical or mathematical model of a particular
oraganisation is called data structure.
[It is mathematical and logical model of an organization.]
Here different data items are related with each other logically.
Eg. Array, Record, Linked list, Tree, Stack, and Queue.
Array: It is collection of homogeneous (same type) data elements.
Elements of array are stored respectively in successive memory locations.
The elements of array are referenced respectively by an index numbers.
{ 14, 51, 10, 18, 97, 68, 89 } Integer numbers

{ 4.5, 9.6, 0.5, 44.2, 78.65 } Real numbers

{ ‘a’, ‘D’, ‘7’, ‘+’ } Characters


Elements of array
Array Name
LA 14 51 10 18 97 68 89
0 1 2 3 4 5 6
Index 1 2 3 4 5 6 7
numbers
Types of data structure :
1. Linear Data Structure :
In this data items are stored sequentially one by one in consecutive
memory locations.
Eg. Array, Record, Linked list, Stack, and Queue.
2. Non-linear Data Structure :
In this data items are stored hierarchically means linear order cannot be
maintained.
Eg. Tree.
Data Structure operations:
1. Traversing: LA 14 51 10 18 97 68 89 47
Accessing each element only
once so that it can processed. 1 2 3 4 5 6 7 8
2. Inserting: LA[1] = 14
Adding new element to the LA[1] = LA[1] + 2 LA[2] = 51
structure is called as inserting. LA[7] = 89
3. Deleting: Loc of data 97 ?
Removing element from the =5
structure is called as deleting. Ascending order :
4. Searching: {10,14,18,51,68,89,97}
Finding location of element is 6. Merging
called as searching.. Combining two records in a
5. Sorting : single file is called as merging..
Re-arranging elements in some {4,9,6,3} + {7,5,0,1} = {4,9,6,3,7,5,0,1}
logical order is called as sorting.
Algorithmic notations :
An algorithm is a finite step-by-step list of well defined instructions for solving particular
problem.
Syntax: Eg. 1
Algorithm : Name and purpose of Sum of two numbers.
algorithm and input data and variable Let A and B are two numbers and this algorithm
used will find sum SUM of these two numbers.
Step 1 : Start Step 1 : Start
Step 2 : Step 2 : Read A and B
. Step 3 : Calculate SUM = A + B
. Step 4 : Display SUM
. Step 5 : Stop
Step n : Stop
Eg. 2
Eg. 2
Even OR Odd
To display 1 to 10 numbers
Let NUM be any int number, this
Let i be index/loop variable this
algorithm will check whether the
algorithm will display 1 to 10 numbers.
number is even or odd.
Step 1 : Start Step 1 : Start
Step 2 : Read NUM Step 2 : Initialize i = 1
Step 3 : if(NUM % 2 == 0) Step 3 : Display i
Display “Its Even Number” Step 4 : increment i = i + 1
else Step 5 : if (i <= 10)
Display “Its Odd Number” goto Step 3
Step 4 : Stop Step 6 : Stop
• Algorithm divided into two parts,
• First part tells the name, purpose of algorithm, variable list, and input data.
• Second part is a list of steps.
• If we want to transfer control of program to a particular step then we can use ‘goto’
statement i.e. goto step n.
• The exit or stop shows completion of algorithm.
Control Flow/ Control Logic/ Control Structure:
Control flows are used to control flow of program.
There are three types of control structures :
1. Sequential control flow ( Linear logic)
• Algorithms consists of modules.
• Each module is set of steps.
• In sequential flow the modules are executed one after the
other.
Syntax / form Diagram / Flowchart
Module A Module A
Module B
Module C Module B
.
. Module B
.
Module N .

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.

Syntax / form Diagram / Flowchart

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]

• Here K is index (loop) variable. Is YES


K>S
• Initial value of K is R and Final Value is S. ?
• T is increment. K=K+T NO

Module A

for(int i = 1; i<= 10; i++)


cout<<“Hello Class \n”;
for(int K = R; K<= S; K++) Is
K <= S
cout<<“Hello Class \n”; ?
Algorithm : To find roots of quadratic equation (ax2 + bx + c = 0)
Let a, b and c are coefficient of quadratic equation. Where a 0
±
Roots are given by :
else if(d == 0)
Solution :
{ i) display “Roots are real and equal”
1) Start
ii) calculate x1 = x2 = -b/(2*a)
2) Read a, b, c
iii) display x1 and x2
3) If(a == 0)
}
{ i) display “Linear equation”.
else
ii) calculate x = -c/b
{
iii) display x
i) display “Roots are real and different”
}
ii) calculate x1 = (-b + sqrt(d) ) / (2 * a)
else
iii) calculate x2 = (-b - sqrt(d) ) / (2 * a)
{
iv) display x1 and x2
i) calculate d = b*b – 4 * a * c
}
ii) if (d < 0)
}
display “Roots are imaginary”
4) Stop
Arrays :
• It is one of the linear data structure.
• It is also called as linear array.
• It is a collection of finite n numbers of homogeneous data elements.
• Data elements are of same type.
• The elements of array are referenced respectively by an index numbers.
• Elements of array are stored respectively in successive memory locations.
Eg. • The total number of elements N is called as length
Marks 80 95 75 62 or size of the array.
Index • Length of array can be obtained by using formula
1 2 3 4
numbers Length = UB – LB + 1
LB UB =4–1+1
Lower Bound Upper Bound =3+1 If here LB = 0 then UB = 3
(Smallest Index number) (Highest Index number) Length = 3 – 0 + 1
=4
=3+1
=4
Eg.
Marks 80 95 75 62
1 2 3 4

Elements of array can be denoted by subscript notation


Marks1 , Marks2 , Marks3 , Marks4
OR
Marks[1] , Marks[2] , Marks[3] , Marks[4]

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.

4) Time complexity of linear search is O(n). 4) Time complexity of binary search is


O(log2 n).
5) Example 5) Example
Algorithm : Sorting
Arranging elements in some logical order is called as sorting.
Bubble sort :
Eg. DATA N = 5
PTR = 1 33 22 22 22 22 PTR = 1 22 22 22 22 PTR =1 22 22 22
PTR = 2 22 33 33 33 33 PTR = 2 33 33 33 33 PTR = 2 33 33 11
PTR = 3 44 44 44 44 44 PTR = 3 44 44 44 11 3 11 11 33
PTR = 4 55 55 55 55 11 4 11 11 11 44 4 44 44 44
5 11 11 11 11 55 5 55 55 55 55 5 55 55 55
Iteration / Pass (K = 1) PTR = 4 Iteration / Pass (K = 2) PTR = 3 Iteration / Pass (K = 3) PTR = 2
DATA[PTR] DATA[PTR + 1] Sorted
K = 1, 2 ,3, 4 PTR = 1 22 11 Array
DATA[1] DATA[2]
33 22 K = 1 to 4 2 11 22 Pass
If( DATA[PTR] > DATA[PTR + 1] ) yes K = 1 to N – 1 3 33 33 (K = 4)
Interchange their values PTR = 1 4 44 44
PTR = 1
PTR = PTR + 1 PTR <= N – K 5 55 55
BUBBLE(DATA, N)
Let DATA is A linear array of size N, This algorithm will sort DATA in ascending order.
1) Start DATA N = 5 K = 1
2) Repeat steps 3rd and 4th for K = 1 to N – 1 by 1 PTR = 1 33 22 22 22 22
3) Set PTR = 1 PTR = 2 22 33 33 33 33
4) Repeat steps 5th and 6th while (PTR <= N – K ) PTR = 3 44 44 44 44 44
5) Compare elements PTR = 4 55 55 55 55 11
if( DATA[PTR] > DATA[PTR+1] ) PTR = 5 11 11 11 11 55
Interchange their values
i) Set TEMP = DATA[PTR] TEMP = 33 Is(PTR <= N – K ) ?
ii) Set DATA[PTR] = DATA[PTR+1] DATA[1]= 22 Is (1 3<=
42<=
5 5 4-441)
<=
<= ???YES
YES
YES
NO
iii) Set DATA[PTR+1] = TEMP DATA[2]= 33 Is (1 <= 4)YES
6) Increment counter PTR
if( DATA[PTR] > DATA[PTR+1] )
PTR = PTR + 1
if( DATA[1] 33
55
44 >> 44
11 ?? YES
> DATA[2]
55 )
NO
7) Display Sorted DATA
if(33 > 22) YES
8) STOP
Pointer Array
• In array if each elements are pointers then it is called as pointer array.
• Pointer is a variable which contains the address of another element.
• Pointer points an element in list.
XII Class : Students
Let We have four groups.


{ Each
14, group
51, 10, 18, 97, 68, 89
consists of list of members.
} Integer array
Memory In array
Addresses
1 MB - 1 if all the elements
(Members)are
1 H
C000 2 integer
MB - 2 numbers MB then= 4it is


{ 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

K Roll No First Name Last Name Age Class


1 701 Vikas Mehra 17 12
2 702 Aman Jain 17 12
3 703 XYZ PQR 16 11
.
.
Difference between Array and Record :
Array Record
1) Array is collection of homogeneous 1) Record is collection of field value.
elements.
2) Elements of array are homogenous. 2) Elements of Record are non-
(same data types) homogenous. (different data types)
3) Elements of array described by index 3) Elements of Record described by
numbers. level numbers.
4) Natural ordering (sorting) of elements 4) Natural ordering (sorting) of
are possible. elements are not possible.
5) Example 5) Example
Linked list :
• It’s a linear collection of data elements, called nodes.
• Linear order is given by means of pointer.
• Each node is divided into two parts. First part contain information part of the elements
and second part contains the address of the next node on the list.
Pointer of next node
Information part NULL pointer
(Link Part)

A 8 B 4 C 2 D 6 E 3 F X
Start / Name
(Head)
5 8 4 2 6 3

• The left part represents the information part of node.


• The right part represents pointer field of node.
• The arrow is drawn from this field to next field.
• Pointer field of last node contain NULL pointer.
• The address of first node in list called as start or name.
• We need only this address to trace the linked list.
Difference between Linked list and Array : NULL pointer

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

Similar binary trees


Algebraic expression and binary tree :
• The algebraic expression can be represented by binary tree.
Eg.
E = ( a – b ) / ( ( c * d ) + e ) Variables / numbers : E, a, b, c, d, e
Operators : = , – , *, +

= 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.

• If we want to restrict insertion and deletion operations,


• So that they can take place only at beginning or at the and of list, not in the middle.
• Then data structure stack and queue are useful in such situation.
Stack : POP
• A stack is data structure in which item may be added PUSH
Top / Front
or removed only at one end.
• The item can removed or added only from the top of
the stack. 84 Last Item added = 84
• PUSH is the term used to insert an element into stack. 26 FirstITEM = 39
item to be
• POP is the term used to delete an element from stack. deleted = 84
73
• A stack is also called as LIFO (Last In First Out). 95
39
Queue: Bottom / Rear
• A queue is a linear list in which items may be
added only at one end and items may be removed First Item added = 39
only at other end. First item to be
• A quequ is also called as FIFO (First In First Out). deleted = 39 ITEM
PUSH= 39
POP
39 95 73 26 84
Top / Front 1 2 3 4 5 Bottom / Rear

You might also like