-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEXP8.cpp
More file actions
137 lines (137 loc) · 3.35 KB
/
Copy pathEXP8.cpp
File metadata and controls
137 lines (137 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream>
#define SIZE 10
using namespace std;
class optimal
{
public:
int p[SIZE];
int q[SIZE];
int a[SIZE];
int w[SIZE][SIZE];
int c[SIZE][SIZE];
int r[SIZE][SIZE];
int n;
int front,rear,queue[20];
optimal() //default constructor
{
front=rear=-1;
}
void getdata();
int minvalue(int,int);
void OBST();
void buildtree();
};
void optimal::getdata()
{
int i;
cout<<"\n Optimal Binary search tree";
cout<<"\n Enter the number of nodes :";
cin>>n;
cout<<"\n Enter the data : \n";
for (i=1;i<=n;i++)
{
cout<<"\n a["<<i<<"]:";
cin>>a[i];
}
cout<<"\n Enter probalities for successful search \n";
for(i=1;i<=n;i++)
{
cout<<"p["<<i<<"]:";
cin>>p[i];
}
cout<<"\n Enter probalities for unsuccessful search \n";
for(i=1;i<=n;i++)
{
}
cout<<"q["<<i<<"]:";
cin>>q[i];
}
/* This function returns a value in range r[i][j-1] to r[i+1][j] so that cost c[i][k-1]+ c[k][j] is
minimum */
int optimal::minvalue(int i,int j)
{
int m,k;
int min=32000;
for(m=r[i][j-1];m<=r[i+1][j];m++)
{
if((c[i][m-1]+c[m][j])<min)
{
min=c[i][m-1]+c[m][j];
k=m;
}
}
return k;
}
/* This function builds table from all given probalities. it basically computes C,r,w value */
void optimal::OBST()
{
int i,j,k,m;
for(i=0;i<n;i++)
{
//initialize
w[i][i]=q[i];
r[i][i]=c[i][i]=0;
//optimal trees with one node
w[i][i+1]=q[i]+q[i+1]+p[i+1];
r[i][i+1]=i+1;
c[i][i+1]=q[i]+q[i+1]+p[i+1];
}
w[n][n]=q[n];
r[n][n]=c[n][n]=0;
//find optimal trees with m nodes
for(m=2;m<=n;m++)
{
for(i=0;i<=n-m;i++)
{
j=i+m;
w[i][j]=w[i][j-1]+p[j]+q[j];
k=minvalue(i,j);
c[i][j]=w[i][j]+c[i][k-1]+c[k][j];
r[i][j]=k;
}
}
}
/* This function builds tree from table made by OBST function */
void optimal::buildtree()
{
int i,j,k;
cout<<"\n The optimal Binary search tree for given nodes is : \n";
cout<<"\n The root of this OBST is :"<<r[0][n];
cout<<"\n The cost of this OBST is: "<<c[0][n];
cout<<"\n\n Node \t Left child \t Right child";
cout<<"\n _____________________________________"<<endl;
queue[++rear]=0;
queue[++rear]=n;
while(front!=rear)
{
i=queue[++front];
j=queue[++front];
k=r[i][j];
cout<<"\n\t"<<k;
if(r[i][k-1]!=0)
{
cout<<" "<<r[i][k-1];
queue[++rear]=i;
queue[++rear]=k-1;
}
else
cout<<" ";
if(r[k][j]!=0)
{
cout<<" "<<r[k][j];
queue[++rear]=k;
queue[++rear]=j;
}
else
cout<<" ";
}
cout<<endl;
}
/* This is main function */
int main() {
optimal obj;
obj.getdata();
obj.OBST();
obj.buildtree();
return 0;
}