Home > other >  Python recursive functions
Python recursive functions

Time:12-02


Python recursive problems
Def partition_tree (n, m) :
"" "Return a partition tree of n using parts of up to m. "" "
If n==0:
Return the tree (True)
Elif n & lt; 0 or m==0:
Return the tree (False)
The else:
Left=partition_tree (n - m, m)
Right=partition_tree (n, m - 1)
Return the tree (m, left, right)
> Partition_tree (2, 2)

Above this code, when the inside of the transfer (2, 2) to partition_tree function, my understanding is that executes the else statement in it, and then left again call partition_tree this function, n - m becomes 0, 0 is n==0 this statement should be executed, it is representative of the else inside right statements and retrun statement won't perform, this is my understanding, I think I understand is wrong, can someone explain?

CodePudding user response:

You understand there is no problem, just the else does not perform, you say is this layer of the else does not perform, back to the recursion at the next higher level, or is likely to perform,
Like your house two rooms one hall and shearing section.at the big balcony, go to one room is equivalent to n==0, equivalent to 2 rooms n<. 1... , go to the balcony is equivalent to the else
Now in the balcony of your house and two identical two rooms one hall, you go to the balcony to the left of the two rooms one hall, found that n==0, then you go in the tree, the tree is finished after you to return to the balcony? At that time the balcony to the right of the two rooms one hall also can go in? Can go to, of course,
So you have to eventually return to your home door, isn't it to level 1 level from the house out to the balcony in the house go out again?
  • Related