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");
}
.