-
Notifications
You must be signed in to change notification settings - Fork 0
/
DecisionTree.h
executable file
·55 lines (34 loc) · 1.31 KB
/
DecisionTree.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <utility>
//
// Created by karl on 05.05.19.
//
#ifndef DECISIONTREE_DECISIONTREE_H
#define DECISIONTREE_DECISIONTREE_H
#include <string>
#include <vector>
#include <memory>
#include <map>
// A node for an attribute. Depending on the attribute value, it leads to other nodes.
// If it's a leaf node, the outcome is stored in the attribute (and the nextNodes are empty)
class DecisionTreeNode {
public:
explicit DecisionTreeNode(std::string _attribute) : attribute(std::move(_attribute)) {};
void addNode(const std::string &attributeVal, std::shared_ptr<DecisionTreeNode> next);
const std::map<std::string, std::shared_ptr<DecisionTreeNode>> &getNextNodes() const;
const std::string &getAttribute() const;
private:
std::map<std::string, std::shared_ptr<DecisionTreeNode>> nextNodes;
std::string attribute;
};
class DecisionTree {
public:
DecisionTree() = default;
void build(const std::vector<std::vector<std::string>>& data);
void print();
std::string classify(std::map<std::string, std::string>);
private:
std::shared_ptr<DecisionTreeNode> root;
std::shared_ptr<DecisionTreeNode> buildRec(const std::vector<std::vector<std::string>> &data);
void printRec(const std::shared_ptr<DecisionTreeNode>& currentNode, int depth);
};
#endif //DECISIONTREE_DECISIONTREE_H