You are on page 1of 13

BCA MCA BA MA BDP B.COM M.COM B.

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-

Q3 Write an algorithm to add two polynomials using linked list.

Let p and q be the two polynomials represented by the linked list.


1. while p and q are not null, repeat step 2.
2. If powers of the two terms ate equal
then if the terms do not cancel then insert the sum of the terms into the sum Polynomial
Advance p
Advance q
Else if the power of the first polynomial> power of second
Then insert the term from first polynomial into sum polynomial
Advance p
Else insert the term from second polynomial into sum polynomial
Advance q
3. copy the remaining terms from the non empty polynomial into the
sum polynomial.

/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

Q5. What is DOUBLY linked list?


Ans. In computer science, a doubly linked list is a linked data structure that consists of a set
of sequentiallylinked records called nodes. Each node contains two fields, called links, that are
references to the previous and to the next node in the sequence of nodes.
ALGORITHM to implement Doubly Linked List.
Step 1 begin
Step 2 define a structure ELEMENT with fields Data Left pointer Right pointer
Step 3 declare a pointer by name head and by using (malloc()) memory allocation function allocate 2
space for one element and store the address in head pointer Head = (ELEMENT *)
IGNOU ASSIGNMENT GURU Page-

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.

ALGORITHM (Deletion of an element from a Circular 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.
BLOCK -02
Q7. What is Stacks? Explain the various operations of stack with an example for each operation.
Ans. A stack is a linear structure in which items may be inserted or removed only at one end called the 3
top of the stack.
IGNOU ASSIGNMENT GURU Page-

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

Q8 WRITE AN ALGORITHM TO IMPLEMENT A STACK USING ARRAYS.

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

Q11 what is binary tree.


Ans. data structure in which a record is linked to two successor records, usually referred to as
the left branch when greater and the right when less than the previous record.

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-

Q13 What is a Binary Search Tree ?


Definition. A binary search tree (BST) is a binary tree where each node has a Comparable
key (and an associated value) and satisfies the restriction that the key in any node is larger than
the keys in all nodes in that node's left subtree and smaller than the keys in all nodes in that
node's right subtree

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.

AVL trees are applied in the following situations:


• There are few insertion and deletion operations
• Short search time is needed
• Input data is sorted or nearly sorted AVL tree structures can be used in situations which require fast
searching. But, the large cost of rebalancing may limit the usefulness.

Q16 Define B-tree. Explain various operations performed in a B-tree.


8
Ans. B-Tree is a self-balanced search tree with multiple keys in every node and more than two children
IGNOU ASSIGNMENT GURU Page-

for every node.


B-trees are special m–ary balanced trees used in databases because their structure allows records to be
inserted, deleted and retrieved with guaranteed worst case performance
Operations on B-Trees The following are various operations that can be performed on B-Trees:
• Search • Create • Insert
B-Tree Search (x, k)
i<-1
while i < = n [x] and k > keyi[x]
do i ← i + 1
if i < = n [x] and k = key1 [x]
then return (x, i)
if leaf [x]
then return NIL

/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.

B-Tree Create (T)


x ← Allocate-Node ( )
Leaf [x] ← True
n [x] ← 0
Disk-write (x)
root [T] ← x
The above mentioned algorithm creates an empty B-tree by allocating a new root that has no keys and
is a leaf node.
The following is the algorithm for insertion into a B-tree:
B-Tree Insert (T,K)
r ← root (T)
if n[r] = 2t – 1
then S ← Allocate-Node ( )
root[T] ← S
leaf [S] ← FALSE
n[S] ← 0
C1 ← r
B–Tree-Split-Child (s, I, r)
B–Tree-Insert-Non full (s, k)
Else
B – Tree-Insert-Non full (r, k)
To perform an insertion on B-tree, the appropriate node for the key must be located. Next, the key must
be inserted into the node.

Q17.Explain Dijkstra's algorithm with an example.


Ans. Djikstra’s algorithm (named after its discover, Dutch computer scientist E.W. Dijkstra) solves the
problem of finding the shortest path from a point in a graph (the source) to a destination with non-
negative weight edge.
Q18 Explain Kruskal's algorithm with an example.
9
Krushkal’s algorithm uses the concept of forest of trees. Initially the forest consists of n single node
IGNOU ASSIGNMENT GURU Page-

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

What is a Minimum Spanning Tree?

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-

BREADTH FIRST SEARCH (BFS)

/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.

Step 1 − Choose the highest index value has pivot


Step 2 − Take two variables to point left and right of the list excluding pivot
Step 3 − left points to the low index
Step 4 − right points to the high
Step 5 − while value at left is less than pivot move right
Step 6 − while value at right is greater than pivot move left
Step 7 − if both step 5 and step 6 does not match swap left and right
Step 8 − if left ≥ right, the point where they met is new pivot

2-Way Merge Sort


Merge sort is also one of the ‘divide and conquer’ class of algorithms

Q19 What is a Splay Tree? How does it differ from a Tree?


Ans. A splay tree is a self-adjusting binary search tree with the additional property that recently
accessed elements are quick to access again. It performs basic operations such as insertion, look-up and
removal in O(log n) amortized time.
Does it differ from a Tree
A binary tree is simply a tree in which each node can have at most two children.
A binary search tree is a binary tree in which the nodes are assigned values, with the following restrictions ;
-No duplicate values.
-The left subtree of a node can only have values less than the node
-The right subtree of a node can only have values greater than the node 12
and recursively defined;
-The left subtree of a node is a binary search tree.
IGNOU ASSIGNMENT GURU Page-

-The right subtree of a node is a binary search tree.

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

You might also like