Question

I need to build an optimal binary search tree with an array of strings and frequency...

I need to build an optimal binary search tree with an array of strings and frequency received from the user.in C

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PLEASE GIVE IT A THUMBS UP

#include <stdio.h>
#include <limits.h>

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

int costbst(int freq[], int i, int j)
{
if (j < i)   
return 0;
if (j == i)
return freq[i];

int funcsum = sum(freq, i, j);
int min = INT_MAX;

for (int r = i; r <= j; ++r)
{
int cost = costbst(freq, i, r-1) +
costbst(freq, r+1, j);
if (cost < min)
min = cost;
}

return min + funcsum;
}

int binary_optimal(char keys[3][10], int freq[], int n)
{
return costbst(freq, 0, n-1);
}

int main()
{
char keys[3][10];
int freq[3],i;
for(i=0;i<3;i++){
printf("Enter key and freq for %d\n",i+1 );
scanf("%s",keys[i]);
scanf("%d",&freq[i]);
}
printf("Optimal binary tree cost is %d ", binary_optimal(keys, freq, 3));
return 0;
}

Add a comment
Know the answer?
Add Answer to:
I need to build an optimal binary search tree with an array of strings and frequency...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT