-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptimal-binary-search-tree.cpp
80 lines (64 loc) · 2.02 KB
/
optimal-binary-search-tree.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
#include <iostream>
#include <vector>
#include <climits>
#define INF INT_MAX
using namespace std;
// Dynamic Programming: Problem 8 [Optimal Binary Search Tree (Successful Search Only)]
/*
Let keys = { 10, 20, 30 }
and freq = { 3, 2, 5 }
Possible binary search trees :
10 | 10 | 20 | 30 | 30
\ | \ | / \ | / | /
20 | 30 | 10 30 | 10 | 20
\ | / | | \ | /
30 | 20 | | 20 | 10
cost=18 cost=19 cost=18 cost=17 cost=18
Optimal binary search tree = (4)
Min Cost = 17
*/
void printArray(vector<vector<int>> &arr) {
int n = arr.size();
for(int i=0; i<n-1; i++) {
for(int j=1; j<n; j++) {
cout << arr[i][j] << "\t";
}
cout << endl;
}
}
int optimalBST(vector<int> &keys, vector<int> &freq, int n) {
vector<vector<int>> W(n+1, vector<int>(n+1, 0));
vector<vector<int>> C(n+1, vector<int>(n+1, 0));
vector<vector<int>> root(n+1, vector<int>(n+1, 0));
for(int i=0; i<=n; i++) {
for(int j=i+1; j<=n; j++) {
if(j==i+1)
W[i][j] = freq[j-1];
else
W[i][j] = W[i][j-1] + freq[j-1];
}
}
for(int s=0; s<=n; s++) {
for(int i=0; i<=n-s; i++) {
int j = i+s;
if(i!=j) C[i][j] = INF;
for(int k=i+1; k<=j; k++) {
int temp = C[i][j];
C[i][j] = min(C[i][j], C[i][k-1] + C[k][j] + W[i][j]);
if(C[i][j] != temp)
root[i][j] = k;
}
}
}
cout << "\nMin Cost Array : " << endl;
printArray(C);
cout << "\nRoots of BSTs : " << endl;
printArray(root);
return C[0][n];
}
int main() {
vector<int> keys = {10, 20, 30, 40};
vector<int> freq = {4, 2, 6, 3};
int minCost = optimalBST(keys, freq, keys.size());
cout << "\nMin Cost : " << minCost;
}