-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.hpp
118 lines (95 loc) · 3.16 KB
/
node.hpp
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
/*
* Email: sam.lazareanu@gmail.com
* ID: ****6281
* @SamuraiPolix - Samuel Lazareanu
*/
#ifndef NODE_HPP
#define NODE_HPP
#include <vector>
#include "complex.hpp"
#include <stdexcept>
using std::vector, std::size_t, std::runtime_error;
template <typename T>
class Node {
private: // by default
T value;
vector<Node<T>*> children;
public:
// ------------------------ Constructors & Destructors ------------------------
Node(const T& value) : value(value) {}
// Copy constructor
Node(const Node<T>& other) : value(other.get_value()) {
vector<Node<T>*> otherChildren = other.get_children();
for (auto child : otherChildren) {
children.push_back(new Node<T>(*child));
}
}
// constructor with number of children - to avoid resizing vector (better performance)
Node(const T& value, size_t numChildren) : Node<T>(value) {
children.reserve(numChildren); // reserve space for children to avoid resizing vector
}
// copy constructor with number of children - to avoid resizing vector (better performance)
Node(Node<T>& other, size_t numChildren) {
children.reserve(numChildren);
*this = other; // calls copy constructor
}
~Node() {
// delete all children nodes
for (auto child : children) {
if (child != nullptr){ // shouldnt really be null in practice but just in case
delete child;
}
}
}
T get_value() const {
return value;
}
const vector<Node<T>*>& get_children() const {
return children;
}
size_t getChildrenSize() const {
return children.size();
}
void add_child(const Node<T>& child) {
// check many things to make sure the child is valid before adding
if (child == *this) {
throw runtime_error("Cannot add node as child to itself");
}
// check if child already exists
for (auto c : children) {
if (*c == child) {
throw runtime_error("Node already exists as child node, cannot add again");
}
}
// all good, add child
children.push_back(new Node<T>(child.get_value()));
}
void remove_child(Node<T>& child) {
for (auto it = children.begin(); it != children.end(); ++it) {
if (**it == child) { // add reference before child???
children.erase(it);
return; // no need to continue
}
}
}
// Comparison operators to use in HeapIterator
bool operator<(const Node<T>& other) const {
return value < other.get_value();
}
bool operator>(const Node<T>& other) const {
return value > other.get_value();
}
bool operator==(const Node<T>& other) const {
return value == other.get_value();
}
bool operator!=(const Node<T>& other) const {
return value != other.get_value();
}
bool operator<=(const Node<T>& other) const {
return value <= other.get_value();
}
bool operator>=(const Node<T>& other) const {
return value >= other.get_value();
}
};
#endif // NODE_HPP