BINARY TREE code on devc++

September 27, 2007

#include <iostream>
#include <conio.h>
using namespace std;

struct node
{
    int num;
    node *lLink;
    node *rLink;
};
void createLeaf(node *&root);
void connectLeaf(node *leaf,node *current);
void preOrder(node *current);
void inOrder(node *current);
void postOrder(node *current);
void smallestNumber(node *current);
void biggestNumber(node *current);
void menu(int& choice);
main()
{
    int choice;
    node *root;
    root = NULL;
    do
    {
        menu(choice);
        switch(choice)
        {
            case 1:
                createLeaf(root);
                getch();
                break;
            case 2:
                cout<<"Pre-order Traversal"<<endl<<endl;
                preOrder(root);
                getch();
                break;
            case 3:
                cout<<"In-order Traversal"<<endl<<endl;
                inOrder(root);
                getch();
                break;
            case 4:
                cout<<"Post-order Traversal"<<endl<<endl;
                postOrder(root);
                getch();
                break;
            case 5:
                cout<<"Smallest Number"<<endl<<endl;
                smallestNumber(root);
                getch();
                break;
            case 6:
                cout<<"Biggest Number"<<endl<<endl;
                biggestNumber(root);
                getch();
                break;
        }
       
    }while(choice!=8);
}
void menu(int& choice)
{
    //int choice
    system("cls");
    cout<<"[1] Add Node"<<endl
        <<"[2] Preorder Traversal"<<endl
        <<"[3] Inorder Traversal"<<endl
        <<"[4] Postorder Traversal"<<endl
        <<"[5] Smallest Number"<<endl
        <<"[6] Biggest Number"<<endl
        <<"[8] Exit"<<endl
        <<"Enter your choice:";
    cin>>choice;
    //return choice;
}
void createLeaf(node* &root)
{
    node *leaf;
    int num;
   
    leaf=new node;
    leaf->lLink=NULL;
    leaf->rLink=NULL;
    cout<<"Enter number:";
    cin>>num;
    leaf->num=num;
   
    if(root==NULL){root=leaf;cout<<num<<" is made as a root";}
    else connectLeaf(leaf,root);
}
void connectLeaf(node *leaf,node *current)
{
    if(leaf->num > current->num  )
    {
        if(current->rLink!=NULL)
        {
            cout<<"Move to the right of "<<current->num<<endl;
            connectLeaf(leaf,current->rLink);
        }
        else
        {
            current->rLink=leaf;
            cout<<leaf->num<<" is connected to the right of "<<current->num<<endl;
        }
    }
    else
    {
        if(current->lLink!=NULL)
        {
            cout<<"Move to the left of "<<current->num<<endl;
            connectLeaf(leaf,current->lLink);
        }   
        else
        {
            current->lLink=leaf;
            cout<<leaf->num<<" is connected to the left of "<<current->num<<endl;
        }   
    }
}
void preOrder(node* current)
{
    cout<<current->num<<" ";
    if(current->lLink!=NULL) {preOrder(current->lLink);}
    if(current->rLink!=NULL) {preOrder(current->rLink);}
}
void inOrder(node *current)
{
    if(current->lLink!=NULL) {inOrder(current->lLink);}
    cout<<current->num<<" ";
    if(current->rLink!=NULL) {inOrder(current->rLink);}
}
void postOrder(node *current)
{
    if(current->lLink!=NULL) {postOrder(current->lLink);}
    if(current->rLink!=NULL) {postOrder(current->rLink);}
    cout<<current->num<<" ";
}
void smallestNumber(node *current)
{
   
}
void biggestNumber(node *current)
{
   
}

Comments »

The URI to TrackBack this entry is: http://iloveyoubee.blogsome.com/2007/09/27/binary-tree-code-on-devc/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.

Get free blog up and running in minutes with Blogsome | Theme designs available here