0% found this document useful (0 votes)
8 views2 pages

9 Dsa

The document presents a C++ program that constructs an optimal binary search tree (BST) based on given keys and their associated search probabilities. It utilizes dynamic programming to calculate the minimum search cost and identifies the root of the optimal BST. The program prompts the user for input and outputs the minimum search cost along with the root key of the constructed tree.

Uploaded by

22yashkumar09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

9 Dsa

The document presents a C++ program that constructs an optimal binary search tree (BST) based on given keys and their associated search probabilities. It utilizes dynamic programming to calculate the minimum search cost and identifies the root of the optimal BST. The program prompts the user for input and outputs the minimum search cost along with the root key of the constructed tree.

Uploaded by

22yashkumar09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <iostream>

#include <vector>
#include <limits>

using namespace std;

struct Node {
int key;
Node* left;
Node* right;

Node(int k) : key(k), left(NULL), right(NULL) {}


};

double sumProbabilities(vector<double>& prob, int i, int j) {


double sum = 0;
for (int k = i; k <= j; k++) {
sum += prob[k];
}
return sum;
}

Node* constructOptimalBST(vector<vector<int> >& root, vector<int>& keys, int i, int


j) {
if (i > j) return NULL;

int r = root[i][j];
Node* node = new Node(keys[r]);

node->left = constructOptimalBST(root, keys, i, r - 1);


node->right = constructOptimalBST(root, keys, r + 1, j);

return node;
}

Node* optimalBST(vector<int>& keys, vector<double>& prob, int n, double& minCost,


int& rootKey) {
vector<vector<double> > cost(n, vector<double>(n, 0));
vector<vector<int> > root(n, vector<int>(n, 0));

for (int i = 0; i < n; i++) {


cost[i][i] = prob[i];
root[i][i] = i;
}

for (int L = 2; L <= n; L++) {


for (int i = 0; i <= n - L; i++) {
int j = i + L - 1;
cost[i][j] = numeric_limits<double>::max();

for (int r = i; r <= j; r++) {


double c = ((r > i) ? cost[i][r - 1] : 0) +
((r < j) ? cost[r + 1][j] : 0) +
sumProbabilities(prob, i, j);

if (c < cost[i][j]) {
cost[i][j] = c;
root[i][j] = r;
}
}
}
}

minCost = cost[0][n - 1];


rootKey = keys[root[0][n - 1]];

return constructOptimalBST(root, keys, 0, n - 1);


}

int main() {
int n;
cout << "Enter the number of keys: ";
cin >> n;

vector<int> keys(n);
vector<double> prob(n);

cout << "Enter the keys in sorted order:\n";


for (int i = 0; i < n; i++) {
cin >> keys[i];
}

cout << "Enter the search probabilities for each key:\n";


for (int i = 0; i < n; i++) {
cin >> prob[i];
}

double minCost;
int rootKey;

Node* root = optimalBST(keys, prob, n, minCost, rootKey);

cout << "\nOptimal BST constructed with minimum search cost: " << minCost <<
endl;
cout << "and the root of the tree: " << rootKey << endl;

return 0;
}

You might also like