Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include SkillTree.hpp / * * * Default Constructor * / Skill::Skill ( ) : id _ { 0 } , name _ {

#include "SkillTree.hpp"
/**
* Default Constructor
*/
Skill::Skill(): id_{0}, name_{""}, description_{""}, leveled_{false}{}
// Parameterized constructor
/**
* @param id: The unique identifier for the Skill
* @param name: The name of the Skill
* @param description: The description of the Skill
* @param leveled: Whether or not the Skill is leveled up
*/
Skill::Skill(const int id, const std::string& name, const std::string& description, bool leveled)
{
id_= id;
name_= name;
description_= description;
leveled_= leveled;
}
/**
* @param: A const reference to Skill
* @return: True if the id_ of the Skill is equal to that of the argument, false otherwise
*/
bool Skill::operator==(const Skill& rhs){
if (id_== rhs.id_){
return true;
}
else{
return false;
}
}
/**
* @param: A const reference to Skill
* @return: True if the id_ of the Skill is less than that of the argument, false otherwise
*/
bool Skill::operator<(const Skill& rhs){
if (id_< rhs.id_){
return true;
}
else {
return false;
}
}
/**
* @param: A const reference to Skill
* @return: True if the id_ of the Skill is greater than that of the argument, false otherwise
*/
bool Skill::operator>(const Skill& rhs){
if (id_> rhs.id_){
return true;
}
else {
return false;
}
}
/**
* Default Constructor
*/
SkillTree::SkillTree() : BinarySearchTree(){}
/**
* @param: A const reference to string: the name of a csv file
* @post: The SkillTree is populated with Skills from the csv file
* The file format is as follows:
* id,name,description,leveled
* Ignore the first line. Each subsequent line represents a Skill to be added to the SkillTree.
*/
SkillTree::SkillTree(const std::string filename){
std::ifstream file(filename);
std::string line;
std::getline(file, line); // Ignore the first line
while (std::getline(file, line)){
std::stringstream ss(line);
std::string str_id, name, description, str_leveled;
std::getline(ss, str_id,',');
std::getline(ss, name, ',');
std::getline(ss, description, ',');
std::getline(ss, str_leveled, ',');
int id = std::stoi(str_id);
bool leveled =(str_leveled == "true")? true : false;
Skill new_Skill(id, name, description, leveled);
addSkill(new_Skill);
}
file.close();
}
/**
* @param: A const reference to int representing the id of the Skill to be found
* @return: A pointer to the node containing the Skill with the given id, or nullptr if the Skill is not found
*/
std::shared_ptr> SkillTree::findSkill(const int& id) const{
std::shared_ptr> current = root_ptr_;
while (current != nullptr){
if (current->getItem().id_== id){
// Skill found
return current;
} else if (current->getItem().id_> id){
// Search left subtree
current = current->getLeftChildPtr();
} else {
// Search right subtree
current = current->getRightChildPtr();
}
}
// Skill not found
return nullptr;
}
/**
* @param: A const reference to Skill
* @post: The Skill is added to the tree (in BST order as implemented in the base class) only if it was not already in the tree. Note that all comparisons are id-based as implemented by the Skill comparison operators.
* @return: True if the Skill is successfully added to the SkillTree, false otherwise
*/
bool SkillTree::addSkill(const Skill& skill){
if (isEmpty()){
root_ptr_= std::make_shared>(skill);
return true;
}
// Start the traversal from the root
std::shared_ptr> current = root_ptr_;
std::shared_ptr> parent = nullptr;
// Traverse the tree to find the insertion point
while (current != nullptr){
parent = current;
// If the skill already exists, return false
if (current->getItem()== skill){
return false;
} else if (current->getItem().id_> skill.id_){
current = current->getLeftChildPtr();
} else {
current = current->getRightChildPtr();
}
}
// Create a new node for the skill and insert it into the tree
std::shared_ptr> new_node = std::make_shared>(skill);
if (parent->getItem().id_> skill.id_){
parent->setLeftChildPtr(new_node);
} else {
parent->setRightChildPtr(new_node);
}
return true;
}
/**
* @param: A const reference to string: the name of a Skill
* @return: True if the Skill is successfully removed from the SkillTree, false otherwise
*/
bool

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Databases Object Oriented Deductive Hypermedia Technologies

Authors: Kamran Parsaye, Mark Chignell, Setrag Khoshafian, Harry Wong

1st Edition

0471503452, 978-0471503453

More Books

Students also viewed these Databases questions