Data Structure Problems
Data Structure Problems
ASSIGNMENT- : 2
/////////////////////////
// Made By: Hardev Singh
// Platform:TC
// Date :24-Feb-2011
/////////////////////////
Q 1:Implement operations of a complet binary search tree with the following
funtionalitiel witheach operaion as a separate function:
1)size which returns total no of nodes in the tree
2) left child return left child of p
3) height returns the height of the node;
4)right child
5)siblings;
6)No of external nodes;
7)No of internal nodes
8)Delete the element
9)Insert the element
Ans:
#include<process.h>
#include<iostream.h>
#include<conio.h>
typedef struct node
{
int data;
ASSIGNMENT 2
DATE OF SUBMISSION :27-FEB-2011
node *left,*right;
};
int count=0,sitem,sib;
node *tree=NULL;
node *ptr=NULL;
node *pptr=NULL;
node *succ=NULL;
void delet(int elemente,node *tree)
{
node *temp;
pptr=tree;
if(tree!=NULL)
{
if(elemente==pptr->left->data)
{
if(tree->left==NULL && tree->right==NULL)
pptr->left=NULL;
else if(tree->left==NULL)
pptr->left=tree->right;
else if(tree->right==NULL)
pptr->right=tree->left;
else if(tree->left!=NULL && tree->right!=NULL)
{
succ=pptr->left->right;
while(succ!=NULL)
{
succ=pptr->left;
}
pptr->left=succ;
}
}
if(elemente==pptr->right->data)
{
if(tree->left==NULL && tree->right==NULL)
pptr->left=NULL;
else if(tree->left==NULL)
pptr->left=tree->right;
else if(tree->right==NULL)
pptr->right=tree->left;
else if(tree->left!=NULL && tree->right!=NULL)
ASSIGNMENT 2
DATE OF SUBMISSION :27-FEB-2011
{
succ=pptr->right->right;
while(succ!=NULL)
{
succ=pptr->left;
}
pptr->left=succ;
}
}
}
delet(elemente,tree->right);
delet(elemente,tree->left);
}
void insert(int element)
{
node *temp;
temp=new node;
temp->data=element;
temp->right=temp->left=NULL;
if(tree==NULL)
{
tree=temp;
}
else
{
ptr=tree;
while(ptr!=NULL)
{
if(element>(ptr->data))
{
if(ptr->left!=NULL)
{
ptr=ptr->left;
}
else
{
ptr->left=temp;
break;
}
}
ASSIGNMENT 2
DATE OF SUBMISSION :27-FEB-2011
else
{
if(ptr->right!=NULL)
{
ptr=ptr->right;
}
else
{
ptr->right=temp;
break;
}
}
}
}
}
void preorder(node *tree)
{
if(tree!=NULL)
{
cout<<"-->"<<tree->data<<"->";
preorder(tree->right);
preorder(tree->left);
}
}
void postorder(node *tree)
{
if(tree!=NULL)
{
postorder(tree->right);
postorder(tree->left);
cout<<"-->"<<tree->data<<"->";
}
}
void inorder(node *tree)
{
if(tree!=NULL)
{
inorder(tree->right);
cout<<"-->"<<tree->data<<"->";
inorder(tree->left);
ASSIGNMENT 2
DATE OF SUBMISSION :27-FEB-2011
}
}
else
{
cout<<tree->right->data;
break;
}
}
else if(tree->right->data==sib)
{
if(tree->right==NULL)
{
cout<<"There is no sibling of that node";
break;
}
else
{
cout<<tree->left->data;
break;
}
}
else
{
sibling(tree->left);
sibling(tree->right);
}
}
}
void height(node *tree)
{
int count1=0,count2=0;
while(tree!=NULL)
{
tree=tree->left;
count1++;
}
while(tree!=NULL)
{
tree=tree->right;
count2++;
}
if(count1==count2)
ASSIGNMENT 2
DATE OF SUBMISSION :27-FEB-2011
void main()
{
clrscr();
int a=1,b=1,item,internal=0,external=0,choice,elemente;
cout<<"**************************--Made By : Hardev Singh
--********************";
while(a==1)
{
cout<<endl<<"----------------------------------------------------------------------------";
cout<<endl<<"1.Insert an item in Tree";
cout<<endl<<"2.Traverse in Inorder";
cout<<endl<<"3.Traverse in Preorder";
cout<<endl<<"4.Traverse in Postorder";
cout<<endl<<"5.Find No of Nodes in Tree";
cout<<endl<<"6.Find No of Internal Nodes";
cout<<endl<<"7.Find No of External Nodes";
cout<<endl<<"8.Search the item in tree";
cout<<endl<<"9.Find sibling";
cout<<endl<<"10.Find Height of the Tree";
cout<<endl<<"11.Delete element from the Tree";
cout<<endl<<"12.Exit from the program";
cout<<endl<<"----------------------------------------------------------------------------";
cout<<endl<<"Enter Choice";
cin>>choice;
switch(choice)
{
case 1: while(b==1)
{
cout<<"Enter the item";
cin>>item;
insert(item);
cout<<endl<<"Wanna more to insert press 1";
cin>>b;
ASSIGNMENT 2
DATE OF SUBMISSION :27-FEB-2011
}
break;
case 2: inorder(tree);
break;
case 3: preorder(tree);
break;
case 4: postorder(tree);
break;
case 5: noofnode(tree);
cout<<endl<<"The no of node in the tree is : "<<count;
break;
case 6: internal=(count-1)/2;
cout<<endl<<"No of internal node is : "<<internal;
break;
case 7: external=(count+1)/2;
cout<<endl<<"No of external node is : "<<external;
break;
case 8: cout<<"Enter the item to be search in tree ";
cin>>sitem;
search(tree);
break;
case 9: cout<<"Enter the no to find sibling";
cin>>sib;
sibling(tree);
break;
case 10: height(tree);
break;
case 11: cout<<"Enter the element you want to delete";
cin>>elemente;
delet(elemente,tree);
break;
case 12: exit(0);
}
}
cout<<endl<<"**** Made By : Hardev Singh****";
getch();
}
ASSIGNMENT 2
DATE OF SUBMISSION :27-FEB-2011
Q 2:Write an algorithm that if inorder and postorder of a tree are given then
find its preorder traversal.
Ans:
Program
/*-------------------------------------------------------------
* Algorithm
*
* Inorder And Preorder
* inorder = g d h b e i a f j c
* preorder = a b d g h e i c f j
* Scan the preorder left to right using the inorder to separate left
* and right subtrees. a is the root of the tree; gdhbei are in the
* left subtree; fjc are in the right subtree.
*------------------------------------------------------------*/
void copy_str(char dest[], char src[], int pos, int start, int end);
void print_t();
{
int i,j,k;
char *pos;
int posn;
for(i=0;i<strlen(io);i++)
{
if(io[i]==po[0])
{
copy_str(t[1],io,1,i,i); // We have the root here
copy_str(t[2],io,2,0,i-1); // Its left subtree
copy_str(t[3],io,3,i+1,strlen(io)); // Its right subtree
print_t();
}
}
copy_str(t[2*j],t[j],2*j,0,posn-1);
copy_str(t[2*j+1],t[j],2*j+1,posn+1,strlen(t[j]));
copy_str(t[j],t[j],j,posn,posn);
print_t();
ASSIGNMENT 2
DATE OF SUBMISSION :27-FEB-2011
}
}
}
}
void copy_str(char dest[], char src[], int pos, int start, int end)
{
char mysrc[100];
strcpy(mysrc,src);
dest[0]='\0';
strncat(dest,mysrc+start,end-start+1);
if(pos>hpos)
hpos=pos;
}
void print_t()
{
int i;
for(i=1;i<=hpos;i++)
{
printf("\nt[%d] = [%s]", i, t[i]);
}
printf("\n");
getch();
}
ASSIGNMENT 2
DATE OF SUBMISSION :27-FEB-2011
Program:
#include<iostream.h>
#include<conio.h>
void main()
{
int ptr,temp,a[100],i,n,j;
clrscr();
cout<<endl<<"Enter the size of array";
cin>>n;
cout<<endl<<"Enter the elements";
j=1;
for(i=n;i>=1;i--)
{
a[j]=i;
j++;
}
cout<<endl;
for(i=1;i<=n;i++)
cout<<"->"<<a[i];
cout<<endl<<"Now sorted list is";
a[0]=-1;
ASSIGNMENT 2
DATE OF SUBMISSION :27-FEB-2011
for(i=2;i<=n;i++)
{
temp=a[i];
ptr=i-1;
while(temp<a[ptr])
{
a[ptr+1]=a[ptr];
ptr=ptr-1;
}
ea[ptr+1]=temp;
}
for(i=1;i<=n;i++)
cout<<"->"<<a[i];
getch();
}
Q 4: Write an algorithm to implement the improved insertion sort
whre it take the help pf binary search to find the insertion place?
Ans:
Program
#include<iostream.h>
#include<conio.h>
if(min==max)
{
if(*(A+max)>value)
{
max=i;
while(max>min)
{
*(A+max)=*(A+max-1);
max--;
}
*(A+max)=value;
}
done=true;
}
else
{
if((max-min)%2)
mid=(max-min+1)/2;
else
mid=(max-min)/2;
mid=min+mid;
if(max-min==1)
{
if (*(A+min) > value)
max=min;
else
min=max;
continue;
}
if (*(A+mid) > value)
max=mid;
else if(*(A+mid) < value)
min=mid;
}
}
}
}
int main()
ASSIGNMENT 2
DATE OF SUBMISSION :27-FEB-2011
{
int a[]={9,7,6,3,5,2,8,4,1,0};
insertionSort(&a[0],10);
for(int i=0; i<10; i++)
cout<<a[i]<<endl;
getch();
return 0;
}
Program:
#include<iostream.h>
#include<conio.h>
int n=0;
void Merge(int low, int mid, int high, int arr[])
{
int h,i,j;
int *b= new int[n];
h=low; i=low; j=mid+1;
while((h<=mid)&&(j<=high))
{
if (arr[h]<=arr[j])
{
ASSIGNMENT 2
DATE OF SUBMISSION :27-FEB-2011
b[i]=arr[h];
h=h+1;
}
else
{
b[i]=arr[j];
j=j+1;
}
i=i+1;
}
if (h>mid)
{
for(int k=j; k<=high; k++)
{
b[i]=arr[k];
i=i+1;
}
}
else
{
for(int k=h; k<=mid; k++)
{
b[i]=arr[k];
i=i+1;
}
ASSIGNMENT 2
DATE OF SUBMISSION :27-FEB-2011
}
for (int k=low;k<=high;k++)
arr[k]=b[k];
}
void MergeSort(int low, int high, int arr[])
{
int mid;
if (low<high)
{
mid=(low+high)/2;
MergeSort(low,mid,arr);
MergeSort(mid+1,high,arr);
Merge(low, mid, high,arr);
}
}
void main()
{
int low, high,i;
clrscr();
cout<<"\n Enter no. of elements: ";
cin>>n;
int *arr= new int[n];
cout<<"\n Enter the elements: ";
for(i=1;i<=n;i++)
cin>>arr[i];
ASSIGNMENT 2
DATE OF SUBMISSION :27-FEB-2011
low=1; high=n;
MergeSort(low, high,arr);
cout<<"\n Sorted list is: ";
for(i=1;i<=n;i++)
cout<<arr[i]<<endl;
getch();
}
Program
#include<iostream.h>
#include<conio.h>
void main()
{
int
a[10],zero[10],one[10],two[10],three[10],four[10],five[10],six[10],seven[10],eight[
10],nine[10];
int
zzero=0,oone=0,ttwo=0,tthree=0,ffour=0,ffive=0,ssix=0,sseven=0,eeight=0,nnine=
0;
int i,j,large=0,count=0,n;
clrscr();
cout<<"////////////////////////";
cout<<endl<<"// Made By: Hardev Singh";
cout<<endl<<"// Platform:TC";
cout<<endl<<"// Date :24-Feb-2011";
cout<<endl<<"/////////////////////////";
cout<<endl<<"Enter the size of array";
cin>>n;
ASSIGNMENT 2
DATE OF SUBMISSION :27-FEB-2011
else if(a[i]%10==3)
{
three[tthree]=a[i];
tthree++;
}
else if(a[i]%10==4)
{
four[ffour]=a[i];
ffour++;
}
else if(a[i]%10==5)
{
five[ffive]=a[i];
ffive++;
}
else if(a[i]%10==6)
{
six[ssix]=a[i];
ssix++;
}
else if(a[i]%10==7)
{
seven[sseven]=a[i];
sseven++;
}
else if(a[i]%10==8)
{
eight[eeight]=a[i];
eeight++;
}
else
{
nine[nnine]=a[i];
nnine++;
}
}
{
a[k]=zero[i];
k++;
}
for(i=0;i<oone;i++)
{
a[k]=one[i];
k++;
}
for(i=0;i<ttwo;i++)
{
a[k]=two[i];
k++;
}
for(i=0;i<tthree;i++)
{
a[k]=three[i];
k++;
}
for(i=0;i<ffour;i++)
{
a[k]=four[i];
k++;
}
for(i=0;i<ffive;i++)
{
a[k]=five[i];
k++;
}
for(i=0;i<ssix;i++)
{
a[k]=six[i];
k++;
}
for(i=0;i<sseven;i++)
{
a[k]=seven[i];
k++;
}
for(i=0;i<eeight;i++)
ASSIGNMENT 2
DATE OF SUBMISSION :27-FEB-2011
{
a[k]=eight[i];
k++;
}
for(i=0;i<nnine;i++)
{
a[k]=nine[i];
k++;
}
int mod=100,div=10,store,left;
else if(left==2)
{
two[ttwo]=a[i];
ttwo++;
}
else if(left==3)
{
three[tthree]=a[i];
tthree++;
}
else if(left==4)
{
four[ffour]=a[i];
ffour++;
}
else if(left==5)
{
five[ffive]=a[i];
ffive++;
}
else if(left==6)
{
six[ssix]=a[i];
ssix++;
}
else if(left==7)
{
seven[sseven]=a[i];
sseven++;
}
else if(left==8)
{
eight[eeight]=a[i];
eeight++;
}
else
{
nine[nnine]=a[i];
nnine++;
}
ASSIGNMENT 2
DATE OF SUBMISSION :27-FEB-2011
}
/*PUT ELEMENTS IN ITS RESPECTIVE PLACE ARRAY AGAIN*/
int k=0;
for(i=0;i<zzero;i++)
{
a[k]=zero[i];
k++;
}
for(i=0;i<oone;i++)
{
a[k]=one[i];
k++;
}
for(i=0;i<ttwo;i++)
{
a[k]=two[i];
k++;
}
for(i=0;i<tthree;i++)
{
a[k]=three[i];
k++;
}
for(i=0;i<ffour;i++)
{
a[k]=four[i];
k++;
}
for(i=0;i<ffive;i++)
{
a[k]=five[i];
k++;
}
for(i=0;i<ssix;i++)
{
a[k]=six[i];
k++;
}
for(i=0;i<sseven;i++)
{
ASSIGNMENT 2
DATE OF SUBMISSION :27-FEB-2011
a[k]=seven[i];
k++;
}
for(i=0;i<eeight;i++)
{
a[k]=eight[i];
k++;
}
for(i=0;i<nnine;i++)
{
a[k]=nine[i];
k++;
}
mod=mod*10;
div=div*10;
count--;
}
for(i=0;i<n;i++)
cout<<endl<<"-->"<<a[i];
getch();
}
Q 7: Given two graphs find out whether one graph s isomorphic to another or not ?
Ans:
#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
int a[10][10],b[10][10],i,j,n,vertex1,vertex2;
int demo[10],demo2[10],count=0,count1=0;
clrscr();
cout<<"Total no of vertex u want to insert in graph 1st ";
cin>>vertex1;
for(i=0;i<20;i++)
{
ASSIGNMENT 2
DATE OF SUBMISSION :27-FEB-2011
demo[i]=0;
demo2[i]=0;
}
for(i=0;i<vertex1;i++)
{
for(j=0;j<vertex1;j++)
{
cin>>a[i][j];
}
}
if(vertex1==vertex2);
else
{
cout<<"not isomorphism";
exit(0);
}
for(i=0;i<vertex1;i++)
{
for(j=0;j<vertex1;j++)
{
if(a[i][j]==1)
count++;
}
}
for(i=0;i<vertex2;i++)
{
ASSIGNMENT 2
DATE OF SUBMISSION :27-FEB-2011
for(j=0;j<vertex2;j++)
{
if(b[i][j]==1)
count1++;
}
}
if(count1==count);
else
{
cout<<"graphs are not isomorphic due to edge disimailarilty";
getch();
exit(0);
}
for(j=0;j<vertex1;j++)
{
for(i=0;i<vertex1;i++)
{
if(a[j][i]==1)
demo[j]=demo[j]+1;
}
}
for(j=0;j<vertex2;j++)
{
for(i=0;i<vertex2;i++)
{
if(b[j][i]==1)
demo2[j]=demo2[j]+1;
}
}
int flag=0;
for(i=0;i<vertex1;i++)
{
if(demo[i]==demo2[i])
flag=1;
else
{
flag=0;
cout<<"graphs are not isomorphic due to degree dissimilarity";
getch();
ASSIGNMENT 2
DATE OF SUBMISSION :27-FEB-2011
exit(0);
}
}
if(flag==1)
cout<<"graph is isomorphism";
getch();
}
Program
#include<conio.h>
#include<iostream.h>
int main()
{
int n,i,a[10],p,x,j,k;
x=0;
p=1;
j=0;
cout<<"enter no=";
cin>>n;
if(n<10)
cout<<"answer="<<n;
else
{
while(n>9)
{
for(i=9;i>1;i--)
ASSIGNMENT 2
DATE OF SUBMISSION :27-FEB-2011
{
if(n%i==0)
{a[j]=i;
n=n/i;
j++;
a[j]=n;
break;
}
}
if(i==1)
{a[j]=n;
break; }
}
for(i=j;i>0;i--)
p=p*10;
if(a[j]<10)
{
for(i=j;i>=0;i--)
{
x=x+a[j]*p; //x=x+a[j]*pow(10,j)
j--;
p=p/10;
}
cout<<"answer="<<x;
}
else
{
cout<<"answer=-1";
}
getch();
return 0;
}
--------------------------------END OF PROGRAMS----------------------------