Vous êtes sur la page 1sur 38

ASSIGNMENT NO.

3
#include<iostream>
#define Max 6
using namespace std;
class Queue
{
int Q[Max],Front,Rear;
public:
Queue()
{
Front=0;
Rear=-1;
for(int i=0;i<Max;i++)
Q[i]=-1;
}
int is_Queue_Full();
int is_Queue_Empty();
int EnQueue(int);
int DQueue(int&);
void Display();
};
int Queue::is_Queue_Full()
{
if(Rear!=-1 && Front==(Rear+1)%Max)
return 1;
else
return 0;
}
int Queue::is_Queue_Empty()
{
if(Rear==-1)
return 1;
else
return 0;
}
int Queue::EnQueue(int Data)
{
if(is_Queue_Full())
return false;
else
Rear=(Rear+1)%Max;
Q[Rear]=Data;
return true;
}
int Queue::DQueue(int& Data)
{
if(!is_Queue_Empty())
{
Data=Q[Front];
Q[Front]=-1;
Front=(Front+1)%Max;

if(Front==(Rear+1)%Max)
{
Front=0; Rear=-1;
}
return true;
}
return false;
}
void Queue:: Display()
{
for(int i=0;i<Max;i++)
cout<<" "<<Q[i];
}

int main()
{
Queue CQ;
int E,Ch;
char Answer;
do
{ cout<<"\n1:Insert an element\n2:Delete an element ";
cout<<"\nEnter your Choice:";
cin>>Ch;
switch(Ch)
{
case 1:
cout<<"Enter Data";
cin>>E;

if(CQ.EnQueue(E))
cout<<"\n Entered Successfully";
else
cout<<"\nQueue Full Can not insert an element";

break;

case 2:
if(CQ.DQueue(E))
cout<<"\nDeleted Element "<<E;
else
cout<<"Queue is Empty can't Remove element";

break;
}
cout<<"\n";
CQ.Display();
cout<<"\nDo You want to Continue(y/n).: ";
cin>>Answer;

}while(Answer=='y');
cout<<"\nFinal Queue is: ";
CQ.Display();
return 1;
}
ASSIGNMENT NO. 4
#include<iostream>
#include<string.h>
using namespace std;
class tree
{
struct node
{ char data;
node *left,*right;
};
node *root;
public:
tree()
{
root=NULL;
}
void create_post();
void create_pre();
void pre(node *);
void in(node *);
void post(node *);
void preN();
void inN();
void postN();
node *return_root()
{
return root;
}
};

void tree::create_post()
{
node *s[100];
int top=-1;
char post[100],token;
cout<<"\nEnter postfix Expression=>";
cin>>post;
for(int i=0;post[i]!='\0';i++)
{
token=post[i];
node *newnode=new node;
newnode->data=token;
newnode->left=newnode->right=NULL;

if(isalnum(token))
s[++top]=newnode;
else
{
newnode->right=s[top--];
newnode->left=s[top--];
s[++top]=newnode;
}
root=s[top];
}
}
void tree::create_pre()
{
node *s[100];
int top=-1;
char pre[100],token;
cout<<"\nEnter prefix Expression=>";
cin>>pre;
for(int i=strlen(pre)-1;i>=0;i--)
{
token=pre[i];
node *newnode=new node;
newnode->data=token;
newnode->left=newnode->right=NULL;
if(isalnum(token))
s[++top]=newnode;
else
{
newnode->left=s[top--];
newnode->right=s[top--];
s[++top]=newnode;
}
root=s[top];
}
}

void tree::pre(node *temp)


{
if(temp!=NULL)
{ cout<<"\t"<<temp->data;
pre(temp->left);
pre(temp->right);
}
}
void tree::in(node *temp)
{
if(temp!=NULL)
{ in(temp->left);
cout<<"\t"<<temp->data;
in(temp->right);
}
}

void tree::post(node *temp)


{
if(temp!=NULL)
{ post(temp->left);
post(temp->right);
cout<<"\t"<<temp->data;

}
}
void tree::preN()
{
node *s[100],*temp=root;
int top=-1;
while(temp!=NULL || top>-1)
{
while(temp!=NULL)
{
cout<<"\t"<<temp->data;
s[++top]=temp;
temp=temp->left;
}
temp=s[top--];
temp=temp->right;
}
}

void tree::inN()
{
node *s[100],*temp=root;
int top=-1;
while(temp!=NULL || top>-1)
{
while(temp!=NULL)
{
s[++top]=temp;
temp=temp->left;
}
temp=s[top--];
cout<<"\t"<<temp->data;
temp=temp->right;
}
}

void tree::postN()
{
node *s[100],*temp=root;
char post[100];
int i=0,top=-1;
while(temp!=NULL || top>-1)
{
while(temp!=NULL)
{
post[i++]=temp->data;
s[++top]=temp;
temp=temp->right;
}
temp=s[top--];
temp=temp->left;
}
for(i=strlen(post)-1;i>=0;i--)
cout<<"\t"<<post[i];
}
int main()
{
tree ob;
int ch,choice;

do
{ cout<<"\n1:Create Expression tree using Postfix Expression\n2:Create
Expression tree using Prefix Expression\n3:End the Execution";
cout<<"\nEnter choice from above ";
cin>>ch;
if(ch==1)
ob.create_post();
else if(ch==2)
ob.create_pre();
else
return 0;
cout<<"\n1:Use Recursive Traversals \n2:Use Non Recursive Traversals";
cout<<"\nEnter your choice ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"\nRecursive Preorder=>";
ob.pre(ob.return_root());
cout<<"\nRecursive Inorder=>";
ob.in(ob.return_root());
cout<<"\nRecursive Postorder=>";
ob.post(ob.return_root());
cout<<endl;
break;

case 2:
cout<<"\nPreorder=>";
ob.preN();
cout<<"\nInorder=>";
ob.inN();
cout<<"\nPostorder=>";
ob.postN();
cout<<endl;
break;
}
}while(ch<3);
}
ASSIGNMENT NO. 9
#include <iostream>

using namespace std;

void heapify(int arr[], int N, int i)

int largest = i;

int l = 2 * i + 1;

int r = 2 * i + 2;

if (l < N && arr[l] > arr[largest])

largest = l;

if (r < N && arr[r] > arr[largest])

largest = r;

if (largest != i) {

swap(arr[i], arr[largest]);

heapify(arr, N, largest);

void heapSort(int arr[], int N)

for (int i = N / 2 - 1; i >= 0; i--)

heapify(arr, N, i);

for (int i = N - 1; i > 0; i--) {

swap(arr[0], arr[i]);

heapify(arr, i, 0);

void printArray(int arr[], int N)

for (int i = 0; i < N; ++i)


cout << arr[i] << " ";

cout << "\n";

int main()

int arr[] = { 12, 11, 13, 5, 6, 7 };

int N = sizeof(arr) / sizeof(arr[0]);

heapSort(arr, N);

cout << "Sorted array is \n";

printArray(arr, N);

}
ASSIGNMENT NO. 10
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
class student
{
int roll,div;
char name[10],address[10];

public:
void getdata()
{
cout<<"\nEnter Rollnumber,Name,Div & Address=\n";
cin>>roll>>name>>div>>address;
}
void putdata()
{
cout<<"\n"<<roll<<"\t"<<name<<"\t"<<div<<"\
t"<<address;
}
int return_Roll(){ return roll;}

};
class seq
{
char fname[15];
public:
void create();
void display();
void Add();
void Remove(int);
void search(int);
};
void seq::create()
{
ofstream fp;//object of class ofstream(write mode)
student s; //class object
char op;
cout<<"\nenter file name=";
cin>>fname;
fp.open(fname);
do
{
s.getdata(); //enter student data
fp.write((char*)&s,sizeof(s));//write student data
in file
cout<<"\nDo u want to add new record(y/n) :";
cin>>op;
}while(op=='y');

fp.close();//close the file


}
void seq::display()
{
ifstream fp; //object of class ifstream(read mode)
student s; //class object
fp.open(fname);

cout<<"\nRoll\tName\tDiv\tAddress";
fp.read((char*)&s,sizeof(s)); //read from file

while(!fp.eof()) //till end of file


{
s.putdata();//display
fp.read((char*)&s,sizeof(s));

}
fp.close(); //close file

}
void seq::Add()
{
ofstream fp; //write mode
student s; //class object

fp.open(fname,ios::app);//open file in append mode


s.getdata(); //enter student data

fp.write((char*)&s,sizeof(s)); //write student data in file


fp.close();
}
void seq::Remove(int key)
{
ifstream f1; //read mode
ofstream f2; //write mode
student s;
int flag=0;
f1.open(fname);
f2.open("Temp.Txt");
f1.read((char*)&s,sizeof(s));

while(!f1.eof())
{
if(key==s.return_Roll())
{
s.putdata();
flag=1;
}
else
f2.write((char*)&s,sizeof(s));

f1.read((char*)&s,sizeof(s));

}
if(flag==0)
cout<<"\nRecord does not present";
f1.close();
f2.close();
remove(fname);
rename("Temp.Txt",fname);
}
void seq::search(int key)
{
ifstream fp;
student s;
int flag=0;
fp.open(fname); //open a file
fp.read((char*)&s,sizeof(s)); //read

while(!fp.eof())
{
if(key==s.return_Roll()) //compare
{
flag=1;//rollno found
break;
}
fp.read((char*)&s,sizeof(s));//read from file

}//end of while

if(flag==0)
cout<<"Record does not present \n";
else
s.putdata();
fp.close();
}

int main()
{
int ch,R;
seq ob;
do
{
cout<<"\n1: create Database\n2: Display \n3: Add a record\n4:
Delete a record\n5: Search a record";
cout<<"\nEnter your choice: "; cin>>ch;
switch(ch)
{
case 1:
ob.create();
break;
case 2:
ob.display();
break;
case 3:
ob.Add();
ob.display();
break;
case 4:
cout<<"\nEnter Roll No to delete";
cin>>R;
ob.Remove(R);
ob.display();
break;
case 5:
cout<<"\nEnter Roll No to search";
cin>>R;
ob.search(R);
break;
}
}while(ch<6);
}
ASSIGNMENT NO. 1
#include <iostream>
#include<string.h>
using namespace std;
struct student
{
int Rollno;
char Name[10];
float SGPA;
};
void getdata(struct student S[10],int n)
{
for(int i=0;i<n;i++)
{
cout<<"\nEnter Rollno,Name,SGPA of student:"<<i<<"\t";
cin>>S[i].Rollno>>S[i].Name>>S[i].SGPA;

}
}
void putdata(struct student S[10],int n)
{
cout<<"\n Rollno\tName\tSGPA";
for(int i=0;i<n;i++)
cout<<"\n"<<S[i].Rollno<<"\t"<<S[i].Name<<"\t"<<S[i].SGPA;

}
void bubble(struct student a[10],int n)
{
for(int i=0;i<n-1 ;i++)
{
for(int j=0;j<n-i-1;j++)

{ if(a[j].Rollno>a[j+1].Rollno)

{ struct student t=a[j];

a[j]=a[j+1];
a[j+1]=t;

}
}
}
}

void insertion(struct student a[10],int n)


{
for( int i=1;i<n;i++)

{
struct student x;
int j;
x=a[i];
for( j=i-1;j>=0;j--)
{
if(strcmp(x.Name,a[j].Name)<0)

a[j+1]=a[j];
else break;
}
a[j+1]=x;
}

int partition(struct student s[20],int left,int right)


{
struct student pivot,temp;
pivot = s[left];
int Pos_pivot=left;
while (left < right)
{
while ((s[right].SGPA < pivot.SGPA) && (left <= right))
right--;
while ((s[left].SGPA >= pivot.SGPA) && (left <= right))
left++;
if(left<right)
{ temp=s[right];
s[right] = s[left];
s[left] =temp; }
}
if(Pos_pivot!=right)

{ s[Pos_pivot]=s[right]; s[right]=pivot; }

return right;

}
void quicksort(struct student s[20], int left, int right)
{
int pivot=partition(s,left,right);

if (left < pivot)


{
quicksort(s, left, pivot-1);
}
if (right > pivot)
{
quicksort(s, pivot+1, right);
}

}
int linearSearch(struct student values[20], int n,int Key)
{ int flag=0;
cout<<"\nRollno\tName\tSGPA";
for(int i = 0; i < n; i++)
{
if (values[i].SGPA == Key )

{
flag=1;

cout<<"\n"<<values[i]. Rollno<<"\t";
cout<<values[i].Name<<"\t"<<values[i].SGPA;
}
}
return flag;
}
int BinarySearchN(struct student S[20], int N,char value[20])
{

int Low = 0, High = N - 1,Mid,count=0;


cout<<"\nEnter the name of student to find it's Record: ";
cin>>value;
while (Low <= High)
{
Mid = (Low + High) / 2;
int diff=strcmp(S[Mid].Name,value);
if (diff >0)
High = Mid - 1;
else if (diff<0)
Low = Mid + 1 ;
else
return Mid;
}
return -1;
}
int main()
{ int n,ch;
char Name[20];
int i,j,k;
struct student S[20];
cout<<"Enter number of records ";
cin>>n;
getdata(S,n);
do
{cout<<"\n1:Bubble sort in ascending order of there Roll no.\
n2:Insertion sort alphabetically\n3:Quicksort according to first ten
toppers from a class\n4:Linearsearch SGPA of a student\n5:Binary search
according student name\n6:Exit ";
cout<<"\n\nEnter the choice ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nRecord of students in ascending order of there Roll no.";
bubble(S,n);
putdata(S,n);
cout<<"\n\n";
break;

case 2:
cout<<"\n\nList of records alphabetically";
insertion(S,n);
putdata(S,n);
cout<<"\n\n";
break;

case 3:
cout<<"\nList of students according to first ten toppers from a class";
quicksort(S,0,n-1);
putdata(S,n);
cout<<"\n\n";
break;

case 4:
float find;
cout<<"\nEnter SGPA of a student to find record: ";
cin>>find;
int flag;
flag=linearSearch(S,n,find);
if(flag==0) cout<<"\n Record Not present";
cout<<"\n\n";
putdata(S,n);
break;

case 5:
char Name[20];
insertion(S,n);
cout<<"Enter the Name : ";
cin>>Name;
int pos;
pos=BinarySearchN(S,n,Name);

if (pos==-1)

cout<<"Record Not present";


else

cout<<"\n\nRollno\tName\tSGPA";
for(i = pos; i>=0; i--)
if(strcmp(S[i].Name,Name)!=0)
break;
for(j = pos; j<n; j++)
if(strcmp(S[j].Name,Name)!=0)
break;
for(k = i+1; k<j; k++)
cout<<"\n"<<S[k].Rollno<<"\t"<<S[k].Name<<"\
t"<<S[k].SGPA<<"\n";
break;

case 6:
return 1;

}
}while(ch<7);
}
ASSIGNMENT NO. 2
#include<iostream>
#include<ctype.h>
#include<string.h>
#include<math.h>
using namespace std;
class stack
{
struct node
{
float data;
node *next;
};
node *top;
public:
stack()
{
top=NULL;
}
int empty();
void push(float);
float pop();
int Top();
};

int stack::empty()
{
if(top==NULL)
return 1;
else
return 0;
}

void stack::push(float x)
{ node *newnode;
newnode=new node;
newnode->data=x;
newnode->next=top;
top=newnode;
}

float stack::pop()
{
node *temp=top;
float x;
top=top->next;
x=temp->data;
delete temp;
return x;
}

int stack::Top()
{ return top->data; }
int Priority(char Op)
{
if(Op=='(' || Op==')')
return 0;
if(Op=='+' || Op=='-')
return 1;
else if(Op=='*' || Op=='/')
return 2;
else if(Op=='^')
return 3;
else
return 4;
}

float Operation(char Op,float A,float B)


{
float P;
if(Op=='*')
P=A*B;
else if(Op=='/')
P=A/B;
else if(Op=='+')
P=A+B;
else if(Op=='-')
P=A-B;
else if (Op=='^')
P=pow(A,B);

return P;
}

void infixtopostfix(char infix[20])


{
char post[20],x,token;
int i,j=0;
stack s;
for(i=0;infix[i]!='\0';i++)
{ token=infix[i];
if(isalnum(token))//alphanumberic
post[j++]=token;
else
if(token == '(')
s.push(token);
else
if(token == ')')
while((x=s.pop())!='(')//pop till )
post[j++]=x;
else
{
while(!s.empty() && Priority(token)<=Priority(s.Top()))
{
post[j++]=s.pop();
}
s.push(token);
}
}

while(!s.empty())
post[j++]=s.pop();
post[j]='\0';
cout<<"\nPostfix Expression=>";
cout<<post;
}

void infixtoprefix(char infix[20])


{
char prefix[20],x,token;
int i,j=0;
stack S;
for(i=strlen(infix)-1;i>=0;i--)
{
token=infix[i];
if(isalnum(token))
prefix[j++]=token;
else
if(token == ')')
S.push(token);
else
if(token == '(')
while((x=S.pop())!=')')
prefix[j++]=x;
else
{
while(!S.empty() && Priority(token)<Priority(S.Top()))
{
prefix[j++]=S.pop();
}
S.push(token);
}
}

while(!S.empty())
prefix[j++]=S.pop();
prefix[j]='\0';
cout<<"\nPrefix Expression=>";
for(i=strlen(prefix)-1;i>=0;i--)
cout<<prefix[i];
}

float Postfix_Evaluation(char String[20])


{
int i;
char token;
float op1,op2,Result,val;
stack S;
for(i=0;String[i]!='\0';i++)
{
token=String[i];
if(isalpha(token))
{
cout<<"\nEnter the value of "<<token<<"=>";
cin>>val;
S.push(val);
}
else if(isdigit(token))
S.push(token-48);
else
{
op2=S.pop();
op1=S.pop();
Result=Operation(token,op1,op2);
cout<<"Result: ";
S.push(Result);
}
}
return S.pop();
}
float Prefix_Evaluation(char Str[20])
{
int i;
float Op1,Op2,Result,val;
stack S;
char token;
for(i=strlen(Str)-1;i>=0;i--)
{
token=Str[i];
if(isalpha(token))// if char
{ cout<<"\nEnter the value of "<<token<<"=>";
cin>>val;
S.push(val);
}
else if(isdigit(token))
S.push(token-48);
else
{
Op1=S.pop();
Op2=S.pop();
Result=Operation(token,Op1,Op2);
cout<<"Result: ";
S.push(Result);
}
}
return S.pop();
}

int main()
{
int ch;
char infix[20],post[20],prefix[20];
do
{
cout<<"\n\nSELECT THE CHOICE FROM BELOW\n1:Infix to Postfix\n2:Infix to
Prefix\n3:Postfix Evaluation\n4:Prefix Evaluation";
cout<<"\nEnter your Choice: ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter infix Expression=>";
cin>>infix;
infixtopostfix(infix);
break;

case 2:
cout<<"\nEnter infix Expression=>";
cin>>infix;
infixtoprefix(infix);
break;

case 3:
cout<<"\nEnter Postfix Expression=>";
cin>>post;
cout<<"\nEvaluated Result=> " <<Postfix_Evaluation(post);
break;

case 4:
cout<<"\nEnter Prefix Expression=>";
cin>>prefix;
cout<<"\nEvaluated Result=> "
<<Prefix_Evaluation(prefix);
break;
}
}while(ch!=5);
}
ASSIGNMENT NO. 5
#include<iostream>
using namespace std;
class BST
{
struct node
{
int data;
node *left, *right;
node()
{
left=right=NULL;
}
}*root;
public:
BST()
{ root=NULL; }
node *return_root()
{ return root; }
void insert(int);
void display(node*);
int search(int);
void leveldisp(node*);
void mirroring(node*);
void del(int);
};

void BST :: insert(int d)


{
node *temp=root, *Nnode=new node;
Nnode->data=d;
if(root==NULL)
{
root=Nnode;
cout<<"=>Root node created !";
}

else
{
while(1)
{
if(d<temp->data)
{ if(temp->left==NULL)
{ temp->left=Nnode;
cout<<"=>Node Inserted Successfully !\n";
break;
}
else
temp=temp->left;

}
else if(d>temp->data)
{ if(temp->right==NULL)
{ temp->right=Nnode;
cout<<"=>Node Inserted Successfully !\n";
break;
}
else
temp=temp->right;

}
else
{
cout<<"Entry is Repeated...RE-ENTER !\n";
break;
}
}
}
}

void BST :: display(node *temp)


{
if(temp!=NULL)
{
display(temp->left);
cout<<temp->data<<" ";
display(temp->right);
}
}

int BST :: search(int key)


{
node *temp=root;
int flag=0;
while(temp!=NULL)
{
if(key<temp->data)
temp=temp->left;
else if(key>temp->data)
temp=temp->right;
else
{
flag=1;
break;
}
}
return flag;
}

void BST :: leveldisp(node *temp)


{
node* Q[20];

int front=0,rear=1,level=0,leaf=0;
if(temp==NULL)
cout<<"Tree is Empty";
else
{
Q[0]=temp, Q[1]=NULL;
cout<<"\n Level "<<level++<<"=>";

while(front<rear)
{
temp=Q[front++];
if(temp==NULL)
{
cout<<"\n Level "<<level++<<"=>";
Q[++rear]=NULL;
}
else
{
cout<<temp->data<<" ";
if(temp->left!=NULL)
Q[++rear]=temp->left;
if(temp->right!=NULL)
Q[++rear]=temp->right;
if(temp->left==NULL&&temp->right==NULL)
leaf++;
}
}
}
cout<<"\n Depth of Tree : "<<level-1;
cout<<"\n Leaf Nodes :"<<leaf;
}

void BST :: mirroring(node *temp)


{
node* Q[20];
int front=0,rear=1;

if(temp==NULL)
cout<<"Tree is Empty";
else
{
Q[0]=temp,Q[1]=NULL;
while(front<rear)
{
temp=Q[front++];
if(temp==NULL)
{ cout<<endl;
Q[++rear]=NULL;
}
else
{
cout<<temp->data<<" ";
if(temp->right!=NULL)
Q[++rear]=temp->right;
if(temp->left!=NULL)
Q[++rear]=temp->left;
}
}
}
}

void BST :: del(int num)


{
node *s[20], *ptemp=root,*temp=root,*rtemp,*prtemp;
char flag;

while(temp->data!=num&& temp!=NULL)
{
if(num>temp->data)
{
ptemp=temp;
flag='r';
temp=temp->right;
}
else
{
ptemp=temp;
flag='l';
temp=temp->left;
}
}
if(temp!=NULL)
{
if(temp->left==NULL && temp->right==NULL)
{
if(root==temp)
root==NULL;
if(flag=='r')
ptemp->right=NULL;
else
ptemp->left=NULL;
}
else if(temp->left!=NULL && temp->right!=NULL)
{
prtemp=temp->right;
rtemp=temp->right;
while(rtemp->left!=NULL)
{
prtemp=rtemp;
rtemp=rtemp->left;
}
prtemp->left=rtemp->right;
if(temp==root)
root=rtemp;
else if(flag=='l')
ptemp->left=rtemp;
else if(flag=='r')
ptemp->right=rtemp;

rtemp->left=temp->left;
if(rtemp!=prtemp)
rtemp->right=temp->right;
}
else if(temp->right!=NULL && temp->left==NULL)
{
if(root==temp)
root=root->right;
if(flag=='l')
ptemp->left=temp->right;
else
ptemp->right=temp->right;
}
else if(temp->left!=NULL && temp->right==NULL)
{
if(root==temp)
root=root->left;
if(flag=='l')
ptemp->left=temp->left;
else
ptemp->right=temp->left;
}
}
else
cout<<"\nNode not present to be deleted";
delete temp;
}

int main()
{
BST tree;
int i,val,key,op;
char ans,ANS;
do
{
cout<<"\nEnter the Node value: ";
cin>>val;
tree.insert(val);
cout<<"\nDo you to add more Nodes (Y/N): ";
cin>>ans;
}while(ans=='Y'||ans=='y');
do
{
cout<<"\nSELECT THE OPERATION TO PERFORM";
cout<<"\n1. Insert a new Node";
cout<<"\n2. Display Tree";
cout<<"\n3. Search the Node";
cout<<"\n4. Display level wise Nodes";
cout<<"\n5. Mirror all the Nodes";
cout<<"\n6. Delete a Node";
cout<<"\nEnter your choice : ";
cin>>op;
switch(op)
{
case 1:
cout<<"\n********** Inserting new Node **********\n";
cout<<"\nEnter Value of new Node: ";
cin>>val;
tree.insert(val);
break;

case 2:
cout<<"\n********** Tree Displayed **********\n";
tree.display(tree.return_root());
break;

case 3 :
cout<<"\n********** Searching a node **********\n";
cout<<"\nEnter the node value to search : ";
cin>>key;
i=tree.search(key);
if(i==1)
cout<<"\nThe node with value "<<key<<" is present";
else
cout<<"\nSearched node is absent !";
break;

case 4:
cout<<"\n********* Levelwise Displayed *********\n";
tree.leveldisp(tree.return_root());
break;

case 5:
cout<<"\n********** Mirrored Nodes **********\n";
tree.mirroring(tree.return_root());
break;

case 6:
cout<<"\n********** Deleting a Node **********\n";
cout<<"\nEnter the no. you want to delete : ";
cin>>val;
tree.del(val);
cout<<"\nTree after deletion of node is : \n";
tree.display(tree.return_root());
break;

default:cout<<"\nInvalid Input !";

}
cout<<"\nDo you want to Continue(Y/N) : ";
cin>>ANS;
}while(ANS=='y'||ANS=='Y');
}
ASSIGNMENT NO. 6
#include<iostream>
using namespace std;
class Ttree
{ public:
char Data;
int LFlag,RFlag;
Ttree *Left,*Right;
Ttree(char c='\0')
{ Data=c;
LFlag=RFlag=1;
Left=Right=NULL;
}
};

class Threaded_Tree
{
Ttree *Head;
public:
Threaded_Tree()
{
Head=new Ttree;
Head->Right=Head;
}
void Create(char[]);
void PreTrav();
void InTrav();
};

void Threaded_Tree ::Create(char Estr[25])


{
Ttree *Stk[20], *Temp;
int Top=-1, I=0;
while(Estr[I]!='\0')
{
Ttree *Node=new Ttree(Estr[I]);
Node->Left=Node->Right=Head;
if(isalnum(Estr[I])); //operand
else //operator
{
Node->Right=Temp=Stk[Top--];//pop and attach to right
while(Temp->Left!=Head) //till leftmost node
Temp=Temp->Left;//move 2 left
Temp->Left=Node;// Threaded link
Node->RFlag=0; //Link

Node->Left=Temp=Stk[Top--];//pop and attach to left


while(Temp->Right!=Head)// till rightmost node
Temp=Temp->Right;//move 2 right
Temp->Right=Node;//Threaded link
Node->LFlag=0;//Link
}
Stk[++Top]=Node;//push
I++;
}
Head->Left=Stk[Top--]; //pop and attach to left of head
}//end of Create()

void Threaded_Tree::InTrav()
{
Ttree *Temp=Head->Left;
do
{
while(Temp->LFlag==0) //follow link to left
Temp=Temp->Left; //move 2 left
cout<<Temp->Data; //print it
Temp=Temp->Right;//follow the thread to right
cout<<Temp->Data; //print it
Temp=Temp->Right;//follow link to right
}
while(Temp!=Head);
}

void Threaded_Tree::PreTrav()
{
Ttree *Temp=Head->Left;
do
{
while(Temp->LFlag==0) //follow link to left
{
cout<<Temp->Data;//print it
Temp=Temp->Left; //move 2 left
}
cout<<Temp->Data;//print
Temp=Temp->Right;//follow the thread 2 right
Temp=Temp->Right;//follow the link 2 right
}
while(Temp!=Head);
}

int main()
{
Threaded_Tree B;
char Estr[25];
cout<<"Enter Postfix Expression: ";
cin>>Estr;

B.Create(Estr);
cout<<" \n----------------------------------\n ";
cout<<" \n Preorder : ";
B.PreTrav();
cout<<" \n----------------------------------\n ";
cout<<" \n Inorder : ";
B.InTrav();
cout<<" \n----------------------------------\n ";
}
ASSIGNMENT NO. 7 A

#include<iostream>
#include<string.h>
using namespace std;
class Graph
{
char Vnames[10][10];
int cost[10][10],n;
public:
Graph();
void creat_graph();
void display();
int Position(char[]);
void prims();
};
Graph::Graph()
{
n=0;
for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
{
cost[i][j]=999;
}
}
void Graph::creat_graph()
{
char ans,Start[10],End[10];
int wt,i,j;
cout<<"Enter number of nodes";
cin>>n;
cout<<"\n Enter vertex name:";
for(i=0;i<n;i++)
cin>>Vnames[i];
do
{
cout<<"\nEnter Start and end point of edge";
cin>>Start>>End;
cout<<"Enter weight";
cin>>wt;
i=Position(Start);
j=Position(End);
cost[i][j]=cost[j][i]=wt;
cout<<"\nMore Edges ";
cin>>ans;
}while(ans=='y' || ans=='Y');
}
void Graph::display()
{
int i,j;
for(i=0;i<n;i++)
{
cout<<"\n";
for(j=0;j<n;j++)
cout<<"\t"<<cost[i][j];
}
}
int Graph::Position(char key[10])
{
for(int i=0;i<n;i++)
if(strcmp(Vnames[i],key)==0)
return i;
return -1;
}

void Graph::prims()
{ int cnt=1,b,i,j,x,y,Total_cost=0,min,visit[10]={0};
char start[10];
cout<<"\n Starting node=";
cin>>start;
x=Position(start);
visit[x]=1;
while(cnt<n)
{
min=999;
for(i=0;i<n;i++)
{
if(visit[i]==1)
{
for(j=0;j<n;j++) // find minimum from adjacent
{ if(cost[i][j]<min && visit[j]==0) //not visited
{ min=cost[i][j];
x=i;
y=j;
}//end of if
}//end of inner for
}//end of if
}//end of outer for
//outer for find minmum from all adjacent that are visited

cout<<"\n"<<Vnames[x]<<"\t"<<Vnames[y]<<"\t"<<min;
Total_cost=Total_cost+min;
cost[x][y]=cost[y][x]=999;
visit[y]=1;
cnt++;
}//end of while
cout<<"\nTotal cost of tree=>"<<Total_cost;
}//end of prims()

int main()
{
Graph G1;
G1.creat_graph();
G1.display();
G1. prims();
}
ASSIGNMENT NO. 7 B

#include<iostream>
#include<string.h>
using namespace std;
class Graph
{
char Vnames[10][10];
int cost[10][10],n;
public:
Graph();
void creat_graph();
void display();
int Position(char[]);
void kru();
};
Graph::Graph()
{
n=0;
for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
{
cost[i][j]=999;
}
}
void Graph::creat_graph()
{
char ans,Start[10],End[10];
int wt,i,j;
cout<<"Enter number of nodes";
cin>>n;
cout<<"\n Enter vertex name:";
for(i=0;i<n;i++)
cin>>Vnames[i];
do
{
cout<<"\nEnter Start and end point of edge";
cin>>Start>>End;
cout<<"Enter weight";
cin>>wt;
i=Position(Start);
j=Position(End);
cost[i][j]=cost[j][i]=wt;
cout<<"\nMore Edges ";
cin>>ans;
}while(ans=='y' || ans=='Y');
}

void Graph::display()
{
int i,j;
for(i=0;i<n;i++)
{
cout<<"\n";
for(j=0;j<n;j++)
cout<<"\t"<<cost[i][j];
}
}
int Graph::Position(char key[10])
{
for(int i=0;i<n;i++)
if(strcmp(Vnames[i],key)==0)
return i;
return -1;
}

void Graph::kru()
{ int i,j,x,y,Total_cost=0,min,gr=1,flag=0,temp, v[10]={0};
while(flag==0)
{ min=999;
for(i=0;i<n;i++) //find the minimum edge
{ for(j=0;j<n;j++)
{ if(cost[i][j]<min)
{ min=cost[i][j];
x=i;
y=j;
}//end of if
}//end inner for
}//end of outer for

if(v[x]==0 && v[y]==0) //both are not visited


{ v[x]=v[y]=gr;
gr++;
}
else if(v[x]!=0 && v[y]==0)// x visited , y not visited
v[y]=v[x]; //assign group of x to y
else if(v[x]==0 && v[y]!=0)// x not visited,y visited
v[x]=v[y];//assign group of y to x
else
{ if(v[x]!=v[y]) // both belong to diffrent group
{ temp=v[x];
for(i=0;i<n;i++)
{ if(v[i]==temp)
v[i]=v[y];
}//end of for
}//end of if
}//end of else
cost[x][y]=cost[y][x]=999;
Total_cost = Total_cost + min;
cout<<"\n"<<Vnames[x]<<"\t"<<Vnames[y]<<"\t"<<min;
//print visit array after every step
temp=v[0]; flag=1;
for(i=0;i<n;i++)
{ if(temp!=v[i])
{ flag=0;
break;
}
}//end of inner for
}//end of while
cout<<"\nTotal cost of tree="<<Total_cost;
}

int main()
{
Graph ob;
ob.creat_graph();
ob.display();
ob.kru();
}
ASSIGNMENT NO. 8

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

class graph
{
char Vnames[10][10];
int i,j,n,cost[10][10],flag[10];
public:
graph();
int Position(char key[10]);
void creat_graph();
void display();
void Dijkstra();
};

graph::graph()
{
n=0;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
{
if(i==j)
cost[i][j]=0;
else
cost[i][j]=999;
}
}

void graph::creat_graph()
{
char ans,Start[10],End[10];
int wt,i,j;
cout<<"Enter number of nodes";
cin>>n;
cout<<"\n Enter vertex name:";
for(i=0;i<n;i++)
{
cin>>Vnames[i];
flag[i]=0;
}
do
{
cout<<"\nEnter Start and end point of edge";
cin>>Start>>End;
cout<<"Enter weight";
cin>>wt;
i=Position(Start);
j=Position(End);
cost[i][j]=cost[j][i]=wt;
//cost[i][j]=wt;
cout<<"\nMore Edges ";
cin>>ans;
}while(ans=='y' || ans=='Y');
}

void graph::display()
{
int i,j;
for(i=0;i<n;i++)
{
cout<<"\n";
for(j=0;j<n;j++)
cout<<"\t"<<cost[i][j];
}
}

int graph::Position(char key[10])


{
for(int i=0;i<10;i++)
if(strcmp(Vnames[i],key)==0)
return i;
return -1;
}

void graph::Dijkstra()
{
int x,dis[10]={0},flag[10]={0};
char visit[10][10]={0};
int i,j,v,sor,min,mnode,k;
char Start[10];

cout<<"\nEnter Source: ";


cin>>Start;
sor=Position(Start);
flag[sor]=1; //init.
dis[sor]=0;

for(v=0;v<n;v++) // initial distance matrix


dis[v]=cost[sor][v];

//visit[0]=sor;
strcpy(visit[0],Vnames[sor]);

i=0;
cout<<"\n";
for(x=0;x<=i;x++)
cout<<" "<<visit[x];
for(x=i+1;x<n;x++)
cout<<" -";
cout<<" : ";
for(x=0;x<n;x++)
cout<<" "<<dis[x];

/*main loop*/
for(i=1;i<n;i++)
{ min=999;

for(k=0;k<n;k++)
{
if(flag[k]==0 && dis[k] < min)
{
min=dis[k];
mnode=k;
}
}
flag[mnode]=1;

strcpy(visit[i],Vnames[mnode]);
for(j=0;j<n;j++)
{ if(flag[j]==0 && cost[mnode][j]!=999)
{ if(dis[j]>dis[mnode]+cost[mnode][j])
dis[j]=dis[mnode]+cost[mnode][j];
}
}
cout<<"\n";
for(x=0;x<=i;x++)
cout<<" "<<visit[x];
for(x=i+1;x<n;x++)
cout<<" -";
cout<<" : ";
for(x=0;x<n;x++)
cout<<" "<<dis[x];
//getch();
}
}

int main()
{
graph obj;
obj.creat_graph();
obj.display();
obj.Dijkstra();

Vous aimerez peut-être aussi