/* 13. Write a C++ program to create a class called BINTREE (binary tree) with member functions inorder,preorder,postorder. Create a BINTREE object demonstrate traversals.*/


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class BTREE 
{
    int info;
    BTREE *left,*right;

    public:
        void Create(BTREE **root);
        void Inorder(BTREE *root);
        void Preorder(BTREE *root);
        void Postorder(BTREE *root);
};
void BTREE::Create(BTREE **root)
{
    BTREE *newnode;
    newnode = new BTREE;
    cout<<"\nEnter an item : ";    
    cin>>newnode->info;
    newnode->left = newnode->right = NULL;
    if (*root == NULL)
    {
        *root = newnode;
        return;
    }
    BTREE *temp,*prev;
    temp = prev = *root;
    while (temp != NULL)
    {
        prev = temp;
        if (newnode->info > temp->info)
            temp = temp->right;
        else if (newnode->info < temp->info)
            temp = temp->left;
        else
        {
            cout<<"\nDuplicate entry not allowed";
            return;
        }
    }
    if (newnode->info > prev->info)
        prev->right = newnode;
    else
        prev->left = newnode;
}
void BTREE::Inorder(BTREE *root)
{
    if (root != NULL)
    {
        Inorder(root->left);
        cout<<root->info<<"\t";
        Inorder(root->right);
    }
}
void BTREE::Preorder(BTREE *root)
{
    if (root != NULL)
    {
        cout<<root->info<<"\t";
        Preorder(root->left);
        Preorder(root->right);
    }
}
void BTREE::Postorder(BTREE *root)
{
    if (root != NULL)
    {
        Postorder(root->left);
        Postorder(root->right);
        cout<<root->info<<"\t";
    }
}
void main()
{
    BTREE x,*root=NULL;
    int ch;
    clrscr();
    while(1)
    {
        cout<<"\nMENU\n1. Insert\n2. Inorder\n3. Preorder\n4. Postorder";    
        cout<<"\n5.Exit\nYour choice = ? ";
        cin>>ch;
        switch(ch)
        {
            case 1:
                x.Create(&root);
                break;
    
            case 2:
                x.Inorder(root);
                break;
    
            case 3:
                x.Preorder(root);
                break;

            case 4:
                x.Postorder(root);
                break;
            
            case 5:
                exit(0);
    
            default:
                cout<<"\nInvalid selection. Try again";
        }
    }
    getch();
}