Let's think about what happens when we do a recursive call. The name space changes. If you were in name space X, now you are in X in X. As described, function calls can be dealt with using stacks. So at the top of the stack is where you are currently. Each node is like that name space. In our exercises, we only dealt with a node having only one value, but that's not a requirement. In fact, I think we can think of the whole recursive process as a tree where each node contains information about all the local variables at that point.
With the context of the meaning of recursive functions defined above let's see how this might be true. If the problem at hand matches all properties of a tree, then it must follow that the problem can be expressed as a tree.
So what are the requirements/properties of a tree?
Tree – A kind of directed graph
1. (Composed of set of nodes
with directed edges between nodes)
2. (Edge
connects parent node to a child
node)
3. (Nodes
may (or may not) contain values)
4. Where
each node has exactly one parent except the root has none
So...
1. yes the recursive problem has nodes and directed edges. The directed edges are function calls.
A calling on B is not symmetric (it is directed).
2. yes the edge connects between two nodes only. There cannot be three way connections.
3. yes the nodes (name space) may or maynot have values
4. yes each function call can only have one "caller". The root is the "original" call.
All of them are satisfied and I think it makes sense. I often think of recursive problems as a reference shift (I guess in actuality it is a name space change). You can call a child node from the parent node and once you move along that path (calling), now the previously child node is the new parent node. I think this kind of thinking might have shown slightly in my lab 6 explanation.
As to the above, I suppose it is not a formal proof and I am not yet convinced that this must be True. At the moment, it is a running hypothesis and possibly another way to think about recursion. It partly answers the question of when recursion may be appropriate: when you can create a tree that is finite... But then it still does not answer the question... just asks the question in a different angle (provides an answer in a different angle).
No comments:
Post a Comment