-
Notifications
You must be signed in to change notification settings - Fork 4
/
treeVoting.cpp
131 lines (113 loc) · 3.15 KB
/
treeVoting.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "string_manip_stl.h"
#include <vector>
#include <string>
#include <ifstream>
#include <algorithm>
using namespace std;
class Tree {
unsigned long depth;
unsigned long ID;
vector<Tree> child;
public:
Tree(unsigned long, unsigned long);
Tree(const Tree& tree);
int compare(const Tree& tree) const;
bool operator<(const Tree& tree) const;
bool operator==(const Tree& tree) const;
bool operator>(const Tree& tree) const;
bool operator!=(const Tree& tree) const;
bool operator<=(const Tree& tree) const;
bool operator>=(const Tree& tree) const;
bool branch(unsigned long time, unsigned long ID);
bool branchChild(unsigned long parentID, unsigned long time, unsigned long ID);
bool removeChild(unsigned long);
}
Tree::Tree(unsigned long id, unsigned long newdepth): ID(id), depth(newdepth) {
if(newdepth > 0) {
child.push_back(Tree(id, newdepth - 1));
}
}
Tree::Tree(const Tree& tree): ID(tree.ID), depth(tree.depth) {
for(vector<Tree>::const_iterator itor = tree.child.begin();
itor != tree.child.end(); itor++) {
child.push_back(*itor);
}
}
int Tree::compare(const Tree& tree) const {
// return -1 if *this < tree
// return 0 if *this == tree
// return 1 if *this > tree
if(child.size() != tree.child.size()) {
return (child.size() < tree.child.size())? -1: 1;
} else {
if(depth <= 1) {
return 0; // should be equal
} else {
// check from the smallest child
for(vector<Tree>::const_iterator itor = child.begin(), targetItor = tree.child.begin();
itor != child.end() && targetItor != child.end(); itor++, targetItor++) {
int result = itor->compare(*targetItor);
if(result) {
return result;
}
}
return 0;
}
}
}
bool Tree::operator==(const Tree& tree) const {
return !(this->compare(tree));
}
bool Tree::operator<(const Tree& tree) const {
return (this->compare(tree) == -1);
}
bool Tree::operator>(const Tree& tree) const {
return (this->compare(tree) == 1);
}
bool Tree::operator!=(const Tree& tree) const {
return !(*this == tree);
}
bool Tree::operator<=(const Tree& tree) const {
return !(*this > tree);
}
bool Tree::operator>=(const Tree& tree) const {
return !(*this < tree);
}
bool Tree::branch(unsigned long childdepth, unsigned long id) {
// default parentID = ID
return branch(ID, childdepth, id);
}
bool Tree::branchChild(unsigned long parentID, unsigned long childdepth, unsigned long id) {
if(childdepth == depth - 1) {
if(child.size() < 2 && ID == parentID) {
// should branch here and can branch here
child.push_back(Tree(id, childdepth));
sort(child.begin(), child.end());
return true;
} else {
return false;
}
} else {
for(vector<Tree>::iterator itor = child.begin(); itor != child.end(); itor++) {
if(itor->branchChild(parentID, childdepth, id)) {
sort(child.begin(), child.end());
return true;
}
}
return false;
}
}
bool Tree::removeChild(unsigned long id) {
for(vector<Tree>::iterator itor = child.begin(); itor != child.end(); itor++) {
if(itor->ID == id) {
child.erase(itor);
sort(child.begin(), child.end());
return true;
}
if(itor->removeChild(id)) {
sort(child.begin(), child.end());
return true;
}
}
return false;
}