Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For the difference between the two versions of the function Keys, note the following difference:</p> <pre><code>y+=[x[2]]+Keys(x[0],y)+Keys(x[1],y) </code></pre> <p>The right side value in this statement is a list which contains x[2], plus the ELEMENTS OF Keys(x[0],y) and the ELEMENTS OF Keys(x[1],y)</p> <pre><code>y+=[x[2],Keys(x[0],y),Keys(x[1],y)] </code></pre> <p>The right side value in this statement is a list which contains x[2], plus the LIST Keys(x[2],y) and the LIST Keys(x[1],y).</p> <p>So the version using [a,b] will causing y contains itself as its elements.</p> <p>Some other notes:</p> <ol> <li><p>Since in python, the default value object is created once when the function is defined, the first version will not work like the example shows. It will contain multiple copy of some keys. It's hard to explain in short, but you can get some idea by printing the values of x, y on each call of Keys. </p> <p>This is confirmed by running the function on my machine with python 2.5.2.</p></li> <li><p>Also because the default value is defined only once at function definition time, even the function works correct for the first time, it will not work when calling with a different a, since the keys in the first binary tree will remain in y.</p> <p>You can see this by calling Keys(a) twice, or calling it on two different lists.</p></li> <li><p>The second parameter is not required for this problem. The function can be like this:</p> <p>def Keys(a): if a = []: return [] else: return [a[2]]+Keys(a[0])+Keys(a[1])</p> <p>Defining a recursive function basically contains two part, solve subproblems and combined the results. In your code, the combining results part is repeated twice: one by accumulating them in y, one by adding the list together.</p></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload