Repository navigation
Expand file tree
/
Copy pathOBST-Build.cpp
More file actions
67 lines (53 loc) · 1.71 KB
/
Copy pathOBST-Build.cpp
File metadata and controls
67 lines (53 loc) · 1.71 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
#include <iostream>
using namespace std;
const int MAX = 100;
void printOptimalBST(float keys[], int root[][MAX], int i, int j, int level);
void constructOBST(float keys[], float searchProbs[], int n) {
float c[MAX][MAX] = {0};
float w[MAX][MAX] = {0};
int root[MAX][MAX] = {0};
for (int i = 0; i < n; i++) {
w[i][i] = searchProbs[i];
c[i][i] = 0;
root[i][i] = i;
}
for (int d = 1; d < n; d++) {
for (int i = 0; i < n - d; i++) {
int j = i + d;
w[i][j] = w[i][j - 1] + searchProbs[j] + keys[j];
c[i][j] = INT_MAX;
for (int r = i; r <= j; r++) {
float cost = ((r > i) ? c[i][r - 1] : 0) + ((r < j) ? c[r + 1][j] : 0) + w[i][j];
if (cost < c[i][j]) {
c[i][j] = cost;
root[i][j] = r;
}
}
}
}
cout << "Optimal BST:\n";
printOptimalBST(keys, root, 0, n - 1, 0);
}
void printOptimalBST(float keys[], int root[][MAX], int i, int j, int level) {
if (i > j) return;
int r = root[i][j];
cout << "(" << keys[r] << " Level: " << level << ") ";
printOptimalBST(keys, root, i, r - 1, level + 1);
printOptimalBST(keys, root, r + 1, j, level + 1);
}
int main() {
int n;
cout << "Enter the number of keys: ";
cin >> n;
float keys[MAX], searchProbs[MAX];
cout << "Enter the keys:\n";
for (int i = 0; i < n; i++) {
cin >> keys[i];
}
cout << "Enter the search probabilities:\n";
for (int i = 0; i < n; i++) {
cin >> searchProbs[i];
}
constructOBST(keys, searchProbs, n);
return 0;
}