-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment_11.cpp
More file actions
52 lines (47 loc) · 1.24 KB
/
assignment_11.cpp
File metadata and controls
52 lines (47 loc) · 1.24 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
#include <iostream>
#include <string>
#include <limits.h>
using namespace std;
int calc(int arr[], int i, int j)
{
int sum = 0;
for (int k = i; k <= j; k++)
sum += arr[k];
return sum;
}
int obst(int key_arr[], int arr[], int n)
{
int cost[n][n];
for (int i = 0; i < n; i++)
cost[i][i] = arr[i];
for (int i = 2; i <= n; i++)
{
for (int j = 0; j <= n - i + 1; j++)
{
int k = j + i - 1;
cost[j][k] = INT_MAX;
for (int r = j; r <= k; r++)
{
int c = ((r > j) ? cost[j][r - 1] : 0) + ((r < k) ? cost[r + 1][k] : 0) + calc(arr, j, k);
if (c < cost[j][k])
cost[j][k] = c;
}
}
}
return cost[0][n - 1];
}
int main()
{
int n;
cout << "Number of keys : ";
cin >> n;
int keys[n], prob[n];
cout << "Value of the keys : " << endl;
for (int i = 0; i < n; i++)
cin >> keys[i];
cout << " Input Search Probabilities of the keys : " << endl;
for (int i = 0; i < n; i++)
cin >> prob[i];
cout << "Cost of Optimal Binary Search Tree is :: " << obst(keys, prob, n) << endl;
return 0;
}