开发者

Java - diagramming a recursive call

I have the following code

public static int unknown(String x)
{
if ((x.length()==1) && (x.equals("1")))
    return 1;
else if ((x.length()==1) && (x.equals("0")))
        return 0;
else if (x.charAt(x.length()-1)=='1')
     return 1+ 2*unknown(x.substring(0,x.length()-1));
else
    return 0+2*unknown(x.substring(0,x.length()-1));
}

My professor says I must diagram the recursive call. What kind of diagram is he talking about? How should I show it? Thanks.

P.S. the String that is being cal开发者_如何学JAVAled upon is "101011", or 43.

-Dan


For instance, the diagram for "101" would look something like this:

unknown(101)
 -> 1 + 2 * unknown(10) 
   -> 1 + 2 * (0 + 2 * unknown(1))
     -> 1 + 2 * (0 + 2 * 1)
   -> 1 + 2 * 2
 -> 5


He wants you to draw a tree where each node is a call to the function, and points to the child calls that it makes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜