include<iostream>
include<stack>

using namespace std;
struct node

#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 postorder();
};
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)
    {
        cout<<"1"<<endl;
    root=leaf=temp;
    temp=NULL;
    //root==NULL;
    count++;
    return true;
    }
    else 
    {   
    if(temp->data<root->data)
    {
    cout<<"2"<<endl;
    if(temp->data<leaf->data)
    {
        leaf->left=temp;
        leaf=leaf->left;
        cout<<"3"<<endl;
        return true;
    }
    else
    {
        cout<<"4"<<endl;
    leaf->right=temp;
    leaf=leaf->right;
    return true;
    }
    }
    else if(temp->data>root->data)
        {
            cout<<"5"<<endl;
        if(temp->data>leaf->data)
        {
            cout<<"6"<<endl;
        leaf->right=temp;
       leaf=leaf->right;
       return true;
        }
        else
        {
            cout<<"7"<<endl;
        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:: postorder()
{
    int i=0;
stack<node*>s;   
    node *t=root;
    bool done=false;
    while(!s.empty()||t!=NULL)
    {
        cout<<"ff"<<endl;
   if(t!=NULL)
   {
    cout<<"ff1"<<endl;
    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.postorder();
system("pause");
}

Recommended Answers

All 2 Replies

Please DO NOT multi-post questions to the forums. I have made some suggestions in your other post. Please delete this one.

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.