Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimal_Binary_Search_Tree #710

Merged
merged 2 commits into from Mar 22, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
75 changes: 75 additions & 0 deletions Optimal_Binary_Search_Tree/Optimal_Binary_Search_Tree.c
@@ -0,0 +1,75 @@
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

// EXAMPLE
//Input: keys[] = {10, 12, 34}, freq[] = {54, 10, 4}
//There can be following possible BSTs
// 10 12 34 10 34
// \ / \ / \ /
// 12 10 34 12 34 10
// \ / / \
// 34 10 12 12
// I II III IV V
//
//Cost of the optimal BST (1 BST) is = 86

int sum(int freq[], int i, int j)
{
int count= 0;
for (int k = i; k <=j; k++)
count += freq[k];
return count;
}

int minCostBST(int keys[], int freq[], int size)
{
int cost[size][size];
for (int i = 0; i < size; i++)
cost[i][i] = freq[i]; // freq is assigned to the diagonal elements of 2-D array cost.

for (int leng=2; leng<=size; leng++)
{
for (int i=0; i<=size-leng+1; i++)
{
int j = i+leng-1;
cost[i][j] = INT_MAX;
// One by one consider all elements as root
for (int r=i; r<=j; r++)
{
//comparing with the cost and update cost if needed
int c = ((r > i)?cost[i][r-1]:0)+((r < j)?cost[r+1][j]:0)+sum(freq, i, j);
if (c < cost[i][j])
cost[i][j] = c;
}
}
}
return cost[0][size-1];
}

int main()
{
int size,i;
printf("Enter the numbers of keys ");
scanf("%d",&size);
int keys[size];
int freq[size];
printf("Enter the data elements ");
for ( i=0;i<size;i++)
scanf("%d",&keys[i]);
printf("Enter the frequencies ");
for (i=0;i<size;i++)
scanf("%d",&freq[i]);

printf("Cost of Optimal Binary search tree is %d ",minCostBST(keys, freq, size));

return 0;
}
//INPUT:Enter the numbers of keys 3
//Enter the data elements 10
//12
//34
//Enter the frequencies 54
//10
//4
//OUTPUT:Cost of Optimal Binary search tree is 86