SOLUTION(100% working)
Link to the zipped file is provided at the end of this
document
Crystal Indigo!
Crystal Indigo!
Providing all solutions you need anytime
+27 76 626 8187
Copy and run the code and submit what you are suppose to submit
,Outputs:
,Question 1
// COS2611 Skeleton for Assessment 3
#include <iostream>
#include <fstream>
#include <string>
#include<vector>
//Add your student name and student number
//
//
//
using namespace std;
class TreeNode
{
//keep this class as is
public:
int value; //key or data
TreeNode* left;
TreeNode* right;
//constructors
TreeNode() {
value = 0;
left = NULL;
right = NULL;
} //default constructor
TreeNode(int v) {
value = v;
left = NULL;
right = NULL;
} //parametrized constructor
}; //TreeNode
class BST
{
public:
TreeNode* root;
//member functions
BST() {
root = NULL;
, }
bool isTreeEmpty()
{
if (root == NULL)
return true; //the tree is empty
else
return false; //the tree is not empty
}
void insertNode(TreeNode* new_node) {
if (root == NULL) {
root = new_node;
} //insert the root node
else {
TreeNode* temp = root;
while (temp != NULL) {
if (new_node->value == temp->value) {
cout << "Value Already exist," <<
"Insert another value!" << endl;
return;
}
else if ((new_node->value < temp->value) && (temp->left == NULL)) {
temp->left = new_node; //insert value to the left
break;
}
else if (new_node->value < temp->value) {
temp = temp->left;
}
else if ((new_node->value > temp->value) && (temp->right == NULL)) {
temp->right = new_node; //insert value to the right
break;
}
else {
temp = temp->right;
}
}
}
} //insertNode