Question

I am building an optimal binary search tree. I have its root table and main table,...

I am building an optimal binary search tree. I have its root table and main table, and i am trying to construct the binary tree from the root table. I do not know how, can you please build a program that does so? for more info, my array is 600 strings, and the frequencies are randomly chosen. Thank You! In C Please!

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

Hi,

The information you provided is not enough but I have shared with you the code of optimal BST. Hope this helps for you. You can modify this code in order to get the solution.

#include <stdio.h>

#include <limits.h>

// A utility function to get sum of array elements

// freq[i] to freq[j]

int sum(int freq[], int i, int j);

// A recursive function to calculate cost of optimal

// binary search tree

int optCost(int freq[], int i, int j)

{

   // Base cases

   if (j < i)      // no elements in this subarray

     return 0;

   if (j == i)     // one element in this subarray

     return freq[i];

   // Get sum of freq[i], freq[i+1], ... freq[j]

   int fsum = sum(freq, i, j);

   // Initialize minimum value

   int min = INT_MAX;

   // One by one consider all elements as root and

   // recursively find cost of the BST, compare the

   // cost with min and update min if needed

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

   {

       int cost = optCost(freq, i, r-1) +

                  optCost(freq, r+1, j);

       if (cost < min)

          min = cost;

   }

   // Return minimum value

   return min + fsum;

}

// The main function that calculates minimum cost of

// a Binary Search Tree. It mainly uses optCost() to

// find the optimal cost.

int optimalSearchTree(int keys[], int freq[], int n)

{

     // Here array keys[] is assumed to be sorted in

     // increasing order. If keys[] is not sorted, then

     // add code to sort keys, and rearrange freq[]

     // accordingly.

     return optCost(freq, 0, n-1);

}

// A utility function to get sum of array elements

// freq[i] to freq[j]

int sum(int freq[], int i, int j)

{

    int s = 0;

    for (int k = i; k <=j; k++)

       s += freq[k];

    return s;

}

// Driver program to test above functions

int main()

{

    int keys[] = {10, 12, 20};

    int freq[] = {34, 8, 50};

    int n = sizeof(keys)/sizeof(keys[0]);

    printf("Cost of Optimal BST is %d ",

               optimalSearchTree(keys, freq, n));

    return 0;

}

Add a comment
Know the answer?
Add Answer to:
I am building an optimal binary search tree. I have its root table and main table,...
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
  • 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

  • I am trying to create a function in c++ that checks if a binary search tree...

    I am trying to create a function in c++ that checks if a binary search tree has duplicate values. It should be very simple and not call any outside values like "data". please use this as a starting point: template<typename X> bool tree_duplicate(tree_node<X>* root) { //code here return true; } I don't need anything else created other then a basic function to tell if the tree has duplicates, and if it doesnt return FALSE. if it helps I have a...

  • I am trying to create a function in c++ that checks if a binary search tree...

    I am trying to create a function in c++ that checks if a binary search tree has duplicate values. It should be very simple and not call any outside values like "data". please use this as a starting point: template<typename X> bool tree_duplicate(tree_node<X>* root) { //code here return true; } I don't need anything else created other then a basic function to tell if the tree has duplicates, and if it doesnt return FALSE. if it helps I have a...

  • IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity...

    IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity with simple binary search trees. 1. Begin this lab by implementing a simple class that represents a "node” in a binary search tree, as follows. public class MyTreeNode<t extends Comparable<T>> { public T data; public MyTreeNode<T> leftchild; public MyTreeNode<T> rightChild; public MyTreeNode<T> parent; 2. Have the second member of your pair type in the code for the simple binary search tree interface. public interface...

  • I need help in C++ implementing binary search tree. I have the .h file for the...

    I need help in C++ implementing binary search tree. I have the .h file for the binary search tree class. I have 4 classic texts, and 2 different dictionaries. Classic Texts: Alice In Wonderland.txt A Tale of Two Cities.txt Pride And Prejudice.txt War and Peace.txt 2 different dictionaries: Dictionary.txt Dictionary-brit.txt The data structures from the standard template library can not be used.The main program should open the text file, read in the words, remove the punctuation and change all the...

  • Hi there, I am working on a binary search tree code in c++. The program must...

    Hi there, I am working on a binary search tree code in c++. The program must store and update students' academic records, each node includes the student name, credits attempted, credits earned and GPA. I have made some progress with the code and written most of the functions in the .cpp file (already did the .h file) but i am struggling with what to put in the main and how to put an update part in the insert function. I...

  • C++ Binary Search Tree question. I heed help with the level 2 question please, as level...

    C++ Binary Search Tree question. I heed help with the level 2 question please, as level 1 is already completed. I will rate the answer a 100% thumbs up. I really appreciate the help!. Thank you! searching.cpp #include <getopt.h> #include <iostream> #include <sstream> #include <stdlib.h> #include <unistd.h> using namespace std; // global variable for tree operations // use to control tree maintenance operations enum Mode { simple, randomised, avl } mode; // tree type // returns size of tree //...

  • C++ Binary Search Tree question. I heed help with the level 2 question please, as level...

    C++ Binary Search Tree question. I heed help with the level 2 question please, as level 1 is already completed. I will rate the answer a 100% thumbs up. I really appreciate the help!. Thank you! searching.cpp #include <getopt.h> #include <iostream> #include <sstream> #include <stdlib.h> #include <unistd.h> using namespace std; // global variable for tree operations // use to control tree maintenance operations enum Mode { simple, randomised, avl } mode; // tree type // returns size of tree //...

  • The INORDER traversal output of a binary tree is U,N,I,V,E,R,S,I,T,Y and the POSTORDER traversal output of the same tree is N,U,V,R,E,T,I,S,I,Y

     a. The INORDER traversal output of a binary tree is U,N,I,V,E,R,S,I,T,Y and the POSTORDER traversal output of the same tree is N,U,V,R,E,T,I,S,I,Y. Construct the tree and determine the output of the PREORDER traversal output.   b. One main difference between a binary search tree (BST) and an AVL (Adelson-Velski and Landis) tree is that an AVL tree has a balance condition, that is, for every node in the AVL tree, the height of the left and right subtrees differ by at most 1....

  • Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to com...

    Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...

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