DSPS PROGRAMS
=====================================================
Roll
No. : SECOA179
Name :
Jadhav Sunil B.
Assignment
No. : 1
Assignment
Name : A vegetable and fruit mall wants to organize its vegetable
and fruit products in a combination of purchase pattern of
customer. Solve the problem by suggested appropriate data
structure, design necessary class.
=====================================================
#include<iostream>
using
namespace
std;
class
veg
{
public:
int
vqty;
string
vname;
void
accept();
};
class
fruit
{
public:
int
fqty;
string
fname;
void
accept1();
};
void
fruit
:: accept1()
{
cout<<"Enter
fruit name: ";
cin>>fname;
cout<<"Enter
fruit quantity: ";
cin>>fqty;
}
void
veg
:: accept()
{
cout<<"Enter
vegetable name: ";
cin>>vname;
cout<<"Enter
vegetable quantity: ";
cin>>vqty;
}
void
sort(veg
v[],int
n,fruit
f[],int
m)
{
int
i;
for(int
pass=1;pass<n;pass++)
{
for(i=0;i<n-pass;i++)
{
if(v[i].vqty
< v[i+1].vqty)
{
int
temp;
temp=v[i].vqty;
v[i].vqty=v[i+1].vqty;
v[i+1].vqty=temp;
string
vtemp;
vtemp=v[i].vname;
v[i].vname=v[i+1].vname;
v[i+1].vname=vtemp;
}
}
}
for(int
pass=1;pass<m;pass++)
{
for(i=0;i<m-pass;i++)
{
if(f[i].fqty
< f[i+1].fqty)
{
int
temp;
temp=f[i].fqty;
f[i].fqty=f[i+1].fqty;
f[i+1].fqty=temp;
string
ftemp;
ftemp=f[i].fname;
f[i].fname=f[i+1].fname;
f[i+1].fname=ftemp;
}
}
}
cout<<"\nThe
suggested order of the vegetables is:\n";
for(i=0;i<n;i++)
{
cout<<v[i].vname<<"\t"<<v[i].vqty<<"\n";
}
cout<<"\nThe
suggested order of the fruit is:\n";
for(i=0;i<m;i++)
{
cout<<f[i].fname<<"\t"<<f[i].fqty<<"\n";
}
}
int
main()
{
veg
v[20];
fruit
f[20];
int
n,m,i;
cout<<"\nEnter
total number of vegetables:";
cin>>n;
for(i=0;i<n;i++)
{
v[i].accept();
}
cout<<"\nEnter
total number of fruit:";
cin>>m;
for(i=0;i<m;i++)
{
f[i].accept1();
}
sort(v,n,f,m);
return
0;
}
==============================================================================
OUTPUT:
Enter
total number of vegetables:5
Enter
vegetable name: carrot
Enter
vegetable quantity: 50
Enter
vegetable name: tomato
Enter
vegetable quantity: 32
Enter
vegetable name: ginger
Enter
vegetable quantity: 54
Enter
vegetable name: spinach
Enter
vegetable quantity: 76
Enter
vegetable name: lemon
Enter
vegetable quantity: 53
Enter
total number of fruit:7
Enter
fruit name: mango
Enter
fruit quantity: 54
Enter
fruit name: orange
Enter
fruit quantity: 76
Enter
fruit name: cherry
Enter
fruit quantity: 21
Enter
fruit name: pineapple
Enter
fruit quantity: 65
Enter
fruit name: apple
Enter
fruit quantity: 98
Enter
fruit name: banana
Enter
fruit quantity: 67
Enter
fruit name: watermelon
Enter
fruit quantity: 100
The
suggested order of the vegetables is:
spinach 76
ginger 54
lemon 53
carrot 50
tomato 32
The
suggested order of the fruit is:
watermelon 100
apple 98
orange 76
banana 67
pineapple 65
mango 54
cherry 21
====================================================
Roll
No. : SECOA179
Name :
Jadhav Sunil B.
Assignment
No. : 2
Assignment
Name : Write a program using object oriented programming features
to implement doubly circular linked list with different
manipulation facilities in c++.
--------------------------------------------------------------------------------
#include
<iostream>
using
namespace
std;
class
node{
public:
node
*n,*p;
int
data;
};
class
dcll
{
private:
node
*head;
public:
dcll()
{
head=NULL;
}
void
accept()
{
node
*nn= new
node();
cout<<"\nEnter
new node in DCLL :";
cin>>nn->data;
nn->n=nn->p=NULL;
if(head==NULL)
{
head=nn;
nn->n=nn->p=head;
}
else
{
node
*t=head;
while(t->n!=head)
t=t->n;
t->n=nn;
nn->p=t;
nn->n=head;
head->p=nn;
}
}
void
display()
{
node
*t=head;
if(head==NULL)
{
cout<<"List
not present";
return;
}
while(t->n!=head)
{
cout<<"\n"<<t->data;
t=t->n;
}
cout<<"\n"<<t->data;
}
void
search()
{
node
*t=head;
int
val;
if(head==NULL)
{
cout<<"List
not present";
}
else
{
cout<<"\nEnter
node which u want to search";
cin>>val;
while(t->n!=head)
{
if(t->data==val)
{
cout<<"\n"<<val<<"
node is present...";
break;
}
t=t->n;
}
if(t->n==head)
{
if(t->data==val)
{
cout<<"\n"<<val<<"
node is present...";
}
else
cout<<"\nInvalid
node please enter node which is present in DCLL";
}
}
}
void
remove()
{
node
*t=head;
int
val;
if(head==NULL)
{
cout<<"List
not present";
}
else
{
cout<<"\nEnter
node which u want to delete";
cin>>val;
if(t->data==val)
{
if(head->n==head)
{
head=NULL;
cout<<"head
is deleded...";
}
else
{
t->n->p=t->p;
t->p->n=t->n;
head=t->n;
cout<<"\nhead
is deleted and now new head is :"<<head->data;
}
}
else
{
while(t->n!=head)
{
if(t->data==val)
{
t->n->p=t->p;
t->p->n=t->n;
cout<<"\nnode
is delete...";
break;
}
t=t->n;
}
if(t->n==head)
{
if(t->data==val)
{
t->n->p=t->p;
t->p->n=t->n;
cout<<"\nnode
is delete...";
}
else
cout<<"\nInvalid
node please enter node which is present in DCLL";
}
}
}}
};
int
main()
{
dcll
d;
int
ch;
do
{
cout<<"\n1.create\n2.display\n3.search\n4.delete";
cout<<"\nEnter
choice";
cin>>ch;
switch
(ch) {
case
1:
d.accept();
break;
case
2:
d.display();
break;
case
3:
d.search();
break;
case
4:
d.remove();
break;
default:
break;
}
}while(ch!=-1);
return
0;
}
---------------------------------------------------------------------------------------------------------------------
OUTPUT:
1.create
2.display
3.search
4.delete
Enter
choice2
List
not present
1.create
2.display
3.search
4.delete
Enter
choice1
Enter
new node in DCLL :11
1.create
2.display
3.search
4.delete
Enter
choice1
Enter
new node in DCLL :22
1.create
2.display
3.search
4.delete
Enter
choice1
Enter
new node in DCLL :33
1.create
2.display
3.search
4.delete
Enter
choice1
Enter
new node in DCLL :44
1.create
2.display
3.search
4.delete
Enter
choice2
11
22
33
44
1.create
2.display
3.search
4.delete
Enter
choice3
Enter
node which u want to search33
33
node is present...
1.create
2.display
3.search
4.delete
Enter
choice3
Enter
node which u want to search55
Invalid
node please enter node which is present in DCLL
1.create
2.display
3.search
4.delete
Enter
choice2
11
22
33
44
1.create
2.display
3.search
4.delete
Enter
choice4
Enter
node which u want to delete33
node
is delete...
1.create
2.display
3.search
4.delete
Enter
choice2
11
22
44
1.create
2.display
3.search
4.delete
Enter
choice2
11
22
44
1.create
2.display
3.search
4.delete
Enter
choice4
Enter
node which u want to delete66
Invalid
node please enter node which is present in DCLL
1.create
2.display
3.search
4.delete
Enter
choice4
Enter
node which u want to delete11
head
is deleted and now new head is :22
1.create
2.display
3.search
4.delete
Enter
choice2
22
44
1.create
2.display
3.search
4.delete
Enter
choice
---------------------------------------------------------------------------------------------------------------------------
Roll
No. : SECOA179
Name :
Jadhav Sunil B.
Assignment
No. : 3
Assignment
Name : Write a program in python to perform bubble sort.
--------------------------------------------------------------------------------
def
main():
print("Enter
how many numbers do you want to sort? : ")
n=(int)(input())
print("Enter
numbers %s to sort : " % n)
i=0
a=[]
while(i<n):
val=(int)(input())
a.append(val)
i=i+1
pas=1
while(pas<n):
i=0
print("pass
no %s :" %pas)
while(i<n-pas):
print("comparison
no %s :" % (i+1))
if(a[i]>a[i+1]):
temp=a[i]
a[i]=a[i+1]
a[i+1]=temp
print(a)
i=i+1
pas=pas+1
print("sorted
numbers are : ")
print(a)
main()
--------------------------------------------------------------------------------
OUTPUT:
Enter
how many numbers do you want to sort? :
8
Enter
numbers 8 to sort :
45
-56
0
7
-5
435
55
72
pass
no 1 :
comparison
no 1 :
[-56,
45, 0, 7, -5, 435, 55, 72]
comparison
no 2 :
[-56,
0, 45, 7, -5, 435, 55, 72]
comparison
no 3 :
[-56,
0, 7, 45, -5, 435, 55, 72]
comparison
no 4 :
[-56,
0, 7, -5, 45, 435, 55, 72]
comparison
no 5 :
[-56,
0, 7, -5, 45, 435, 55, 72]
comparison
no 6 :
[-56,
0, 7, -5, 45, 55, 435, 72]
comparison
no 7 :
[-56,
0, 7, -5, 45, 55, 72, 435]
pass
no 2 :
comparison
no 1 :
[-56,
0, 7, -5, 45, 55, 72, 435]
comparison
no 2 :
[-56,
0, 7, -5, 45, 55, 72, 435]
comparison
no 3 :
[-56,
0, -5, 7, 45, 55, 72, 435]
comparison
no 4 :
[-56,
0, -5, 7, 45, 55, 72, 435]
comparison
no 5 :
[-56,
0, -5, 7, 45, 55, 72, 435]
comparison
no 6 :
[-56,
0, -5, 7, 45, 55, 72, 435]
pass
no 3 :
comparison
no 1 :
[-56,
0, -5, 7, 45, 55, 72, 435]
comparison
no 2 :
[-56,
-5, 0, 7, 45, 55, 72, 435]
comparison
no 3 :
[-56,
-5, 0, 7, 45, 55, 72, 435]
comparison
no 4 :
[-56,
-5, 0, 7, 45, 55, 72, 435]
comparison
no 5 :
[-56,
-5, 0, 7, 45, 55, 72, 435]
pass
no 4 :
comparison
no 1 :
[-56,
-5, 0, 7, 45, 55, 72, 435]
comparison
no 2 :
[-56,
-5, 0, 7, 45, 55, 72, 435]
comparison
no 3 :
[-56,
-5, 0, 7, 45, 55, 72, 435]
comparison
no 4 :
[-56,
-5, 0, 7, 45, 55, 72, 435]
pass
no 5 :
comparison
no 1 :
[-56,
-5, 0, 7, 45, 55, 72, 435]
comparison
no 2 :
[-56,
-5, 0, 7, 45, 55, 72, 435]
comparison
no 3 :
[-56,
-5, 0, 7, 45, 55, 72, 435]
pass
no 6 :
comparison
no 1 :
[-56,
-5, 0, 7, 45, 55, 72, 435]
comparison
no 2 :
[-56,
-5, 0, 7, 45, 55, 72, 435]
pass
no 7 :
comparison
no 1 :
[-56,
-5, 0, 7, 45, 55, 72, 435]
sorted
numbers are :
[-56,
-5, 0, 7, 45, 55, 72, 435]
–---------------------------------------------------------------------------
Roll
No. : SECOA179
Name :
Jadhav Sunil B.
Assignment
No. : 4
Assignment
Name : A dictionary store keyword and its meaning, provides
facility
for adding new keyword, deleting keyword and updating values of any
entry. Also provides facility to
display
whole data stored in ascending/descending order. Also find how many
maximum comparisons may be required for finding.
--------------------------------------------------------------------------------
#include
<iostream>
using
namespace
std;
class
node
{
public:
node
*l,*r;
string
k,m;
};
class
dict
{
private:
node
*root;
public:
dict()
{
root=NULL;
}
void
create()
{
node
*nn=new
node();
cout<<"\nEnter
keyword :";
cin>>nn->k;
cout<<"\nEnter
meaning :";
cin>>nn->m;
nn->l=nn->r=NULL;
if(root==NULL)
{
root=nn;
}
else
{
node
*t=root,*p;
while(t!=NULL)
{
p=t;
if(nn->k<t->k)
t=t->l;
else
t=t->r;
}
if(nn->k<p->k)
{
p->l=nn;
}
else
p->r=nn;
}
}
void
display()
{
if(root==NULL)
{
cout<<"\nDictionary
is empty\n";
}
else
{
cout<<"\nDictionary
in Ascending order :\n";
ascending(root);
cout<<"\nDictionary
in Descending order :\n";
descending(root);
}
}
void
ascending(node
*t)
{
if(t)
{
ascending(t->l);
cout<<"\t"<<t->k<<"\t"<<t->m<<endl;
ascending(t->r);
}
}
void
descending(node
*t)
{
if(t)
{
descending(t->r);
cout<<"\t"<<t->k<<"\t"<<t->m<<endl;
descending(t->l);
}
}
void
search()
{
node
*t=root;
string
val;
if(root==NULL)
{
cout<<"Dictionary
not present";
}
else
{
cout<<"\nEnter
keyword which u want to search :";
cin>>val;
while(t!=NULL)
{
if(val==t->k)
{
cout<<"Keyword
"<<val<<"
is present and its meaning is ==>"<<t->m;
return;
}
if(val<t->k)
t=t->l;
else
t=t->r;
}
if(t==NULL)
{
cout<<"Keyword
is not present in Dictionary";
}
}
}
void
remove(string
val)
{
node
*t=root,*p;
if(root==NULL)
{
cout<<"Dictionary
not present";
}
else
{
while(t!=NULL)
{
if(t->k==val)
{
if(t->l==NULL
&& t->r==NULL)
{
if(t==root)
{
root=NULL;
cout<<"Root
key is deleted..... \nAnd now Dictionary is empty";
return;
}
else
{
if(p->l==t)
{
p->l=NULL;
cout<<"key
is deleted...\n";
return;
}
else
{
p->r=NULL;
cout<<"key
is deleted...\n";
return;
}
}
}
else
{
if(t->l!=NULL
&& t->r==NULL)
{
if(t==root)
{
node
*t1=t->l;
while(t1->r)
t1=t1->r;
string
kk,mm;
kk=t1->k;
mm=t1->m;
remove(t1->k);
if(t==root)
{
cout<<"Root
key is deleted.....";
}
t->k=kk;
t->m=mm;
cout<<"\n
now next root is ==>"<<t->k;
return;
}
if(p->l==t)
{
p->l=t->l;
cout<<"key
is deleted...\n";
return;
}
else
{
p->r=t->l;
cout<<"key
is deleted...\n";
return;
}
}
else
{
if(t->l==NULL
&& t->r!=NULL)
{
if(t==root)
{
node
*t1=t->r;
while(t1->l)
t1=t1->l;
string
kk,mm;
kk=t1->k;
mm=t1->m;
remove(t1->k);
if(t==root)
{
cout<<"Root
key is deleted.....";
}
t->k=kk;
t->m=mm;
cout<<"\n
now next root is ==>"<<t->k;
return;
}
if(p->l==t)
{
p->l=t->r;
cout<<"key
is deleted...\n";
return;
}
else
{
p->r=t->r;
cout<<"key
is deleted...\n";
return;
}
}
}
}
if(t->l!=NULL
&& t->r!=NULL)
{
if(t==root)
{
node
*t1=t->r;
while(t1->l)
t1=t1->l;
string
kk,mm;
kk=t1->k;
mm=t1->m;
remove(t1->k);
if(t==root)
{
cout<<"Root
key is deleted.....";
}
t->k=kk;
t->m=mm;
cout<<"\nNow
next root is ==>"<<t->k;
return;
}
node
*t1=t->l;
while(t1->r)
t1=t1->r;
string
kk,mm;
kk=t1->k;
mm=t1->m;
remove(t1->k);
t->k=kk;
t->m=mm;
return;
}
}
else{
p=t;
if(val<t->k)
t=t->l;
else
t=t->r;
}
}
if(t==NULL)
{
cout<<val<<"
keyword is not present in dictionary";
}
} }
};
int
main()
{
dict
d;
int
ch;
string
val;
do
{
cout<<"\n1.insert\n2.display\n3.search\n4.delete\n";
cout<<"\nEnter
ur
choice :";
cin>>ch;
switch
(ch) {
case
1:
d.create();
break;
case
2:
d.display();
break;
case
3:
d.search();
break;
case
4:
cout<<"\nEnter
key which u want to delete :";
cin>>val;
d.remove(val);
break;
default:
cout<<"Invalid
choice";
break;
}
}while(ch!=-1);
return
0;
}
----------------------------------------------------------------------------------------------------------------------
OUTPUT:
1.insert
2.display
3.search
4.delete
Enter
ur choice :1
Enter
keyword :security
Enter
meaning :protection
1.insert
2.display
3.search
4.delete
Enter
ur choice :1
Enter
keyword :act
Enter
meaning :low
1.insert
2.display
3.search
4.delete
Enter
ur choice :1
Enter
keyword :java
Enter
meaning :programming_lang.
1.insert
2.display
3.search
4.delete
Enter
ur choice :1
Enter
keyword :www
Enter
meaning :world_wide_web
1.insert
2.display
3.search
4.delete
Enter
ur choice :1
Enter
keyword :movie
Enter
meaning :picture
1.insert
2.display
3.search
4.delete
Enter
ur choice :2
Dictionary
in Ascending order :
act low
java programming_lang.
movie picture
security protection
www world_wide_web
Dictionary
in Descending order :
www world_wide_web
security protection
movie picture
java programming_lang.
act low
1.insert
2.display
3.search
4.delete
Enter
ur choice :3
Enter
keyword which u want to search :java
Keyword
java is present and its meaning is ==>programming_lang.
1.insert
2.display
3.search
4.delete
Enter
ur choice :3
Enter
keyword which u want to search :radhika
Keyword
is not present in Dictionary
1.insert
2.display
3.search
4.delete
Enter
ur choice :4
Enter
key which u want to delete :act
key
is deleted...
1.insert
2.display
3.search
4.delete
Enter
ur choice :4
Enter
key which u want to delete :security
key
is deleted...
Root
key is deleted.....
Now
next root is ==>www
1.insert
2.display
3.search
4.delete
Enter
ur choice :2
Dictionary
in Ascending order :
java programming_lang.
movie picture
www world_wide_web
Dictionary
in Descending order :
www world_wide_web
movie picture
java programming_lang.
1.insert
2.display
3.search
4.delete
Enter
ur choice :4
Enter
key which u want to delete :java
key
is deleted...
1.insert
2.display
3.search
4.delete
Enter
ur choice :4
Enter
key which u want to delete :www
key
is deleted...
Root
key is deleted.....
now
next root is ==>movie
1.insert
2.display
3.search
4.delete
Enter
ur choice :1
Enter
keyword :SAndy
Enter
meaning :SUnil
1.insert
2.display
3.search
4.delete
Enter
ur choice :2
Dictionary
in Ascending order :
SAndy SUnil
movie picture
Dictionary
in Descending order :
movie picture
SAndy SUnil
1.insert
2.display
3.search
4.delete
Enter
ur choice :4
Enter
key which u want to delete :movie
key
is deleted...
Root
key is deleted.....
now
next root is ==>SAndy
1.insert
2.display
3.search
4.delete
Enter
ur choice :4
Enter
key which u want to delete :SAndy
Root
key is deleted.....
And
now Dictionary is empty
1.insert
2.display
3.search
4.delete
Enter
ur choice :2
Dictionary
is empty
1.insert
2.display
3.search
4.delete
Enter
ur choice :
---------------------------------------------------------------------------------------------------------------------
Roll
No. : SECOA179
Name :
Jadhav Sunil B.
Assignment
No. : 5
Assignment
Name : Write a program using object oriented programming using c++
to
create a binary tree if inorder and preorder or inorder or postorder
any and traversals are given.
------------------------------------------------------------------------------
1)
if inorder and preorder
--------------------------
#include<iostream>
using
namespace
std;
struct
tnode
{
int
data;
tnode
*l,*r;
};
class
tree
{
private:
int
in[10],
pre[10],n;
tnode
*root;
int
preI=0;
public:
tree()
{
root=
NULL;
n=0;
}
void
accept();
void
create_tree();
int
find_index(int);
tnode
*b_tree(int,int);
void
display();
void
inorder(tnode
*);
void
postorder(tnode
*);
};
void
tree::
accept()
{
cout<<"\n
How many nodes are there in the tree?";
cin>>n;
cout<<"\n
Enter the Inorder
Travaersal\n";
for(int
i=0;i<n;i++)
cin>>in[i];
cout<<"\n
Enter the preorder
Traversal\n ";
for(int
i=0;i<n;i++)
cin>>pre[i];
}
int
tree::find_index(int
val)
{
for(int
i=0;i<n;i++)
{
if(in[i]==val)
return
i;
}
return
-1;
}
tnode
*tree::b_tree(int
SI, int
EI)
{
int
idx;
tnode
*nn;
if(SI>EI)
return
NULL;
nn=
new
tnode;
nn->data=pre[preI];
preI++;
nn->l=nn->r=NULL;
if(SI==EI)
return
nn;
idx=find_index(nn->data);
nn->l=b_tree(SI,idx-1);
nn->r=b_tree(idx+1,EI);
return
nn;
}
void
tree::create_tree()
{
root=b_tree(0,n-1);
}
void
tree::
inorder(tnode
*temp)
{
if(temp)
{
inorder(temp->l);
cout<<"\t"<<temp->data;
inorder(temp->r);
}
}
void
tree
:: postorder(tnode
*temp)
{
if(temp!=NULL)
{
postorder(temp->l);
postorder(temp->r);
cout<<"\t"<<temp->data;
}
}
void
tree::display()
{
cout<<"Inorder:"<<"\t";
inorder(root);
cout<<"\n";
cout<<"postorder:"<<"\t";
postorder(root);
}
int
main()
{
tree
t;
t.accept();
t.create_tree();
t.display();
}
------------------------------------------------------------------------------
OUTPUT:
How
many nodes are there in the tree?3
Enter
the Inorder Travaersal
10
20
30
Enter
the preorder Traversal
20
10
30
Inorder: 10 20 30
postorder: 10 30 20
–------------------------------------------------------------------------
1)
if inorder and postorder
--------------------------
#include
<iostream>
using
namespace
std;
class
node
{
public:
node
*l,*r;
int
data;
};
class
bst
{
private:
int
n,in[10],post[10],p=0;
node
*root;
public:
bst()
{
root=NULL;
n=0;
}
void
accept()
{
cout<<"\nEnter
how many nodes are present in tree :";
cin>>n;
p=n-1;
cout<<"\nEnter
Inorder
data :\n";
for(int
i=0;i<n;i++)
{
cin>>in[i];
}
cout<<"\nEnter
Postorder
data :\n";
for(int
i=0;i<n;i++)
{
cin>>post[i];
}
}
void
create()
{
root=insert(0,n-1);
}
node
*insert(int
si,int
ei)
{
int
idx;
node
*nn=new
node();
if(si>ei)
return
NULL;
nn->data=post[p];
p--;
nn->l=nn->r=NULL;
if(si==ei)
return
nn;
idx=index(nn->data);
nn->r=insert(idx+1,ei);
nn->l=insert(si,idx-1);
return
nn;
}
int
index(int
val)
{
for(int
i=0;i<n;i++)
{
if(in[i]==val)
return
i;
}
return
-1;
}
void
display()
{
cout<<"\nInorder
data :";
inorder(root);
cout<<"\nPreorder
data :";
preorder(root);
}
void
inorder(node
*t)
{
if(t)
{
inorder(t->l);
cout<<"\t"<<t->data;
inorder(t->r);
}
}
void
preorder(node
*t)
{
if(t)
{
cout<<"\t"<<t->data;
preorder(t->l);
preorder(t->r);
}
}
};
int
main()
{
bst
t;
t.accept();
t.create();
t.display();
return
0;
}
------------------------------------------------------------------------------
Output
--------
Enter
how many nodes are present in tree :7
Enter
Inorder data :
20
30
40
50
60
70
80
Enter
Postorder data :
20
40
30
60
80
70
50
Inorder
data : 20 30 40 50 60 70 80
Preorder
data : 50 30 20 40 70 60 80
----------------------------------------------------------------------------------------------------------------------------
Roll
No. : SECOA179
Name :
Jadhav Sunil B.
Assignment
No. : 6
Assignment
Name : Write a c++ program to implement traversals on threaded
binary
tree using object oriented language programming features. Design
necessary class.
--------------------------------------------------------------------------------
#include
<iostream>
using
namespace
std;
class
node
{
public:
node
*l,*r;
int
data;
bool
lch,rch;
};
class
tree
{
private:
node
*root;
public:
node
*dummy;
tree()
{
dummy=new
node();
root=NULL;
dummy->data=-999;
}
void
accept()
{
node
*nn=new
node();
cout<<"Enter
new data";
cin>>nn->data;
nn->l=nn->r=NULL;
nn->lch=nn->rch=0;
if(root==NULL)
{
root=nn;
nn->lch=nn->rch=0;
root->l=root->r=dummy;
dummy->l=root;
}
else
{
node
*t=root,*p;
while(t!=NULL)
{
p=t;
if(nn->data<t->data)
{
if(t->lch==1)
t=t->l;
else
t=NULL;
}
else
{
if(t->rch==1)
t=t->r;
else
t=NULL;
}
}
if(nn->data<p->data)
{
nn->l=p->l;
nn->r=p;
p->l=nn;
p->lch=1;
}
else
{
nn->r=p->r;
nn->l=p;
p->r=nn;
p->rch=1;
}
}
}
void
display()
{
cout<<"\nInorder
:\n";
inorder();
cout<<"\nPreorder
:\n";
preorder();
}
void
inorder()
{
node
*t=root;
while(t!=dummy)
{
while(t->lch==1)
t=t->l;
cout<<"\t"<<t->data;
while(t->rch==0)
{
if(t->r==dummy)
return;
t=t->r;
cout<<"\t"<<t->data;
}
t=t->r;
}
}
void
preorder()
{
node
*t=root;
while(t!=dummy)
{
while(t->lch==1)
{
cout<<"\t"<<t->data;
t=t->l;
}
cout<<"\t"<<t->data;
while(t->rch==0)
{
if(t->r==dummy)
return;
t=t->r;
}
t=t->r;
}
}
};
int
main()
{
tree
t;
int
ch;
do
{
cout<<"\n1.create\n2.display\n";
cout<<"Enter
ur
choice";
cin>>ch;
switch
(ch) {
case
1:
t.accept();
break;
case
2:
t.display();
break;
default:
cout<<"Invalid
choice";
break;
}
}while(ch!=-1);
return
0;
}
----------------------------------------------------------------------------
OUTPUT
:
1.create
2.display
Enter
ur choice1
Enter
new data50
1.create
2.display
Enter
ur choice1
Enter
new data70
1.create
2.display
Enter
ur choice1
Enter
new data10
1.create
2.display
Enter
ur choice1
Enter
new data30
1.create
2.display
Enter
ur choice1
Enter
new data90
1.create
2.display
Enter
ur choice2
Inorder
:
10 30 50 70 90
Preorder
:
50 10 30 70 90
1.create
2.display
Enter
ur choice
---------------------------------------------------------------------------------------------------------------------------
Roll
No. : SECOA179
Name :
Jadhav Sunil B.
Assignment
No. : 7
Assignment
Name : Write a program for searching a word and its meaning and
create
reasonably balanced tree using object oriented feature.
------------------------------------------------------------------------------
avlfile.txt
-----------
| v : Vaibhav |
| s
: SUnil |
| p
: Pratik |
| k
: Kaushal |
| b
: Bhagwat |
| s :
sai |
| h
: Harshad |
| u
: Umakant |
| b : Balaji |
| a : Anil |
| r : Ram |
---------------------------------------------------------------------------------------------------------------------
#include<iostream>
#include<fstream>
using
namespace
std;
class
node
{
public:
string
key,mean;
node
*l,*r;
};
class
avl
{
private:
node
*root;
public:
avl()
{
root=NULL;
}
void
create();
void
dispaly();
void
inorder(node
*);
node
*insert(node
*,string,string);
node
*RR(node
*);
node
*RL(node
*);
node
*LL(node
*);
node
*LR(node
*);
int
height(node
*);
int
max(int,int);
};
void
avl::create()
{
fstream
f;
f.open("7.file.txt",ios::in);
char
k[10],m[20],data[30];
while(f)
{
int
i,j;
f.getline(data,30);
if(f)
{
for(i=0;data[i]!=':';i++)
k[i]=data[i];
k[i]='\0';
i++;
for(j=0;data[i]!='\0';j++,i++)
{
m[j]=data[i];
}
m[j]='\0';
root=insert(root,k,m);
}
}
}
node
* avl
:: insert(node
*t,string
k,string
m)
{
if(t==NULL)
{
t=new
node();
t->key=k;
t->mean=m;
t->l=t->r=NULL;
}
else
{
if(k<t->key)
{
t->l=insert(t->l,k,m);
if(height(t->l)-height(t->r)==2
|| height(t->l)-height(t->r)==-2)
{
if(k<t->l->key)
t=LL(t);
else
t=LR(t);
}//
if
}//else
else
{
t->r=insert(t->r,k,m);
if(height(t->l)-height(t->r)==2
|| height(t->l)-height(t->r)==-2)
{
if(k>t->r->key)
t=RR(t);
else
t=RL(t);
}//if
}//else
}
return
t;
}
node
* avl::LL(node
*p)
{
node
*t=p->l;
p->l=t->r;
t->r=p;
return
t;
}
node
* avl::RR(node
*p)
{
node
*t=p->r;
p->r=t->l;
t->l=p;
return
t;
}
node
* avl::LR(node
*p)
{
p->l=RR(p->l);
p=LL(p);
return
p;
}
node
* avl::RL(node
*p)
{
p->r=LL(p->r);
p=RR(p);
return
p;
}
void
avl::dispaly()
{
inorder(root);
}
void
avl::inorder(node
*root)
{
node
* t=root;
if(t!=NULL)
{
inorder(t->l);
cout<<"\n"<<t->key<<"
: "<<t->mean;
inorder(t->r);
}
}
int
avl::height(node
*t)
{
if(t==NULL)
return
-1;
if(t->l==NULL
&& t->r==NULL)
return
0;
return(1+max(height(t->l),height(t->r)));
}
int
avl::max(int
a, int
b)
{
if(a>b)
return
a;
else
return
b;
}
int
main()
{
avl
t1;
cout<<"|
Keyword : "<<"meaning |"<<endl;
cout<<"-------------------------------------";
t1.create();
t1.dispaly();
cout<<endl<<"-------------------------------------";
return
0;
}
------------------------------------------------------------------------------
OUTPUT
:
|
Keyword : meaning |
-------------------------------------
| a
: Anil |
| b
: Balaji |
| b
: Bhagwat |
| h
: Harshad |
| k
: Kaushal |
| p
: Pratik |
| r
: Ram |
| s
: sai |
| s
: SUnil |
| u
: Umakant |
| v
: Vaibhav |
-------------------------------------
---------------------------------------------------------------------------------------------------------------------------
Roll
No. : SECOA179
Name :
Jadhav Sunil B.
Assignment
No. : 8
Assignment
Name : A newspaper delivery boy everyday drops newspaper in a
society
having many lanes and each lane have many houses. Design a program
to provide different path that he could
follow
and also suggest the pat which will make him to
finish
his task with less efforts. Solve the problem by
suggesting
appropriate data structure.
--------------------------------------------------------------------------------
#include
<iostream>
using
namespace std;
class
graph
{
private:
int
n,adj[10][10],visited[10],sum;
public:
graph()
{
for(int
v=0;v<10;v++)
{
for(int
i=0;i<10;i++)
{
adj[v][i]=0;
}
}
for(int
i=0;i<10;i++)
visited[i]=0;
sum=0;
n=0;
}
void
accept();
void
display();
void
dfs(int v);
void
init();
};
void
graph :: accept()
{
cout<<"\n\nEnter
number of houses : ";
cin>>n;
for(int
v=1;v<=n;v++)
{
for(int
i=1;i<=n;i++)
{
if(v!=i)
{
cout<<"\nEnter
distance between house "<<v<<"-"<<i<<"
(if they are connected else enter 0) : ";
cin>>adj[v][i];
}
}
}
}
void
graph :: init()
{
for(int
i=0;i<10;i++)
visited[i]=0;
}
void
graph :: dfs(int v)
{
visited[v]=1;
cout<<v<<"-->";
for(int
i=1;i<=n;i++)
{
if(visited[i]==0
&& adj[v][i]!=0)
{
sum=sum+adj[v][i];
dfs(i);
}
}
}
void
graph :: display()
{
for(int
v=1;v<=n;v++)
{
sum=0;
cout<<"\nAssuming
"<<v<<" as starting house : \n";
init();
dfs(v);
cout<<"\nTotal
distance : "<<sum;
}
}
int
main()
{
graph
g;
g.accept();
g.display();
return
0;
}
--------------------------------------------------------------------------------
OUTPUT:
Enter
number of houses : 6
Enter
distance between house 1-2 (if they are connected else enter 0) : 1
Enter
distance between house 1-3 (if they are connected else enter 0) : 0
Enter
distance between house 1-4 (if they are connected else enter 0) : 3
Enter
distance between house 1-5 (if they are connected else enter 0) : 0
Enter
distance between house 1-6 (if they are connected else enter 0) : 0
Enter
distance between house 2-1 (if they are connected else enter 0) : 1
Enter
distance between house 2-3 (if they are connected else enter 0) : 0
Enter
distance between house 2-4 (if they are connected else enter 0) : 0
Enter
distance between house 2-5 (if they are connected else enter 0) : 2
Enter
distance between house 2-6 (if they are connected else enter 0) : 0
Enter
distance between house 3-1 (if they are connected else enter 0) : 5
Enter
distance between house 3-2 (if they are connected else enter 0) : 0
Enter
distance between house 3-4 (if they are connected else enter 0) : 0
Enter
distance between house 3-5 (if they are connected else enter 0) : 0
Enter
distance between house 3-6 (if they are connected else enter 0) : 7
Enter
distance between house 4-1 (if they are connected else enter 0) : 3
Enter
distance between house 4-2 (if they are connected else enter 0) : 0
Enter
distance between house 4-3 (if they are connected else enter 0) : 0
Enter
distance between house 4-5 (if they are connected else enter 0) : 4
Enter
distance between house 4-6 (if they are connected else enter 0) : 8
Enter
distance between house 5-1 (if they are connected else enter 0) : 0
Enter
distance between house 5-2 (if they are connected else enter 0) : 2
Enter
distance between house 5-3 (if they are connected else enter 0) : 0
Enter
distance between house 5-4 (if they are connected else enter 0) : 4
Enter
distance between house 5-6 (if they are connected else enter 0) : 0
Enter
distance between house 6-1 (if they are connected else enter 0) : 0
Enter
distance between house 6-2 (if they are connected else enter 0) : 0
Enter
distance between house 6-3 (if they are connected else enter 0) : 7
Enter
distance between house 6-4 (if they are connected else enter 0) : 8
Enter
distance between house 6-5 (if they are connected else enter 0) : 0
Assuming
1 as starting house :
1-->2-->5-->4-->6-->3-->
Total
distance : 22
Assuming
2 as starting house :
2-->1-->4-->5-->6-->3-->
Total
distance : 23
Assuming
3 as starting house :
3-->1-->2-->5-->4-->6-->
Total
distance : 20
Assuming
4 as starting house :
4-->1-->2-->5-->6-->3-->
Total
distance : 21
Assuming
5 as starting house :
5-->2-->1-->4-->6-->3-->
Total
distance : 21
Assuming
6 as starting house :
6-->3-->1-->2-->5-->4-->
Total
distance : 19
–----------------------------------------------------------------------------
Roll
No. : SECOA179
Name :
Jadhav Sunil B.
Assignment
No. : 9
Assignment
Name : Write a program to find shortest path for given source and
destination of a given graph using c.
--------------------------------------------------------------------------------
#include<stdio.h>
#include<math.h>
void
dij(int n,int s,int d,int cost[10][10],int dist[10])
{
int
i,j,k,min,flag[10],count=2;
printf("\n\nEnter
following data for finding Shortest path :");
printf("\nEnter
Source : ");
scanf("%d",&s);
printf("\nEnter
Destination : ");
scanf("%d",&d);
for(i=1;i<=n;i++)
{
dist[i]=cost[s][i];
flag[i]=0;
}
flag[s]=1;
while(count<n)
{
min=999;
for(i=1;i<=n;i++)
{
if(min>dist[i]&&flag[i]==0)
{
min=dist[i];
k=i;
}
}
flag[k]=1;
count++;
for(j=1;j<=n;j++)
{
if(dist[j]>dist[k]+cost[k][j]&&flag[j]==0)
dist[j]=dist[k]+cost[k][j];
}
}
printf("Shortest
distance between node %d - %d : %d",s,d,dist[d]);
}
int
main()
{
int
n,cost[10][10],dist[10],s,d,i,j;
printf("\n\nEnter
how many nodes are present in graph : ");
scanf("%d",&n);
printf("\n\nEnter
the weight of edge between nodes(if not connected enter 0) : \n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i==j)
{
cost[i][j]=999;
}
else
{
printf("\nEnter
the weight of edge between nodes %d - %d : ",i,j);
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
}
}
dij(n,s,d,cost,dist);
dij(n,s,d,cost,dist);
dij(n,s,d,cost,dist);
return
0;
}
--------------------------------------------------------------------------------
OUTPUT:
Enter
how many nodes are present in graph : 6
Enter
the weight of edge between nodes(if not connected enter 0) :
Enter
the weight of edge between nodes 1 - 2 : 1
Enter
the weight of edge between nodes 1 - 3 : 2
Enter
the weight of edge between nodes 1 - 4 : 0
Enter
the weight of edge between nodes 1 - 5 : 0
Enter
the weight of edge between nodes 1 - 6 : 0
Enter
the weight of edge between nodes 2 - 1 : 1
Enter
the weight of edge between nodes 2 - 3 : 0
Enter
the weight of edge between nodes 2 - 4 : 2
Enter
the weight of edge between nodes 2 - 5 : 1
Enter
the weight of edge between nodes 2 - 6 : 0
Enter
the weight of edge between nodes 3 - 1 : 2
Enter
the weight of edge between nodes 3 - 2 : 0
Enter
the weight of edge between nodes 3 - 4 : 0
Enter
the weight of edge between nodes 3 - 5 : 2
Enter
the weight of edge between nodes 3 - 6 : 0
Enter
the weight of edge between nodes 4 - 1 : 0
Enter
the weight of edge between nodes 4 - 2 : 2
Enter
the weight of edge between nodes 4 - 3 : 0
Enter
the weight of edge between nodes 4 - 5 : 0
Enter
the weight of edge between nodes 4 - 6 : 3
Enter
the weight of edge between nodes 5 - 1 : 0
Enter
the weight of edge between nodes 5 - 2 : 1
Enter
the weight of edge between nodes 5 - 3 : 2
Enter
the weight of edge between nodes 5 - 4 : 0
Enter
the weight of edge between nodes 5 - 6 : 1
Enter
the weight of edge between nodes 6 - 1 : 0
Enter
the weight of edge between nodes 6 - 2 : 0
Enter
the weight of edge between nodes 6 - 3 : 0
Enter
the weight of edge between nodes 6 - 4 : 3
Enter
the weight of edge between nodes 6 - 5 : 1
Enter
following data for finding Shortest path :
Enter
Source : 1
Enter
Destination : 6
Shortest
distance between node 1 - 6 : 3
Enter
following data for finding Shortest path :
Enter
Source : 4
Enter
Destination : 3
Shortest
distance between node 4 - 3 : 5
Enter
following data for finding Shortest path :
Enter
Source : 5
Enter
Destination : 1
Shortest
distance between node 5 - 1 : 2
-----------------------------------------------------------------------------
Roll
No. : SECOA179
Name :
Jadhav Sunil B.
Assignment
No. : 10
Assignment
Name : Write a tic-tac-toe gaming application using c++
programming.
--------------------------------------------------------------------------------
#include
<iostream>
using
namespace
std;
char
sq[10]={'0','1','2','3','4','5','6','7','8','9'};
char
sq1[10]={'
','
','
','
','
','
','
','
','
','
'};
void
board()
{
cout<<"\n'Tic
Tac
Toe'\n ";
cout<<"-------------------";
cout<<"\nPlayer
1:*\n";
cout<<"Player
2:o\n";
cout<<"
"<<"_____"<<"_"<<"_____"<<"_"<<"_____"<<"_"<<endl;
cout<<"|"<<"
"<<sq1[1]<<"
"<<"|"<<"
"<<sq1[2]<<"
"<<"|"<<"
"<<sq1[3]<<"
"<<"|"<<endl;
cout<<"|"<<"_____"<<"|"<<"_____"<<"|"<<"_____"<<"|"<<endl;
cout<<"|"<<"
"<<sq1[4]<<"
"<<"|"<<"
"<<sq1[5]<<"
"<<"|"<<"
"<<sq1[6]<<"
"<<"|"<<endl;
cout<<"|"<<"_____"<<"|"<<"_____"<<"|"<<"_____"<<"|"<<endl;
cout<<"|"<<"
"<<sq1[7]<<"
"<<"|"<<"
"<<sq1[8]<<"
"<<"|"<<"
"<<sq1[9]<<"
"<<"|"<<endl;
cout<<"|"<<"_____"<<"|"<<"_____"<<"|"<<"_____"<<"|"<<endl<<endl;
}
int
checkwin()
{
if(sq[1]==sq[2]
&& sq[2]==sq[3])
return
1;
else
if(sq[4]==sq[5]
&& sq[5]==sq[6])
return
1;
else
if(sq[7]==sq[8]
&& sq[8]==sq[9])
return
1;
else
if(sq[1]==sq[4]
&& sq[4]==sq[7])
return
1;
else
if(sq[2]==sq[5]
&& sq[5]==sq[8])
return
1;
else
if(sq[3]==sq[6]
&& sq[6]==sq[9])
return
1;
else
if(sq[1]==sq[5]
&& sq[5]==sq[9])
return
1;
else
if(sq[3]==sq[5]
&& sq[5]==sq[7])
return
1;
else
if(sq[1]!='1'
&& sq[2]!='2'
&& sq[3]!='3'
&& sq[4]!='4'
&& sq[5]!='5'
&& sq[6]!='6'
&& sq[7]!='7'
&& sq[8]!='8'
&& sq[9]!='9')
return
0;
else
return
-1;
}
int
main()
{
int
i,ch,player=0;
string
p1,p2;
char
mark;
cout<<"Enter
Playes
name :\n";
cout<<"Player
1:";
cin>>p1;
cout<<"\nPlayer
2:";
cin>>p2;
do
{
board();
player++;
player=(player%2)?1:2;
if(player==1)
cout<<"Player
1:"<<p1;
else
cout<<"Player
2:"<<p2;
cout<<"\nEnter
ur
choice";
cin>>ch;
mark=(player==1)?'*':'o';
if(ch==1
&& sq[1]=='1')
{
sq[1]=mark;
sq1[1]=mark;
}
else
if(ch==2
&& sq[2]=='2')
{
sq[2]=mark;
sq1[2]=mark;
}
else
if(ch==3
&& sq[3]=='3')
{
sq[3]=mark;
sq1[3]=mark;
}
else
if(ch==4
&& sq[4]=='4')
{
sq[4]=mark;
sq1[4]=mark;
}
else
if(ch==5
&& sq[5]=='5')
{
sq[5]=mark;
sq1[5]=mark;
}
else
if(ch==6
&& sq[6]=='6')
{
sq[6]=mark;
sq1[6]=mark;
}
else
if(ch==7
&& sq[7]=='7')
{
sq[7]=mark;
sq1[7]=mark;
}
else
if(ch==8
&& sq[8]=='8')
{
sq[8]=mark;
sq1[8]=mark;
}
else
if(ch==9
&& sq[9]=='9')
{
sq[9]=mark;
sq1[9]=mark;
}
else
{
player--;
cout<<"Invalid
choice";
}
i=checkwin();
}while(i==-1);
board();
if(i==1)
{
if(player==1)
cout<<"\n==>
Player 1:"<<p1<<"
own the game";
else
cout<<"\n==>
player 2:"<<p2<<"
own the game";
}
else
cout<<"\n==>
Game Draw";
return
0;
}
------------------------------------------------------------------------------
1.OUTPUT:
Enter
Playes name :
Player
1:SAndy
Player
2:SUnil
'Tic
Tac Toe'
-------------------
Player
1:*
Player
2:o
__________________
|
| | |
|_____|_____|_____|
|
| | |
|_____|_____|_____|
|
| | |
|_____|_____|_____|
Player
1:SAndy
Enter
ur choice5
'Tic
Tac Toe'
-------------------
Player
1:*
Player
2:o
__________________
|
| | |
|_____|_____|_____|
|
| * | |
|_____|_____|_____|
|
| | |
|_____|_____|_____|
Player
2:SUnil
Enter
ur choice1
'Tic
Tac Toe'
-------------------
Player
1:*
Player
2:o
__________________
|
o | | |
|_____|_____|_____|
|
| * | |
|_____|_____|_____|
|
| | |
|_____|_____|_____|
Player
1:SAndy
Enter
ur choice3
'Tic
Tac Toe'
-------------------
Player
1:*
Player
2:o
__________________
|
o | | * |
|_____|_____|_____|
|
| * | |
|_____|_____|_____|
|
| | |
|_____|_____|_____|
Player
2:SUnil
Enter
ur choice7
'Tic
Tac Toe'
-------------------
Player
1:*
Player
2:o
__________________
|
o | | * |
|_____|_____|_____|
|
| * | |
|_____|_____|_____|
|
o | | |
|_____|_____|_____|
Player
1:SAndy
Enter
ur choice4
'Tic
Tac Toe'
-------------------
Player
1:*
Player
2:o
__________________
|
o | | * |
|_____|_____|_____|
|
* | * | |
|_____|_____|_____|
|
o | | |
|_____|_____|_____|
Player
2:SUnil
Enter
ur choice6
'Tic
Tac Toe'
-------------------
Player
1:*
Player
2:o
__________________
|
o | | * |
|_____|_____|_____|
|
* | * | o |
|_____|_____|_____|
|
o | | |
|_____|_____|_____|
Player
1:SAndy
Enter
ur choice2
'Tic
Tac Toe'
-------------------
Player
1:*
Player
2:o
__________________
|
o | * | * |
|_____|_____|_____|
|
* | * | o |
|_____|_____|_____|
|
o | | |
|_____|_____|_____|
Player
2:SUnil
Enter
ur choice8
'Tic
Tac Toe'
-------------------
Player
1:*
Player
2:o
__________________
|
o | * | * |
|_____|_____|_____|
|
* | * | o |
|_____|_____|_____|
|
o | o | |
|_____|_____|_____|
Player
1:SAndy
Enter
ur choice9
'Tic
Tac Toe'
-------------------
Player
1:*
Player
2:o
__________________
|
o | * | * |
|_____|_____|_____|
|
* | * | o |
|_____|_____|_____|
|
o | o | * |
|_____|_____|_____|
==>
Game Draw
2.OUTPUT:
Enter
Playes name :
Player
1:SAndy
Player
2:SUnil
'Tic
Tac Toe'
-------------------
Player
1:*
Player
2:o
__________________
|
| | |
|_____|_____|_____|
|
| | |
|_____|_____|_____|
|
| | |
|_____|_____|_____|
Player
1:SAndy
Enter
ur choice7
'Tic
Tac Toe'
-------------------
Player
1:*
Player
2:o
__________________
|
| | |
|_____|_____|_____|
|
| | |
|_____|_____|_____|
|
* | | |
|_____|_____|_____|
Player
2:SUnil
Enter
ur choice5
'Tic
Tac Toe'
-------------------
Player
1:*
Player
2:o
__________________
|
| | |
|_____|_____|_____|
|
| o | |
|_____|_____|_____|
|
* | | |
|_____|_____|_____|
Player
1:SAndy
Enter
ur choice1
'Tic
Tac Toe'
-------------------
Player
1:*
Player
2:o
__________________
|
* | | |
|_____|_____|_____|
|
| o | |
|_____|_____|_____|
|
* | | |
|_____|_____|_____|
Player
2:SUnil
Enter
ur choice4
'Tic
Tac Toe'
-------------------
Player
1:*
Player
2:o
__________________
|
* | | |
|_____|_____|_____|
|
o | o | |
|_____|_____|_____|
|
* | | |
|_____|_____|_____|
Player
1:SAndy
Enter
ur choice6
'Tic
Tac Toe'
-------------------
Player
1:*
Player
2:o
__________________
|
* | | |
|_____|_____|_____|
|
o | o | * |
|_____|_____|_____|
|
* | | |
|_____|_____|_____|
Player
2:SUnil
Enter
ur choice2
'Tic
Tac Toe'
-------------------
Player
1:*
Player
2:o
__________________
|
* | o | |
|_____|_____|_____|
|
o | o | * |
|_____|_____|_____|
|
* | | |
|_____|_____|_____|
Player
1:SAndy
Enter
ur choice3
'Tic
Tac Toe'
-------------------
Player
1:*
Player
2:o
__________________
|
* | o | * |
|_____|_____|_____|
|
o | o | * |
|_____|_____|_____|
|
* | | |
|_____|_____|_____|
Player
2:SUnil
Enter
ur choice8
'Tic
Tac Toe'
-------------------
Player
1:*
Player
2:o
__________________
|
* | o | * |
|_____|_____|_____|
|
o | o | * |
|_____|_____|_____|
|
* | o | |
|_____|_____|_____|
==>
player 2:SUnil own the game
------------------------------------------------------------------------------
Name :
Jadhav Sunil B.
Assignment
No. : 11
Assignment
Name : Write a program to create a binary tree if preorder and
inorder
traversals are given in JAVA.
--------------------------------------------------------------------------------
import
java.io.*;
class
Node
{
public
int
data;
public
Node left,right;
}
public
class
traverse
{
int
in[]=new
int[10];
int
pre[]=new
int[10];
public
static
int
preI=0;
Node
root=null;
BufferedReader
br=new
BufferedReader(new
InputStreamReader(System.in));
public
void
Readin()
{
for(int
i=0;i<5;i++)
{
try
{
System.out.println("Inorder:"+(i+1)+":");
in[i]=Integer.parseInt(br.readLine());
}
catch(Exception
e)
{
System.out.println(e);
}
}
}
public
void
Readpre()
{
for(int
i=0;i<5;i++)
{
try
{
System.out.println("Preorder:"+(i+1)+":");
pre[i]=Integer.parseInt(br.readLine());
}
catch(Exception
e)
{
System.out.println(e);
}
}
}
public
int
position(int
data)
{
for(int
i=0;i<5;i++)
{
if(in[i]==data)
return
i;
}
return
0;
}
public
void
create()
{
root=insert(0,4);
}
public
Node insert(int
SI,int
EI)
{
int
idx;
Node
nn;
nn=new
Node();
if(SI>EI)
return
null;
nn.data=pre[preI];
preI++;
nn.left=nn.right=null;
if(SI==EI)
return
nn;
idx=position(nn.data);
nn.left=insert(SI,idx-1);
nn.right=insert(idx+1,EI);
return
nn;
}
public
void
postorder(Node temp)
{
if(temp!=null)
{
postorder(temp.left);
postorder(temp.right);
System.out.println(temp.data);
}
}
public
static
void
main(String args[])
{
traverse
t=new
traverse();
System.out.println("Enter
inorder:");
t.Readin();
System.out.println("Enter
preorder:");
t.Readpre();
t.create();
System.out.println("Postorder
of created tree:");
t.postorder(t.root);
}
}
-----------------------------------------------------------------
OUTPUT:
Enter
inorder:
Inorder:1:
40
Inorder:2:
20
Inorder:3:
50
Inorder:4:
10
Inorder:5:
30
Enter
preorder:
Preorder:1:
10
Preorder:2:
20
Preorder:3:
40
Preorder:4:
50
Preorder:5:
30
Postorder
of created tree:
40
50
20
30
10
==============================================================================
Thanks!
==============================================================================