× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

[Algorithm] Find the number of balanced binary search tree using Dynamic programming

Shikhill Gupta
Sep 12, 2015
<p>Dynamic programming problems can be solved by two approach</p> <p>1) Top Down approach (on Demand memorization methods)<br> 2) bottom up approach(tabulation Method)</p> <p><strong>Plain Description&nbsp;</strong></p> <p>for a balanced binary search tree the difference b/w height of left and right subtree should be atmost 1</p> <p>n = 0 &nbsp;output = 1</p> <p>now for n = 1<br> output = 1</p> <p>for n = 2 &nbsp; &nbsp; the valid options are &nbsp;0,1 &nbsp;and 1,0 &nbsp;where two values are nodes in left and right &nbsp;so&nbsp;<br> output will be 1*1 + 1*1 = 2</p> <p>for n = 3 &nbsp;the valid options are 1,1 only<br> so output be 1*1 = 1</p> <p>for n = 4 &nbsp;the valid options are 1,2 , 2,1 so&nbsp;<br> output = 2*1 + 1*2 = 4</p> <p>for n = 5 &nbsp;valid options are 1,3, 2,2, 3,1<br> output = 1*1 + 2*2 + 1*1 = 6</p> <p>for n = 6 &nbsp;valid options are 2,3 , 3,2&nbsp;<br> output = 2*1 + 1*2 = 4</p> <p>for n = 7 valid options are 2,4 , 3,3 , 4,2<br> output = 2*4 + 1*1 + 4*2 = 17</p> <p>&nbsp;</p> <p><strong>Top Down Approach</strong></p> <p>int countNoOfTreeUsingDP(int * array ,int N) {</p> <p>&nbsp; &nbsp; if(array[N] ==0){</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; if(N&nbsp; &gt;=0)</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array[0] =1;</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; if(N &gt;=1)</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array[1] =1;</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; if(N &gt;=2)</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array[2]= 2;</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; if(N &gt;= 3)</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array[3] =1;</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; if(N&lt;=3)</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return array[N];</p> <p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;int sum =0;</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; int left =0;</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; int right =0;</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; if( N %2 !=0) {</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; left= countNoOfTreeUsingDP(array, N/2);</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; right = countNoOfTreeUsingDP(array,N/2);</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum += left * right;</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; left = countNoOfTreeUsingDP(array,N/2 -1);</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; right = countNoOfTreeUsingDP(array,N/2 +1);</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum += 2*(left * right);</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array[N] = sum;</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; }</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; else {</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; left =&nbsp; countNoOfTreeUsingDP(array,N/2 -1);</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; right =&nbsp; countNoOfTreeUsingDP(array,N/2);</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum += sum + 2*(left*right);</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array[N]= sum;</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; }</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; return sum;</p> <p>&nbsp; &nbsp; }</p> <p>&nbsp; &nbsp; else {</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; return array[N];</p> <p>&nbsp; &nbsp; }</p> <p>}</p> <p><strong>Bottom Up Approach</strong></p> <p>int countNoOfTreeUsingDP1(int * array ,int N) {</p> <p>&nbsp; &nbsp; if(N&gt;=0)</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; array[0]=1;</p> <p>&nbsp; &nbsp; if(N&gt;=1)</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; array[1]=1;</p> <p>&nbsp; &nbsp; if(N&gt;=2)</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; array[2] =2;</p> <p>&nbsp; &nbsp; if(N&gt;=3)</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; array[3] =1;</p> <p>&nbsp; &nbsp; if(N&lt;=3)</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; return array[N];</p> <p>&nbsp; &nbsp; for(int i=4; i&lt;=N; i++) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; int k = i/2;</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; if(i%2 ==0)</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array[i] = 2 * array[k] * array[N-1-k];</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; else</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array[i] = array[k] * array[k] + 2 * array[k-1] * array[k+1];</p> <p>&nbsp; &nbsp; }</p> <p>&nbsp; &nbsp; return array[N];</p> <p>}</p> <p>&nbsp;</p>