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

Chap2 - Data Structure

1. Data can be organized into structures like records, files, arrays to store and manage it efficiently. 2. Linear arrays have elements arranged sequentially while non-linear structures like trees and graphs don't follow a sequential order. 3. Common operations on data structures include traversing, inserting, deleting, searching, and sorting elements. Algorithms use steps, loops, and conditions to perform these operations.

Uploaded by

Aman Prajapati
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Chap2 - Data Structure

1. Data can be organized into structures like records, files, arrays to store and manage it efficiently. 2. Linear arrays have elements arranged sequentially while non-linear structures like trees and graphs don't follow a sequential order. 3. Common operations on data structures include traversing, inserting, deleting, searching, and sorting elements. Algorithms use steps, loops, and conditions to perform these operations.

Uploaded by

Aman Prajapati
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 70

Data Structure

BY:- PRAJAKTA DEEPAK SHETYE


Terms
2
Data
- simply value or set of values.
Group Items
- data which can be divided into subitems.
Elementary Items
- data which cannot be divided into subitems.
Entity
- has certain attributes or properties.
Field
- single unit which represent attribute of an entity.
Record
- collection of field values.
File
- collection of records.
3
Attribute

NAME GENDER D.O.B. PINCODE

RAVI M 19-01-1998 256310

NITU F 04-06-1998 123560

Record

Field
Data Structure 4

Data Structures are the programmatic way of storing data so that


data can be used efficiently.
A data structure is a data organization, management, and
storage format that enables efficient access and modification.
Types:-
1. Linear data structure
2. Non Linear data structure
Linear Data Structure 5

A Linear data structure have data elements arranged in


sequential manner and each member element is connected to
its previous and next element.

Linear data structures are easy to implement because


computer memory is arranged in a linear way.

Its examples are array, stack, queue, linked list, etc.


Non Linear data Structure 6

Data structures where data elements are not arranged


sequentially or linearly are called non-linear data structures.
Non-linear data structures are not easy to implement.
Its examples are trees and graphs.
Data Structure Operations 7

Traversing
Inserting

Deleting
Searching
Sorting
Merging
Algorithm Notation 8

Algorithm consist of two part:-

First part :- a paragraph which tells the


purpose of algorithm.

Second part :- consist of steps in algorithm


that are executed one after the
other.
Largest[DATA , N, MAX]
9
Here, DATA is a linear array with N elements. This algorithm finds largest
element
MAX of DATA.
Steps 1: [Initialize counter]
set k=1 and MAX=DATA[1]
Step 2: [Compare and Update]
If MAX<DATA[k+1],
then: MAX=DATA[k+1]
End If structure
Step 3: [Increment counter]
set k=k+1
Step 4: [Test counter]
If k<N,
then go to step 2
End If
Step 5: Write MAX
Step 6: Exit
Flow Control 10

Sequence Logic
- modules are executed sequentially, one after another.

Module 1

Module 2

Module 3
Flow Control 11

 Selection Logic
- uses number of conditions to execute the modules.
1. Single alternative
2. Double alternative

If no
no condi
If
tion
condition

yes
yes
Module 1 Module 2
Module 1
Flow Control 12

 Iteration Logic
- certain module is executed repeatedly until
condition is satisfied.

If no
condition

yes

Module
Linear Array 13

It consists of finite, ordered set of homogeneous data elements.


The simplest type of data structure is a linear array, also called
one-dimensional array.
Following are the important terms to understand the concept
of Array.
Element − Each item stored in an array is called an element.
Index − Each location of an element in an array has a
numerical index, which is used to identify the element.
DATA = linear array 14
DATA N= no. of elements in linear array
LB
1 10

2 20

Array index 3 30
Elements
4 40

5 50

6 60

UB

UB= the largest index called Upper Bound.


LB=the smallest index called Lower Bound.
Traversing Array 15
Traversing an array means accessing with each element of array only at once,
so that it can be processed.
Algorithm
Here LA is a linear array with lower bound LB and upper bound UB. Following
algorithm apply operation PROCESS to each element of LA.

Step 1: [initialize counter]


set k=LB
Step 2: Repeat steps 3 and 4 while k<= UB
Step 3: [visit element]
Apply PROCESS to LA[k]
Step 4: [increment counter]
set k= k+1
[end of step 2 Loop]
Step 5: Exit
LB
16
LA
Set k=1 While 1<=6 visit LA[1] 1+1
1 10
While 2<=6 visit LA[2] 2+1
2 20
While 3<=6 visit LA[3] 3+1
3 30 While 4<=6 visit LA[4] 4+1
4 40 While 5<=6 visit LA[5] 5+1

5 50 While 6<=6 visit LA[6] 6+1

6
While 7<=6
60
false
UB
Inserting 17

Inserting refers to the operation of adding an element to the


existing elements of array.
The element can be easily inserted at the end of array. But for
insertion in middle of array, it requires to move the elements of
array one byte forward.
Algorithm
It inserts a data element ITEM into the kth position in an array
LA with N elements.
INSERT ( LA, N, K, ITEM )
Step 1: [initialize counter]
18
set j=N
Step 2: Repeat steps 3 and 4 while j>= k:
Step 3: [Move jth element forward]
set LA[j+1] = LA[j]
Step 4: [decrement counter]
set j=j-1
[end of step 2 Loop]
Step 5: [insert the element]
set LA[k] = ITEM
Step 6: [reset N]
set N = N+1
Step 7: Exit
N = 6 (no. of elements in LA) ITEM = 45(to be inserted in LA)
LA K = 3 (3th position of LA)
1 10 19
2 20 Set j=6 While 6 >=3 true LA[6+1]=LA[6]
Set 6-1 =5
3 30
set LA[j+1] = LA[j]
While 5 >=3 true LA[5+1]=LA[5]
4 40
Set 5-1=4
5 50
While 4>=3 true LA[4+1]=LA[4]
6 60
Set 4-1=3
While 3>=3 TRUE LA[3+1]=LA[3]
1 10 10 10 SET3-1=2
2 20 20 20 WHILE2>=3 FALSE

3 30 30 LA[3] =45
45
6+1 =7
4 40 40 30
N=7
5 50 40
6 50 50
7 60 60 60
Deleting 20

Deleting means removing an element from the existing


elements of an array.
Deletion at the end of array is easier. But if to delete an
element from middle of array, then to move the elements of
array one location upward.
Algorithm
Here LA is a linear array with N elements algorithm deletes kth
element from LA and assigns it to variable ITEM.
21
Step 1: set ITEM = LA[ k ]
Step 2: Repeat for j=k to N-1:
[Move ( j + 1) element backward]
set LA[ j ] = LA[ j + 1]
[End of Loop]
Step 3: [reset number N of elements in LA]
set N = N - 1
Step 4: Exit
LA N = 6 (no. of elements in LA)
K = 3 (3rd position of LA to be deleted)
1 10 for j=k to N-1 22
2 20 Set 30=LA[3]

3 J= 3 to 6-1(5) J=3 TO 5 J=4 TO 5 J=5 TO 5


30
4 FALSE
40
LA[3] =LA[3+1]
5 50
LA[4] =LA[4+1]
6 60
LA[5] =LA[5+1]
N=6-1=5
1 10 10 10 10
2 20 20 20 20
3 40 40 40
4 40 50 50
5 50 60
50
6 60 60
60
Bubble Sort 23

Bubble sort is a simple sorting algorithm. This


sorting algorithm is comparison-based algorithm in
which each pair of adjacent elements is
compared and the elements are swapped if they
are not in order. This algorithm is not suitable for
large data sets.
Bubble Sort 24
Algorithm
Bubble Sort ( DATA , N )
Here DATA is a linear array with N elements. This algorithm
sort elements of DATA in ascending order.

Step 1: Repeat steps2 and 3 for K=1 to N - 1:


Step 2: Set Ptr = 1
Step 3: Repeat While Ptr <= N – K
(a) If DATA[Ptr] > DATA[Ptr + 1], then interchange
DATA[Ptr] and DATA[Ptr + 1]
End IF
(b) set Ptr = Ptr + 1
[End While]
[End For]
Step 4: Exit
1 N=5
2 25
for K=1 to N - 1
4
5 K=1 TO 5-1(4) K= 1 TO 4 K=1 K=2 K=3 K=4
8 PTR=1

While Ptr <= N – K


1<=5-4(1) 1<=1 TRUE 2<=1 FALSE

DATA[1]>DATA[1+1] 1>2 FALSE NO INTERCHANGED PTR=1+1=2


Example :- Consider a linear array consisting of 5 elements,
given below 26
09 06 02 12 07

09 06 02 12 07 Compare 1 and 2 element i.e. 09 and 06


Exchange 09 and 06 as 09 is larger than 06

Compare 2 and 3 element i.e. 09 and 02


06 09 02 12 07 Exchange 09 and 02 as 09 is larger than 02

Compare 3 and 4 element i.e. 09 and 12


06 02 09 12 07 No Exchange between 09 and 12 as
09 is smaller than 12

Compare 4 and 5 element i.e. 12 and 07


06 02 09 12 07 Exchange 12 and 07 as 12 is lager than 07

06 02 09 07 12 First Pass
Now we will consider First Pass array for further comparison
06 02 09 07 12 27

06 02 09 07 12 Compare 1 and 2 element i.e. 06 and 02


Exchange 06 and 02 as 06 is larger than 02

Compare 2 and 3 element i.e. 06 and 09


02 06 09 07 12 No Exchange between 06 and 09 as
06 is smaller than 09
Compare 3 and 4 element i.e. 09 and 07
02 06 09 07 12 Exchange 09 and 07 as 09 is lager than 07

02 06 07 09 12 Second Pass
Linear Search 28

Linear search is a very simple search algorithm. In


this type of search, a sequential search is made
over all items one by one. Every item is checked
and if a match is found then that particular item is
returned, otherwise the search continues till the
end of the data collection.
Algorithm LINEAR(DATA, N, ITEM, LOC) 29
Here DATA is a linear array with N elements and ITEM is given
element. This algorithm finds, the location LOC of ITEM in DATA or
sets LOC=0, if search is unsuccessful.

Step1: set DATA[N+1] =ITEM


Step2: Set LOC=1
Step3: Repeat While DATA[LOC]≠ITEM
set LOC =LOC + 1
Step4: If LOC =N + 1,then
set LOC=0
Step5: Exit
N=5 ITEM= 33

DATA 30
11 22 33 44 55

11 22 33 44 55 33

= 33
11 22 33 44 55 33

= 33
11 22 33 44 55 33
= 33

Search found at location 03


N=5 ITEM= 33
31
11 22 34 44 55 33
= 33
11 22 34 44 55 33
= 33
11 22 34 44 55 33
= 33
11 22 34 44 55 33
= 33
11 22 34 44 55 33
= 33
11 22 34 44 55 33
= 33
Search not found because we found similar value at N +1 location
LOC=0
Binary Search 32

Binary search is a fast search algorithm. This


search algorithm works on the principle of divide
and conquer. For this algorithm to work properly,
the data collection should be in the sorted form.
Binary search looks for a particular item by
comparing the middle most item of the
collection. If a match occurs, then the index of
item is returned. If the middle item is greater than
the item, then the item is searched in the sub-
array to the left of the middle item. Otherwise, the
item is searched for in the sub-array to the right of
the middle item. This process continues on the
sub-array as well until the size of the sub array
reduces to zero.
Algorithm Binary (DATA, LB, UB, ITEM, LOC)
Here DATA is sorted array with lower bound LB and upper bound 33
UB. ITEM is given element. BEG denotes beginning, MID denote
middle and END denote end location of array. This algorithm finds
the location LOC of ITEM in DATA or sets LOC = NULL, if search is
unsuccessful.

Step 1: Set BEG= LB END = UB MID = INT((BEG+END)/2)


Step 2: WHILE BEG = END AND DATA[MID] ≠ ITEM
Step 3: IF ITEM < DATA[MID], THEN
SET END = MID -1
ELSE:
SET BEG= MID+1
Step 4: SET MID =INT((BEG+END)/2)
Step5: IF DATA[MID] = ITEM, THEN
SET LOC = MID
ELSE:
LOC = NULL
Step 6: EXIT
34
11 22 30 33 40 44 55 N=7
search ITEM=40
BEG= LB(1) END=UB(7)
11 22 30 33 40 44 55 MID=INT((BEG+END)/2
1 2 3 4 5 6 7 Array index 4= 1+7/2
6 = 5+7/2
5 5+ 5/2
B ITEM<DATA[MID]
E 40<33
M 40<44

BEG=MID+1
END=MID -1 5 4+1
5 6-1
The following is our sorted array and let us assume that we need to search
the location of value 31 using binary search. 35
36
37
Difference between Linear and
38
Binary Search

Linear search is Elements in a list should


performed on sorted as be in sorted manner
well as unsorted list. only.
Compare desired Compare the value of
element with all midpoint with desired
elements in array. value.
More time required. Time required less.
Pointer Array 39

An array is called a pointer array ,if each element of


that array is pointer.
The variable is called pointer variable, if it points to
another variable i.e. It contains the memory address of
another variable.

Consider an organization which divides its employee into 4 groups


according to their departments.

Group 1 Group 2 Group 3 Group 4


Deepak Swapnil Rajdeep Kishori
Nitu Amit Yogesh Rohit
Vivek Shekhar Pooja
Ravi Neelam
If these groups are to
1 Deepak
be represented in 40
2 Nitu
memory, the most
3 Swapnil
efficient way is to use GROUP
array. 4 Amit
So to access these 5 Vivek 1 1
group from memory 6 Ravi 3 2
we need pointer. 7 Rajdeep 3
7
Each element of Group array is 8 Yogesh
10 4
pointer, 9 Shekhar
which holds the starting address 10 Kishori
of different groups. 11 Rohit
Hence it is called pointer array. 12 Pooja
13 Neelam
41
Record

A record is a collection of relatives data items,


each of which is called as field or attributes.
Collection of records is known as files. Collection
of data is frequently organized into a hierarchy of
fields, records and files.
A record may contain non-homogeneous data
i.e. Data items of record need not to be of same
type.
• An organization keeps the records of the
employees. 42
• It contains following data items (Name,
Gender, Salary,
Birth Date).
1. Employee
• The structure of records is shown in figure.
2. Name
• The number to the left of each variable
3. First
indicates level number.
Eg. Employee(30) means file of 30 3. Middle
records.
3. Last
• To access first of 3rd employee we need 2. Gender
to write Employee(3)
2. Salary
2. Birth Date
3. Date
3. Month
3. Year
Records are represented in memory using array
43
1. Employee
2. Name
First Middle Last Gender Salary
3. First
3. Middle Record Record Record Record Record
1 1 1 1 1
3. Last
Record Record Record Record Record
2. Gender 2 2 2 2 2
2. Salary
2. Birth Date
3. Date
3. Month
3. Year
One separate linear array is used for each elementary items
of record.
Difference between Records and
44
Linear Array

Record is a collection of An array is list of


fields. homogeneous data
elements.
Natural ordering is not
possible. Can be natural ordered.
Elements of record are Array can be referred by
referred by level array index.
number.
Stack 45
A stack is commonly used in most programming languages. It is
named stack as it behaves like a real-world stack.
for example – a deck of cards or a pile of plates, etc.
A real-world stack allows operations at one end only.
For example, we can place or remove a card or plate from the top
of the stack only.
Likewise, Stack allows all data operations at one end only. At any
given time, we can only access the top element of a stack.
This feature makes it LIFO data structure.
LIFO stands for Last-in-first-out.
Here, the element which is placed (inserted or added) last, is
accessed first. In stack terminology, insertion operation is
called PUSH operation and removal operation is
called POP operation
46
Queue 47

Queue is, somewhat similar to Stacks. Unlike stacks, a queue is


open at both its ends.
One end is always used to insert data (enqueue) and the
other is used to remove data (dequeue).
Queue follows First-In-First-Out methodology, i.e., the data item
stored first will be accessed first.
Queue Representation 48
As we now understand that in queue, we access both
ends for different reasons.
The following diagram given below tries to explain
queue representation
as data structure −
Linked List 49
A linked list is a sequence of data structures, which are
connected
together via links. Linked List is a sequence of links which
contains items.
Each link contains a connection to another link. Linked list is
the second most-used data structure after array.

Following are the important terms to understand the concept


of Linked List.

 Link − Each link of a linked list can store a data called an


element.
 Next − Each link of a linked list contains a link to the next
link called Next.
 Linked List − A Linked List contains the connection link to
the first link called First.
50
Linked List Representation

Linked list can be visualized as a chain of nodes, where every node points
to the next node.
As per the above illustration, following are the important points to be
considered.
1. Linked List contains a link element called first.
2. Each link carries a data field(s) and a link field called next.
3. Each link is linked with its next link using its next link.
4. Last link carries a link as null to mark the end of the list.
10 20 30 40
51

head
05 04 08 07

10 04 20 08 30 07 40 null
Link List Representation 52
IN Memory
Linked lists can be represented in memory by
using two arrays respectively known as INFO and
LINK, such that INFO[K] and LINK[K] contains
information of element and net node address
respectively.
The list also requires a variable ‘Name’ or ‘Start’,
which contains address of first node.
Pointer field of last node denoted by NULL which
indicates the end of list.
Start
53
5

A 9 B 4 C 1 D 8

5 9 4 1

Info Link
Start
1 D 8
5 E X
2
3 8
4 C 1
5 A 9
6
7
8 E 0
9 B 4
54

Insertion and Deletion


from link list
Start
55
2

26 1 94 4 89 3 65 X

2 1 4 3

Info Link
Start
1 94 4
2
2 26 1
3 65 0
4 89 3
5 0
Avail 6 0
5 Insert element on second position of the list (39)
Start
56
2

26 5 94 4 89 3 65 X

2 1 4 3

39 1 Info Link
Start
1 94 4
5 2
2 26 5
3 65 0
4 89 3
5 39 1
Avail 6 0
6
Start
57
2

26 1 94 4 89 3 65 X

2 1 4 3

Info Link
Start
1 94 4
2
2 26 1 Delete second node from the list
3 65 0
4 89 3
5 0
Avail 6 0
5
Start
58
2

26 4 94 4 89 3 65 X

2 4 3

Info Link
Start
1 94 0
2
2 26 4

3 65 0

4 89 3

Avail 5 0
5 6 0
Tree 59

Tree is non linear hierarchical data structure which consist of


finite set of one or more nodes.
Tree represents the nodes connected by edges.
Tree Terminologies 60
Node
A node is an entity that contains a key or value and pointers to its child nodes.
The last nodes of each path are called leaf nodes or external nodes that do
not contain a link/pointer to child nodes.
The node having at least a child node is called an internal node.
Edge
It is the link between any two nodes.
Root
It is the topmost node of a tree.
Tree Terminologies 61
Height of a Node
The height of a node is the number of edges from the
node to the deepest leaf (ie. the longest path from the
node to a leaf node).
Depth of a Node
The depth of a node is the number of edges
from the root to the node.
Height of a Tree
The height of a Tree is the height of the root
node or the depth of the
deepest node.
Degree of a Node
The degree of a node is the total number
of branches of that node.
Binary Tree 62
Binary Tree is a special data structure used for data storage
purposes. A binary tree has a special condition that each node can
have a maximum of two children. A binary tree has the benefits of
both an ordered array and a linked list as search is as quick as in a
sorted array and insertion or deletion operation are as fast as in
linked list.
63

Types of Binary Tree


64
Full Binary Tree
A full Binary tree is a special
type of binary tree in which
every parent node/internal
node has either two or no
children.
Perfect Binary Tree 65
A perfect binary tree is a
type of binary tree in which
every internal node has
exactly two child nodes and
all the leaf nodes are at the
same level.
Complete Binary Tree 66
A complete binary tree is just
like a full binary tree, but with
two major differences
Every level must be
completely filled
All the leaf elements must
lean towards the left.
The last leaf element might
not have a right sibling i.e. a
complete binary tree doesn't
have to be a full binary tree.
Expression 67

1. E = (A + B) / [ (C * D) –E]
2. E = (A - B) / (C * D) + E
3. [(A + B) * C] / [ A * ((B – C) +
A)]
4. (2X + Y) (A – 7B)3
5. X = (P + Q) / ((R + S) + T)
6. E = (2X – Y) / (5A – B)3
7. Z = (A + B / C) * (C + D / F)
E=(a+b)/[(c*d)-e]
68
=

E /

+ -

a b * e

c d
= =
69
E / E /

+ - - +

A B * E A B * E

C D C D
/
70
* *

+ C A +

A B - A *
B C + ↑

* Y - 3

2 X A *

7 B

You might also like