Skip to content

Commit

Permalink
added iterators to bst class
Browse files Browse the repository at this point in the history
  • Loading branch information
spirosmaggioros committed Feb 3, 2024
1 parent 536e873 commit b2ad976
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/classes/tree/avl_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ template <typename T> class avl_tree<T>::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) {
Expand Down Expand Up @@ -360,7 +360,7 @@ template <typename T> class avl_tree<T>::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; }
Expand Down
90 changes: 90 additions & 0 deletions src/classes/tree/bst.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,28 @@ template <typename T> class bst {
*/
void remove(T key) { root = __remove(root, key); }

class Iterator;

/**
* @brief pointer that points to begin
*
* @return Iterator
*/
Iterator begin() {
std::vector<T> ino = this->inorder();
return Iterator(0, ino);
}

/**
* @brief pointer that points to end
*
* @return Iterator
*/
Iterator end() {
std::vector<T> ino = this->inorder();
return Iterator(ino.size(), ino);
}

/**
*@brief inorder function.
*@returns vector<T>, the elements inorder.
Expand Down Expand Up @@ -240,4 +262,72 @@ template <typename T> class bst {
}
};

/**
* @brief Iterator class
*/
template <typename T> class bst<T>::Iterator {
private:
std::vector<T> elements;
int64_t index;

public:
/**
* @brief Construct a new Iterator object
*
* @param els vector<T> - the elements in inorder fashion
*/
explicit Iterator(const int64_t &index, std::vector<T> &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
11 changes: 11 additions & 0 deletions tests/tree/bst.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,14 @@ TEST_CASE("checking postorder") {
std::vector<char> post = {'b', 'a', 'w', 'g'};
REQUIRE(b.postorder() == post);
}

TEST_CASE("checking iterators") {
bst<int> b({5, 3, -10, 4, 15, 20});
std::vector<int> els = b.inorder();
std::vector<int> check;
for (auto it = b.begin(); it != b.end(); it++) {
check.push_back(*(it));
}

REQUIRE(els == check);
}

0 comments on commit b2ad976

Please sign in to comment.