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!
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;
}
I am building an optimal binary search tree. I have its root table and main table,...
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 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 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 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 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...
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....
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 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
//...
Hello. I have written the following function to remove a value from a Binary Search Tree using resources my professors gave and stuff I found online. The problem is I don't know how it works and I need to know how it works to finish the rest of the project. public boolean remove(T value){ if(value == null) return false; class RemoveBSTObj{ private boolean found = false; private Node removeBST(Node root, T value){ if(root == null) return root; //IF there is...
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...