Theory Paper: Arid Agriculture University, Rawalpindi
Theory Paper: Arid Agriculture University, Rawalpindi
Theory Paper
Question 1.
Answer: (a)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
if (!stk)
return NULL;
stk->t = -1;
stk->size = size;
return stk;
}
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
while (!isVaccant(stk))
input[++m] = POP(stk);
input[++m] = '\0';
printf("Postfix result : %s", input); // Printing the result
}
// Driver Function
int main()
{
char input[] = "3-2*4+1-2*(5/6)+8^9"; // Input string as provided in question
printf("Input Infix Expression : %s\n", input);
convertInfToPost(input); // Passing input string to function to convert to postfix expression
return 0;
}
Answer (b)
#include <iostream>
#define SIZE 5
public:
Queue() {
front = -1;
rear = -1;
}
if (front == rear + 1)
{
return true;
}
return false;
}
// Append an element
void Enqueue(int element) {
if (isFull())
{
cout<<" Queue Is Full";
}
else
{
if (front == -1) front = 0;
rear = (rear + 1) % SIZE;
items[rear] = element;
cout <<"\n"<<"Inserted "<<element<<"\n";
}
}
// Withdrawn an element
int Dequeue()
{
int element;
if (isEmpty())
{
cout<<"Queue Is Empty"<<"\n";
return (-1);
}
else
{
element = items[front];
if (front == rear)
{
front = -1;
rear = -1;
}
void display() {
// Display function of Queue
int i;
if (isEmpty()) {
cout << endl
<< "Queue Is Empty" << endl;
} else {
cout <<"Front -> " << front;
cout << endl
<< "Items -: ";
for (i = front; i != rear; i = (i + 1) % SIZE)
cout << items[i];
cout << items[i];
cout << "\n"<< "Rear -: " <<rear;
}
}
};
int main() {
/* K-2*L+C-D*(E/F)+G^6
Queue a;
a.Dequeue();
a.Enqueue(3);
a.Enqueue(2);
a.Enqueue(1);
a.Enqueue(2);
a.Enqueue(5);
a.Enqueue(6);
a.display();
int elem = a.Dequeue();
if (elem != -1)
cout << "\n"<< "Dequeue Element -: " << elem;
a.display();
a.Enqueue(8);
a.display();
a.Enqueue(8);
return 0;
}
Question 2
Answer: (i)
Tree Traversals
In Order Traversal - The parent node is traversed between left and right subtree
2 10 12 23 27 32 36 39 45 68 76 89 98 115 123
Pre Order Traversal - The parent node is traversed before left and right subtree
68 45 36 23 12 10 2 27 32 39 76 89 115 98 123
Post Order Traversal - The parent node is traversed after left and right subtree
2 10 12 32 27 23 39 36 45 98 123 115 89 76 68
Level Order Traversal - The nodes at each level are traversed from left to right
68 45 76 36 89 23 39 115 12 27 98 123 10 32 2
(ii)
The level-3 contains 3 nodes with values 23, 39, and 115.
The level-4 contains 4 nodes with values 12, 27, 98 and 123
(iii)
The degree of a tree = highest degree of a node.
Degree of a tree is 2
Depth of a tree = count of edges from the root node to the furthest leaf node
(iv)
The terminal node will be 2, 32, 39, 98, 123
if(root->data<data){
root->right = insertNode(root->right, data);
}
else if(root->data>data){
root->left = insertNode(root->left, data);
}
return root;
}
//driver program to check the correct working of above class and its functions
int main(){
//creating object of BST class
BST bst;
//inserting 68 ,45, 36, 76, 23, 89, 115, 98, 39, 27, 32, 12, 10, 2, 123 into the BST
cout<<"After inserting 68 ,45, 36, 76, 23, 89, 115, 98, 39, 27, 32, 12, 10, 2, 123 into the bst:";
//call to insertNode function
bst.insertNode(68);
bst.insertNode(45);
bst.insertNode(36);
bst.insertNode(76);
bst.insertNode(23);
bst.insertNode(89);
bst.insertNode(115);
bst.insertNode(98);
bst.insertNode(39);
bst.insertNode(27);
bst.insertNode(32);
bst.insertNode(12);
bst.insertNode(10);
bst.insertNode(2);
bst.insertNode(123);
L Rotation:
LR Rotation:
R Rotation:
R Rotation:
Index 0 1 2 3 4 5 6 7 8 9 10
Elemen 21 32 41 50 54 65 74 77 78 87 90
t
Index 11 12 13 14
Element 90 92 96 132
Maximum heapsort:
Insertion: The elements are inserted from the left to the right in this process. The heap parent
property is then added, which implies that the node's root is larger than its child node.
Deletion: In this operation, the root element is swapped with the last node, then removed from the
heap and placed into the last index array. Until all elements are removed from the heap, this process
will continue.
Question 4
Answer:
Question 5.
Answer:
Start from A Goal = f
DFS
Start from A Goal = f