-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcattle_tree.cpp
More file actions
221 lines (190 loc) · 6.55 KB
/
Copy pathcattle_tree.cpp
File metadata and controls
221 lines (190 loc) · 6.55 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//NAME: Ignacio Jimenez
//RED ID: 826019175
#include "cattle_tree.h"
#include <cstring>
//Constructor definition
CattleNode::CattleNode(const std::string &cattleID, int level, const std::vector<int> &maxCattlePerLevel){
this->cattleID = cattleID;
this->level = level;
this->maxCattlePerLevel = maxCattlePerLevel;
children = new CattleNode*[maxCattlePerLevel[level]];//Alocate space for node's new children
for (int i = 0; i < maxCattlePerLevel[level]; i++) {
children[i] = nullptr; //initializing child pointers to null
}
}
//AddCattle Definition
bool CattleNode::addCattle(const char* CattlePath){
if (strncmp(CattlePath, "s_", 2) != 0) {
return false; // Verify cattle path starts with "s_"
}
CattleNode *current = this;
bool lastCharWasUnderscore = true;
//bool lastCharWasUnderscore = true; //changed so we start checking from the third char
for(int i = 2; CattlePath[i] != '\0'; ++i){
if (CattlePath[i] == '_' && CattlePath[i - 1] != '_') {
lastCharWasUnderscore = true;
continue;
}
if(!isdigit(CattlePath[i]) || !lastCharWasUnderscore){
break; //Stop if not a digit or more than one
}
int index = CattlePath[i] - '0';// Make it an int
if(index < 0 || index >= maxCattlePerLevel[current->level]){
return false; // if index is not within the allowed range break (0-indexed)
}
if (current->children[index] == nullptr) {
// Create node if still not created
std::string newCattleID = "s_" + std::to_string(index);
current->children[index] = new CattleNode(newCattleID, current->level + 1, maxCattlePerLevel);
}
current = current->children[index]; // Update current node
lastCharWasUnderscore = false;
}
return true;
}
int CattleNode::findCattle(const char* CattlePath){
if (strncmp(CattlePath, "s_", 2) != 0) {
return 0; // Verify cattle path starts with "s_"
}
CattleNode *current = this;
bool lastCharWasUnderscore = true;
for(int i = 2; CattlePath[i] != '\0'; ++i){
if (CattlePath[i] == '_') {
lastCharWasUnderscore = true;
continue;
}
if(!isdigit(CattlePath[i]) || !lastCharWasUnderscore){
return 0; //Stop if not a digit or more than one
}
int index = CattlePath[i] - '0';// Make it an int
if(index < 0 || index >= current->maxCattlePerLevel[current->level]){
return 0; // if index is not within the allowed range break (0-indexed)
}
if (current->children[index] == nullptr) {
return 0; //Cattle not found
}
current = current->children[index]; // Update current node
lastCharWasUnderscore = false;
}
return countCattle(current);
}
int CattleNode::countCattle(CattleNode *node){
//DFS implementation to count nodes
if (node == nullptr){
return 0;
}
int count = 1;
for (int i = 0; i < node->maxCattlePerLevel[node->level]; i++) {
if(node->children[i] != nullptr){
count += countCattle(node->children[i]);
}
}
return count;
}
//RemoveCattle Definition
bool CattleNode::removeCattle(const char* CattlePath){
if (strncmp(CattlePath, "s_", 2) != 0) {
return false; // Verify cattle path starts with "s_"
}
CattleNode *current = this;
CattleNode *parent = nullptr;
int childIndex = -1;
bool lastCharWasUnderscore = true;
for(int i = 2; CattlePath[i] != '\0'; ++i){
if (CattlePath[i] == '_' && CattlePath[i - 1] != '_') {
lastCharWasUnderscore = true;
continue;
}
if(!isdigit(CattlePath[i]) || !lastCharWasUnderscore){
return false; //Stop if not a digit or more than one
}
int index = CattlePath[i] - '0';// Make it an int
if(index < 0 || index >= maxCattlePerLevel[current->level]){
return false; // if index is not within the allowed range break (0-indexed)
}
parent = current;
childIndex = index;
current = current->children[index];
if (current == nullptr) {
return false; //Cattle not found
}
lastCharWasUnderscore = false;
}
// Found the node to delete
if (parent != nullptr && childIndex >= 0) {
// Check if this node has any children
bool hasChildren = false;
for (int i = 0; i < current->maxCattlePerLevel[current->level]; i++) {
if (current->children[i] != nullptr) {
hasChildren = true;
break;
}
}
// Only delete if no children (leaf node)
if (!hasChildren) {
delete current;
parent->children[childIndex] = nullptr;
return true;
} else {
return false; // Cannot delete node with children
}
}
return false; // Cannot delete root node
}
//SetAnnotation Definition
bool CattleNode::setAnnotation(const char* CattlePath, const char* annotation){
if (strncmp(CattlePath, "s_", 2) != 0) {
return false; // Verify cattle path starts with "s_"
}
CattleNode *current = this;
bool lastCharWasUnderscore = true;
for(int i = 2; CattlePath[i] != '\0'; ++i){
if (CattlePath[i] == '_' && CattlePath[i - 1] != '_') {
lastCharWasUnderscore = true;
continue;
}
if(!isdigit(CattlePath[i]) || !lastCharWasUnderscore){
return false; //Stop if not a digit or more than one
}
int index = CattlePath[i] - '0';// Make it an int
if(index < 0 || index >= maxCattlePerLevel[current->level]){
return false; // if index is not within the allowed range break (0-indexed)
}
if (current->children[index] == nullptr) {
return false; //Cattle not found
}
current = current->children[index];
lastCharWasUnderscore = false;
}
// Found the node, set annotation
current->annotation = std::string(annotation);
return true;
}
//GetAnnotation Definition
std::string CattleNode::getAnnotation(const char* CattlePath){
if (strncmp(CattlePath, "s_", 2) != 0) {
return ""; // Verify SKU path starts with "s_"
}
CattleNode *current = this;
bool lastCharWasUnderscore = true;
for(int i = 2; CattlePath[i] != '\0'; ++i){
if (CattlePath[i] == '_' && CattlePath[i - 1] != '_') {
lastCharWasUnderscore = true;
continue;
}
if(!isdigit(CattlePath[i]) || !lastCharWasUnderscore){
return ""; //Stop if not a digit or more than one
}
int index = CattlePath[i] - '0';// Make it an int
if(index < 0 || index >= maxCattlePerLevel[current->level]){
return ""; // if index is not within the allowed range break (0-indexed)
}
if (current->children[index] == nullptr) {
return ""; //Cattle not found
}
current = current->children[index];
lastCharWasUnderscore = false;
}
// Found the node, return annotation
return current->annotation;
}