Chapter2 DataStructure Notes
Chapter2 DataStructure Notes
v. Entity:
An entity is something that has certain attributes or properties which may
be assigned values. The values can be numeric or non-numeric.
For example, employee of an organisation is entity. The attributes
associated with this entity are employee number name, age, sex, address
etc. Every attribute has certain value. All the employees in an organisation
forms entity set.
vi. Field
For example, employee number, name, age, sex are all fields.
vii. Record
A record is a collection of field values of a given entity. i.e. we can have one
record for one employee.
Field
s
Record
viii. File
A field having unique value is called primary key or key field. For example
employee number can be a primary key.
Q.2) What is Data Structure? Explain linear data structure and non-linear
data structure. (2018)
Deleting: Deleting means removing the record (element) from the structure.
Merging: Merging means combining the records in two different files into a
single file.
(2024)
Define data structure. Explain any four operations of Data Stucture.
Q.4) List any six data structure operations. (2020)
1. Traversing
2. Searching
3. Inserting
4. Deleting
5. Sorting
6. Merging
Q5.) Explain the following control structures and their used in data
structure
Q6.) What is Array? How are they represented in memory with example.
A linear array is a list of finite number of n homogeneous data elements.
i.e. the data elements are of same type.
The elements of array are referenced respectively by an index set consisting
of n consecutive numbers. The elements of array are stored respectively in
successive memory locations.
The number n of elements is called the length or size of the array. In general,
the length or number of elements in array can be obtained by using formula
Length = UB - LB + 1
Representation of linear arrays in memory
The elements of linear array are stored in successive memory cells. The
number of memory cells required depends on type of data elements.
Computer keep track of address of the first element of array. If LA is linear
array then address of first element is denoted by Base (LA) and called base
address of LA. Using this base address computer calculates the address any
element of LA by using the formula –
LOC (LA [K]) = Base (LA) + W (K – LB)
where W = number of words per memory cell for LA.
Memory
Address
1000 Array elements are stored
sequentially
1001
1002
:
:
:
Memory representation of array
Q7.) What is array? Write an algorithm for traversing linear array. (2018)
A linear array is a list of finite number of n homogeneous data
elements. i.e. the data elements are of same type.
Traversing array means accessing each element of the array only once.
We need to perform this operation several times in a program.
The algorithm for traversing operation is described below.
Algorithm - Traversing a linear array,
Suppose LA is linear array with lower bound LB and upper bond
UB.
1. Initialize counter
Set K = LB
2. Repeat steps 3 and 4 while K <= UB
3. Visit element LA [K]
4. Increase counter
Set K = K+1
[End of step 2 loop]
5. EXIT.
Q8.) State algorithm for inserting an element in an array. (2016, 2024)
Inserting refers to the operation of adding another element to the existing
elements.
Algorithm for inserting into linear array is given below:
Algorithm - Inserting element into linear array.
INSERT (LA, N, K, ITEM)
Here LA is linear array with N elements and K is positive integer
such that K<= N. This algorithm inserts an element ITEM into the
Kth position in LA.
1. Initialize counter
Set J = N – 1.
2. Repeat steps 3 and 4 while J >= K
3. Move Jth element downwards
Set LA [J+1] = LA [J]
4. Decrease the counter
Set J = J – 1.
[End of Loop]
5. Insert the element
Set LA [K] = ITEM.
6. Reset N
N=N+1
7. EXIT.
Q9.) State algorithm for deleting an element in an array.
Deleting refers to the operation of removing one of the elements from existing
elements.
Algorithm for deleting element from linear array is given below:
Algorithm - Deleting element from linear array
DELETE (LA, N, K, ITEM)
Here LA is linear array with N elements and K is a positive integer
such that K <= N. This algorithm deletes the Kth element from
LA.
1. Set ITEM = LA [K]
2. Repeat for J = K to N – 1
Move (J + 1)st element upward
Set LA [J] = LA [J+1]
[End of loop]
3. Reset N
N = N - 1.
4. EXIT
Q10.) State algorithm to find smallest element in an array. (2015)
Algorithm to find smallest element in an array is given below:
Algorithm – Find smallest element in a linear array
Smallest (LA, N, Small)
Here LA is linear array with N elements. This algorithm finds
smallest element in array LA and stores in variable Small.
OR
1. Initialize Small = LA [0]
1. Initialize counter
2. Repeat step 3 for J = 1 to N – 1 Set J = 1 and Small = LA[0]
3. If Small > LA [J] then: 2. Repeat steps 3 and 4 while J <= N-1
Small = LA [J] 3. IF LA[J] < Small THEN:
[End of IF Structure] Set Small = LA [J]
[End of loop] [End of IF structure]
4. EXIT Set J = J + 1.
5. Print Small
6. EXIT.
Q11.) Explain Bubble sort algorithm with suitable example. (2020, 2017,
2020, 2023)
Ans:
Algorithm - Bubble sort.
Sort (DATA, N)
Here DATA is a linear array with N elements. This algorithm
sorts elements of DATA in ascending order.
1. Repeat steps 2 and 3 for K=1 to N-1
2. Set J = 0
3. Repeat while J <= N - K:
(a) If DATA [J] > DATA [J+1] then:
Interchange DATA [J] and DATA [J+1]
[End of IF structure]
(b) Set J = J +1
[End of Step 3 inner loop]
[End of step 1 outer loop]
4. EXIT
Example:
Consider a linear array A, consisting of 5 elements
Step 1:
i) Compare A[0] and A[1] i.e. 8 and 4 and arrange them in desired order,
so that A[0] < A[1].
New list is 4 8 19 2 7
ii) Compare A[1] and A[2] i.e. 8 and 19 and arrange them so that
A[1] < A[2]. No exchange.
New list is 4 8 19 2 7
iii) Now compare A[2] and A[3] i.e. 19 and 2 since 19 > 2 interchange
New list is 4 8 2 19 7
Continue this process until we compare A[n-2] with A[n-1], so that
A[N-2] < A[N-1].
iv) Compare A[3] and A[4] Since 19 > 7 interchange
New list is 4 8 2 7 19
Step 1 involves n-1 comparisons. During this step, the largest element
is coming like a bubble to the nth position. When step 1 is completed,
A[N-1] will be the largest element.
New list is 4 2 8 7 19
New list is 4 2 7 8 19
At the end of second pass, the second largest element 8 has moved to
Pass 3: Repeat step1 with two less comparisons. Pass 3 involves N-3
New list is 2 4 7 8 19
New list is 2 4 7 8 19
Pass 4: Compare A[0] and A[1] and arrange them so that A[0] < A[1]
After n-1 steps or passes, the list will be sorted in increasing order.
So, bubble sort has n-1 passes.
Ans:
Q14.) Write difference between Linear Search and Binary Search. (2017)
Linear Search Binary Search
In linear search the given element is In binary search the given element is
compared with each element of list compared with middle element, after
one by one. finding the midpoint of the array. If the
value is less than midpoint value, the
first half is checked, otherwise second
half is checked until search is
successful.
Linear search can be performed on For binary search, array has to be
unsorted as well as sorted list. sorted in ascending order.
For large size array, time required for For large size array, time required is
this search is very large. comparatively less.
In worst case, the running time of The number of comparisons required
algorithm is proportional to n. here are approximately equal to log2n
which is less than that of linear search.
Group Member
…
Parallel arrays for records
Q18.) What are Linked lists? How linked list are represented in memory? (2014, 2015,
2017)
Ans:
A linked list is a linear collection of data elements, called nodes pointing to
next nodes by means of pointers.
-and the second part called the link or next pointer containing the
address of the next node in the list.
iii) The pointer START is a special pointer, which stores address of first
node in the list.
iv) The left part represents the information part of the node.
vi) The next pointer of last node stores NULL value, which means this node
is not pointing to any other i.e. it is the last node in the list.
vii) When list do not have any node then it is called null list or empty list
and START variable contains Null value.
Ans:
-and the second part called the link or next pointer containing the
address of the next node in the list.
Ans:
Tree
Binary tree.
A binary tree is a finite set of nodes that is, either empty or consists of a
root and two disjoint binary trees called left and right subtrees.
If every node in a tree can have at the most 2 children or degree 2, the
tree is called a binary tree.
2 TREE or EXTENDED BINARY TREE.
N N LN N LN
Q23.) What is Binary Tree? With suitable example show the relationship
between total number of nodes and depth of Binary tree. (2018)
Ans:
Binary tree.
A binary tree is a finite set of nodes that is, either empty or consists of a
root and two disjoint binary trees called left and right subtrees.
If every node in a tree can have at the most 2 children, the tree is called a
binary tree.
For example:
2n – 1 = 2 3 – 1
= 8–1
= 7
OR
In a tree each item is called node or leaf. Each item contains data and pointer,
which contain address of the node. The first node in the tree is called root. Each
piece under node is called subtree. A node under which the subtree exists is
called parent node. A node that has no subtree is called terminal node.
Level of root = 0
Each node in a tree is assigned a level number. Generally, the level number
of root ‘R’ of the tree is zero and every other node is assigned a level number
which is one more than the level number of its parent. It is the distance from the
root.
Define:
ROOT. A
A node which has no parent is called root node. The first node from where the
tree originates is called as a root node. Eg. In figure Node A is the root of the
tree.
LEAF.
The node which has no successors or child or children is called Leaf or
terminal node. E.g. In figure, D, F, G, K, J are leaf nodes.
SIBLING.
Nodes or children of the same parent node are said to be siblings. Eg. The
nodes D and E are children of node B. So, D and E are siblings.
DEPTH.
The depth (or height) of a tree T is the maximum number of nodes in a branch
of T. This is one more than the largest level number of T.
For ex. The tree above has depth 5. (largest level = 4; depth=4+1=5)
The line drawn from a node N to a successor is called an edge. In a tree with
n number of nodes, there are exactly (n-1) number of edges.
BRANCH.
DEGREE
Degree of a node is the total number of children of that node. In a binary tree,
all nodes have degree 0, 1, or 2. E.g. Degree of node B is 2 and degree of node
E is 1.
Degree of a tree is the highest degree of a node among all the nodes in the
tree. Degree of Binary tree is 2.
INTERNAL NODE
The node which has at least one child is called as an internal node.
COMPLETE TREE.
The tree is said to be complete if all its levels, except the last, have maximum
no. of possible node.
Q24.) Define Binary Tree. Draw a Tree diagram for following expression:
Ɣ = [ (a – b – c ) + ( a + b – c ) ]3/2
A binary tree is a finite set of nodes that is, either empty or consists of a
root and two disjoint binary trees called left and right subtrees.
If every node in a tree can have at the most 2 children, the tree is called a
binary tree.
Q.) Draw a binary tree structure for expression:
E=(p–q)/[(r*s)+t] (2022)
[Note: For extra tree diagrams of algebraic expressions refer PPT’s PDF.]
Ans:
Let T be a binary tree.
T can be represented in memory either by
i) linked representation or
ii) sequential representation by using array
The given binary tree can be represented using three parallel arrays as
given below:
The Root stores address of first node of tree T.
In this ex. Root contains address 3.
INFO [3] = contains data i.e. A
LEFT [3] contain address of left node of A - here LEFT [3] = 4 and
RIGHT [3] contain address of right node of A – here RIGHT [3] = 7
So, INFO [4] = B and INFO [7] = C.
LEFT [4] = 2 and LEFT [7] = 0
RIGHT [4] = 6 RIGHT [7] = 5
Like this all the nodes can be obtained by using K of INFO, LEFT and
RIGHT.
Ans:
For sequential representation of Binary Tree in memory, a linear array is
used.
The Root R is stored TREE [1].
If a node n occupies TREE [K], then its left child is stored in TREE [ 2*K ]
and right child in TREE [ 2 * K+1]
Consider a binary tree given below
The given binary tree can be represented using one linear array as given
below:
*********************