Preorder, Postorder, inorder Traversal in an Binary Search Tree This is code for Inorder, Preorder, Postorder Traversal of a Tree. Suggest some ways to improve it (since I have made root public and want to make it Private).

#include&lt;iostream.h&gt;
#include&lt;conio.h&gt;

struct Node
{
  int data;
  Node *left;
  Node *right;
};


class Tree
{
  private:
    Node *p;
  public:
    Node *root;
    Tree()
    {
      root=NULL;
    }
    void Insertion();
    void Traversal();
  protected:
    void SearchPos(Node *);
    void Inorder_Traversal(Node *);
    void Preorder_Traversal(Node *);
    void Postorder_Traversal(Node *);
};

void Tree::Insertion()
{
  p=new Node;
  p-&gt;left=p-&gt;right=NULL;
  if(root==NULL)
  {
    cout&lt;&lt;"Enter Root Value to be inserted:";
    cin&gt;&gt;p-&gt;data;
    root=p;
  }
  else
  {
    cout&lt;&lt;"\nEnter Value to be inserted:";
    cin&gt;&gt;p-&gt;data;
    SearchPos(root);
  }
}

void Tree::SearchPos(Node *tmp)
{
  if(p-&gt;data&lt;tmp-&gt;data &amp;&amp; tmp-&gt;left==NULL)
  {
    tmp-&gt;left=p;
    return;
  }
  else if(p-&gt;data&gt;tmp-&gt;data &amp;&amp; tmp-&gt;right==NULL)
  {
    tmp-&gt;right=p;
    return;
  }
  else if(p-&gt;data&lt;tmp-&gt;data &amp;&amp; tmp-&gt;left!=NULL)
  {
    SearchPos(tmp-&gt;left);
  }
  else if(p-&gt;data&gt;tmp-&gt;data &amp;&amp; tmp-&gt;right!=NULL)
  {
    SearchPos(tmp-&gt;right);
  }
}

void Tree::Traversal()
{
  cout&lt;&lt;"\nInorder Traversal:";
  Inorder_Traversal(root);
  cout&lt;&lt;"\nPreorder Traversal:";
  Preorder_Traversal(root);
  cout&lt;&lt;"\nPostorder Traversal:";
  Postorder_Traversal(root);
}

void Tree::Inorder_Traversal(Node *tmp)
{
  if(tmp-&gt;left==NULL &amp;&amp; tmp-&gt;right==NULL)
  {
    cout&lt;&lt;tmp-&gt;data&lt;&lt;" ";
    return;
  }
  else if(tmp-&gt;left!=NULL)
  {
    Inorder_Traversal(tmp-&gt;left);
  }
  cout&lt;&lt;tmp-&gt;data&lt;&lt;" ";
  if(tmp-&gt;right!=NULL)
  {
    Inorder_Traversal(tmp-&gt;right);
  }
}

void Tree::Preorder_Traversal(Node *tmp)
{
  if(tmp-&gt;left==NULL &amp;&amp; tmp-&gt;right==NULL)
  {
    cout&lt;&lt;tmp-&gt;data&lt;&lt;" ";
    return;
  }
  else if(tmp-&gt;left!=NULL)
  {
    cout&lt;&lt;tmp-&gt;data&lt;&lt;" ";
    Preorder_Traversal(tmp-&gt;left);
  }
  if(tmp-&gt;right!=NULL)
  {
    Preorder_Traversal(tmp-&gt;right);
  }
}

void Tree::Postorder_Traversal(Node *tmp)
{
  if(tmp-&gt;left==NULL &amp;&amp; tmp-&gt;right==NULL)
  {
    cout&lt;&lt;tmp-&gt;data&lt;&lt;" ";
    return;
  }
  else if(tmp-&gt;left!=NULL)
  {
    Postorder_Traversal(tmp-&gt;left);
  }
  if(tmp-&gt;right!=NULL)
  {
    Postorder_Traversal(tmp-&gt;right);
    cout&lt;&lt;tmp-&gt;data&lt;&lt;" ";
  }
}

void main()
{
  clrscr();
  char choice;
  Tree t;
  do
  {
    t.Insertion();
    cout&lt;&lt;"\nMore to Insert???Enter(y/n):";
    cin&gt;&gt;choice;
  }while(choice=='y'||choice=='Y');
  t.Traversal();
  getch();
}
Recursion


Test if the current node is valid (return if it is not).
Processes node or Recurse


All your functions can be written much more succinctly and easier to read if you follow those rules.

void Tree::preorderTraversal(Node *tmp)
{
    // Always check to see if the node is valid first.
    if (tmp == nullptr) {
        return;
    }

    //processes your data (this could come after recursion or between the calls depending on the type of the traversal needed).
    std::cout &lt;&lt; tmp-&gt;data &lt;&lt; " ";

    // Recursion.
    preorderTraversal(tmp-&gt;left);   // Don't care if these are null
    preorderTraversal(tmp-&gt;right);  // This will be checked at the beginning of the next call.
}


Code Review

Make this part of Tree class (ie declare this as a private member of Tree). There is no reason for anbody outside the tree to know or use this class.

struct Node
{
  int data;
  Node *left;
  Node *right;
};


Why is root public?

  public:
    Node *root;


This breaks encapsulation and allows a non member to mutate the state of the class without the class knowing or understanding about the mutation.

    Tree()
    {
      root=NULL;
    }


Your Tree class does not obey the rule of Three or Five. This needs to be fixed immediately. Currently it does not break much because you have missed the destructor but it will lead to unexpected results when you copy and mutate the tree.

Also because you don't have a destructor you are leaking memory. You basically need to add the following methods to your Tree class.

     ~Tree();
     Tree(Tree const&amp; rhs);
     Tree&amp; operator=(Tree const&amp; rhs);


You should avoid protected. Though it has its uses these are mostly advanced reasons to use protected. Stick to private and public.

  protected:


Note: As all these expose Node these should be private members. Your interface to the public should not expose internal types and Node is an internal type that affects the implementation.

    void SearchPos(Node *);
    void Inorder_Traversal(Node *);
    void Preorder_Traversal(Node *);
    void Postorder_Traversal(Node *);
};


You can initialize objects created with new.

  p=new Node;
  p-&gt;left=p-&gt;right=NULL;

  // Like this:
  p = new Node{0, nullptr, nullpre};


In 2011 ( 6 years ago) we introduced nullptr to replace the nasty C macro NULL. Please use nullptr.

  if(root==NULL)


Also a bit of horizontal space makes your code less clustered and nicer to read.

 if (root == nullptr) {
 }


Better Insertion

public:
    void Tree::insert(int data)
    {
        root = insertData(root, data);
    }
private:
    Node* Tree::insertData(Node* current, int data)
    {
        if current == nullptr) {
            return new Node{data, nullptr, nullptr};
        }
        if (date &lt;= current-&gt;data) {
            current-&gt;left  = insertData(current-&gt;left,  data);
        }
        else {
            current-&gt;right = insertData(current-&gt;right, data);
        }
        return current;
    }