First, We Will Calculate The Values Where J-I Is Equal To Zero
First, We Will Calculate The Values Where J-I Is Equal To Zero
Now to calculate the cost, we will consider only the jth value.
The cost of c[0,1] is 4 (The key is 10, and the cost corresponding to key 10 is 4).
The cost of c[1,2] is 2 (The key is 20, and the cost corresponding to key 20 is 2).
The cost of c[2,3] is 6 (The key is 30, and the cost corresponding to key 30 is 6)
The cost of c[3,4] is 3 (The key is 40, and the cost corresponding to key 40 is 3)
o When i=0 and j=2, then keys 10 and 20. There are two possible trees that can be
made out from these two keys shown below:
o When i=2 and j=4, we will consider the keys at 3 and 4, i.e., 30 and 40. There are
two possible trees that can be made out from these two keys shown as below:
The following are the trees that can be made if 10 is considered as a root node.
In the above tree, 10 is the root node, 20 is the right child of node 10, and 30 is the right
child of node 20.
In the above tree, 20 is the root node, 30 is the right child of node 20, and 10 is the left
child of node 20.
The following are the trees that can be created if 30 is considered as the root node.
In the above tree, 30 is the root node, 20 is the left child of node 30, and 10 is the left
child of node 20.
In the above tree, 30 is the root node, 10 is the left child of node 30 and 20 is the right
child of node 10.
Therefore, the minimum cost is 20 which is the 3rd root. So, c[0,3] is equal to 20.
o When i=1 and j=4 then we will consider the keys 20, 30, 40
= min{12, 5, 10} + 11
In this case, we will consider four keys, i.e., 10, 20, 30 and 40. The frequencies of 10, 20, 30
and 40 are 4, 2, 6 and 3 respectively.
w[0, 4] = 4 + 2 + 6 + 3 = 15
= min{4 + 12} + 15
= 16 + 15 = 31
= min {8 + 3} + 15
= 26
= min{20 + 0} + 15
= 35
In the above cases, we have observed that 26 is the minimum cost; therefore, c[0,4] is
equal to 26.