From b2ad9762babdeb8b24a7532b990505bc6eb935c3 Mon Sep 17 00:00:00 2001 From: Spiros Maggioros Date: Sat, 3 Feb 2024 05:59:21 +0200 Subject: [PATCH] added iterators to bst class --- src/classes/tree/avl_tree.h | 4 +- src/classes/tree/bst.h | 90 +++++++++++++++++++++++++++++++++++++ tests/tree/bst.cc | 11 +++++ 3 files changed, 103 insertions(+), 2 deletions(-) diff --git a/src/classes/tree/avl_tree.h b/src/classes/tree/avl_tree.h index e68529b4..bf942c42 100644 --- a/src/classes/tree/avl_tree.h +++ b/src/classes/tree/avl_tree.h @@ -325,7 +325,7 @@ template class avl_tree::Iterator { /** * @brief = operator for Iterator type * - * @param current smart pointer of type node + * @param index the current index * @return Iterator& */ Iterator &operator=(int64_t index) { @@ -360,7 +360,7 @@ template class avl_tree::Iterator { * @brief operator != for type Iterator * * @param it const Iterator - * @return true if curr_root == it.curr_root + * @return true if index == it.index * @return false otherwise */ bool operator!=(const Iterator &it) { return index != it.index; } diff --git a/src/classes/tree/bst.h b/src/classes/tree/bst.h index 87debf43..2f40ccac 100644 --- a/src/classes/tree/bst.h +++ b/src/classes/tree/bst.h @@ -48,6 +48,28 @@ template class bst { */ void remove(T key) { root = __remove(root, key); } + class Iterator; + + /** + * @brief pointer that points to begin + * + * @return Iterator + */ + Iterator begin() { + std::vector ino = this->inorder(); + return Iterator(0, ino); + } + + /** + * @brief pointer that points to end + * + * @return Iterator + */ + Iterator end() { + std::vector ino = this->inorder(); + return Iterator(ino.size(), ino); + } + /** *@brief inorder function. *@returns vector, the elements inorder. @@ -240,4 +262,72 @@ template class bst { } }; +/** + * @brief Iterator class + */ +template class bst::Iterator { +private: + std::vector elements; + int64_t index; + +public: + /** + * @brief Construct a new Iterator object + * + * @param els vector - the elements in inorder fashion + */ + explicit Iterator(const int64_t &index, std::vector &els) noexcept + : index(index), elements(els) {} + + /** + * @brief = operator for Iterator type + * + * @param index the current index + * @return Iterator& + */ + Iterator &operator=(int64_t index) { + this->index = index; + return *(this); + } + + /** + * @brief operator ++ for type Iterator + * + * @return Iterator& + */ + Iterator &operator++() { + if (this->index < elements.size()) { + this->index++; + } + return *(this); + } + + /** + * @brief operator ++ for type Iterator + * + * @return Iterator + */ + Iterator operator++(int) { + Iterator it = *this; + ++*(this); + return it; + } + + /** + * @brief operator != for type Iterator + * + * @param it const Iterator + * @return true if index == it.index + * @return false otherwise + */ + bool operator!=(const Iterator &it) { return index != it.index; } + + /** + * @brief operator * for type Iterator + * + * @return T the value of the node + */ + T operator*() { return elements[index]; } +}; + #endif \ No newline at end of file diff --git a/tests/tree/bst.cc b/tests/tree/bst.cc index e59b17ac..352a83aa 100644 --- a/tests/tree/bst.cc +++ b/tests/tree/bst.cc @@ -85,3 +85,14 @@ TEST_CASE("checking postorder") { std::vector post = {'b', 'a', 'w', 'g'}; REQUIRE(b.postorder() == post); } + +TEST_CASE("checking iterators") { + bst b({5, 3, -10, 4, 15, 20}); + std::vector els = b.inorder(); + std::vector check; + for (auto it = b.begin(); it != b.end(); it++) { + check.push_back(*(it)); + } + + REQUIRE(els == check); +}