BCS304 Model Question Paper-I With Effect From 2023-24 (CBCS Scheme)
BCS304 Model Question Paper-I With Effect From 2023-24 (CBCS Scheme)
Note: 01. Answer any FIVE full questions, choosing at least ONE question from each MODULE.
*Bloom’s
Module -1 Taxonomy Marks
Level
Q.01 a Define data structures. With a neat diagram, explain the classification of
L2 5
data structures with examples.
b What do you mean by pattern matching? Outline the Knuth Morris Pratt
(KMP) algorithm and illustrate it to find the occurrences of the following
pattern. L3 8
P: ABCDABD
S: ABC ABCDAB ABCDABCDABDE
c Write a program in C to implement push, pop and display operations for
L3 7
stacks using arrays.
OR
Q.02 a Explain in brief the different functions of dynamic memory allocation. L2 5
b Write functions in C for the following operations without using built-in
functions
i) Compare two strings. L3 8
ii) Concatenate two strings.
iii) Reverse a string
c Write a function to evaluate the postfix expression.
Illustrate the same for the given postfix expression: L3 7
ABC-D*+E$F+ and assume A=6, B=3, C=2, D=5, E=1 and F=7.
Module-2
Q. 03 a Develop a C program to implement insertion, deletion and display
L3 10
operations on Linear queue.
b Write a program in C to implement a stack of integers using a singly
L3 10
linked list.
OR
Q.04 a Write a C program to implement insertion, deletion and display operations
L3 10
on a circular queue.
b Write the C function to add two polynomials. Show the linked
representation of the below two polynomials and their addition using a
circular singly linked list
P1: 5x3 + 4x2 +7x + 3 L3 10
P2: 6x2 + 5
Output: add the above two polynomials and represent them using the
linked list.
Page 01 of 03
BCS304
Module-3
Q. 05 a Write recursive C functions for inorder, preorder and postorder traversals
of a binary tree. Also, find all the traversals for the given tree.
L3 8
L3 6
OR
Q. 06 a Write C Functions for the following
i) Inserting a node at the beginning of a Doubly linked list L3 8
Deleting a node at the end of the Doubly linked list
b Define Binary tree. Explain the representation of a binary tree with a
L2 6
suitable example.
c Define the Threaded binary tree. Construct Threaded binary for the
L3 6
following elements: A, B, C, D, E, F, G, H, I
Module-4
Q. 07 a Design an algorithm to traverse a graph using Depth First Search (DFS).
Apply DFS for the graph given below.
L3 8
b Construct a binary tree from the Post-order and In-order sequence given
below
L2 6
In-order: GDHBAEICF
Post-order: GHDBIEFCA
c Define selection tree. Construct min winner tree for the runs of a game
given below. Each run consists of values of players. Find the first 5
winners.
10 9 20 6 8 9 90 17 L2 6
15 20 20 15 15 11 95 18
16 38 30 25 50 16 99 20
28
Page 02 of 03
BCS304
OR
Q. 08 a Define Binary Search tree. Construct a binary search tree (BST) for the
following elements: 100, 85, 45, 55, 120, 20, 70, 90, 115, 65, 130, 145.
L3 8
Traverse using in-order, pre-order, and post-order traversal techniques.
Write recursive C functions for the same.
b Define Forest. Transform the given forest into a Binary tree and traverse
using inorder, preorder and postorder traversal.
L2 6
c Define the Disjoint set. Consider the tree created by the weighted union
function on the sequence of unions: union(0,1), union(2,3), union(4,5),
L2 6
union(6,7), union(0,2), union(4,6), and union(0,4). Process the simple
find and collapsing find on eight finds and compare which find is efficient.
Module-5
Q. 09 a What is chained hashing? Discuss its pros and cons. Construct the hash
table to insert the keys: 7, 24, 18, 52, 36, 54, 11, 23 in a chained hash table L3 10
of 9 memory locations. Use h(k) = k mod m.
b Define the leftist tree. Give its declaration in C. Check whether the given
binary tree is a leftist tree or not. Explain your answer.
L2 5
L2 5
Page 03 of 03
1a) Define data structures. With a neat diagram, explain the classification of data
structures with examples.
Data Structure: It can be defined as a method of storing and organizing the data items in the
computer’s memory.
Mainly deal with
✓Storing data in memory
✓Organizing data in memory
✓Fetching and processing data
Primitive data structure The data structures, that are directly operated upon by machine
level instructions i.e. the fundamental data types such as ➢ int, ➢float, ➢ char >double.
A data structure that cannot be manipulated directly by machine instructions are called
non-premitive data structure . ex: arrays, stacks, queues ,lists Files, trees, graphs.
There are two types of-non primitive data structures. Linear and Non-Linear Data Structures:
In a linear data structure, the data items are arranged in a linear order or sequential.
For Example: array, Stack, lists, queue.
In a non-linear data structure, the data items that are not in sequence. For Example: trees and
graphs.
Stacks A stack is a linear data structure in which an element may be inserted or deleted only
at one end called the top end of the stack
Queue is a linear data structure in which insertion can take place at only one end called rear
end and deletion can take place at other end called front end
A linked list is a data structure which is collection of zero or more nodes with each node
consisting of two field’s data and link
A tree is a nonlinear data structure and is generally defined as a nonempty finite set of
elements, called nodes.
A graph normally a combination of the set of vertices V and set of edges E. G=(V,E)
1 b) what do you mean by pattern matching? Outline the Knuth Morris Pratt (KMP)
algorithm and illustrate it to find the occurrences of the following pattern.
Pattern: ABCDABD
String: ABC ABCDAB ABCDABCDABDE
It is a fundamental problem in computer science where the goal is to find all occurrences of a
given pattern within a larger text or string.
The Knuth-Morris-Pratt (KMP) algorithm is a widely used pattern matching algorithm that
efficiently searches for occurrences of a pattern within a text by exploiting the information
gathered during the preprocessing phase.
KMP algorithm works in linear time O(n + m), where n is the length of the text and m is the
length of the pattern.
Search: Use the failure function to guide the search process, avoiding unnecessary
comparisons by efficiently moving through the text.
void stringmatch()
{
while(str[c] !='\0')
{
if(str[m] == pat[i])
{
i++; m++;
if(pat[i] == '\0')
{
flag = 1;
for(k=0; rep[k]!='\0'; k++, j++)
{
ans[j] = rep[k];
}
c = m;
}
}
else
{
ans[j]= str[c];
j++; c++;
m=c;
i=0;
}
1c)Write a program in C to implement push, pop and display operations for stacks using arrays.
#include<stdio.h>
int stk[10], ch, n, top=-1, item, i;
void push()
{
if(top>=n-1)
{
printf("STACK is over flow\n");
}
else
{
printf(" Enter a value to be pushed:");
scanf("%d", &item);
top++;
stk[top]=item;
printf(" pushed successfully\n:"); } }
void pop()
{
if(top == -1)
{
printf("Stack is under flow\n");
}
else
{
printf("The popped elements is %d\n", stk[top]);
top--;
}
}
void display()
{
if(top == -1)
{
printf(" STACK is empty \n");
}
else
{
printf("The elements in STACK \n");
for(i=top; i>=0; i--)
printf("%d\n" ,stk[i]);
}
}
void main()
{
printf("Enter the size of STACK\n");
scanf("%d", &n);
for(;;)
{
printf("1.PUSH 2.POP 3.DISPLAY \n");
printf("Enter the Choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1: push(); break;
case 2: pop(); break;
case 3: display(); break;
}
}
}
2 a) Explain in brief the different functions of dynamic memory allocation
Dynamic memory allocation is the process of allocating memory during run time(execution
time).
Additional storage can be allocated whenever needed.
1. malloc( ):
Allocates requested number of bytes and returns a pointer to the first byte of
the allocated space.
Syntax:
ptr=(datatype *)malloc(sizeof(datatype);
where,
ptr is a pointer variable of type datatype
datatpe can be any of the basic datatype or user define datatype
Size is number of bytes required.
Example:
int *p;
ptr=(int*)malloc(100*sizeof(int));
int *p;
ptr=(int*)calloc(100,sizeof(int));
realloc()
It changes the size of block by deleting or extending the memory at end of the
block.
If memory is not available it gives complete new block.
Syntax:
ptr=(datatype *)realloc (ptr , sizeof(datatype);
where,
ptr is a pointer to a block previously allocated memory either using malloc()
or calloc()
Size is new size of the block.
int *p;
ptr=(int*)calloc(100,sizeof(int));
ptr=(int*)realloc(ptr,sizeof(int));
free()
This function is used to de-allocate(or free) the allocated block of memory
which is allocated by using functions malloc(), calloc(), realloc().
Syntax:
free(ptr);
2b)Write functions in C for the following operations without using built-in functions
i)Compare two strings. ii) Concatenate two strings. iii) Reverse a string
return 1;
}
#include<stdio.h>
float compute(char symbol, float op1, float op2)
{
switch (symbol)
{
case '+': return op1 + op2;
case '-': return op1 - op2;
case '*': return op1 * op2;
case '/': return op1 / op2; ABC-D*+E$F+
case '$':
case '^': return pow(op1,op2); 632-5*+1$7+
}
Ans : 8
}
void main()
{
float s[20], res, op1, op2; Refer class notes for solving the steps you need to write
int top, i; the steps also
char postfix[20], symbol;
printf("\nEnter the postfix expression:\n");
scanf ("%s", postfix);
top=-1;
for (i=0; i<strlen(postfix) ;i++)
{
symbol = postfix[i];
if(isdigit(symbol))
s[++top]=symbol - '0';
else
{
op2 = s[top--];
op1 = s[top--];
res = compute(symbol, op1, op2);
s[++top] = res;
}
}
res = s[top--];
printf("\nThe result is : %f\n", res);
}
3a Develop a C program to implement insertion, deletion and display operations on Linear queue
#include<stdio.h>
int q[10], ch, size, front=-1, rear=-1item, i;
void enqueue()
{
if(rear == size - 1)
printf("Queue Overflow\n");
else
{
if(front== - 1)
front = 0;
printf("Inset the element in queue\n: ");
scanf("%d", &item);
q[rear++] = item;
}
}
void dequeue()
{
if(front== -1 || front>rear)
{
printf("Queue Underflow\n");
}
else
{
printf("Element deleted from queue is : %d\n", q[front]);
front++;
}
}
void display()
{
if(front== -1 || front>rear)
{
printf("Queue is empty\n");
}
else
{
printf("Queue is :\n");
for(i = front; i <= rear; i++)
printf("%d\n", q[i]);
}
}
void main()
{
printf("Enter the size of STACK\n");
scanf("%d", &size);
for(;;)
{
printf("1.insert 2.delete 3.DISPLAY \n");
printf("Enter the Choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1: enqueue(); break;
case 2: dequeue();break;
case 3: display(); break;
}
}
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
int data ;
struct node* next;
}*tmp,*ptr,*head=NULL;
void display()
{
if(head==NULL)
{
printf("List is empty!!");
}
else
{
tmp=head;
int c=0;
while (tmp!=NULL)
{
printf("%d\n tmp->data);
tmp=tmp->next;
c++;
}
printf("THE NO. OF NODES ARE %d\n",c);
}
}
void main()
{
int ch ;
for(;;)
{
printf("Enter your choice\n");
printf("1-create 2-display and count 3-Insert front\n 4.insert end \n 5.Delete front\n6.deletion
from end \n");
scanf("%d",&ch);
switch(ch)
{
case 1:create(); break;
case 2: display(); break;
case 3: insertfront(); break;
case 4: insertend(); break;
case 5: delfront(); break;
case 6: delend(); break;
}
}
}
4 a Write a C program to implement insertion, deletion and display
operations on a circular queue ( dsa lab pgm 6th)
#include <stdio.h>
int cq[10], front = -1, rear = -1,item,ch,i,size;
void enQueue()
{
if(front ==(rear+1)%SIZE)
{
printf("C Q IS OVERFLOW\n");
}
else
{
rintf("enter element\n");
scanf("%d",&item);
if (front == -1)
front = 0;
rear = (rear + 1) % SIZE;
cq[rear] = item;
printf("Inserted\n");
}
}
void deQueue()
{
if(front == -1)
{
printf("CIRCULAR QUEUE IS UNDERFLOW\n");
}
else
{
printf("\n Deleted element -> %d \n", cq[front]);
if (front == rear)
front = rear = -1;
else
5a Write recursive C functions for inorder, preorder and postorder traversals of a binary tree.
Also, find all the traversals for the given tree.
5 b) Write C functions for the following i) Search an element in the singly linked list. ii)
Concatenation of two singly linked list
1.Array representation
2. Linked representation
Array representation:
A tree can be represented using an array, which is called sequential representation.
The nodes are numbered from 1 to n, and one dimensional array can be used to store the
nodes.
Position 0 of this array is left empty and the node numbered i is mapped to position i of the
array.
In a threaded binary tree, each node has an additional pointer called a thread, which points
to either its in-order predecessor or successor. This allows us to efficiently traverse the tree
without using recursion or a stack.
Depth First Search: Depth First Search (DFS) algorithm traverses a graph in a
depth ward motion and uses a stack to remember to get the next vertex to start a
search
DFS Algorithm :
Step 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Push
it in a stack.
Step 2 − If no adjacent vertex is found, pop up a vertex from the stack. (which
do not have adjacent vertices.)
Step 3 − Repeat Rule 1 and Rule 2 until the stack is empty
7c Define selection tree. Construct min winner tree for the runs of a game
given below. Each run consists of values of players. Find the first 5 winners
SELECTION TREE
• This is also called as a tournament tree. This is such a tree data structure using which the
winner (or loser) of a knock out tournament can be selected.
• There are two types of selection trees namely: winner tree and loser tree.
WINNER TREE
• This is a complete binary tree in which each node represents the smaller of its two children.
Thus, the root node represents the smallest node in the tree.
6 ,8
6 ,8,9 6 ,8,9,10
6 ,8,9,10,11
Recursive C functions
A set is a collection of elements. Disjoint sets are such sets in which common
elements are not present. Examples:
Algorithm collapsing_find(i)
{
r=i
while(p[r]>0)
{
r=p[r]
}
while(i!=r)
{
s=p[i]
p[i]=r
i=s
}
return r;
}
9a What is chained hashing? Discuss its pros and cons. Construct the hash
table to insert the keys: 7, 24, 18, 52, 36, 54, 11, 23 in a chained hash table
of 9 memory locations. Use h(k) = k mod m
Chained hashing, also known as separate chaining, is a technique used to resolve collisions in
hash tables. When two or more keys hash to the same index (known as a collision), chained
hashing handles these collisions by maintaining a linked list of all elements that hash to the
same index.
Pros of Chained Hashing:
1.Simple Implementation: Chained hashing is relatively easy to implement.
2.Efficient Insertion and Deletion: Insertion and deletion operations are efficient in chained
hashing.
3.Dynamic Data Sets: Chained hashing allows for dynamic resizing of the hash table..
4.Adaptability: Chained hashing can handle a wide range of input data distributions
C declaration
struct node
{
int data;
struct node *left;
struct node *right;
int rank;
};
Definition:
A min-leftist tree (max leftist tree) is a leftist tree in which the key value in
each node is no larger (smaller) than the key values in its children.
Given :
Melding
Hint: meld means joining two leftist tree into single leftist tree
10 c) Define hashing. Explain different hashing functions with examples.
Discuss the properties of a good hash function.
A function that converts a given big number to a small integer value. The
mapped integer value is used as an index in the hash table.
x = 23, m = 10 then
H(x) = (23 % 10) = 3
The key whose value is 23 is placed in 3rd location
Midsquare Methods
In this case, we square the value of a key and take the number of digits required
to form an address, from the middle position of squared value.
Folding Method
The key is actually partitioned into number of parts, each part having the same
length as that of the required address.
This is done in two ways :
Fold‐shifting
Fold‐boundary
Fold‐shifting: Here actual values of each parts of key are added
Example:
Fold‐boundary: Here the reversed values of outer parts of key are added.
Example:
The key is : 12345678, and the required address is of two digits,
Digit Analysis
Here we make a statistical analysis of digits of the key, and select those digits
(of fixed position) which occur quite frequently.
For example, if the key is : 9861234.
Here third and fifth position digits occur quite frequently, then we choose the
digits in these positions from the key.
So we get, 62.
Reversing it we get 26 as the address