This is quite good!!
To check how it works, I shall write to make it look similar to mathematical induction.
It is clear that
partial-treereturns cons with first element as empty and second element containing the list passed.
This is correct as empty tree is balanced :) with zero nodes and the second element of cons contains list of the remaining elements which happens to be the original list passed.
In this case calls to build left tree and right tree are done using and respectively. We know from previous step that these calls will return in empty tree.
But notice that call for creating the left tree is done by passing the complete list to it while call to create right tree is done by removing one element from the list returned from the result of call to left tree.
Now the call for creating right tree will return the same list passed to it(because it was called with ). Since list passed to this call was one created by removing the first element from it. This same list is returned.
Thus the result returned will be contains a cons with first element containing a tree with only one element and second element of the cons will contain the list with first element removed.
Clearly this is what is expected from the partial tree to do for .
In this case call for creating left tree will be done with and right-tree with .
The call for creating left tree will return the same list as it was passed because .
As noted in the previous case that call to right-tree will again be done with the remaining list, with one element removed from it.
The call for building right tree will result in a cons with first element containing tree with one element and second element of cons will contain the remaining list except the item that was added in the tree.(this can be verified by n = 1 case).
Thus the result will be a cons with first element is a tree with two nodes and second element with the list with first two elements removed.
Now here comes the fun part, lets assume that for for all , the procedure works as expected.
Call to create left tree is done with . Since , it follows . Thus . By our hypothesis this will result in a cons with first element a tree with nodes and second element containing .
Call to create right tree is done with and the list with first elements removed. Clearly . Thus . By our hypothesis this will result in a cons with first element a tree with nodes and second element a list which has first elements removed from it. Thus effectively the list returned is the list with first elements removed.
Thus the result will be a cons with first element with a tree with element in left and elements in right. And the second element of the cons will be the remaining list with first elements removed.
We can note that for each element in the tree, procedure is invoked only once. Assuming all other calls are of constant order we can conclude that the overall time complexity is of the order .