Preorder, inorder, postorder for tree in C++11 Please review my code for preorder, inorder and postorder of a tree. I am using C++11 (or wanted to use), so let me know if anything is deviating from the C++11 standard.

#include &lt;iostream&gt;
#include &lt;memory&gt;

using std::cout;
using std::endl;
using std::shared_ptr;

struct node
{
    int data;
    shared_ptr&lt;node&gt; left;
    shared_ptr&lt;node&gt; right;
};

shared_ptr&lt;node&gt; newNode (int i)
{
    shared_ptr&lt;node&gt; n (new node);
    n-&gt;data = i;
    n-&gt;left = nullptr;
    n-&gt;right = nullptr;

    return n;
}

void preOrder(shared_ptr&lt;node&gt; n)
{
    if(n == nullptr)
        return;

    cout&lt;&lt;n-&gt;data&lt;&lt;" ";

    preOrder((n-&gt;left));
    preOrder((n-&gt;right));
}

void inOrder(shared_ptr&lt;node&gt; n)
{
    if(n == nullptr)
        return;

    inOrder((n-&gt;left));

    cout&lt;&lt;n-&gt;data&lt;&lt;" ";

    inOrder((n-&gt;right));
}

void postOrder(shared_ptr&lt;node&gt; n)
{
    if(n == nullptr)
        return;

    postOrder((n-&gt;left));
    postOrder((n-&gt;right));

    cout&lt;&lt;n-&gt;data&lt;&lt;" ";

}

int main(int argc, char* argv[])
{
    shared_ptr&lt;node&gt; r = newNode(1); 
     (r-&gt;left) = newNode(2);
     (r-&gt;right) = newNode(3);
    (r-&gt;left-&gt;left) = newNode(4);
    (r-&gt;left-&gt;right) = newNode(5);

    preOrder(r);
    cout&lt;&lt;endl;

    inOrder(r);
    cout&lt;&lt;endl;

    postOrder(r);
    cout&lt;&lt;endl;

    return 0;
}
Memory management

Don't think that is the appropriate smart pointer.

shared_ptr&lt;node&gt; newNode (int i)


Do you have plans to actually share ownership of a node? Or is it more likely that ownership will be done by the patent of the node? I would have gone with unique_ptr&lt;node&gt; if I was going to use smart pointers.

make_X

You should not be using new with smart pointers. There are make_XXX commands that combine all the memory allocation required into a single operation (thus making it more efficient).

   std::shared_ptr&lt;node&gt; n  = std::make_shared&lt;node&gt;(val);  // or make_unique


Using

I suppose this is better than using namespace std;

using std::cout;
using std::endl;
using std::shared_ptr;


But its still pretty sloppy and lazy. Pull those declarations into the tightest scope possible (ie inside a function).

Braces

This looks strange

preOrder((n-&gt;left));
preOrder((n-&gt;right));


Now I am all for using braces to make expression easy to read or to force a particular evaluation order. But this just looks strange and does not help in the evaluation.

Prefer '\n'

Prefer '\n' to std::endl.

The difference between the two is that std::endl forces a flush after placing the new line on the stream. It is very rare that you actually want to force a flush (as the runtime is much better at making that decision than you). Excessive flushing just makes the stream libraries slow.


“\n” or '\n' or std::endl to std::cout?
Why does endl get used as a synonym for “\n” even though it incurs significant performance penalties?