Optimal binary search tree in DSA
Implementation
// A simple way of the recursive implementation of the optimal search that we will perform on the binary tree.
#include <bits/stdc++.h>
using namespace std;
// we have to create a basic utility function to help us get the sum of both the frequencies present.
int sm(int frequency[], int g, int k);
// creating a function that is recursive and also a function that will help us in estimating the cost of the optimal binary search tree.
int optimal__Cost(int frequency[], int g, int k)
{
if (k < g)
// this subarray is empty, which implies it doesn't contain any elements in them.
return 0;
if (k == g) // this subarray contains one type of element.
return frequency[g];
// in this subarray we have to get the sum of the following frequencies:- frequency[g], frequency [g+1], frequency[k].
int fsm = sm(frequency, g, k);
// we have to initialize the latest minimal value and see where it goes:-
int minimum= INT__MAX;
// start considering all the elements present but one by one
// then we have to find the cost of the root and recursive
// present in the BST and then compare this cost with the
// minimum val and then update if required.
for (int r = g; r <= k; ++r)
{
int cost = optimal__Cost(frequency, i, r - 1) +
optimal__Cost(frequency, r + 1, j);
if (cost < min)
minimum= cost;
}
// Return minimum value
return minimum+ fsm;
}
// now create the main function that will calculate the minimum cost of a particular binary tree, and it will possibly only use the optimal__Cost() function to find the optimal cost of the binary search tree.
int optimal search tree(int kys[],
int frequency[], int n)
{
// given the array keys here are presumed to be aligned, which will be in increasing order. If the keys are not aligned, then we have to technically add a code to solve and arrange the keys and refigure the frequency of the same accordingly.
return optimal__Cost(frequency, 0, n - 1);
}
//creating a utility function that will help us get the sum of the elements of the array and the frequency of the gth and kth elements.
int sm(int frequency[], int g, int k)
{
int s = 0;
for (int k = g; k <= h; k++)
s += frequency[k];
return s;
}
// writing the main code.
int main()
{
int kys[] = {10, 12, 20};
int frequency[] = {34, 8, 50};
int n = sizeof(kys) / sizeof(kys[0]);
cout << "Cost of Optimal BST is "
<< optimalSearchTree(kys, frequency, n);
return 0;
}
Output:
Example 2)
// A simple way of the recursive implementation of the optimal search that we will perform on the binary tree.
#include <bits/stdc++.h>
using namespace std;
// in this subarray we have to get the sum of the following frequencies:- frequency[g], frequency [g+1], frequency[k].
int sm(int frequency[], int g, int k);
// creating a function that is recursive and also a function that will help us in estimating the cost of the optimal binary search tree.
int optimalSearchTree(int kys[], int frequency[], int n)
{
int cost[n][n];
/* cost[i][j] = Optimal cost of binary search tree
that can be formed from kys[i] to kys[j].
cost[0][n-1] will store the resultant cost */
// if we are calculating the cost for a single key, then the frequency can be calculated by: -
for (int g = 0; g < n; g++)
cost[g][g] = frequency[g];
// we will now see whether we can see the chains of the length or not, which is 2, 3…
for (int L = 2; L <= n; L++)
{
// g is considered as the row number in cost[][]
for (int g = 0; i <= n-L+1; i++)
{
// we have to get the column number h from the row number g
// chain length L
int k = g+L-1;
cost[g][h] = INT__MAX;
int off_set_sm = sm(frequency, i, j);
// we have to try and convert all the keys present as root.
for (int r = i; r <= j; r++)
{
// c is generally the cost of the tree when keys are the root of this subtree.
int c = ((r > i)? cost[i][r-1]:0) +
((r < j)? cost[r+1][j]:0) +
off_set_sm;
if (c < cost[i][j])
cost[i][j] = c;
}
}
}
return cost[0][n-1];
}
// in this subarray we have to get the sum of the following frequencies:- frequency[g], frequency [g+1], frequency[k].
int sm(int frequency[], int g, int k)
{
int s = 0;
for (int k = i; k <= j; k++)
s += frequency[k];
return s;
}
// writing the main code.
int main()
{
int kys[] = {10, 12, 20};
int frequency[] = {34, 8, 50};
int n = sizeof(kys)/sizeof(kys[0]);
cout << "Cost of Optimal BST is " << optimalSearchTree(kys, frequency, n);
return 0;
}
Output:
Example 3)
//creating a utility function that will help us get the sum of the elements of the array and the frequency of the gth and kth elements.
int sm(int frequency[], int g, int k);
// creating a function that is recursive and also a function that will help us in estimating the cost of the optimal binary search tree.
int optimal search tree(int kys[], int frequency[], int n)
#include <stdio.h>
#include <limits.h>
//creating a 2D matrix will help us store all the essential problems.
int cost[n][n];
/* cost[i][j] = Optimal cost of binary search tree
that can be formed from kys[i] to kys[j].
cost[0][n-1] will store the resultant cost */
// if we are calculating the cost for a single key, then the frequency can be calculated by: -
for (int g = 0; i < n; i++)
cost[i][i] = frequency[i];
// we will now see whether we can see the chains of the length or not, which is 2, 3…
// L is chain length.
for (int L=2; L<=n; L++)
{
// g is row number in cost[][]
for (int g=0; i<=n-L+1; i++)
{
// we have to get the column number h from the row number g
// chain length L
int k = i+L-1;
int off_set_sm = sm(frequency, i, j);
cost[i][j] = INT__MAX;
// we have to try and convert all the keys present as root.
for (int r=i; r<=j; r++)
{
// c is generally the cost of the tree when keys are the root of this subtree.
int c = ((r > i)? cost[i][r-1]:0) +
((r < j)? cost[r+1][j]:0) +
off_set_sm;
if (c < cost[i][j])
cost[i][j] = c;
}
}
}
return cost[0][n-1];
}
// in this subarray we have to get the sum of the following frequencies:- frequency[g], frequency [g+1], frequency[k].
int sm(int frequency[], int g, int k)
{
int s = 0;
for (int k = i; k <=j; k++)
s += frequency[k];
return s;
}
// Driver program to test the above functions
int main()
{
int kys[] = {10, 12, 20};
int frequency[] = {34, 8, 50};
int n = sizeof(kys)/sizeof(kys[0]);
printf("Cost of Optimal BST is %d ",
optimalSearchTree(kys, frequency, n));
return 0;
}
Output:
Example 4)
// A simple way of the recursive implementation of the optimal search that we will perform on the binary tree.
#include <stdio.h>
#include <limits.h>
// creating a function that is recursive and also a function that will help us in estimating the cost of the optimal binary search tree.
int sm(int frequency[], int g, int k);
int optimal__Cost(int frequency[], int g, int k)
{
If (j < i) //, this subarray is empty, which implies it doesn't contain any elements in them.
return 0;
if (j == i) // this subarray contains one type of element.
return frequency[i];
// in this subarray we have to get the sum of the following frequencies:- frequency[g], frequency [g+1], frequency[k].
int fsm = sm(frequency, i, j);
// Initialize minimum value
int minimum= INT__MAX;
// we have to initialize the latest minimal value and see where it goes:-
// start considering all the elements present but one by one
// then we have to find the cost of the root and recursive
// present in the BST and then compare this cost with the
// minimum val and then update if required.
for (int r = i; r <= j; ++r)
{
int cost = optimal__Cost(frequency, i, r-1) +
optimal__Cost(frequency, r+1, j);
if (cost < min)
minimum= cost;
}
// Return minimum value
return minimum+ fsm;
}
// now create the main function that will calculate the minimum cost of a particular binary tree, and it will possibly only use the optimal__Cost() function to find the optimal cost of the binary search tree.
int optimalSearchTree(int kys[], int frequency[], int n)
{
// given the array keys here are presumed to be aligned, which will be in increasing order. If the keys are not aligned, then we have to technically add a code to solve and arrange the keys and refigure the frequency of the same accordingly.
return optimal__Cost(frequency, 0, n-1);
}
//creating a utility function that will help us get the sum of the elements of the array and the frequency of the gth and kth elements.
int sm(int frequency[], int g, int k)
{
int s = 0;
for (int k = i; k <=j; k++)
s += frequency[k];
return s;
}
// Driver program to test the above functions
int main()
{
int kys[] = {10, 12, 20};
int frequency[] = {34, 8, 50};
int n = sizeof(kys)/sizeof(kys[0]);
printf("Cost of Optimal BST is %d ",
optimalSearchTree(kys, frequency, n));
return 0;
}
Output:
Example 5)
// A simple way of the recursive implementation of the optimal search that we will perform on the binary tree.
public class TFM
{
// creating a function that is recursive and also a function that will help us in estimating the cost of the optimal binary search tree.
static int optimal__Cost(int frequency[], int g, int k)
{
If (j < i) //, this subarray is empty, which implies it doesn't contain any elements in them.
return 0;
if (j == i) // this subarray contains one type of element.
return frequency[i];
// in this subarray we have to get the sum of the following frequencies:- frequency[g], frequency [g+1], frequency[k].
int fsm = sm(frequency, i, j);
// we have to initialize the latest minimal value and see where it goes:-
int minimum= Integer.MAX_VALUE;
// start considering all the elements present but one by one
// then we have to find the cost of the root and recursive
// present in the BST and then compare this cost with the
// minimum val and then update if required.
for (int r = i; r <= j; ++r)
{
int cost = optimal__Cost(frequency, i, r-1) +
optimal__Cost(frequency, r+1, j);
if (cost < min)
minimum= cost;
}
// Return minimum value
return minimum+ fsm;
}
// now create the main function that will calculate the minimum cost of a particular binary tree, and it will possibly only use the optimal__Cost() function to find the optimal cost of the binary search tree.
static int optimalSearchTree(int kys[], int frequency[], int n)
{
// given the array keys here are presumed to be aligned, which will be in increasing order. If the keys are not aligned, then we have to technically add a code to solve and arrange the keys and refigure the frequency of the same accordingly.
return optimal__Cost(frequency, 0, n-1);
}
//creating a utility function that will help us get the sum of the elements of the array and the frequency of the gth and kth elements.
static int sm(int frequency[], int g, int k)
{
int s = 0;
for (int k = i; k <=j; k++)
s += frequency[k];
return s;
}
// writing the main code.
public static void main(String[] args) {
int kys[] = {10, 12, 20};
int frequency[] = {34, 8, 50};
int n = kys.length;
System.out.println("Cost of Optimal BST is " +
optimalSearchTree(kys, frequency, n));
}
}
Output: