You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
676 B
C++

#pragma once
#include <stack>
#include "iterators/BaseIterator.h"
template <typename T>
class DFSIterator : public BaseIterator<T> {
public:
explicit DFSIterator(T* ptr) : BaseIterator<T>(ptr) {
if (!this->current)
return;
for (auto& it : BaseIterator<T>::current->children())
s.push(it.get());
}
IIterator<T>& operator++() override {
advance();
return *this;
}
virtual size_t level() const {
return level_;
}
protected:
std::stack<T*> s;
size_t level_;
void advance() {
if (s.empty()) {
this->current = nullptr;
return;
}
this->current = s.top();
s.pop();
for (auto& it : this->current->children())
s.push(it.get());
}
};