-
Notifications
You must be signed in to change notification settings - Fork 0
/
iris-node.cpp
70 lines (60 loc) · 1.32 KB
/
iris-node.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
#include <iostream>
#include <vector>
class Node
{
public:
explicit Node(int score_) : score(score_) {}
int getScore() const { return this->score; }
int getTotalScore() const { return this->totalScore; }
void addChild(Node *child)
{
this->children.push_back(child);
child->parent = this;
}
void calculateTotalScore()
{
this->totalScore = this->score;
for (auto child : this->children)
{
child->calculateTotalScore();
this->totalScore += child->totalScore;
}
}
void setScore(int newScore)
{
int diff = newScore - this->score;
this->score = newScore;
this->totalScore += diff;
Node *root = this->parent;
while (root)
{
root->totalScore += diff;
root = root->parent;
}
}
private:
int score = 0;
int totalScore = 0; // new
std::vector<Node *> children;
Node *parent = NULL;
};
int main()
{
auto a = Node(3);
auto b = Node(5);
auto c = Node(7);
auto d = Node(11);
auto e = Node(13);
auto f = Node(17);
a.addChild(&b);
a.addChild(&c);
c.addChild(&d);
c.addChild(&e);
c.addChild(&f);
a.calculateTotalScore();
std::cout << a.getTotalScore() << std::endl;
d.setScore(42);
std::cout << d.getTotalScore() << std::endl;
std::cout << c.getTotalScore() << std::endl;
std::cout << a.getTotalScore() << std::endl;
}