CS201 MID TERM SOLVED MCQs || PAST PAPERS || GROUP-1 || INTRODUCTION TO PROGRAMMING || VuTech Visit Website For More Solutions www.vutechofficial.blogspot.com …
CS301 ASSIGNMENT NO. 2 FALL 2022 || 100% RIGHT SOLUTION || DATA STRUCTURES || BY VuTech
Visit Website For More Solutions
www.vutechofficial.blogspot.com
SEND WHATSAPP OR E-MAIL FOR ANY QUERY
0325-6644800
kamranhameedvu@gmail.com
Instructions
Please read the following instructions carefully before submitting the
assignment solution:
It should be clear that your assignment will not
get any credit/marks if:
oAssignment is
submitted after due date.
oSubmitted
assignment does not open or file is corrupt.
oAssignment is
copied (From internet/students).
Recommended Tools
Dev C++
Assignment Submission
Instructions
You have to submit only “.cpp” file of your code on the assignments
interface from your LMS account.
Assignment
submitted in any other format will not be accepted and will be scaled with zero marks. No excuse will be accepted
on submitting solution file in any other format.
For any query related to assignment, please contact cs301@vu.edu.pk.
Problem Statement:
You are required to create a program
using C++ programming language to implement binary search tree (BST).
Use the information given in the
following table as data source.
10
8
12
4
11
15
6
17
7
9
16
You are required to build the
binary search tree using the above data. Then search any number and check the
following details.
Query
Possible Reply
Element found?
Yes/No
Is it leaf node?
Yes/No
Sibling of node found?
b is sibling or no sibling
Left child?
c is left child or no left
child
Right child?
d is right child or no right child
Is node even or odd?
Odd/Even
Is prime?
Yes/No
Parent of node?
e is parent of node
Total external node?
Count of external nodes
Total internal nodes?
Count of internal nodes
Total internal links
Count of internal links
Total external links
Count of external links
Node: “b”, “c”, “d” and “e” are nodes which are associated
with searched node.
Important Instructions:
You need to fulfill the following
requirements while solving the assignment task.
1.For the construction of BST use
only class, the use of struct will be considered an invalid
solution.
2.Read the whole give data from
left to right and save into BST (using 10 as root node).
Hint:
The structure of classes which you need to create in the
required program is given below.
Graphical
Representation:
Sample Output:
Lectures
Covered:Lecture # 16 - 27
Due
Date:24th
January, 2023
SOLUTION
#include <iostream>
using namespace std;
class TreeNode {
int data;
TreeNode *left, *right;
public:
TreeNode(int data) {
this->data = data;
left = right = NULL;
}
void insert(int value) {
if (value < data) {
if (left == NULL) {
left = new TreeNode(value);
} else {
left->insert(value);
}
} else if (value > data) {
if (right == NULL) {
right = new TreeNode(value);
} else {
right->insert(value);
}
}
}
bool search(int value) {
if (data == value) {
cout << "Yes, " << value << " is found" << endl;
if (isLeaf()) {
cout << "Yes, it is a leaf node" << endl;
} else {
cout << "No, it is not a leaf node" << endl;
}
if (left != NULL) {
cout << "The left child of " << value << " is " << left->data << endl;
} else {
cout << "There is no left child for " << value << endl;
}
if (right != NULL) {
cout << "The right child of " << value << " is " << right->data << endl;
} else {
cout << "There is no right child for " << value << endl;
}
return true;
} else if (value < data) {
if (left != NULL) {
return left->search(value);
}
} else if (value > data) {
if (right != NULL) {
return right->search(value);
}
}
cout << "No, " << value << " is not found" << endl;
return false;
}
bool isLeaf() {
if (left == NULL && right == NULL) {
return true;
}
return false;
}
int internalNodes() {
int count = 0;
if (left != NULL) {
count += left->internalNodes();
}
if (right != NULL) {
count += right->internalNodes();
}
if (!isLeaf()) {
count++;
}
return count;
}
int externalNodes() {
int count = 0;
if (left != NULL) {
count += left->externalNodes();
}
if (right != NULL) {
count += right->externalNodes();
}
if (isLeaf()) {
count++;
}
return count;
}
int internalLinks() {
int count = 0;
if (left != NULL) {
count += left->internalLinks();
count++;
}
if (right != NULL) {
count += right->internalLinks();
count++;
}
return count;
}
int externalLinks() {
int count =0;
if (left != NULL) {
if (left->isLeaf()) {
count++;
}
count += left->externalLinks();
}
if (right != NULL) {
if (right->isLeaf()) {
count++;
}
count += right->externalLinks();
}
return count;
}
int findParent(int value, int parent) {
if (data == value) {
cout << "The parent of " << value << " is " << parent << endl;
return parent;
} else if (value < data) {
if (left != NULL) {
return left->findParent(value, data);
}
} else if (value > data) {
if (right != NULL) {
return right->findParent(value, data);
}
}
return 0;
}
int findSibling(int value, int parent) {
if (data == value) {
if (parent != 0) {
TreeNode *temp = new TreeNode(parent);
if (temp->left != NULL && temp->left->data == value) {
cout << "The sibling of " << value << " is " << temp->right->data << endl;
We provide Virtual University of Pakistan Study Materials such as Solution of Assignments, GDBs, Mid Term Solved Papers, Final Term Solved Papers, Mid Term Solved MCQs, and Final Term Solved MCQs. We also provide regular Semester Quizzes, Updated Handouts, and Short Questions and Answers. We help you with your research and many other educational-related topics, as far as we know. Furthermore, Share your problem with us and Please feel free to ask any related questions.