-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path09.txt
87 lines (84 loc) · 1.99 KB
/
09.txt
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
//Given sequence k = k1 <k2 < … < kn of n sorted keys, with a search probability pi for each key
ki . Build the Binary search tree that has the least search cost given the access probability for
each key.
#include<iostream>
using namespace std;
void con_obst(void);
void print(int,int);
float a[20],b[20],wt[20][20],c[20][20];
int r[20][20],n;
int main()
{
int i;
cout<<"\n****** PROGRAM FOR OBST ******\n";
cout<<"\nEnter the no. of nodes : ";
cin>>n;cout<<"\nEnter the probability for successful search :: ";
cout<<"\n————————————————\n";
for(i=1;i<=n;i++)
{
cout<<"p["<<i<<"]";
cin>>a[i];
}
cout<<"\nEnter the probability for unsuccessful search :: ";
cout<<"\n————————————————–\n";
for(i=0;i<=n;i++)
{
cout<<"q["<<i<<"]";
cin>>b[i];
}
con_obst();
print(0,n);
cout<<endl;
}
void con_obst(void)
{
int i,j,k,l,min;
for(i=0;i<n;i++)
{ //Initialisation
c[i][i]=0.0;
r[i][i]=0;
wt[i][i]=b[i];
// for j-i=1 can be j=i+1
wt[i][i+1]=b[i]+b[i+1]+a[i+1];
c[i][i+1]=b[i]+b[i+1]+a[i+1];
r[i][i+1]=i+1;
}
c[n][n]=0.0;
r[n][n]=0;
wt[n][n]=b[n];
//for j-i=2,3,4....,n
for(i=2;i<=n;i++)
{
for(j=0;j<=n-i;j++)
{
wt[j][j+i]=b[j+i]+a[j+i]+wt[j][j+i-1];
c[j][j+i]=9999;
for(l=j+1;l<=j+i;l++)
{
if(c[j][j+i]>(c[j][l-1]+c[l][j+i]))
{
c[j][j+i]=c[j][l-1]+c[l][j+i];
r[j][j+i]=l;
}
}
c[j][j+i]+=wt[j][j+i];
}
cout<<endl;
}
cout<<"\n\nOptimal BST is :: ";
cout<<"\nw[0]["<<n<<"] :: "<<wt[0][n];
cout<<"\nc[0]["<<n<<"] :: "<<c[0][n];
cout<<"\nr[0]["<<n<<"] :: "<<r[0][n];
}
void print(int l1,int r1)
{
if(l1>=r1)
return;
if(r[l1][r[l1][r1]-1]!=0)
cout<<"\n Left child of "<<r[l1][r1]<<" :: "<<r[l1][r[l1][r1]-1];
if(r[r[l1][r1]][r1]!=0)
cout<<"\n Right child of "<<r[l1][r1]<<" :: "<<r[r[l1][r1]][r1];
print(l1,r[l1][r1]-1);
print(r[l1][r1],r1);
return;
}