A decision tree is a decision support tool that uses a tree-like model of decisions and their
possible consequences. It is widely used in classification and prediction problems. You are required to
implement the below decision tree to help you decide whether you should go for a trip or not based on
various factors. You also need to take string inputs from the user in this program.
#include <stdexcept>
#include <vector>
using std::runtime_error;
using std::vector;
class TreeNode {
protected:
vector<TreeNode> next;
int s;
public:
TreeNode(int n_out) { next.resize(n_out); }
virtual ~TreeNode() {}
virtual void setState(int val) { s = val; }
virtual TreeNode branch_next();
};
class DecisionNode : public TreeNode { T
Nivate:
vector<int> bpcts;
public:
DecisionNode(int n_out) : TreeNode{n_out} {
bpcts.resize(n_out);
}
DecisionNode(int n_out, vector<int> b_percents
) : DecisionNode(n_out) {
bpcts = b_percents;
}
virtual ~DecisionNode() {}
virtual TreeNode branch_next() {
return next[s];
}
};
class chance_node : public TreeNode {
public:
chance_node() : TreeNode{1} {}
virtual ~chance_node() {}
virtual TreeNode branch_next() {
return next[0];
}
};
class EndNode : public TreeNode {
public:
EndNode() : TreeNode{0} {}
virtual ~EndNode() {}
virtual TreeNode branch_next() {
runtime_error("Decision is made");
return TreeNode(0);
}
};
Comments
Leave a comment