-
Notifications
You must be signed in to change notification settings - Fork 0
/
TreeNode.cpp
66 lines (50 loc) · 1.19 KB
/
TreeNode.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
//
// Created by nisal on 7/19/2023.
//
#include "TreeNode.h"
#include <utility>
TreeNode::TreeNode(std::string l) : label(std::move(l)) {
children = std::vector<TreeNode *>();
}
void TreeNode::addChild(TreeNode *child) {
children.push_back(child);
}
void TreeNode::reverseChildren() {
std::reverse(children.begin(), children.end());
}
void TreeNode::removeChild(int index, bool deleteNode) {
if (index < 0 || index >= children.size())
{
throw std::out_of_range("Index out of range");
}
if (deleteNode)
{
releaseNodeMemory(children[index]);
}
children.erase(children.begin() + index);
}
int TreeNode::getNumChildren() {
return static_cast<int>(children.size());
}
std::string TreeNode::getLabel() {
return label;
}
std::vector<TreeNode *> &TreeNode::getChildren() {
return children;
}
std::string TreeNode::getValue() {
return value;
}
void TreeNode::setValue(std::string v) {
value = std::move(v);
}
// NOLINTNEXTLINE
void TreeNode::releaseNodeMemory(TreeNode *node) {
if (node == nullptr)
return;
for (TreeNode *child : node->getChildren())
{
releaseNodeMemory(child);
}
delete node;
}