I wanna make my class iterable for this function (assume I can't change this)
// basically my class wrapped in a unique_ptr
using tree_ptr = typename std::unique_ptr<binary_search_tree::binary_tree<T>>;
// then passed as a reference to this tester function along with a the correct sequence
static void test_sort(const tree_ptr<T> &tree, const std::vector<T> &expected)
{
std::vector<T> actual;
for (auto& x : *tree) {
actual.push_back(x);
}
REQUIRE(expected == actual);
}
Here is my class created in the namespace binary_search_tree
namespace binary_search_tree {
template <typename T>
class binary_tree {
private:
T t_data, min;
std::unique_ptr<binary_tree<T>> t_left, t_right;
public:
binary_tree(T d) : t_data(d), t_left(nullptr), t_right(nullptr)
{}
void insert(T data) {
if(data <= t_data) {
if(t_left == nullptr) {
t_left = std::unique_ptr<binary_tree<T>>(new binary_tree<T>(data));
} else {
t_left->insert(data);
}
} else {
if(t_right == nullptr)
t_right = std::unique_ptr<binary_tree<T>>(new binary_tree<T>(data));
else
t_right->insert(data);
}
}
const T data() const {
return t_data;
}
const std::unique_ptr<binary_tree<T>>& left() const {
return t_left;
}
const std::unique_ptr<binary_tree<T>>& right() const {
return t_right;
}
// const std::unique_ptr<binary_tree<T>>& begin() const {
//
// std::unique_ptr<binary_tree<T>>& leaf = this;
// while(leaf) {
// leaf = leaf->left();
// }
// return leaf;
// }
// const std::unique_ptr<binary_tree<T>>& end() const {
// return this;
// }
//
// bool operator!=(const leaf &rhs) {
// return this;
// }
//
// bool operator*(const leaf &rhs) {
// return this;
// }
};
} // namespace binary_search_tree
The begin() should keep calling left() until it reaches a nullptr, while end() should do the same with right(). I'm confused on what these functions should be returning. I know I need to define an incrementer at some point as well.
Aucun commentaire:
Enregistrer un commentaire