-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphTree.cpp
More file actions
173 lines (149 loc) · 4.22 KB
/
GraphTree.cpp
File metadata and controls
173 lines (149 loc) · 4.22 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
8888ba.88ba 888888ba a88888b. dP
88 `8b `8b 88 `8b d8' `88 88
88 88 88 a88aaaa8P' 88 .d8888b. dP dP 88d888b. d8888P
88 88 88 88 88 88' `88 88 88 88' `88 88
88 88 88 88 Y8. .88 88. .88 88. .88 88 88 88
dP dP dP dP Y88888P' `88888P' `88888P' dP dP dP
MPCount
Pedro M P Ribeiro - pribeiro@fc.up.pt
Andre Couto Meira - andre.meira@fc.up.pt
*/
/*
* Implementation of the graph tree data structure.
*
* Last Update: 02/2026
*
*/
#include "GraphTree.h"
GraphTreeNode::GraphTreeNode() {
frequency = 0;
zero = one = NULL;
}
GraphTreeNode::~GraphTreeNode() {
if (zero != NULL)
delete zero;
if (one != NULL)
delete one;
}
void GraphTreeNode::zeroFrequency() {
frequency = 0;
if (zero != NULL)
zero->zeroFrequency();
if (one != NULL)
one->zeroFrequency();
}
void GraphTreeNode::incrementString(int pos, char *s, unsigned long f) {
if (s[pos] == 0) {
frequency += f;
} else {
if (s[pos] == '1') {
if (one == NULL)
one = new GraphTreeNode();
one->incrementString(pos + 1, s, f);
} else if (s[pos] == '0') {
if (zero == NULL)
zero = new GraphTreeNode();
zero->incrementString(pos + 1, s, f);
}
}
}
void GraphTreeNode::incrementString(int pos, char *s, GraphTreeNode **final,
unsigned long f) {
if (s[pos] == 0) {
frequency += f;
} else {
if (s[pos] == '1') {
if (one == NULL)
one = new GraphTreeNode();
*final = one;
one->incrementString(pos + 1, s, final, f);
} else if (s[pos] == '0') {
if (zero == NULL)
zero = new GraphTreeNode();
*final = zero;
zero->incrementString(pos + 1, s, final, f);
}
}
}
void GraphTreeNode::populateMap(mapStringInt *m, int size, int pos, char *s) {
if (zero == NULL && one == NULL) {
s[pos] = 0;
(*m)[s] = frequency;
} else {
if (zero != NULL) {
s[pos] = '0';
zero->populateMap(m, size, pos + 1, s);
}
if (one != NULL) {
s[pos] = '1';
one->populateMap(m, size, pos + 1, s);
}
}
}
unsigned long GraphTreeNode::countOccurrences() {
unsigned long aux = frequency;
if (zero != NULL)
aux += zero->countOccurrences();
if (one != NULL)
aux += one->countOccurrences();
return aux;
}
unsigned long GraphTreeNode::countGraphs() {
unsigned long aux = 0;
if (frequency > 0)
aux++;
if (zero != NULL)
aux += zero->countGraphs();
if (one != NULL)
aux += one->countGraphs();
return aux;
}
/*
void GraphTreeNode::showFrequency(int pos, char *s) {
if (zero == NULL && one == NULL) {
s[pos] = 0;
printf("%s\n",s);
} else {
if (zero != NULL) {
s[pos] = '0';
zero->showFrequency(pos + 1, s);
}
if (one != NULL) {
s[pos] = '1';
one->showFrequency(pos + 1, s);
}
}
}
*/
/***************************************************/
GraphTree::GraphTree() { root = new GraphTreeNode(); }
GraphTree::~GraphTree() {
if (root != NULL)
delete root;
}
void GraphTree::zeroFrequency() {
root->zeroFrequency();
occurrences = 0;
leaves = 0;
}
void GraphTree::incrementString(char *s) { root->incrementString(0, s, 1); }
void GraphTree::incrementString(char *s, int f) {
root->incrementString(0, s, f);
}
void GraphTree::addSubstring(GraphTreeNode *initial, char *s,
GraphTreeNode **final, int f) {
initial->incrementString(0, s, final, f);
}
double GraphTree::countOccurrences() { return root->countOccurrences(); }
unsigned long GraphTree::countGraphs() { return root->countGraphs(); }
void GraphTree::populateMap(mapStringInt *m, int maxsize) {
char s[maxsize * maxsize + 1];
root->populateMap(m, maxsize, 0, s);
}
/*
void GraphTree::showFrequency(int maxsize) {
char s[maxsize*maxsize+1];
root->showFrequency(0, s);
}
*/