0

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");
}
3
Contributors
2
Replies
9
Views
1 Year
Discussion Span
Last Post by rubberman
0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.