0% found this document useful (0 votes)
446 views14 pages

Theory Paper: Arid Agriculture University, Rawalpindi

The document is a final exam paper for a Data Structures and Algorithms course. It contains two questions. Question 1 asks the student to write C code to convert an infix expression to postfix notation using a stack. The student provides a detailed C program to do this conversion. Question 2 has multiple parts asking about tree traversals, the structure of a sample level order binary tree, and binary search tree operations like insertion and deletion. The student provides thorough answers to all parts of Question 2.

Uploaded by

Umair Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
446 views14 pages

Theory Paper: Arid Agriculture University, Rawalpindi

The document is a final exam paper for a Data Structures and Algorithms course. It contains two questions. Question 1 asks the student to write C code to convert an infix expression to postfix notation using a stack. The student provides a detailed C program to do this conversion. Question 2 has multiple parts asking about tree traversals, the structure of a sample level order binary tree, and binary search tree operations like insertion and deletion. The student provides thorough answers to all parts of Question 2.

Uploaded by

Umair Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Pir Mehr Ali Shah

Arid Agriculture University, Rawalpindi


Office of the controller of Examinations
Final Exam / Fall 2020 (Paper Duration 24 hours)
To be filled by Teacher

Course No.: CS-745 Course Title: Data Structure and Algorithms


Total Marks: 30 Date of Exam: February 12, 2021
Degree: MCS Semester: 2nd Section: A

Theory Paper

Question 1.
Answer: (a)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// Creating ADT named Stack


struct Stk
{
int t;
unsigned size;
int* buffer;
};

// Function to generate stack initially


struct Stk* generateStk(unsigned size)
{
struct Stk* stk = (struct Stk*)malloc(sizeof(struct Stk));

if (!stk)
return NULL;

stk->t = -1;
stk->size = size;

stk->buffer = (int*)malloc(stk->size * sizeof(int));

return stk;
}

// Function to determine if stack has some elements or not


int isVaccant(struct Stk* stk)
{
return stk->t == -1 ;
}

char peek(struct Stk* stk)


{
return stk->buffer[stk->t];
}

// POP function of stack


char POP(struct Stk* stk)
{
if (!isVaccant(stk))
return stk->buffer[stk->t--] ;
return '$';
}

// PUSH function of stack


void PUSH(struct Stk* stk, char input)
{
stk->buffer[++stk->t] = input;
}

// Helper function to determine whether the input is operand or not


int checkOpernd(char input)
{
return (input >= '0' && input <= '9') || (input >= 'a' && input <= 'z') || (input >= 'A' && input <=
'Z');
}

// Helper function to return the values as per precedence of operators


int getPrecedence(char input)
{
switch (input)
{
case '+':
case '-':
return 1;

case '*':
case '/':
return 2;

case '^':
return 3;
}
return -1;
}

// Function to get the desired result


int convertInfToPost(char* input)
{
int j, m;

struct Stk* stk = generateStk(strlen(input));

// checking if stack is generated or not


if(!stk)
return -1 ;

for (j = 0, m = -1; input[j]; ++j)


{
if (checkOpernd(input[j]))
input[++m] = input[j];
else if (input[j] == '(')
PUSH(stk, input[j]);
else if (input[j] == ')')
{
while (!isVaccant(stk) && peek(stk) != '(')
input[++m] = POP(stk);
if (!isVaccant(stk) && peek(stk) != '(')
return -1;
else
POP(stk);
}
else
{
while (!isVaccant(stk) && getPrecedence(input[j]) <= getPrecedence(peek(stk)))
input[++m] = POP(stk);
PUSH(stk, input[j]);
}

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

//Circular Queue Size

using namespace std;


class Queue {
private:
int items[SIZE], front, rear;

public:
Queue() {
front = -1;
rear = -1;
}

// Check whether queue is full or not


bool isFull()
{
if (front == 0 && rear == SIZE - 1)
{
return true;
}

if (front == rear + 1)
{
return true;
}
return false;
}

// Check whether queue is empty or not


bool isEmpty() {
if (front == -1)
return true;
else
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;
}

// Reset queue after dequeue.


else
{
front =(front + 1) % SIZE;
}
return (element);
}
}

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

Whereas K=3, B=4, L=1, D=2, E=5, F=6, G=8*/

Queue a;

// Here Variable and Operator not allowed in Enqueue and Dequeue

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-0 contains only root node that is 68.

The level-1 contains 2 nodes with values 45 and 76.

The level-2 contains 2 nodes with values 36 and 89.

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

The level-5 contains 2 nodes with values 10 and 32

The level-6 contains 1 node with a value of 2.

(iii)
The degree of a tree = highest degree of a node.

Degree of a tree is 2

The height of a tree = height of a root node

The height of a tree is 6

Depth of a tree = count of edges from the root node to the furthest leaf node

The depth of a tree is 6

Level of the tree = depth + 1

The level of the tree is 7.

(iv)
The terminal node will be 2, 32, 39, 98, 123

The predecessor of Node 115 in inorder traversal is 98.

The predecessor of Node 115 in preorder traversal is 89.

The predecessor of Node 115 in postorder traversal is 123.


Answer (b) (i)
#include<iostream>

using namespace std;

//Node class definition


class Node{
//public members
public:
int data;
Node *left;
Node *right;
//constructor
Node(int data){
this->data = data;
left = right = NULL;
}
};

//BST class definition


class BST{
//private members
private:
//head node pointing to the root of Binary search tree
Node *head;

//insertNode function to insert a node into the BST


Node *insertNode(Node *root, int data){
if(root==NULL){
Node *newNode = new Node(data);
return newNode;
}

if(root->data<data){
root->right = insertNode(root->right, data);
}
else if(root->data>data){
root->left = insertNode(root->left, data);
}
return root;
}

//deleteNode function to delete a node from the BST


Node *deleteNode(Node *root, int data){
if(root==NULL){
return root;
}
if(root->data<data){
root->right = deleteNode(root->right, data);
}
else if(root->data>data){
root->left = deleteNode(root->left, data);
}
else{
Node *temp;
if(root->left==NULL && root->right==NULL){
delete root;
return NULL;
}
else if(root->left==NULL){
temp = root;
root = root->right;
delete temp;
}
else if(root->right==NULL){
temp = root;
root = root->left;
delete temp;
}
else{
Node *inorderPredecessor = root->left;
while(inorderPredecessor->right!=NULL){
inorderPredecessor = inorderPredecessor->right;
}
temp = inorderPredecessor;
root->data = inorderPredecessor->data;
root->left = deleteNode(root->left,temp->data);
}
}
return root;
}

//display function to display the nodes of BST in ascending order


void display(Node *root){
if(root!=NULL){
display(root->left);
cout<<root->data<<" ";
display(root->right);
}
}
//public members
public:
//constructor
BST(){
head=NULL;
}

//insertNode function to insert a node into the BST


void insertNode(int data){
head = insertNode(head, data);
}

//deleteNode function to delete a node from the BST


void deleteNode(int data){
head = deleteNode(head, data);
}

//display function to display the nodes of BST in ascending order


void display(){
cout<<endl;
display(head);
}
};

//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);

//displaying inserted node in the bst


bst.display();

//deleting node having data as 10


//call to deleteNode function
bst.deleteNode(10);
cout<<"\nAfter deleting 10 BST becomes: ";
bst.display();

//deleting node having data as 5


//call to deleteNode function
bst.deleteNode(5);
cout<<"\nAfter deleting 5, BST becomes: ";
bst.display();
return 0;
}

Answer (b) (ii)


Unbalanced AVL Tree

L Rotation:

LR Rotation:
R Rotation:

R Rotation:

Balanced AVL Tree


Question 3
Answer:
Minimum Heap

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

Expended Node Frontier List


A
A not a goal D,B
D not a goal B,C
B not a goal C,E,G
C not a goal E,G,A
E not a goal G,A,H
G not a goal A,H,F
H not a goal F,G,F
F is a goal G,F

DFS
Start from A Goal = f

Expended Node Frontier List


A
A not a goal D,B
D not a goal C,B
C not a goal A,B
B not a goal C,E,G
E not a goal H,G
H not a goal G,F,G
G is a goal F,G

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy