Simple Tree C++ implementation I've done this simple c++ assignment. The homework was "Design a Tree class that allows insertion of nodes and visit of the graph".

What do you think of the style/design I used? I chose to store in the STL container the pointers of sub trees. 

Is it memory efficient?

Is it sufficiently readable?

Did I chose the right STL container?

Thanks a lot for any tips about problems or bad practices!

What do you think of the style/design I used? I chose to store in the STL container the pointers of sub trees. 

Is it memory efficient?

Is it sufficiently readable?

Did I chose the right STL container?

Thanks a lot for any tips about problems or bad practices!

#include &lt;set&gt; 
#include &lt;deque&gt; 
#include &lt;iostream&gt;
  using namespace std;

template &lt; typename T &gt;
class Tree {

struct compare {
  bool operator()(const Tree * t1,
    const Tree * t2) const {
    return t1 -&gt; GetContent() &lt; t2 -&gt; GetContent();
  }
};

typedef typename std::multiset &lt; Tree * , typename Tree::compare &gt; NodeSet;

private:
  NodeSet children;
T content;

public:

  Tree&amp; AppendNode(const T&amp; node) {
    Tree *t = new Tree(node);
    AttachTree(t);
    return *t;
  }
void Clear() {
  typename NodeSet::iterator it = children.begin();
  while (children.end() != it) {
    children.erase( *it);
    delete *it;
    it++;
  }

}
const T&amp; GetContent() const {
  return content;
}
Tree(const T&amp; root) {
  content = root;
}
void AttachTree(Tree* t) {
  children.insert(t);
}
void Visit(std::deque &lt;T&gt;&amp; exp) const {
  exp.push_back(content);
  typename NodeSet::iterator it = children.begin();
  while (it != children.end()) {
    (*it) -&gt; Visit(exp);
    it++;
  }
}
Tree() {}
Tree(Tree &amp; c) {
  c.DeepCopyTo(this);
}
T &amp; operator = (const Tree &amp; b) {
  b.DeepCopyTo(this);
}
~Tree() {
  Clear();
}
void DeepCopyTo(Tree* dest) const {
  dest -&gt; content = content;
  typename NodeSet::iterator it = children.begin();
  while (it != children.end()) {
    Tree* t = new Tree();
    (*it)-&gt;DeepCopyTo(t);
    dest-&gt;AttachTree(t);
    it++;
  }
}

 };


https://ideone.com/62Ggwu
Use using keyword to define your type instead of typedef

C++11 introduced type alias which can be used to define your own type and it's compatible with templates, so instead of:

typedef typename std::multiset &lt; Tree * , typename Tree::compare &gt; NodeSet;


you'll get this:

using NodeSet = std::multiset &lt; Tree * , Tree::compare &gt;;


Consider using range-based for loop instead of while loop with iterator

I think that it will result in simplier and more readable code, for example Visit function will look like this:

void Visit(std::deque &lt;T&gt;&amp; exp) const {
  exp.push_back(content);
  for (const auto&amp; child : children)
        child-&gt;Visit(exp);
}


Unnecessary erase call in Clear function

The following call:

children.erase( *it);


is unnecessary - multiset childern will be erased automatically.

Reconsider memory management

Currently it's possible to add new child to existing Tree by calling function

void AttachTree(Tree* t)


which expects pointer to existing Tree object as argument. It means that Tree is not repsonsible for creation of children. By the other hand, in destructor the Clear function is called which deletes all the children, which means that Tree takes responsibility for deletion of children. This might lead to undefined behaviour. Consider the following code:

Tree&lt;int&gt;* t1 = new Tree&lt;int&gt;(1);
{
    Tree&lt;int&gt; t2(2);
    t2.AttachTree(t1);
    {
        Tree&lt;int&gt; t3(3);
        t3.AttachTree(t1);
        std::cout &lt;&lt; "1. t1 content: " &lt;&lt; t1-&gt;GetContent() &lt;&lt; "\n";
    } // t3 desctructor is called, which means that t1 has been deleted
    std::cout &lt;&lt; "2. t1 content: " &lt;&lt; t1-&gt;GetContent() &lt;&lt; "\n"; // t1 doesn't exist anymore - what's the result of t1-&gt;GetContent() ?
} // t2 desctructor is called and t1 will be deleted again
std::cout &lt;&lt; "3. t1 content: " &lt;&lt; t1-&gt;GetContent() &lt;&lt; "\n"; // t1 doesn't exist anymore - what's the result of t1-&gt;GetContent() ?