-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tree.cpp
80 lines (63 loc) · 1.42 KB
/
Tree.cpp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "Tree.h"
Tree::Tree(int classNum, int maxDepth, int minLeafSample, int minInfoGain) {
_classNum = classNum;
_maxDepth = maxDepth;
_minLeafSample = minLeafSample;
_minInfoGain = minInfoGain;
_maxNodeNum = pow(2, _maxDepth) - 1;
_cTree = new Node*[_maxNodeNum];
for (int i = 0 ; i < _maxNodeNum; i++) {
_cTree[i] = NULL;
}
}
Tree::Tree(int maxNodeNum) {
_maxNodeNum = maxNodeNum;
_cTree = new Node*[_maxNodeNum];
for (int i = 0; i < _maxNodeNum; i++) {
_cTree[i] = NULL;
}
}
Tree::~Tree() {
if (_cTree != NULL) {
for (int i = 0; i < _maxNodeNum; i++) {
if (_cTree[i] != NULL) {
delete _cTree[i];
}
}
delete [] _cTree;
_cTree = NULL;
}
}
void Tree::generateTree(Sample* sample) {
_cTree[0] = new Node(sample, _classNum); //tree head
for (int i = 0 ; i < _maxNodeNum; i++) {
if (_cTree[i] == NULL) {
continue;
}
if (_cTree[i]->_isLeaf) {
continue;
}
if (_cTree[i]->_sample->_selectedSampleNum <= _minLeafSample) {
_cTree[i]->setLeaf();
continue;
}
if (2*i+2 > _maxNodeNum) {
_cTree[i]->setLeaf();
continue;
}
_cTree[i]->calculateGainInfo(_cTree, i, _minInfoGain);
}
}
int Tree::predict(float *data) {
Node* node = _cTree[0];
int id = 0;
while (!node->_isLeaf) {
id = node->predict(data, id);
node = _cTree[id];
}
// Result result;
int label = node->_label;
// result._label = label;
// result._prob = node->_probs[label];
return label;
}