-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathOptimalBinarySearchTree.cpp
81 lines (55 loc) · 1.28 KB
/
OptimalBinarySearchTree.cpp
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
#include<iostream>
#include<climits>
using namespace std;
//--------------------------------------Recursive Solution
int optBST(int freq[],int i,int j){
if(i==j){
return freq[i];
}
if(j<i){
return 0;
}
int freqTotal = 0;
for(int v = i;v<=j;v++){
freqTotal += freq[v];
}
int subans = INT_MAX;
for(int root=i;root<=j;root++){
subans = min(subans, optBST(freq,i,root-1)+ optBST(freq,root+1,j));
}
return subans+freqTotal;
}
//----------------------------------------Top Down DP Solution
int sum(int *freq,int i,int j){
int sum=0;
for(int k=i;k<=j;k++){
sum += freq[k];
}
return sum;
}
int optBSTDP1(int *freq,int n){
int dp[100][100]={0};
for(int i=0;i<n;i++){
dp[i][i]=freq[i];
}
for(int len=2;len<=n;len++){
for(int i=0;i<=n-len+1;i++){
int j = i+len-1;
dp[i][j]=INT_MAX;
for(int root = i;root<=j;root++){
int cost = ((root>i)? dp[i][root-1]:0) + ((root<j)?dp[root+1][j]:0) + sum(freq,i,j);
dp[i][j] = min(dp[i][j],cost);
//cout<<cost<<endl;
}
}
}
return dp[0][n-1];
}
int main(){
//Key should be sorted in ascending order.
int keys[] = {10, 12, 20 };
int freq[] = { 34, 8, 50};
cout<<optBST(freq,0,2)<<endl;
cout<<optBSTDP1(freq,3)<<endl;
return 0;
}