Find the optimal cost to construct a binary search tree where each key can repeat several times. We are given each key’s frequency in the same order as corresponding keys in the inorder traversal of a binary search tree.

To construct a binary search tree, we have to determine if the key already exists in the BST or not for each given key. The cost of finding a BST key is equal to the level of the key (if present in the BST).

For example, consider the following frequency array
 
freq[] = { 25, 10, 20 }
 
As frequency follows inorder order (ascending keys), let’s consider the index of freq[] as corresponding keys, i.e.,
 

  • Key 0 occurs 25 times
  • Key 1 occurs 10 times
  • Key 2 occurs 20 times

 
Output: The optimal cost of constructing BST is 95.
 
Following is the optimum BST:
 
    0(25×1)              — — — — — level 1
        \
         \
          \
          2(20×2)        — — — — — level 2
            /
           /
          /
       1(10×3)           — — — — — level 3
 
25 lookups of the key 0 will cost 1 each.
20 lookups of the key 2 will cost 2 each.
10 lookups of the key 1 will cost 3 each.
 
So, Optimal Cost is 25×1 + 20×2 + 10×3 = 95
 
 
Other possible BSTs are:

    0(25×1)               — — — — — level 1
        \
         \
          \
         1(10×2)          — — — — — level 2
            \
             \
              \
           2(20×3)        — — — — — level 3

Cost is 25 + 10×2 + 20×3 = 105
which is more than the optimal cost 95
 
 
 
            2(20×1)      — — — — — level 1
            /
           /
          /
      1(10×2)            — — — — — level 2
        /
       /
      /
  0(25×3)                — — — — — level 3

Cost is 20 + 10×2 + 25×3 = 115
which is more than the optimal cost 95
 
 
 
       1(10×1)            — — — — — level 1
        /  \
       /    \
      /      \
  0(25×2)    2(20×2)      — — — — — level 2

Cost is 10 + 25×2 + 20×2 = 100
which is more than the optimal cost 95
 
 
 
          2(20×1)        — — — — — level 1
          /
         /
        /
      0(25×2)            — — — — — level 2
        \
         \
          \
        1(10×3)          — — — — — level 3
 
Cost is 20 + 25×2 + 10×3 = 100
which is more than the optimal cost 95

Practice this problem

The idea is simple – consider each key as a root and find an optimal solution by recursively finding the optimal cost of left and right subtree and add left and right child cost to the current node’s price (frequency of that key × level of that node). If the current node’s cost is optimal, update the result.

The algorithm can be implemented as follows in C++, Java, and Python:

C++


Download  Run Code

Output:

The optimal cost of constructing BST is 95

Java


Download  Run Code

Output:

The optimal cost of constructing BST is 95

Python


Download  Run Code

Output:

The optimal cost of constructing BST is 95

The time complexity of the above solution is exponential and occupies space in the call stack.

 
The problem has optimal substructure. We have seen that the problem can be broken down into smaller subproblems, which can further be broken down into yet smaller subproblems, and so on. The problem also exhibits overlapping subproblems, so we will end up solving the same subproblem over and over again. We know that problems with optimal substructure and overlapping subproblems can be solved by dynamic programming, where subproblem solutions are memoized rather than computed and again.

Following is the C++, Java, and Python program that demonstrates this method:

C++


Download  Run Code

Output:

The optimal cost of constructing BST is 95

Java


Download  Run Code

Output:

The optimal cost of constructing BST is 95

Python


Download  Run Code

Output:

The optimal cost of constructing BST is 95

The time complexity of the above solution is O(n4), where n is the total number of keys. The auxiliary space required by the program is O(n3) for recursion (call stack).

 
We can also implement the bottom-up version of the above memoized solution. The following code shows how to implement it in C, Java, and Python (see algorithm used here).

C


Download  Run Code

Output:

The optimal cost of constructing BST is 95

Java


Download  Run Code

Output:

The optimal cost of constructing BST is 95

Python


Download  Run Code

Output:

The optimal cost of constructing BST is 95