CS301 Assignment 3 Solution 2022 - VU Answer

VU Answer Gives CS301 Assignment No 3 Solution Fall 2022. Get A Perfect Solved CS301 Assignment 3 Solution 2022 PDF File Download Below.

CS301 ASSIGNMENT 3 SOLUTION FALL 2022

Due Date: 21 Feb 2022

Total Marks: 20

Submit only code in .cpp format.

CODE Solution:

#include <iostream>

#include <conio.h>

using namespace std;

class TreeNode

{

private:

int object;

TreeNode *left,*right;

public:

void setInfo(int a)

{

object =a;

}

int getInfo()

{

return object;

}

void setLeft(TreeNode *TN)

{

left = TN;

}

TreeNode* getLeft()

{

return left;

}

void setRight(TreeNode *TN)

{

right = TN;

}

TreeNode* getRight()

{

return right;

}

bool isLeaf(TreeNode *TN)

{

if(TN -> getLeft()==NULL &&TN-> getRight()==NULL)

return true;

else

return false;

}

};

class Heap

{

private:

int currentSize,capacity,*array;

public:

Heap()

{

capacity=15;

array=new int[capacity];

currentSize=0;

}

void insert(int a, int n)

{

if(isFull())

cout<<"\n\n ***Heap is Full***";

else if(n == 1)

{

array[currentSize]=a;

percolateDown();

currentSize++;

}

else

{

array[currentSize] = a;

currentSize++;

}

}

bool isEmpty()

{

if(currentSize == 0)

return true;

else

return false;

}

bool isFull()

{

if(currentSize == capacity)

return true;

else

return false;

}

void buildHeap()

{

p:

int p,found=0,tmp,min;

for(int i=currentSize; i>=0; i--)

{

p=(i*2)+1;

if(p < currentSize && (array[i]  >  array[p]  || array[i] > array[p+1]))

{

if(array[p] < array[p+1])

min = p;

else

min = p+1;

tmp = array[i];

array[i] = array[min];

array[min] = tmp;

found++;

}

}

if(found > 0)

goto p;

}

void traverse()

{

for(int i=0; i<currentSize; i++)

cout<<" "<<array[i];

}

void percolateDown()

{

int p, found=0,tmp;

for(int i=0; i<currentSize;i++)

{

p = (i*2)+1;

if(p <= currentSize && array[i] > array[p])

{

tmp = array[i];

array[i] = array[p];

array[p] = tmp;

found++;

}

if((p+1) <= currentSize && array[i] > array[p+1])

{

tmp = array[i];

array[i] = array[p+1];

array[p+1] = tmp;

found++;

}

}

if(found > 0)

percolateDown();

}

};

TreeNode *root=NULL;

int b[15],c=0;

{

int found=0;

TreeNode *newNode = new TreeNode;

newNode -> setInfo(a);

newNode -> setLeft(NULL);

newNode -> setRight(NULL);

if(root == NULL)

{

root = newNode;

cout<<"\n"<<a<<" is inserted as root in BST";

}

else

{

TreeNode *pre = root;

TreeNode *ptr = root;

while(ptr != NULL)

{

if(newNode -> getInfo() < ptr -> getInfo())

{

pre = ptr;

ptr = ptr -> getLeft();

if(ptr == NULL)

pre -> setLeft(newNode);

}

else if(newNode -> getInfo() > ptr->getInfo())

{

pre = ptr;

ptr = ptr -> getRight();

if(ptr == NULL)

pre -> setRight(newNode);

}

else

{

cout<<"\n Duplicate Record Found";

delete newNode;

found++;

break;

}

}

if(found == 0)

{

(a < 10) ? cout<<"\n" <<a: cout<<"\n"<<a;

cout<<" inserted in BST";

}

}

}

void inputDate()

{

int noOfNodes,nodeValue;

cout<<"How many nodes you want to insert into BST: ";

cin>>noOfNodes;

cout<<"Enter "<<noOfNodes<<" values to insert into BST: ";

for(int i=0;i<noOfNodes;i++)

{

cin>>nodeValue;

}

}

void preOrder(TreeNode *TN)

{

if(TN != NULL)

{

cout<<" "<<TN->getInfo();

b[c++] = TN -> getInfo();

preOrder(TN -> getLeft());

preOrder(TN -> getRight());

}

}

void postOrder(TreeNode *TN)

{

if(TN != NULL)

{

postOrder(TN -> getLeft());

postOrder(TN -> getRight());

cout<<" "<<TN->getInfo();

b[c++] = TN -> getInfo();

}

}

int populateTree(int a)

{

return b[a];

}

int main()

{

Heap H, H2;

inputDate();

cout<<"\n\n Post-Order Traversal of BST: ";

postOrder(root);

for(int i=0;i<=14;i++)

H.insert(populateTree(i),1);

cout<<"\n Min Heap using Insert Method: ";

H.traverse();

c=0;

cout<<"\n\n Pre-Order Traversal of BST: ";

preOrder(root);

for(int i=0;i<=14;i++)

H2.insert(populateTree(i),2);

H2.buildHeap();

cout<<"\n Min Heap using Build Method: ";

H2.traverse();

getch();

return 0;

}

Click to Get Solution File Below:

Make sure you can make some changes to your solution file before submitting copy-paste solution will be marked zero.

Before submitting an assignment check your assignment requirement file.

CS301 Assignment 2022 Solution 3

CS301 Assignment 3 Fall 2022 Solution