SE-Comps SEM3 DS-CBCGS DEC18 SOLUTION
SE-Comps SEM3 DS-CBCGS DEC18 SOLUTION
SE-Comps SEM3 DS-CBCGS DEC18 SOLUTION
(DEC 2018)
Q1
a) What are various operations possible on data structures? (05)
The data appearing in our data structure is processed by means of certain
operations. In fact, the particular data structure that once chooses for a
given situation depends largely on the frequency with which specific
operations are performed:
1. Insertion: Insertion means addition of a new data element in a data
structure.
2. Deletion: Deletion means removal of a data element from a data
structure if it is found.
3. Searching: Searching involves searching for the specified data element
in a data structure.
4. Traversal: Traversal of a data structure means processing all the data
elements present in data structure.
5. Sorting: Arranging data elements of a data structure in a specified order
is called sorting.
6. Merging: Combining elements of two similar data structures to form a
new data structure of the same type, is called merging.
0 1 2 3 4
0 0 1 0 0 1
1 1 0 1 1 1
2 0 1 0 1 0
3 0 1 1 0 1
4 1 1 0 1 0
2. Adjacency List
- An array of lists is used. Size of the array is equal to the number of
vertices.
- Let the array be array[]. An entry array[i] represents the list of
vertices adjacent to the ith vertex.
- This representation can also be used to represent a weighted graph.
- The weights of edges can be represented as lists of pairs.
- Following is adjacency list representation of the above graph.
c) Describe Tries with an example. (05)
- A trie is a tree-like data structure whose nodes store the letters of an
alphabet. By structuring the nodes in a particular way, words and strings
can be retrieved from the structure by traversing down a branch path of the
tree.
- A trie is a tree of degree P ≥ 2.
- Tries re useful for sorting words as a string of characters.
- In a trie, each path from the root to a leaf corresponds to one word.
- Root node is always null.
- To avoid confusion between words like THE and THEN, a special end
marker symbol ‘\0’ is added at the end of each word.
- Below fig shows the trie of the following words (THE, THEN, TIN, SIN,
THIN, SING)
- Most nodes of a trie has at most 27 children one for each letter and for
'\0'
- Most nodes will have fewer than 27 children.
- A leaf node reached by an edge labelled '\0' cannot have any children and
it need not be there.
#include<stdio.h>
#include<string.h>
#define MAX 20
#define true 1
#define false 0
/*Begin of push*/
char push(char item)
{
if(top == (MAX-1))
printf("Stack Overflow\n");
else
{
top=top+1;
stack[top] = item;
}
}
/*Begin of pop*/
char pop()
{
if(top == -1)
printf("Stack Underflow\n");
else
return(stack[top--]);
}
main()
{
char exp[MAX],temp;
int i,valid=true;
printf("Enter an algebraic expression : ");
gets(exp);
for(i=0;i<strlen(exp);i++)
{
if(exp[i]=='(' || exp[i]=='{' || exp[i]=='[')
push( exp[i] );
if(exp[i]==')' || exp[i]=='}' || exp[i]==']')
if(top == -1) /* stack empty */
valid=false;
else
{
temp=pop();
if( exp[i]==')' && (temp=='{' || temp=='[') )
valid=false;
if( exp[i]=='}' && (temp=='(' || temp=='[') )
valid=false;
if( exp[i]==']' && (temp=='(' || temp=='{') )
valid=false;
}
}
if(top>=0) /*stack not empty*/
valid=false;
if( valid==true )
printf("Valid expression\n");
else
printf("Invalid expression\n");
}
Output:
Enter an algebraic expression: {([])}
Valid expression
Enter an algebraic expression: (){}
Valid expression
Enter an algebraic expression: (){[[[)
Invalid expression
b) Give the frequency for the following symbols, compute the Huffman code
for each symbol. (10)
Symbol A B C D E
Frequency 24 12 10 8 8
Huffman Code:
- Huffman code is an application of binary trees with minimum weighted
external path length is to obtain an optimal set for messages M1, M2, …Mn
- Message is converted into a binary string.
- Huffman code is used in encoding that is encrypting or compressing the
text in the WSSS communication system.
- It use patterns of zeros and ones in communication system these are used
at sending and receiving end.
- suppose there are n standard message M1, M2, ……Mn. Then the
frequency of each message is considered, that is message with highest
frequency is given priority for the encoding.
- The tree is called encoding tree and is present at the sending end.
- The decoding tree is present at the receiving end which decodes the string
to get corresponding message.
- The cost of decoding is directly proportional to the number of bits in the
transmitted code is equal to distance of external node from the root in the
tree.
- Example
Symbol A B C D E
Frequency 24 12 10 8 8
Huffman code
A=0
B = 111
C = 110
D = 100
E = 101
Q3
a) Write a C program to implement priority queue using arrays. The
program should perform the following operations (12)
i) Inserting in a priority queue
ii) Deletion from a queue
iii) Displaying contents of the queue
Program:
#include <stdio.h>
#include <stdlib.h>
#define MAX 30
void main()
{
int x,op,n,i;
pqueue q;
initialize(&q);
do
{
printf("\n1)Create \n2)Insert \n3)Delete \n4)Print \n5)EXIT");
printf("\nEnter Choice: ");
scanf("%d",&op);
switch (op) {
case 1: printf("\nEnter Number of Elements");
scanf("%d",&n );
initialize(&q);
printf("Enter the data");
case 3: if(empty(&q))
{
printf("\nQueue is empty..");
exit(0);
}
x=dequeue(&q);
printf("\nDeleted Element=%d",x);
break;
case 4: display(&q);
break;
default: break;
}
}while (op!=5);
}
return(0);
}
return(0);
}
while(x>p->data[i])
{
p->data[(i+1)%MAX]=p->data[i];
i=(i-1+MAX)%MAX; //anticlockwise movement inside the
queue
if((i+1)%MAX==p->front)
break;
}
//insert x
i=(i+1)%MAX;
p->data[i]=x;
//re-adjust rear
p->rear=(p->rear+1)%MAX;
}
}
}
if(empty(p))
{
printf("\nUnderflow..");
}
else
{
x=p->data[p->front];
if(p->rear==p->front) //delete the last element
initialize(p);
else
p->front=(p->front +1)%MAX;
}
return(x);
}
if(empty(p))
{
printf("\nQueue is empty..");
}
else
{
i=p->front;
while(i!=p->rear)
{
x=p->data[i];
printf("\n%d",x);
i=(i+1)%MAX;
}
1)Create
2)Insert
3)Delete
4)Display
5)EXIT
Enter Choice: 4
12
9
6
4
1)Create
2)Insert
3)Delete
4)Display
5)EXIT
Enter Choice: 3
Deleted Element=12
1)Create
2)Insert
3)Delete
4)Display
5)EXIT
Enter Choice: 5
b) What are expression trees? What are its advantages? Derive the
expression tree for the following algebraic expression (08)
(a + (b/c)) * ((d/e) - f)
Expression Tree: Compilers need to generate assembly code in which one
operation is executed at a time and the result is retained for other
operations.
- Therefore, all expression has to be broken down unambiguously into
separate operations and put into their proper order.
- Hence, expression tree is useful which imposes an order on the
execution of operations.
- Expression tree is a binary tree in which each internal node corresponds
to operator and each leaf node corresponds to operand
- Parentheses do not appear in expresion trees, but their intent remains
intact in tree representation.
Construction of Expression Tree:
Now for constructing expression tree we use a stack. We loop through
input expression and do following for every character.
1) If character is operand push that into stack
2) If character is operator pop two values from stack make them its child
and push current node again.
At the end only element of stack will be root of expression tree.
Advantage:
1. Expression trees are using widely in LINQ to SQL, Entity Framework
extensions where the runtime needs to interpret the expression in a
different way (LINQ to SQL and EF: to create SQL, MVC: to
determine the selected property or field).
2. Expression trees allow you to build code dynamically at runtime
instead of statically typing it in the IDE and using a compiler.
Q4
a) Write a C program to represent and add two polynomials using linked
list. (12)
Program:
#include<stdio.h>
#include<stdlib.h>
if(NULL == *head)
{
*head = temp;
(*head)->next = NULL;
}
else
{
while(NULL != ptr->next)
{
ptr = ptr->next;
}
ptr->next = temp;
temp->next = NULL;
}
}
while(poly1->next || poly2->next)
{
if(poly1->next)
{
poly->coef = poly1->coef;
poly->exp= poly1->exp;
poly1 = poly1->next;
}
if(poly2->next)
{
poly->coef = poly2->coef;
poly->exp= poly2->exp;
poly2 = poly2->next;
}
poly->next = (node*)malloc(sizeof(node));
poly = poly->next;
poly->next = NULL;
}
}
int main()
{
node* head1 = NULL;
node* head2 = NULL;
node* head = (node*)malloc(sizeof(node));
int ch;
do
{
get_input(&head1);
printf("\nEnter more node in poly1? (1,0) :");
scanf("%d",&ch);
}while(ch);
do
{
get_input(&head2);
printf("\nEnter more node in poly2? (1,0) :");
scanf("%d",&ch);
}while(ch);
add(head,head1,head2);
display(head1);
display(head2);
display(head);
return 0;
}
Output:
Enter coef: 6
Enter exp: 2
Enter more node in poly1? (1/0): 1
Enter coef: 4
Enter exp: 1
Enter more node in poly1? (1/0): 1
Enter coef: 2
Enter exp: 0
Enter more node in poly? (1/0): 0
Enter coef: 4
Enter exp: 2
Enter more node in poly2? (1/0): 1
Enter coef: 2
Enter exp: 1
Enter more node in poly2? (1/0): 1
Enter coef: 3
Enter exp: 0
Enter more node in poly2? (1/0): 0
(6.x^2)+(4.x^1)+(2.x^0)
(4.x^2)+(2.x^1)+(3.x^0)
(10.x^2)+(6.x^1)+(5.x^0)
b) How does the Quicksort technique work? Give C function for the same.
(08)
Quick Sort:
- Quick sort is the fastest internal sorting algorithm with the time and space
complexity = O(n log n).
- The basic algorithm to sort an array a[] of n elements can be describe
recursively as follows:
1. If n <= 1, then return
2. Pick any element V in array a[]. This element is called as pivot.
Rearrange elements of the array by moving all elements xᵢ > V right of
V and all elements xᵢ ≤ V left of V. if the place of the V after re-
arrangement is j, all elements with value less than V, appear in a[0], a[1]
… a[j-1] and all those with value greater than V appear in a[j+1] … a[n-1]
3. Apply quick sort recursively to a[0] … a[j-1] and to a a[j+1] … a[n-1]
Entire array will thus be sorted by as selecting an element V.
a) Partitioning the array around V.
b) Recursively, sorting the left partition.
c) Recursively, sorting the right partition.
C function for partition
int partition(int a[], int l, int u)
{
int v,i,j,temp;
v=a[l];
i=l;
j=u+1;
do;
{
do
i++;
while(a[i]<v && i<=u);
do
j--;
while(v<a[j]);
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}while(i<j);
a[l]=a[j];
a[j]=v;
return(j);
}
Structure of doubly link list will contain three fields LeftPointer (prev),
RightPointer (next), and the data as shown below
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
b) Given the postorder and inorder traversal of a binary tree, construct the
original tree: (10)
Postorder: D E F B G L J K H C A
Inorder: D B F E A G C L J H K
Construction of Tree:
Step1: Select last element from the postorder as root node. So element A
becomes root node. Divide the inorder into left and right with respect to
root node A.
Original Tree
Address of A [ I ] = B + W * ( I – LB )
Where, B = Base address 1600 (given)
W = Storage Size of one element stored in the array (in byte) = 4
I = Subscript of element whose address is to be found = 5 (given)
LB = Lower limit / Lower Bound of subscript, if not specified assume 0
Address of A [ 5 ] = 1600 + 4 * ( 5 – 0 )
= 1600 + 4 * 5
= 1600 + 20
A [5] = 1620
One can verify it from table too A[5] has element 75 stored at address
1620.
void BFS(int V)
{
q : a queue type variable;
initialize q;
visited[v] = 1; // mark v as visited
add the vertex V to queue q;
while(q is not empty)
{
v delete an element from the queue;
for all vertices w adjacent from V
{
if(!visited[w])
{
visited[w] = 1;
add the vertex w to queue q;
}
}
}
}
Example:
V6 V7 V8 V1 V2 V3 V4 V5 V6 Delete (q)
V7 V8
V8 V1 V2 V3 V4 V5 V6 Delete (q)
V7 V8
#include<stdio.h>
#include<conio.h>
#include<process.h>
struct node
{
int data;
struct node *next;
}*start=NULL,*q,*t;
int main()
{
int ch;
void insert_end();
int delete_pos();
void display();
while(1)
{
printf("\n\n---- Singly Linked List(SLL) Menu ----");
printf("\n1.Insert at end \n2.Delete specific node \n 3.Display
\n4.Exit\n\n");
printf("Enter your choice(1-4):");
scanf("%d",&ch);
switch(ch)
{
case 1: insert_end();
break;
case 2: delete_pos();
break;
case 3: display();
break;
case 4: exit(0);
default:
printf("Wrong Choice!!");
}
}
return 0;
}
void insert_end()
{
int num;
t=(struct node*)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&num);
t->data=num;
t->next=NULL;
int delete_pos()
{
int pos,i;
if(start==NULL)
{
printf("List is empty!!");
return 0;
}
q=start;
for(i=1;i<pos-1;i++)
{
if(q->next==NULL)
{
printf("There are less elements!!");
return 0;
}
q=q->next;
}
t=q->next;
q->next=t->next;
printf("Deleted element is %d",t->data);
free(t);
return 0;
}
void display()
{
if(start==NULL)
{
printf("List is empty!!");
}
else
{
q=start;
printf("The linked list is:\n");
while(q!=NULL)
{
printf("%d->",q->data);
q=q->next;
}
}
}
Output:
**********