Iterator for traversing a tree What I want to achieve is the following:

#include "tree.h"

#include &lt;iostream&gt;

int main() {

  Tree&lt;std::string&gt;* tA = new Tree&lt;std::string&gt;("A");
  Tree&lt;std::string&gt;* tB = new Tree&lt;std::string&gt;("B", tA);
  Tree&lt;std::string&gt;* tC = new Tree&lt;std::string&gt;("C", tA);
  Tree&lt;std::string&gt;* tD = new Tree&lt;std::string&gt;("D", tB);
  Tree&lt;std::string&gt;* tE = new Tree&lt;std::string&gt;("E", tB);
  Tree&lt;std::string&gt;* tF = new Tree&lt;std::string&gt;("F", tB);

  for (auto cur : *tA) {
    std::cout &lt;&lt; "traversed from " &lt;&lt; (cur-&gt;parent ? cur-&gt;parent-&gt;val : "NULL") &lt;&lt; " to " &lt;&lt; cur-&gt;val &lt;&lt;  std::endl;
  }

  // std::next(std::next(tF-&gt;begin()));

  return 0;
}


And here is my implementation.

#ifndef TREE_H
#define TREE_H

#include &lt;iterator&gt;
#include &lt;stack&gt;
#include &lt;vector&gt;

#include &lt;iostream&gt;

template &lt;typename T&gt;
class Tree {
 public:
  class iterator {
   public:
    using iterator_category = std::forward_iterator_tag;
    using value_type = T;
    using difference_type = std::ptrdiff_t;
    using pointer = T*;
    using reference = T&amp;;

   public:
    iterator() = default;

    iterator(Tree&lt;T&gt;* const root);

    Tree&lt;T&gt;* operator*() const;

    iterator&amp; operator++();

    bool operator!=(iterator const&amp; other) const;

    Tree&lt;T&gt;* cur;

   private:
    std::stack&lt;Tree&lt;T&gt;*&gt; s_;
  };

 public:
  Tree() = default;

  Tree(T const&amp; val, Tree&lt;T&gt;* parent = NULL);

  void* operator new(size_t size);

  iterator begin();

  iterator end();

  T val;

  Tree&lt;T&gt;* const parent = NULL;

 private:
  std::vector&lt;Tree&lt;T&gt;*&gt; children_;
};


template &lt;typename T&gt;
Tree&lt;T&gt;::iterator::iterator(Tree&lt;T&gt;* const root)
    : cur(root) {}

template &lt;typename T&gt;
Tree&lt;T&gt;* Tree&lt;T&gt;::iterator::operator*() const {
  return cur;
}

template &lt;typename T&gt;
typename Tree&lt;T&gt;::iterator&amp; Tree&lt;T&gt;::iterator::operator++() {
  if (cur == NULL) {
    throw std::out_of_range("No more nodes for traversal");
  }

  for (auto&amp; child : cur-&gt;children_) {
    s_.push(child);
  }

  if (s_.empty()) {
    cur = NULL;
  } else {
    cur = s_.top(); s_.pop();
  }

  return *this;
}

template &lt;typename T&gt;
bool Tree&lt;T&gt;::iterator::operator!=(Tree&lt;T&gt;::iterator const&amp; other) const {
  return cur != other.cur;
}

template &lt;typename T&gt;
Tree&lt;T&gt;::Tree(T const&amp; val, Tree&lt;T&gt;* const parent)
    : val(val), parent(parent) {
  if (parent) {
    parent-&gt;children_.push_back(this);
  }
}

template &lt;typename T&gt;
void* Tree&lt;T&gt;::operator new(size_t size) {
  void* p = ::new Tree&lt;T&gt;();
  return p;
}

template &lt;typename T&gt;
typename Tree&lt;T&gt;::iterator Tree&lt;T&gt;::begin() {
  return iterator(this);
}

template &lt;typename T&gt;
typename Tree&lt;T&gt;::iterator Tree&lt;T&gt;::end() {
  return iterator();
}

#endif


User should be able to directly modify val (hence it is public). However, they shouldn't be able to modify any parent/children relationship. Therefore I try to make parent a const pointer, but I cannot make children a list of const pointers because anything inside a std::vector must be assignable. I am considering exposing a GetChildren() method which returns a const std::vector, but that would be inconsistent with how I dealt with parent. Can anyone show a good way to resolve this?

General reviews are welcome. More specifically, I want to know


Is this a good and correct use of pointers?
Is the interface well designed?
Are there any const that I missed?
Clean up allocated memory (everything newed should be deleted), or use smart pointers to do it for you. (Perhaps each tree node should own its children, and store them in unique_ptrs, and the parent should be a raw pointer).
There doesn't seem to be any benefit to overloading operator new for the Tree class.
Currently, the iterator is more of a sub-tree iterator, as it never returns to the parent. e.g. for a tree with edges: A->B->C and A->D if you start iterating at B, you will only iterate from B to C, and never reach D.
Iterators are normally defined outside of the corresponding container class. We can use an iterator typedef in the container to refer to the iterator class.
operator* should return a reference, not a pointer (operator-&gt; should return a pointer).
We have !=, but no ==.
We have a pre-increment operator++, but not post-increment operator++.
It would probably be best to write unit tests for things that are expected to work, e.g.: *i (returns a modifiable reference), *i++ (returns a modifiable reference and then increments i to point at the next value), etc. This can be tedious, but is really the only way to ensure correctness.
The full requirements for a forward iterator are listed here (and in the linked pages), and many of them could be translated into tests cases. This iterator should arguably be a bidirectional iterator.
Standard containers also supply a const_iterator version (and reverse iterators, though that's usually simple to add).


To be honest, I'd suggest starting with something a bit simpler like an array class, and corresponding iterators.



Note that we can traverse a tree in many different ways (depth-first in-order, depth-first pre-order, depth-first post-order, breadth-first). For a full-featured implementation of this sort of thing, see the ASL Forest class.

Since we're iterating nodes, not values, we don't have to worry about "order", but we should still call the class something like tree_depth_first_iterator, to distinguish it from the alternative(s).