Data Structure & Algorithm Notes
Data Structure & Algorithm Notes
Employee name get divided into three subitems as first name, middle
name and last name.
ii) Elementary items :- Data items that are not divided into
subitems are called elementary item.
Ex.: 14 – 03 – 2022.
Social security number is treated as a single item.
(c) Organization of data :-
Collection of data are frequently organized into hierarchy of fields,
records and files.
- The above concept can be more cleared using following additional
terminology i.e. attribute, entity and entity set.
i) Attribute :- A attribute is a specified memory area used to store
data.
ii) Entity :- A entity is something that has retain attributes or
properties which may be assigned values. Here assigned values may
be either numeric or non-numeric.
Ex.: Attributes: Roll No. Name Grade
Values: 01 Ajay A
iii) Entity Set :- Entities with similar attributes form an entity set.
Each attribute of an entity set as a range of values, the set of all possible
values that could be assigned to the particular attribute.
Ex.: All the employees in an organization.
(d) Information :- A meaningful or processed data at particular attribute
is called as “information”.
The way that data are organized into the hierarchy of fields, records and files,
shows the relationship between attributes, entities and entity set as:
i) Field :- A field is a single elementary unit of information
representing an attribute of an entity.
1. By:Chimle Mahesh 2 / 35
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
By:Chimle Mahesh 3 / 35
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
Linked list
[A] Primitive Data Structure :-
Primitive data structure are basic data types i.e. int, real, character,
Boolean, float. These data types are primitive because they are directly
understand by the machine.
1. By:Chimle Mahesh 4 / 35
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
Using a linear array, a list of finite number ‘n’ of similar data elements
referenced respectively by the set of ‘n’ consecutive (serially)
numbers, usually 1, 2, 3, ……….. n.
The elements of an array A may be denoted by the notation :
1. Subscript notation as A1, A2, A3, ……, An.
2. Parenthesis notation as A(1), A(2), A(3), …….., A(n).
3. By the bracket notation as A[1], A[2], A[3], ……., A[n]
Regardless of the notation, the number K in A[K] is called a
subscript and A[K]is called a subscripted variable.
II) Stacks :-
A stack, also called a last-in-first-out (LIFO) system, is a linear list in
which insertions and deletions place only at one end, called the Top.
This structure is similar in its operation to a stack of dishes on a spring
system as shown in following figure.
TOP
Disk
Sprig Container
As container is open at top side, new dishes are interested only at the
top of the stack and dishes can be deleted only from the top of the
stack.
III) Queue :-
A queue, also called a first-in first-out (FIFO) system, is a linear list in
which deletions can take place only at one end of the list, the ‘front’ of
By:Chimle Mahesh 5 / 35
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
the list, and insertions can take place only at other end of the list, the
‘rear’ of the list.
The structure operates in a same way as line of people waiting for bus
at bus-stop as Bus Stop shown in
figure.
Front -End
Rear-End
Fig: Queue of People
START
1. By:Chimle Mahesh 6 / 35
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
- It is clear that, in above fig. each node is pictured with two fields.
- The left field use the actual information, and right field use store
the address of the next node.
- Arrow shows the order of the nodes.
- The next pointer or LINK field of the last node contains a invalid
address called ‘null pointer’.
- The null pointer (X) is used to shows the end of the list.
- The linked list has also a list pointer variable called START which
contains address of first node in the list.
- The starting address o the list which is stored in START is used
to traverse the whole linked list.
- Here if the linked list has no any node then such a list is called
‘null list’ or ‘empty list’ and it is denoted by the null pointer
assigning to START i.e. START = NULL.
❖ Non – Linear Data Structure :-
Tree and graph are the non – linear data structure.
a) Tree :- data frequently contain a hierarchy relationship between
various elements, the data structure which shows such relationship is
called ‘Tree’.
Getting more about trees consider the following two examples.
i) Record structure. ii) Algebraic
expression.
i) Record Structure :- An employee personal record may contain
following data items.
Ex.: Social_security_no Name Address Age Salary.
By:Chimle Mahesh 7 / 35
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
- In above data items, name and address may be group items with
sub item i.e.
Name :- F_Name, M_Name, L_Name.
Address :- Street Address & Area Address.
- Further are its may be group item having sub item city, state and
pin code number.
- The hierarchical structure of above data element is in term of
tree’s is as follows,
Employee
F_Name Street
M_Name Area
L_Name
City
State
Pin Code
1. By:Chimle Mahesh 8 / 35
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
(3) F_Name
(3) M_Name
(3) L_Name
(2) Address
(3) Street
(3) Area
(4) City
(4) State
(4) Pin Code
(2) Age
(2) Salary.
By:Chimle Mahesh 9 / 35
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
Delhi
Mumbai Kolkatta
Aurangabad Pune
1. By:Chimle Mahesh 10 / 35
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
7. Copying.
8. Concatenation.
1. Traversing :-
- This operations is used to accessing each record exactly once of
process certain items in the records.
- Ex.: We have 1, 5, 10, 7 items into data structure.
- Then access (visit) all items means read one five, seven, ten in
certain manner called as ‘Traversing’.
2. Searching :-
- This operation is used to finding the location of the record with
the given key value.
- Ex.: If we have data 1, 5, 10, 7 stored sequentially.
- We want to find location of data 10, it is located ‘3 rd’ location is
called as, ‘Searching’.
3. Inserting :-
- This operation is used to adding a new record to the structure.
- Ex.: Data as 1, 5, 10, 7.
- We want to insert ‘20’ at ‘4th’ location after that output is 1, 5, 10,
20, 7.
4. Deleting :-
- This operation is used to removing a record from the structure.
- Ex.: Data as 1, 5, 10, 20, 7
- We want to delete data on ‘3rd’ location after delete output is 1,
5, 20, 7.
By:Chimle Mahesh 11 / 35
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
5. Sorting :-
- This operation is used to arrange the records in same logical
order.
6. Merging :-
- This operation is used to combining the records in two different
sorted fields into a single a sorted file.
7. Copying :-
- This operation is used to make the duplication of the record.
8. Concatenation :-
- This operation is used to combine any two records into single
record.
1. By:Chimle Mahesh 12 / 35
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
By:Chimle Mahesh 13 / 35
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
If Condition, Then :
1. By:Chimle Mahesh 14 / 35
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
[Module A]
[End of If structure]
ii) Double Alternative :- This structure has a form:
If Condition(1), Then :
[Module A]
Else :
[Module B]
[End of If structure]
iii) Multiple Alternative :- This structure has the form:
If condition(1), Then :
[Module A]
Else if condition (2),Then:
[Module A2]
. .
. .
Else if condition (n), Then:
[Module An]
Else [Module B]
[End of If structure]
By:Chimle Mahesh 15 / 35
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
1. By:Chimle Mahesh 16 / 35
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
By:Chimle Mahesh 17 / 35
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
- The time and space are two important measurements which are
used to calculate the efficiency of an algorithm.
- The complexity of an algorithm is the function which gives the
running time and/or memory space used by the input size.
- There are two case usually investigates to find complexity.
1. Worst Case.
2. Average Case.
1. Worst Case :-
- The maximum value of F(n) for any possible input.
- C(n) = n is complexity of linear search algorithm.
2. Average Case :-
- The excepted value of F(n).
1. By:Chimle Mahesh 18 / 35
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
By:Chimle Mahesh 19 / 35
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
1. By:Chimle Mahesh 20 / 35
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
1. Linear Search :-
- It is clear that time required execute the algorithm is
proportional to the number of comparisons performed by the
linearly.
- In this method each name of the file is get compared so the
average number of comparisons for the file with ‘n’ records is
equal to
- Hence, the complexity of linear search algorithm is
2. Binary Search :-
- In this method the name are stored in sorting order
alphabetically.
- This algorithm compare a given name with the name in the
middle of the list : This tells which half of the list the name
contain.
- Then compare name with the name in the middle of the correct
half to determine which quarter of the list contains name.
- Continue the process until finding name in the list. - Hence, the
complexity of the binary search method is c(n) = log 2n
By:Chimle Mahesh 21 / 35
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
Here ‘UB’ is the largest index called upper bound and ‘LB’ is the lower bound
i.e. smallest index of an array.
DATA 30 15 35 40 50
LB 30 10 1 2 3
4 15
2 35
3 40
4 50
UB 5
length = UB – LB + 1
length =5–1+1 length= 4 – 0 + 1
=4+1 =4+1
=5 =5
- The element of an array may be denoted by the notation :
4. Subscript notation as :
A1, A2, A3, ……, An.
5. Parenthesis notation as:
1. By:Chimle Mahesh 22 / 35
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
1000
1001
1002
1003
1004
Fig. Computer
Memory
By:Chimle Mahesh 23 / 35
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
1. By:Chimle Mahesh 24 / 35
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
set k LB
Step 2. :
Repeat step 3 & 4 while
(k <= UB)
By:Chimle Mahesh 25 / 35
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
1. By:Chimle Mahesh 26 / 35
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
By:Chimle Mahesh 27 / 35
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
1. By:Chimle Mahesh 28 / 35
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
test weather LA[1] = ITEM. Then last weather LA[2] = ITEM and
so on.
- The method which traverses LA sequentially to search ITEM is
called ‘Linear Search’ or ‘Sequential Search’.
- In this search, first the ITEM is stored at LA (N + 1) location as =
LA (N + 1) = ITEM.
- Then searching is started from the first element of the list, here
if ITEM is present at that time this search method display a
message successful search and if ITEM is not present in the list
then this search method display a message unsuccessful search
and reset the location variable LOC to Null or Zero.
- Following is a algorithm to find the location LOC of given
element ITEM for an array LA.
Step 1. :
[Insert ITEM at the end of the LA] set
LA (N + 1) ITEM
Step 2. : [Initialize counter location]
Set LOC 1
Step 3. : [Search for ITEM in LA]
Repeat step 4
while (LA [LOC] ITEM)
By:Chimle Mahesh 29 / 35
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
1. By:Chimle Mahesh 30 / 35
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
By:Chimle Mahesh 31 / 35
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
1.
10 11 20 22 30 33 40 44 50 55 60 66 70 77
1 2 3 4 5 6 7 8 9 10 11 12 13 14
1. By:Chimle Mahesh 32 / 35
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
8 9 10
Let, START = 8 & END = 10
By:Chimle Mahesh 33 / 35
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
1. By:Chimle Mahesh 34 / 35
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
By:Chimle Mahesh 35 / 35
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
By: Chimle. M. D. 1 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
ii. Compare DATA[2] with DATA[3]. Since 42 < 74, a list not alter.
iii. Compare DATA[3] with DATA[4]. Since 74 > 11, interchange them.
Hence given list becomes,
23 42 11 74 65 15 10 08
iv. Compare DATA[4] with DATA[5]. Since 75 > 65, interchange them.
Hence given list becomes,
23 42 11 65 74 15 10 08
vi. Compare DATA[6] with DATA[7]. Since 74 > 10, interchange them.
Hence given list becomes,
23 42 11 65 15 10 74 08
By: Chimle. M. D. 2 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
vii. Compare DATA[7] with DATA[8]. Since 74 > 08, interchange them.
Hence given list becomes,
23 42 11 65 15 10 08 74
By: Chimle. M. D. 3 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
By: Chimle. M. D. 4 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
By: Chimle. M. D. 5 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
i. 11 10 08 15 23 42 65 74
By: Chimle. M. D. 6 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
ii. 10 11 08 15 23 42 65 74
10 08 11 15 23 42 65 74
By: Chimle. M. D. 7 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
i. 10 08 11 15 23 42 65 74
By: Chimle. M. D. 8 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
08 10 11 15 23 42 65 74
After completing of pass 7, list get sorted i.e.,
08 10 11 15 23 42 65 74
By: Chimle. M. D. 9 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
[End of if structure]
Step 5: [Increase PTR by 1]
set PTR = PTR + 1
[End of step 3 loop]
[End of step 1 loop]
Step 6 : [Finished] Exit.
= 2 = 2 + 0(N) F(N)
= 0(N2)
- That means time require execute the bubble sort algorithm is
propositional to N2.
- Here N is the number of input elements.
By: Chimle. M. D. 10 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
By: Chimle. M. D. 11 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
Ex.: Sort the given list in ascending order using selection sort method
write all passes.
- Consider unsorted list DATA contains 8 elements as follows,
42 23 74 11 65 12 10 05
By: Chimle. M. D. 12 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
By: Chimle. M. D. 13 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
By: Chimle. M. D. 14 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
Ex.: Sort the given list LA in ascending order by using insertion sort
method and shows all passes.
LA={20,50,70,55,15,10,25}
Ans: Following table shows insertion sort algorithm.
The circle element indicate Pass.
The arrow indicate proper place for element.
By: Chimle. M. D. 15 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
By: Chimle. M. D. 16 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
= 2 = 0(N2)
- Further more, one can show that on the average their will be
approximately comparisons in the linear loop.
- Accordingly for the average case,
N–1
F(N) = + +……….. + 2
–
N(N 1)
= 4
= 0(N2)
By: Chimle. M. D. 17 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
- It is clear that, in above fig. each node is pictured with two fields.
- The left field use the actual information, and right field use store the
address of the next node.
- Arrows are used to show the order of the nodes.
- The next pointer or LINK field of the last node contain a invalid
address called ‘null pointer’.
- The null pointer (X) is used to shows the end of the list.
- The linked list has also a list pointer variable called START which
contains address of first node in the list.
- The starting address o the list which is sorted in START is used to
traverse the whole linked list.
- Here if the linked list has no any node then such a list is called ‘null
list’ or ‘empty list’. And it is denoted by the null pointer assigning to
START i.e. START = NULL.
By: Chimle. M. D. 18 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
- Suppose list be a linked list for the storage of such a list in memory it
requires linear arrays.
- One is a general array set “INFO”, another pointer array set “LINK”.
- An array INFO[K] will contain the information part and an array
LINK[k] will contain the address of the next element of the list.
- In linked list, a pointer variable START is used to store starting
location of the list. NULL is used to show the end of the list.
- If the subscripts of an array INFO & LINK will be positive integer, then
NULL = 0 used to show end of the linked list.
- Here, the nodes of the linked list doesn’t need occupy the adjacent
dement in the array INFO & LINK.
- Also more than one linked list may be maintain in the same linear
array but only by storing the static address of the second list. Another
pointer variable set START = 1 & so on.
- Ex.: suppose we want to store the words :- BCA–SEM 3 using a linked
list.
- The linked list representation of these words is as follows.
START
B C A S E M 5
By: Chimle. M. D. 19 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
START = 3 INFO[3] = B
LINK[3] = 6 INFO[6] = C
LINK[6] = 5 INFO[5] = A
LINK[5] = 7 INFO[7] = –
LINK[7] = 9 INFO[9] = S
LINK[9] = 4 INFO[4] = E
LINK[4] = 8 INFO[8] = M
LINK[8] = 2 INFO[2] = 3
LINK[2] = 0
By: Chimle. M. D. 20 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
1. LIST is Unsorted :
- An algorithm to search an ITEM in unsorted linked List.
- Let LIST is a unsorted linked list stored in memory with INFO and
LINK.
- START is a pointer variable which is used to stored initial address of
linked list.
- Then one searches for ITEM in list by traversing to the list using
pointer variable PTR and comparing ITEM with the contains
INFO[PTR] of each node, one by one of list.
By: Chimle. M. D. 21 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
By: Chimle. M. D. 22 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
By: Chimle. M. D. 23 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
- Let a linked list stored in memory using linear array INFO and pointer
array LINK.
- The linked representation of a linked list as follows,
STAR
15 50 10 20 35
START
INFO LINK
1 1 20 4
2 15 5
2
3 10
3 1
4 35
4 5 50 0
5 3
By: Chimle. M. D. 24 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
By: Chimle. M. D. 25 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
- Ex.: Consider a list of books is stored in linear array book & LINK then
available memory space in the linear array book may be linked as :
AVAIL
INFO LINK
1 1 DBMS 4
2
START
2 6
3 BASIC
3 5
4 HTML
4 5 C++ 0
6 5 1
6 0
- In above fig. AVAIL indicates first location i.e. 2 is the available space,
next 6 is available space.
2. Garbage Collection :
By: Chimle. M. D. 26 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
1. Overflow :
a. Overflow refers the situation one wants to insert the data into the
data structure, which is already filled.
b. This situation can be handle by printing message “OVERFLOW”.
c. In case of linked list overflow will occur when AVAIL = NULL.
2. Underflow :
a. Underflow refers a situation where one wants to delete data from a
empty data structure.
b. This situation can be handle by printing message “UNDERFLOW”.
By: Chimle. M. D. 27 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
ITEM
STAR
NEW
ITEM
By: Chimle. M. D. 28 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
Let LINK is a linked list with memory array INFO and LINK.
START and AVAIL is a link pointer variable which indicates
address of beginning with actual linked list. Here NEW is a
local pointer variable used to store address of node. This
algorithm perform insert an ITEM of information at the
beginning of the given linked list.
STAR
By: Chimle. M. D. 29 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
10 05 08 40
AVAIL
ITEM
Let linked list is stored in memory with INFO and LINK. START
and AVAIL is a LINK pointer variable which indicate the
address of beginning actual linked list. This algorithm perform
to insert ITEM after the given location LOC.
Otherwise is LOC = NULL insert at beginning.
By: Chimle. M. D. 30 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
- Suppose list is a sorted linked list stored in memory using linear array
INFO and LINK.
- When an ITEM is to be inserted into a sorted linked list, then it must
be satisfy the condition.
INFO[A] < ITEM < INFO[B]. Where A and B are two successive
nodes.
- To insert a NEW ITEM into a sorted linked list, one must know the
required location LOC of a node after which ITEM is to be inserted.
Procedure to find location LOC of a node is a sorted linked list, ITEM = 20.
LOC P PTR
STAR
9 10 15 25 30
- This procedure find location LOC of a node A. here value is < ITEM.
By: Chimle. M. D. 31 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
By: Chimle. M. D. 32 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
[End of if structure]
Step 3.: [Initialize LOCP and PTR]
Set LOCP = START and
PTR = LINK[START]
Step 4.: [Make search]
By: Chimle. M. D. 33 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
STAR
Data List
Node A
Node B
10 05 50 20
AVAIL
By: Chimle. M. D. 34 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
▪ If deleting the node X is the first node in the list then START
will point node B.
▪ If deleting node X is the last node in the list, then the LINK
pointer field of node A will contain a NULL pointer.
LOC
Node X
AVAIL
Let LIST be a linked list stored in memory with INFO and LINK.
LCO is a location of Node X which will delete and LOCP is a
location of the node precidy X. When X is the first node then
LOCP = NULL. This algorithm delete the node X with location
LOC.
By: Chimle. M. D. 35 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
By: Chimle. M. D. 36 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
This procedure find the location LOC of the first node X which
contains ITEM and location LCOP of the preceding node X. If
ITEM doesn’t appear in the LIST, then the procedure set LOC =
NULL and if ITEM appears in the first node then it sets LCOP =
NULL.
By: Chimle. M. D. 37 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
This algorithm deletion from linked list the first node X which
contain the ITEM of information.
By: Chimle. M. D. 38 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
Exit
Step 3.: [Is ITEM of node present at the beginning the list ?]
If LOCP = NULL, then
[Delete node]
Set START = LINK[START]
[End of if structure]
Step 4.: [Delete node from location LOC using preceding
node LOCP]
AVAIL LOC
Step 6.: [Finished] Exit.
By: Chimle. M. D. 39 / 39
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
TOP
3.1 Stacks :-
Disk
Sprig Container
Figure: Stack
By: Chimle M. D. 1 / 17
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
STACK STACK
1 N AA .
Top 5 2 BB N–13 Top .
CC 5 EE
DD 4 4 DD
EE 5 3 CC
N–1 2 BB
N 1 AA
Fig.(a)
Fig(b)
Top 5
1 2 3 5 6 47 8
AA BB CC D EE
D
STACK Fig (c) N– N
1
Use of Stack :-
Stack are used to indicate the order of it processing of data when
certain steps of the processing are postponed until other condition
are fulfill.
By: Chimle M. D. 2 / 17
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
Top MAXSTK
- In above fig. TOP = 4 indicate that stack has four elements and
MAXSTK = 9, which gives the maximum length of this stack.
- Here the location 5, 6, 7 indicate blank places for the new ITEM.
By: Chimle M. D. 3 / 17
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
1. Exponentiation ( )
2. Multiplication (*) and division (/)
3. Addition (+) and subtraction (–)
- In any parenthesis expression be operators of same precedence level
are executed from left–to–right.
- Ex.: Consider the following parenthesis are arithmetical expression.
x = (3 3+4*2 3 – 20 / 5)
=3 3+4*2 3–4
= 27 + 4 * 8 – 4
= 27 + 32 – 4
= 59 – 4
= 55
By: Chimle M. D. 4 / 17
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
By: Chimle M. D. 5 / 17
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
3.5 Recursion :-
- Suppose P is a procedure containing either a call statement to itself
or call statement to a second procedure that may result in a call
statement back to the original procedure P.
- Then P is a recursive procedure.
- The recursion is mainly divided into two parts.
1. Recursive procedure.
2. Recursion function.
1. Recursive Procedure :-
By: Chimle M. D. 6 / 17
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
By: Chimle M. D. 7 / 17
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
Step 1. : If N = 0, or N = 1 then
FIB = 1 and Exit
[End of if structure]
Step 2. : Call FIBONACCI (FIB.A, N – 2)
Call FIBONACCI (FIB.B, N – 1)
Step 3. : set FIB=FIB.A + FIB.B
Step 4. : [Finished] Exit.
By: Chimle M. D. 8 / 17
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
3.6 Queue :-
- A queue is a linear list element of in which deletion can take place
only at one end called the front and insertion can take place only at
the other end, called the rear.
- The term ‘front’ and ‘rear’ are used to describing linear list only when
it is implemented as a queue.
- Queue are also called FIFO (First In First Out) or FCFS (First Come
First Serve), because element in a queue will be first element out of
the queue.
- In other word the order in which element enter a queue is the order
in which they leave.
- Following figure shows the queue of people waiting for bus.
BUS Stop
Rear- Front-
End End
- The people waiting a BUS stop, each new person to comes takes
his/her place at the end of the line and when the bus come the front
in line board first line.
By: Chimle M. D. 9 / 17
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
FRONT = 1 REAR = 4
- Similarly when an element is array in the queue the value of rear is
increased by one as REAR = REAR +1.
- Hence, after increasing one element queue become.
AA BB CC DD EE
1 2 3 4 5
FRONT =1 REAR =5
- When an element is deleted the value of FRONT increased by one as
,
BB CC DD EE
1 2 3 4 5
FRONT = 2 REAR = 5
- It is REAR that after N insertion, the REAR element of the queue will
be occupy.
- Queue of (N), it means that queue will occupy last port of array.
By: Chimle M. D. 10 / 17
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
By: Chimle M. D. 11 / 17
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
By: Chimle M. D. 12 / 17
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
DE-QUEUE
YY ZZ W XX
W
1 2 3 4 5 6 7 8
LEFT = 7 fig. (b) RIGHT = 2
By: Chimle M. D. 13 / 17
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
-
By: Chimle M. D. 14 / 17
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
-
AA 1 BB 2 CC 2 DD 4 EE 4 FF 4 GG 5
- Following fig. shows the way the priority queue may appear in
memory using arrays INFO, PRN, LINK.
By: Chimle M. D. 15 / 17
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
- BB 2 6
7
- DD 3 4
EE 4 9
1
AA 1 1
2
3 CC 2 3
10
4
5 GG 5 0
AVAIL
6 FF 4 8
7 11
8
9 12
1
0 0
1
1
1
2
By: Chimle M. D. 16 / 17
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
-
AA 1 BB 1 CC 2 DD 4 EE 4 FF 4 GG 5
FRON REA
1 2 3 4 5 6T R
1
2 2 2 1 AA
3 B
4 1 3 2 CC
B
5 0 0 3
5 D
- The new 1 4 FF EE
4 D
that
maintains 4 5 GG
the queue of
elements with priority number K.
By: Chimle M. D. 17 / 17
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
4. Tree
Tree is a non-linear data structure. This structure is
mainly used to represent data containing a hierarchy or
hierarchical relationship between elements.
Ex.: Records, tables & family tree.
❖ Binary Tree :
- A binary tree T is defined as a finite set of elements called nodes
such that:
a) T is empty (called null tree or empty tree), or
b) T contains a distinguished node R, called the root of T and
the remaining nodes of T form an ordered pair of disjoint
binary trees T1 & T2.
- If binary tree T does contain a root R, then two trees T1 and T2
are called left and right sub trees of R respectively.
- If T1 is non-empty then its root is called left successor of R.
- Similarly, if T2 is non-empty then its root is called the right
successor of R.
- Consider following binary tree:
By: Chimle M. D. 1 / 14
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
C
B
E
D H
G
F
K
J
By: Chimle M. D. 2 / 14
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
-
A E
Fig. T Fig. T1
B F
D G H
C
❖ Tree Terminology :
In tree terminology, the relationship between the node of the tree is
described using three ways:
1.Family relationship.
2.Graph theory.
3.Horticulture concept.
1. Family Relationship :-
Suppose N is a node in T with left successor S1 and right successor S2.
Then N is called the parent (or father) of S1 and S2. S1 and S2 are called
left and right child of N.
By: Chimle M. D. 3 / 14
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
1
A
2
B C
3
G H
4 K
J
L 5
By: Chimle M. D. 4 / 14
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
-
3
2
6 7
5
4
12
11
9 10
8
By: Chimle M. D. 5 / 14
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
By: Chimle M. D. 6 / 14
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
-
+ +
z
x y –
a b
By: Chimle M. D. 7 / 14
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
2) Inorder Traversing.
3) Postorder Traversing.
- For detail study of above traversal methods, consider a following
binary tree.
B D
E E G
C
2. Preorder Traversing :-
- This traversing method works on follows, I. Process the Root
node R.
II. Traverse the left sub tree of R in preorder.
III. Traverse the right sub tree of R in P preorder.
- Also this traversing method is called node-left-right (N-L-R)
traversal.
- The preorder traversal of given tree T shown in above fig. is:
A B C E D E F G.
3. Inorder Traversal :-
- This traversing method works as follows, I. Traverse the left sub
tree of R in inorder.
II. Process the root R.
III. Traverse the right sub tree of R in inorder.
By: Chimle M. D. 8 / 14
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
-
By: Chimle M. D. 9 / 14
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
HEAD
Header Node
B C
D E G H
F J K
By: Chimle M. D. 10 / 13
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
By: Chimle M. D. 11 / 14
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
B C
E G H
D
F K
J
By: Chimle M. D. 12 / 13
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
❖ General Tree :-
- A general tree is defined to be non empty finite set T of elements
called nodes, such that:
1. T contains a distinguished elements R, called the ROOT of
T.
2. The remaining elements T from an ordered collection of 0
[zero] or more disjoints tree T1, T2, ….., Tm.
- The trees T1, T2, ….., Tm are called sub trees of the root R and the
roots of T1, T2, ….., Tm are called successors of R.
- Terminology from family relationships, graph theory and
horticulture is used for general trees in the same way as for
binary tree.
- In particular if is node with successors S1, S2, ….., Sm then N is
called parent and S1, S2, ….., Sm. are called children’s of N. And
relationship between S1 and S2 are called sibling of each other.
- Ex.: Following figure shows general tree T with 13 nodes, A B C
D E F G H J K L M N.
By: Chimle M. D. 13 / 14
Class: BCASY(Sem IV) COCSIT, Latur Sub: Data Structure and algorithm
D
B
E F
K
G J
H
M N
By: Chimle M. D. 14 / 13