Important Questions
Important Questions
Important Questions
SC
http://www.ignouassignmentguru.com
MCS-021 : DATA AND FILE STRUCTURES
BLOCK -01
Q1. what is complexity in data structure?
Ans. Complexity Analysis. An essential aspect to data structures is algorithms. Data structures are implemented
using algorithms. An algorithm is a procedure that you can write as a C function or program, or any other language.
An algorithm states explicitly how the data will be manipulated.
Time Complexity of Algorithms
Time complexity of an algorithm signifies the total time required by the program to run to completion. The
time complexity of algorithms is most commonly expressed using the big O notation.
Time Complexity is most commonly estimated by counting the number of elementary functions performed
by the algorithm. And since the algorithm's performance may vary with different types of input data,
hence for an algorithm we usually use the worst-case Time complexity of an algorithm because that is
the maximum time taken for any input size.
Space Complexity
When we design an algorithm to solve a problem, it needs some computer memory to complete its
execution. For any algorithm, memory is required for the following purposes...
Memory required to store program instructions
Memory required to store constant values
Memory required to store variable values
And for few other things
Space complexity of an algorithm can be defined as follows...
Total amount of computer memory required by an algorithm to complete its execution is called as space
complexity of that algorithm
Q2.what is a Sparse Matrix?
Ans.Matrices with good number of zero entries are called sparse matrices
1
IGNOU ASSIGNMENT GURU Page-
/IGNOUASSIGNMENTGURU
BCA MCA BA MA BDP B.COM M.COM B.SC
http://www.ignouassignmentguru.com
Q4. What is singly linked list?
Ans. Singly Linked Lists are a type of data structure. It is a type of list. In a singly linked
list each node in the list stores the contents of the node and a pointer or reference to the next
node in the list. It does not store any pointer or reference to the previous node.
The Linked list is a chain of structures in which each structure consists of data as well as pointer, which
stores the address (link) of the next logical structure in the list.
ALGORITHM (Insertion of element into a linked list)
Step 1 Begin
Step 2 if the list is empty or a new element comes before the start (head) element, then insert the
new element as start element.
Step 3 else, if the new element comes after the last element, then insert the new element as the end
element.
Step 4 else, insert the new element in the list by using the find function, find function returns the
address of the found element to the insert_list function.
Step 5 End.
SEE PAGE NO 41 OF BLOCK 1
ALGORITHM (Deletion of an element from the linked list)
Step 1 Begin
Step 2 if the list is empty, then element cannot be deleted
Step 3 else, if element to be deleted is first node, then make the start (head) to point to the second
element.
Step 4 else, delete the element from the list by calling find function and returning the found address of
the element.
Step 5 End
SEE PAGE NO 43 OF BLOCK 1
malloc(sizeof(ELEMENT))
Step 4 read the value for head->data head->left = NULL head->right = (ELEMENT *) malloc(size of
(ELEMENT))
Step 5 repeat step3 to create required number of elements
Step 6 end
/IGNOUASSIGNMENTGURU
BCA MCA BA MA BDP B.COM M.COM B.SC
http://www.ignouassignmentguru.com
Q6. What is Circularly Linked Lists Implementation.
Ans. A linked list in which the last element points to the first element is called CIRCULAR linked list. The
chains do not indicate first or last element; last element does not contain the NULL pointer. The
external pointer provides a reference to starting element.
The possible operations on a circular linked list are: • Insertion, • Deletion, and • Traversing
ALGORITHM (Insertion of an element into a Circular Linked List)
Step 1 Begin
Step 2 if the list is empty or new element comes before the start (head) element, then insert the new
element as start element.
Step 3 else, if the new element comes after the last element, then insert the new element at the end
element and adjust the pointer of last element to the start element.
Step 4 else, insert the new element in the list by using the find function. find function returns the
address of the found element to the insert_list function.
Step 5 End.
be added or removed only from the top of the stack. It concludes that the item added last will be the
item removed first. Therefore, stacks are also called LIFO (Last In First Out) or FILO (First In Last Out)
lists. We also call these lists as “piles” or “push-down list”.
Generally, two operations are associated with the stacks named Push & Pop.
• Push is an operation used to insert an element at the top.
• Pop is an operation used to delete an element from the top
/IGNOUASSIGNMENTGURU
BCA MCA BA MA BDP B.COM M.COM B.SC
http://www.ignouassignmentguru.com
4
IGNOU ASSIGNMENT GURU Page-
/IGNOUASSIGNMENTGURU
BCA MCA BA MA BDP B.COM M.COM B.SC
http://www.ignouassignmentguru.com
Q9 What is a Circular Queue ? Write an algorithm for insertion and deletion in a Circular Queue.
Ans Circular Queue is a linear data structure in which the operations are performed based on FIFO
(First In First Out) principle and the last position is connected back to the first position to make a
circle.
In circular queue the last node is connected back to the first node to make a circle.
Circular linked list fallow the First In First Out principle
Elements are added at the rear end and the elements are deleted at front end of
the queue
Both the front and the rear pointers points to the beginning of the array.
It is also called as “Ring buffer”.
Items can inserted and deleted from a queue in O(1) time.
Algorithm for Addition of an element to the circular queue:
Step-1: If “rear” of the queue is pointing to the last position then go to step-2 or else Step-
3
Step-2: make the “rear” value as 0
Step-3: increment the “rear” value by one
Step-4: a. if the “front” points where “rear” is pointing and the queue holds a not NULL
value for it, then its a “queue overflow” state, so quit; else go to step-b
b. add the new value for the queue position pointed by the "rear"
Algorithm for deletion of an element from the circular queue:
Step-1: If the queue is empty then say “queue is empty” and quit; else continue
Step-2: Delete the “front” element
Step-3: If the “front” is pointing to the last position of the queue then go to
step-4 else go to step-5
Step-4: Make the “front” point to the first position in the queue and quit
Step-5: Increment the “front” position by one
Q10Write the recursive algorithm for various tree traversals
Ans. Traversal is a process to visit all the nodes of a tree and may print their values too. Because, all
nodes are connected via edges (links) we always start from the root (head) node. That is, we cannot
randomly access a node in a tree. There are three ways which we use to traverse a tree −
In-order Traversal
Pre-order Traversal
Post-order Traversal
Generally, we traverse a tree to search or locate a given item or key in the tree or to print all the
5
values it contains.
IGNOU ASSIGNMENT GURU Page-
/IGNOUASSIGNMENTGURU
BCA MCA BA MA BDP B.COM M.COM B.SC
http://www.ignouassignmentguru.com
Block 3
Q12. Compare and contrast linear search and binary search.
Ans. A linear search scans one item at a time, without jumping to any item .
1. The worst case complexity is O(n), sometimes known an O(n) search
2. Time taken to search elements keep increasing as the number of elements are increased.
A binary search however, cut down your search to half as soon as you find middle of
a sorted list.
1. The middle element is looked to check if it is greater than or less than the value to be searched.
2. Accordingly, search is done to either half of the given list
Important Differences
Input data needs to be sorted in Binary Search and not in Linear Search
Linear search does the sequential access whereas Binary search access data randomly.
Time complexity of linear search -O(n) , Binary search has time complexity O(log n).
Linear search performs equality comparisons and Binary search performs ordering
comparisons
Let us look at an example to compare the two:
6
IGNOU ASSIGNMENT GURU Page-
Linear Search to find the element “J” in a given sorted list from A-X
Binary Search to find the element “J” in a given sorted list from A-X
/IGNOUASSIGNMENTGURU
BCA MCA BA MA BDP B.COM M.COM B.SC
http://www.ignouassignmentguru.com
Q13 Draw a binary search tree (BST) for the input 8, 14, 23, 18, 38, 45, 56, 82, 70. Trace the algorithm
to insert the node 20 into the BST.
for the input 8, 14, 23, 18, 38, 45, 56, 82, 70
7
IGNOU ASSIGNMENT GURU Page-
Q14 Explain any two rotations performed on an AVL tree with examples.
Ans. Left Rotation
If a tree becomes unbalanced, when a node is inserted into the right subtree of the right subtree, then
we perform a single left rotation −
/IGNOUASSIGNMENTGURU
BCA MCA BA MA BDP B.COM M.COM B.SC
http://www.ignouassignmentguru.com
In our example, node A has become unbalanced as a node is inserted in the right subtree of A's right
subtree. We perform the left rotation by making A the left-subtree of B.
Right Rotation
AVL tree may become unbalanced, if a node is inserted in the left subtree of the left subtree. The tree
then needs a right rotation.
As depicted, the unbalanced node becomes the right child of its left child by performing a right rotation.
Q15 Define "AVL Tree". Write any two applications of AVL Trees.
Ans. In computer science, an AVL tree is a self-balancing binary search tree. It was the first
such data structure to be invented. In an AVL tree, the heights of the two child subtrees of any
node differ by at most one; if at any time they differ by more than one, rebalancing is done to
restore this property.
/IGNOUASSIGNMENTGURU
BCA MCA BA MA BDP B.COM M.COM B.SC
http://www.ignouassignmentguru.com
else Disk – Read (ci[x])
return B – Tree Search (Ci[x], k)
The search operation is similar to binary tree. Instead of choosing between a left and right child as in
binary tree, a B-tree search must make an n-way choice.
trees (and no edges). At each step, we add one (the cheapest one) edge so that it links two trees
together. If it forms a cycle, it would simply mean that it links two nodes that were already connected.
So, we reject it.
The steps in Kruskal’s Algorithm are as follows:
1. The forest is constructed from the graph G - with each node as a separate tree in the forest.
2. The edges are placed in a priority queue.
3. Do until we have added n-1 edges to the graph,
1. Extract the cheapest edge from the queue.
2. If it forms a cycle, then a link already exists between the concerned nodes. Hence reject it.
3. Else add it to the forest. Adding it to the forest will join two trees together.
/IGNOUASSIGNMENTGURU
BCA MCA BA MA BDP B.COM M.COM B.SC
http://www.ignouassignmentguru.com
The cost of the spanning tree is the sum of the weights of all the edges in the tree. There can be
many spanning trees. Minimum spanning tree is the spanning tree where the cost is minimum
among all the spanning trees. There also can be many minimum spanning trees.
10
IGNOU ASSIGNMENT GURU Page-
/IGNOUASSIGNMENTGURU
BCA MCA BA MA BDP B.COM M.COM B.SC
http://www.ignouassignmentguru.com
Breadth-first search (BFS) is an algorithm for traversing or searching tree or graph data structures.
It starts at the tree root (or some arbitrary node of a graph, sometimes referred to as a 'search key’)
and explores the neighbor nodes first, before moving to the next level neighbors.
DEPTH FIRST SEARCH (DFS)
Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures.
One starts at the root (selecting some arbitrary node as the root in the case of a graph) and
explores as far as possible along each branch before backtracking.
Insertion Sort
This is an in-place comparison-based sorting algorithm. Here, a sub-list is maintained which is always
sorted. For example, the lower part of an array is maintained to be sorted. An element which is to be
'insert'ed in this sorted sub-list, has to find its appropriate place and then it has to be inserted there.
Hence the name, insertion sort.
Step 1 −
If it is the first element, it is already sorted. return 1;
Step 2 −
Pick next element
Step 3 −
Compare with all elements in the sorted sub-list
Step 4 −
Shift all the elements in the sorted sub-list that is greater than the
value to be sorted
Step 5 − Insert the value
Step 6 − Repeat until list is sorted
11
Bubble Sort
IGNOU ASSIGNMENT GURU Page-
In this sorting algorithm, multiple swappings take place in one pass. Smaller elements move or ‘bubble’
up to the top of the list, hence the name given to the algorithm.
Algorithm:
1. Begin
2. Read the n elements
3. for i=1 to n
for j=n downto i+1
if a[j] <= a[j-1]
swap(a[j],a[j-1])
4.End // of Bubble Sort
Quick Sort
/IGNOUASSIGNMENTGURU
BCA MCA BA MA BDP B.COM M.COM B.SC
http://www.ignouassignmentguru.com
Quick sort is a highly efficient sorting algorithm and is based on partitioning of array of data into smaller arrays. A
large array is partitioned into two arrays one of which holds values smaller than the specified value, say pivot,
based on which the partition is made and another array holds values greater than the pivot value.
AA tree
An AA tree in computer science is a form of balanced tree used for storing and retrieving ordered
data efficiently. AA trees are named for Arne Andersson, their inventor.
Q20 What is a red-black tree? Explain the properties of a red-black tree with an example.
Ans. A Red-Black Tree (RBT) is a type of Binary Search tree with one extra bit of storage per node, i.e. its
color which can either be red or black. Now the nodes can have any of the color (red, black) from root
to a leaf node. These trees are such that they guarantee O(log n) time in the worst case for searching.
Properties of a Red-Black Tree
Any binary search tree should contain following properties to be called as a red-black tree.
1. Each node of a tree should be either red or black.
/IGNOUASSIGNMENTGURU
BCA MCA BA MA BDP B.COM M.COM B.SC
http://www.ignouassignmentguru.com
2. The root node is always black.
3. If a node is red, then its children should be black.
4. For every node, all the paths from a node to its leaves contain the same number of black nodes.
We define the number of black nodes on any path from but not including a node x down to a leaf, the
black height of the node is denoted by bh (x).
13
IGNOU ASSIGNMENT GURU Page-
/IGNOUASSIGNMENTGURU