Answered step by step
Verified Expert Solution
Question
1 Approved Answer
#include SkillTree.hpp / * * * Default Constructor * / Skill::Skill ( ) : id _ { 0 } , name _ {
#include "SkillTree.hpp
Default Constructor
Skill::Skill: id name description leveledfalse
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::Skillconst 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::operatorconst Skill& rhs
if id rhsid
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::operatorconst Skill& rhs
if id rhsid
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::operatorconst Skill& rhs
if id rhsid
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:
idname,description,leveled
Ignore the first line. Each subsequent line represents a Skill to be added to the SkillTree.
SkillTree::SkillTreeconst std::string filename
std::ifstream filefilename;
std::string line;
std::getlinefile line; Ignore the first line
while std::getlinefile line
std::stringstream ssline;
std::string strid name, description, strleveled;
std::getliness strid;
std::getliness name, ;
std::getliness description, ;
std::getliness strleveled, ;
int id std::stoistrid;
bool leveled strleveled "true" true : false;
Skill newSkillid name, description, leveled;
addSkillnewSkill;
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::sharedptr SkillTree::findSkillconst int& id const
std::sharedptr current rootptr;
while current nullptr
if currentgetItemid id
Skill found
return current;
else if currentgetItemid id
Search left subtree
current currentgetLeftChildPtr;
else
Search right subtree
current currentgetRightChildPtr;
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 idbased as implemented by the Skill comparison operators.
@return: True if the Skill is successfully added to the SkillTree, false otherwise
bool SkillTree::addSkillconst Skill& skill
if isEmpty
rootptr std::makesharedskill;
return true;
Start the traversal from the root
std::sharedptr current rootptr;
std::sharedptr parent nullptr;
Traverse the tree to find the insertion point
while current nullptr
parent current;
If the skill already exists, return false
if currentgetItem skill
return false;
else if currentgetItemid skill.id
current currentgetLeftChildPtr;
else
current currentgetRightChildPtr;
Create a new node for the skill and insert it into the tree
std::sharedptr newnode std::makesharedskill;
if parentgetItemid skill.id
parentsetLeftChildPtrnewnode;
else
parentsetRightChildPtrnewnode;
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started