In order node generator:
struct Node {
char c;
Node* l{};
Node* r{};
};
class NodeGenerator{
static const int max = 50;
const Node* v[max];
int size = 0;
const Node* cur;
bool right;
struct It{
NodeGenerator* p;
const Node& operator*() const {return *p->cur;};
bool operator!=(const It& o) const {return p != o.p;}
void operator++() {p = p->next();};
};
public:
NodeGenerator(const Node& root) : size{0}, cur{&root}, right{false} {}
NodeGenerator* next() {
if (right) cur = cur->r;
while (size || cur) {
if (cur) {
if (size == max) break;
v[size++] = cur;
cur = cur->l;
} else {
cur = v[--size];
right = true;
return this;
}
}
return nullptr;
};
It begin() { return It{next()};}
It end() const { return It{nullptr};}
};
extern "C" int putchar(int);
int main() {
Node tree[] {
{'D', tree + 1, tree + 2},
// |
// +---------------+----------------+
// | |
{'B', tree + 3, tree + 4}, {'F', tree + 5, tree + 6},
// | |
// +---------+-------------+ +-----------+-------------+
// | | | |
{'A'}, {'C'}, {'E'}, {'G'}
};
for (auto node : NodeGenerator{*tree}) putchar(node.c);
}