Data Structures Overview and Algorithms
Data Structures Overview and Algorithms
PAPER 1 CHAPTER 2
NAME FIRST NAME MIDDLE NAME LAST NAME group item
ROLL NUMBER
STATE
PINCODE
MOBILE
EMAIL ID
STREAM
attributes values
NAME X Y Z
DIVISION J
ROLL NUMBER 1
STATE MAHARASHTRA
PINCODE 400022
MOBILE 1332456879
EMAIL ID xyz@[Link]
STREAM science
RECORD
• DATA : Data are simply values or set of values
• GROUP ITEMS : Data items which are divided into subitems are called
as group items.
• ELEMENTARY ITEMS : Data items which are not divided into subitems
are called as elementary items .
• ENTITY : An entity is something that has certain attributes or
properties which may be assigned values. Values may be numeric or
nonnumeric.
• FIELD : Field is a single elementary unit of information representing
an attribute of an entity.
• RECORD : Record is a collection of field values of a given entity .
• FILE : File is a collection of records of the entities in a given entity set.
• Data may be organized in many different ways.
• Data structure is the way in which different data elements are
logically related.
• Types
1. Linear data structure
2. Non – linear data structure
• Linear data structure : data elements are stored in consecutive
memory locations or by using linked representation e.g. arrays ,
linked lists.
• Non – linear data structure : linear order cannot be maintained
between data elements .Data elements have hierarchical
relationship between them e.g. Trees.
Data Structure operations :
1. Traversing – Accessing each record or element exactly once , so that
it can be processed is called as traversing.
2. Inserting – Adding a new record to the existing structure is called as
inserting.
3. Deleting - Removing a record from the existing structure is called as
deleting.
4. Searching – Finding the location of a record with given key values or
finding the locations of all records which satisfy one or more
conditions is called as searching.
5. Sorting – Arranging records in some logical order is called as sorting.
6. Merging – Merging means combining the records in two different
sorted files into a single sorted file .
Iteration logic
conditional flow or selection logic conditional flow or selection logic
sequential logic single alternative double alternative
Module A
if no if if
no
conditi condition condition
on ? ? ?
Module B
yes Module B
yes
Module C Module A Module A Module
If condition then :
If condition then :
Module A
Module A
Else : Repeat While condition :
End of If structure
Module B Module
End of If structure End of loop
KR
yes
if K > S ?
Repeat For K = R to S by T :
Module
no
End of For loop
Module K is called index variable
R and S are initial and final values of K
T is increment value
KK+T
Algorithm :
An algorithm is a finite step by step list of well defined
instructions for solving a particular problem
An algorithm consists of two parts :
• First part is a paragraph which tells the purpose of algorithm.
In this part, we define variables in algorithm and lists the input
data
• The second part of algorithm consists of steps in algorithm that
are executed one after the another, generally beginning with
step 1, unless stated otherwise. The control can be transferred
to step n, by the statement ‘go to step n’. The algorithm is
completed, when the statement ‘Exit’ or ‘Stop’ is encountered.
• Traversing an array means accessing
each element of array atleast once , so
that it can be processed. LA[0] LA[1] LA[2] LA[3] LA[4] LA[5] LA[6] LA[7] LA[8] LA[9]
• Algorithm : 10 20 30 40 50 60 70 80 90 100
5 1 3 2 7 1 3 2 5 7 1 3 2 5 7
DATA[0] DATA[1] DATA[2] DATA[3] DATA[4]
DATA[0] DATA[1] DATA[2] DATA[3] DATA[4] DATA[0] DATA[1] DATA[2] DATA[3] DATA[4]
1 3 2 5 7
1 5 3 2 7 1 3 2 5 7
N = 5 , K = 0 TO 4 ,
DATA[0] DATA[1] DATA[2] DATA[3] DATA[4] DATA[0] DATA[1] DATA[2] DATA[3] DATA[4] K = 3 , Ptr = 0
While Ptr <= (N - 1) – K → Ptr < = 1
1 3 5 2 7 1 2 3 5 7 If DATA [ 0 ] > DATA [ 1 ] SWAP , 1 < 3 DATA [0] =1 DATA [ 1 ] = 3
Ptr = 1
DATA[0] DATA[1] DATA[2] DATA[3] DATA[4] N = 5 , K = 0 TO 4 , If DATA [ 1 ] > DATA [ 2 ] SWAP , 2 < 3 DATA [1] = 2 DATA [ 2 ] = 3
K = 2 , Ptr = 0 Ptr = 2
1 3 2 5 7 While Ptr <= (N - 1) – K → Ptr < = 2
If DATA [ 0 ] > DATA [ 1 ] SWAP , 1 < 3 DATA [ 0 ] = 1 DATA [ 1 ] = 3 Ptr = 1
N = 5 , K = 0 TO 4 , If DATA [ 1 ] > DATA [ 2 ] SWAP , 3 > 2 DATA [ 1 ] = 2 DATA [ 2 ] = 3 Ptr = 2
K = 1 , Ptr = 0 If DATA [ 2 ] > DATA [ 3 ] SWAP , 3 < 5 DATA [ 2 ] = 3 DATA [ 3 ] = 5 Ptr = 3
While Ptr <= (N - 1) – K → Ptr < = 3
If DATA [ 0 ] > DATA [ 1 ] SWAP , 5 > 1 DATA [ 0 ] = 1 DATA [ 1 ] = 5 Ptr = 1
If DATA [ 1 ] > DATA [ 2 ] SWAP , 5 > 3 DATA [ 1 ] = 3 DATA [ 2 ] = 5 Ptr = 2
If DATA [ 2 ] > DATA [ 3 ] SWAP , 5 > 2 DATA [ 2 ] = 2 DATA [ 3 ] = 5 Ptr = 3 STEP 3. Repeat While Ptr <= N – K
If DATA [ 3 ] > DATA [ 4 ] SWAP , 5 < 7 DATA [ 3 ] = 5 DATA [ 4 ] = 7 Ptr = 4
(a) If DATA [ Ptr ] > DATA [ Ptr + 1 ] ,
DATA[0] DATA[1] DATA[2] DATA[3] DATA[4] Even though the then interchange
1 3 2 5 7 array is sorted the DATA [ Ptr ] and DATA[ Ptr+1 ]
process will [End of If structure]
N = 5 , K = 0 TO 4 ,
continue for K = 3 (b) [Increment pointer]
K = 4 , Ptr = 0
While Ptr <= (N - 1) – K → Ptr < = 0
Set Ptr := Ptr + 1
and K = 4 [ End of inner loop ]
If DATA [ 0 ] > DATA [ 1 ] SWAP , 1 < 3 DATA [ 0 ] = 1 DATA [ 1 ] = 3 Ptr = 1
• In linear search the given element is LA[0] LA[1] LA[2] LA[3] LA[4] LA[5] LA[6] LA[7] LA[8] LA[9]
compared with each element of list one
10 20 30 40 50 60 70 80 90 40
• Algorithm : LINEAR ( LA , N , ITEM ,
LOC ) ITEM = 40 , N = 9 , LA [ 9 ] = 40 , LOC = 0
LA [ 0 ] ≠ 40 , LOC = 1
• Here LA is a linear array with N LA [ 1 ] ≠ 40 , LOC = 2
elements and ITEM is given element . Element to be searched
LA [ 2 ] ≠ 40 , LOC = 3
This algorithm finds the location LOC of LA [ 3 ] = 40 , LOC = 4
ITEM in DATA or sets LOC = 0 , if search
is unsuccessful.
STEP 1. [ Insert ITEM at the end of DATA ] LA[0] LA[1] LA[2] LA[3] LA[4] LA[5] LA[6] LA[7] LA[8] LA[9]
Set LA [ N ] := ITEM 10 20 30 40 50 60 70 80 90 55
STEP 2. [ Initialize counter ] Set LOC := 0 ITEM = 55 , N = 9 ,LA [ 9 ] = 40 , LOC = 0
STEP 3. [Search for ITEM] LA [ 0 ] ≠ 55 , LOC = 1
Repeat While LA [ LOC] ≠ ITEM LA [ 1 ] ≠ 55 , LOC = 2
LA [ 2 ] ≠ 55 , LOC = 3
Set LOC : = LOC + 1 LA [ 3 ] ≠ 55 , LOC = 4
[End of loop] LA [ 4 ] ≠ 55 , LOC = 5
LA [ 5 ] ≠ 55 , LOC = 6
STEP 4. If LOC = N , then : LA [ 6 ] ≠ 55 , LOC = 7
Set LOC := 0 LA [ 7 ] ≠ 55 , LOC = 8
LA [ 8 ] ≠ 55 , LOC = 9
STEP 5. Exit. HENCE LOC = 0
• Binary search is used to search an element from sorted array.
DATA[0] DATA[1] DATA[2] DATA[3] DATA[4] DATA[5] DATA[6] DATA[7]
• Algorithm : BINARY SEARCH ( DATA , LB , UB , ITEM , LOC )
• Here DATA is a sorted array with lower bound LB and upper bound
UB. ITEM is given element. BEG denotes beginning , MID denotes 1 2 3 5 7 9 10 11
middle and END denotes end location of DATA. This algorithm finds
the location LOC of ITEM in DATA or sets LOC = 0 if search is
unsuccessful.
[ End of If structure]
STEP 4. Set MID := INT (( BEG + END )/2) [ End of STEP 2 loop]
Else :
LOC := NULL
[ End of If structure]
STEP 6. Exit.
Advantages of Binary search :
1. It is efficient as the search scope gets reduced by half
the size of the array , with each iteration
2. The number of comparisons required are
approximately equal to log2n which are less than linear
search
Disadvantages of Binary search :
1. The given list must be sorted.
2. At each iteration middle element calculation is
required.
Linear Search Binary Search
1. Linear search is performed on unsorted 1. Binary search is performed on sorted
list of elements as well as sorted list. list of elements.
2. Compare the desired element with all 2. Compare the value of midpoint with
elements in an array until the match is desired element. If the value is greater
found. than midpoint value the first half is
checked , otherwise second half is
checked until search is unsuccessful or
interval is empty.
3. Insertion of an element in an array can 3. Insertion of an element in an array
be performed easily when array is not requires that many elements be
sorted. physically moved to preserve the order.
4. For large size array , time required is 4. For large size array , time required is
very large. less.
5. Time complexity is as follows : 5. Time complexity is as follows :
Worst case : N comparison Worst case : log2N comparison
Best case : 1 comparison Best case : 1 comparison
POINTER ARRAYS :
• An array is called pointer array , if each element of that array is a pointer.
• The variable is called a pointer variable , if it points to another variable i.e. it contains the memory address of another variable
FRUIT VEGETABLE PULSES MILK POINTER ITEM
PRODUCTS 1 APPLE
APPLE TOMATO MOONG CREAM 2 MANGO
MANGO POTATO RAJMA CHEESE 3 ORANGE GROUP POINTER
ORANGE PUMPKIN CHANNA BUTTER 4 PAPAYA FRUIT 1
PAPAYA CABBAGE 5 TOMATO VEGETABLE 5
PEAS 6 POTATO PULSES 10
7 PUMPKIN MILK 13
8 CABBAGE PRODUCTS
9 PEAS
10 MOONG
11 RAJMA
12 CHANNA
13 CREAM
14 CHEESE
15 BUTTER
RECORDS :
• A record is a collection of relative data items , each of which is called as field or
attribute.
• Collection of records is known as files .
• Collection of data is frequently organised 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 data type.
• In a record, natural ordering of elements is not possible. The elements in record
can be described by level number.
Name Class Contact details
Roll number First name Middle name Last name Class Div Land line number Mobile number
Record [ 1 ]
Record [ 2 ]
Record [ 3 ]
B C
D E F G
H I J
BINARY TREE : BASIC TERMINOLOGY
• Left successor: B is the left successor of node A
• Right successor: C is the right successor of node A
• Left subtree: Left subtree consists of node B, D, E, H
• Right subtree: Right subtree consists of C, F, G ,I ,J
• Terminal node: The nodes with no successors are called terminal nodes. E, G, H, I
and J are called terminal nodes.
• Binary tree T1 and T2 are similar if they have the same structure.
• Any node N in a binary tree has 0, 1 or 2 successors.
A
• Depth of a tree means maximum level of any node in a tree.
• Maximum number of nodes in a binary tree with B C
depth n is 2n – 1.
D E F G
In the given tree structure depth is 4
Hence total number of nodes = 2n – 1 = 24 – 1 = 16 – 1 =15 H I J
• Complete binary tree: if all leaf nodes of a binary tree have same level number
and every non leaf node has non empty left and right subtrees then the tree is
called as complete binary tree.
• Extended binary tree or 2 – tree: A binary tree T is said to be a 2 – tree or an
extended binary tree if each node N has 0 or 2 children. The nodes with 2
children are called internal nodes and the nodes with 0 children are called
external nodes.
• Binary search tree: It is a binary tree in which each node N of the tree has the
property that the value at N is greater than every node value in the left subtree
of N and is less than or equal to every node value in the right subtree of N
14
A
A
B C 4 15
B C
D E 3 9 12 18
D E F G
F G
7
INFO LEFT RIGHT
Binary tree can be represented in memory in two ways: 1 B 7 14
1. Linked representation ROOT 5 2 4
3 F 0 0
2. Sequential representation AVAIL 10
* A * 4 13
Linked representation. 5 A 1 9
* C * 6 I 0 0
* B *
7 D 0 0
8 H 0 0
X D X E X X F X * G *
9 C 3 11
10 2
X H X X I X X J X 11 G 6 12
Linked representation uses three parallel arrays INFO, LEFT and RIGHT and two 12 J 0 0
variables ROOT and AVAIL such that for an index K , INFO[K] contains actual element, 13 0
LEFT [K] contains address of left child and RIGHT [K] contains address of right child.
14 E 8 0
ROOT stores address of first node of the tree.
AVAIL stores address of first null node . It is used to add a node to the tree.
In the given example to add an element K, AVAIL is used . Hence K will be added at
location INFO[10] . LEFT[10] and RIGHT[10] wil contain zero and AVAIL will contain 2
i.e. the next element is to be added at 2
• Sequential representation:
For sequential representation only one array is used.
INFO
This array is generally known as TREE such that : 1 A
a) The root of the tree is stored in TREE[1] 2 B
* E X X F X 12 -
X D X * G *
13 -
14 I
X H X X I X X J X 15 J
Representing equations as a binary tree:
E=(a+b)/[(c*d)–e]
=
E /
+ -
a b * e
c d
Representing equations as a binary tree:
[ ( a + b ) * c ] / [ a * (( b – c ) + a )]
/
* *
+ c a +
a b - a
b c
Representing equations as a binary tree:
(2x+y)(a–7b)3
*
+
↑
* y 3
-
2 x a *
7 b
Representing equations as a binary tree
Z=(A+B/E)*(C+D/F)
Z *
+
+
/ /
A C
B E
D F