Showing posts with label Tree. Show all posts
Showing posts with label Tree. Show all posts

Friday, July 29, 2011

Algorithm of Searching on Tree using c/c++

A search algorithm is an algorithm that accepts an argument a and tries to find a record whose key is a.The algorithm may return the

entire record on more commonly it may return a pointer to that record.

Search in which the entire table is constantly in main memory are called internal memory.Whereas those in which most of the table is kept in auxiliary storage are called external searches.

/*
IDE : CODEBLOCKS 10.05
FILE: Search.cpp*/
#include <iostream>
using namespace std;
class seqsearch
{
     int arr[100],key,n;
     public:
     seqsearch(){n=0;}
     void insert();
     void display();
     int search();
};
void seqsearch::insert()
{
     cout<<"Enter the data to be inserted:";
     cin>>arr[n];
     n++;
}
void seqsearch::display()
{
     if(n==0) cout<<"No data to display";
     for(int i=0;i<n;i++)
          cout<<arr[i]<<'\t';
}
int seqsearch::search()
{
     cout<<"Enter the key of data to be searched:";
     cin>>key;
     for(int i=0;i<n;i++)
     {
          if(key==arr[i]) return i;
     }
     return -1;
}
int main()
{
     seqsearch a1;
     int ch;
     do
     {
          cout<<"\n*****Menu****"<<endl;
          cout<<"1.Insert data"<<endl;
          cout<<"2.Display data"<<endl;
          cout<<"3.Search data"<<endl;
          cout<<"4.Exit"<<endl;
          cout<<"Enter your choice : ";
          cin>>ch;
          switch(ch)
          {
               case 1:
                    a1.insert();
                    break;
               case 2:
                    a1.display();
                    break;
               case 3:
                    int x;x=a1.search();
                    cout<<(x+1);
                    if(x==-1) cout<<"OOPS!!Data not found";
                    else     cout<<"The data is at "
                        <<x+1<<" position";
                break;
               case 4:
                    break;
          }
    }while(ch!=4);
    return 0;
}

Drop Comment if any correction required !!!

Tuesday, July 5, 2011

Algorithm of Tree Traversals | Node Insertion , Deletion

PreOrder Travel | PostOrder Travel | InOrder Travel

Insert a node on a Tree , Traversing node (preorder,postopder,inorder) ,  Delete a node of a tree can be done as shown.

Source-Code :-
#include <iostream>
using namespace std;
struct node
{
int data;
node *left;
node *right;
}*btree;

class tree
{
public:
tree(){btree=NULL;}
void ins(int n);
node* ins1(node *temp,node *newnode);
void preorder(node *tree=btree);
void inorder(node *tree=btree);
void postorder(node *tree=btree);
void remove(int n);
};

node* tree::ins1(node *temp,node *newnode)
{
if(temp==NULL)
{
temp=newnode;
}
else if(temp->data<newnode->data)
{
ins1(temp->right,newnode);
if(temp->right==NULL) temp->right=newnode;
}
else
{
ins1(temp->left,newnode);
if(temp->left==NULL) temp->left=newnode;
}
return temp;
}

void tree::ins(int n)
{
node *temp=btree,*newnode;
newnode=new node;
newnode->left=NULL;
newnode->right=NULL;
newnode->data=n;
btree=ins1(temp,newnode);
}
void tree::preorder(node *tree)
{
if(tree==NULL) cout<<"Tree is empty";
else
{
cout<<tree->data<<'\t';
if(tree->left!=NULL) preorder(tree->left);
if(tree->right!=NULL) preorder(tree->right);
}
}
void tree::inorder(node *tree)
{
if(tree==NULL) cout<<"Tree is empty";
else
{
if(tree->left!=NULL) inorder(tree->left);
cout<<tree->data<<'\t';
if(tree->right!=NULL) inorder(tree->right);
}
}
void tree::postorder(node *tree)
{
if(tree==NULL) cout<<"Tree is empty";
else
{
if(tree->left!=NULL) postorder(tree->left);
if(tree->right!=NULL) postorder(tree->right);
cout<<tree->data<<'\t';
}
}
void tree::remove(int n)
{
node *temp=btree,*parent=btree,*marker;
if(temp==NULL) cout<<"The tree is empty"<<endl;
else
{
while(temp!=NULL && temp->data!=n)
{
parent=temp;
if(temp->data<n)
{
temp=temp->right;
}
else
{
temp=temp->left;
}
}
}
marker=temp;
if(temp==NULL) cout<<"No node present";
else if(temp==btree)
{
if(temp->right==NULL && temp->left==NULL)
{
btree=NULL;
}
else if(temp->left==NULL)
{
btree=temp->right;
}
else if(temp->right==NULL)
{
btree=temp->left;
}
else
{
node *temp1;
temp1=temp->right;
while(temp1->left!=NULL)
{
temp=temp1;
temp1=temp1->left;
}
if(temp1!=temp->right)
{
temp->left=temp1->right;
temp1->right=btree->right;
}
temp1->left=btree->left;
btree=temp1;
}
}
else
{
if(temp->right==NULL && temp->left==NULL)
{
if(parent->right==temp) parent->right=NULL;
else parent->left=NULL;
}
else if(temp->left==NULL)
{
if(parent->right==temp) parent->right=temp->right;
else parent->left=temp->right;
}
else if(temp->right==NULL)
{
if(parent->right==temp) parent->right=temp->left;
else parent->left=temp->left;
}
else
{
node *temp1;
parent=temp;
temp1=temp->right;
while(temp1->left!=NULL)
{
parent=temp1;
temp1=temp1->left;
}
if(temp1!=temp->right)
{
temp->left=temp1->right;
temp1->right=parent->right;
}
temp1->left=parent->left;
parent=temp1;
}
}
delete marker;
}

int main()
{
int ch,ch1;
tree t1;
int n;
do
{
cout<<"\n-----------------Menu---------------------"<<endl;
cout<<"1.Insert a node"<<endl;
cout<<"2.Traversing"<<endl;
cout<<"3.Delete a node"<<endl;
cout<<"4.Exit"<<endl;
cout<<"Enter your choice:";
cin>>ch;
switch(ch)
{
case 1:

cout<<"Enter the number to be inserted in tree:";
cin>>n;
t1.ins(n);
break;
case 2:

cout<<"\n^^^^^^^^^^^^^^Sub Menu^^^^^^^^^^^^^^"<<endl;
cout<<"1.Preorder Traversing"<<endl;
cout<<"2.Inorder Traversing"<<endl;
cout<<"3.Postorder Traversing"<<endl;
cout<<"4.Exit"<<endl;
cout<<"Enter your choice:";
cin>>ch1;
switch(ch1)
{
case 1:

cout<<"The numbers in preorder traversing are"<<endl;
t1.preorder();
break;
case 2:

cout<<"The numbers in inorder traversing are"<<endl;
t1.inorder();
break;
case 3:

cout<<"The numbers in postorder traversing are"<<endl;
t1.postorder();
break;
default:
break;
}
break;
case 3:
cout<<"Enter the number to be deleted in tree:";
cin>>n;
t1.remove(n);
break;
case 5:
break;
default:
cout<<"Out of menu";
break;
}
}while(ch!=4);
return 0;
}