开发者

Java - diagramming a recursive call

开发者 https://www.devze.com 2023-01-30 16:48 出处:网络
I have the following code public static int unknown(String x) { if ((x.length()==1) && (x.equals(\"1\")))

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

精彩评论

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