DSU - Solution CT2
DSU - Solution CT2
Q. QUESTION MARK
NO. S
Q.1) Attempt any four of the following Marks
10
(5*2)
2
a) Evaluate prefix expression + - 2 7 * 8 / 12 4
Ans- convert the expression into postfix:- 4 12 / 8 * 7 2 - +
Token Operand1 Operand2 output Stack
4 4
12 4,12
/ 12 4 12/4=3 3
8 3,8
* 8 3 8*3=24 24
7 24,7
2 24,7,2
- 2 7 2-7= -5 24,-5
+ -5 24 19
Result of the above prefix expression evaluation = 19.
2
b) Write a program of factorial using Recursion.
Ans- Program –
#include<stdio.h>
#include<conio.h>
Int fact (int n)
{
If(n==0)
return 1;
else
return (n*fact(n-1));
}
Void main()
{
Int n;
Clrscr();
Printf(“enter a number: “);
Scanf(“%d”, &n);
Printf (“\n The factorial of % d = %d”, n, fact(n));
Getch();
}
c) Draw and explain circular queue. 2
Ans:-
- A circular queue is a type of data structure that follows the First-In-FirstOut (FIFO) principle, where
elements are added and removed in a circular manner.
- In circular queue, the elements are not only physically arranged in the circular manner but logically
they are treated as circularly arranged.
- In circular queue, the Last position of queue is connected to first position hence it is called as circular
queue.
- Circular queue has front and rear pointers and they move about in clockwise direction.
- The queue is implemented using an array. It performs operation such as Enqueue, Dequeue, IsFull,
IsEmpty, display, etc.
Top --->
e) Draw expressio n tree for the expression. (A-B)/((C*D) +E).
Ans- A heap is a specialized tree-based data structure that satisfies the heap property.
- The heap is complete binary i.e, all levels and completely filled accept possibly the lowest one, which is
filled form the left. 2
-A heap is a tree based data structure in which all the nodes of the tree are in a specific order each
internal node V satisfies the heap property: the key of V is smaller then or equal to the key of either of its
two childrens.
-A binary heap is a complete binary tree which satisfies the heap ordering property. -The ordering can be
one of two types:
a) Min heap property: the value of each node is greater than or equal to the value of its parent, with the
minimum value element at the root.
b) Max heap property: the value of each node is less than or equal to value of its parents with the
maximum value element at the root. -since, a heap is a complete binary tree, it has a smallest possible
hight- a heap with ‘n’ nodes always has 0(log n) height.
g) Write a C function for preorder traversal.
Ans:- C function –
void preorderTraversal(Node* root)
{
if (root == NULL)
return;
printf ("%d ", root->data);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
Q.2) Attempt any five of the following. Marks
20
(5*4)
a) Convert infix to post fix. (A*B+C)/(D+E*F/G)
Token Stack Queue
( ( Empty
A ( A
* (* A
B (* AB
+ (+ AB*
C (+ AB*C
) Empty AB*C+
/ / AB+C+
( /( AB*C+
D /( AB*C+D 4
+ /(+ AB*C+D
E /(+ AB*C+DE
* /(+* AB*C+DE
F /(+* AB*C+DEF
/ /(+/ AB*C+DEF*
G /(+/ AB*C+DEF*G
) / AB*C+DEF*G/+
Empty Empty AB*C+DEF*G/+/
Code:
Void insert (int no)
{
If(rear>=size-1)
{
Printf(“\n Queue is full.”);
}
Else
{
Printf(“Enter no to insert into Queue”);
Scanf(“%d”, &no);
rear++;
q[rear]=no;
if(front==-1)
front++;
}
4
Write an algorithm and snippet code to delete an element into circular queue.
e)
Ans-
Algorithm:
1.Start.
2.Check if queue is empty or not , if yes then print circular queue is empty.
3.Else assign front value to n i.e. n=queue[front].
4.Check if(front==rear). If yes then assign front= rear= -1.
5.Else increment front pointer as front=(front+1)%size.
6.Display deleted element.
7.Stop.
Code:
Void delete ()
{
Int n;
If ( qempty() )
{
Printf(“\n circular queue is empty”);
}
Else
{ 4
n=queue[front];
if(front==rear)
{
front=rear=-1; }
else
{
front=(front+1)%size;
}
printf(“\n deleted element is %d”, n);
}
}
2. Heapify:
a) compare 7 with its child: