hy ,i make binary tree,the input of binary tree in correct.but issues traversing of binary tree using stack ,because the result of traversing is wrong .please solve this issues.

#include<iostream>
#include<stack>
using namespace std;
struct node
{
    int data;
    node *left;
    node *right;
};
class usman
{
private:
    node *root,*leaf;
    int count;
public:
    usman();
    //~usman();
    bool input();
    void call();
    void preorder();
    void inorder();
};
usman::usman()
{
    root=leaf=NULL;
    count=0;
}
bool usman::input()
{
    node *temp=new node;
    int d;
    cout<<"Enter id"<<endl;
    cin>>d;
    temp->data=d;
    temp->left=NULL;
    temp->right=NULL;
    if(root==NULL)
    {
    root=leaf=temp;
    temp=NULL;
    return true;
    }
    else 
    {   
    if(temp->data<root->data)
    {
    if(temp->data<leaf->data)
    {
        leaf->left=temp;
        leaf=leaf->left;
        return true;
    }
    else
    {
    leaf->right=temp;
    leaf=leaf->right;
    return true;
    }
    }
    else if(temp->data>root->data)
        {
        if(temp->data>leaf->data)
        {
        leaf->right=temp;
       leaf=leaf->right;
       return true;
        }
        else
        {
        leaf->left=temp;
        leaf=leaf->left;
        return true;
        }
        }
        count++;
    }
    return false;
    }
void usman::preorder()
{
stack<node*>travStack; 
node *t=root; 
if(t!=0)
{ 
travStack.push(t);
 while(!travStack.empty())
 { 
 t=travStack.top();
 travStack.pop();
cout<<t->data<<",";
if(t->right!=NULL)
{
travStack.push(t->right);
}
if(t->left!=NULL)
{
travStack.push(t->left);
}
}
}
}
void usman:: inorder()
{
    int i=0;
stack<node*>s;   
    node *t=root;
    while(!s.empty()||t!=NULL)
    {
   if(t!=NULL)
   {
    s.push(t);
    t=t->left;
}
else
{   
   t=s.top();
  s.pop();
    cout<<t->data<<",";
    t=t->right;
}
   }
    }

int main()
{
usman s1;
int s;
cout<<"Enetr size"<<endl;
cin>>s;
for(int i=0;i<s;i++)
{
s1.input();
}
s1.preorder();
cout<<"postorder"<<endl;
    s1.inorder();
system("pause");
}

.

This constructor is bad - do NOT initialize in the body of the constructor, but in the initializer block:

// Bad
usman::usman()
{
    root=leaf=NULL;
    count=0;
}

// Correct
usman::usman()
: root(0),
  leaf(0),
  count(0)
{
}

Also, with C++ you can add a constructor to structs, which are just public classes, so you can also automatically initialize their members when you construct one. Much safer than what you do with your construction of node in your code (lines 35 and 36), and reduces the amount of code that you have to deal with and verify for correctness.

There are also a number of problems with usman::input() that you need to correct. I'm not going further, but you need to review your code with a more critical eye and not depend upon us to resolve all of your mistakes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.