我对递归的理解很差,但这个算法看起来自然是最好的递归算法。基本上,我有一个C程序中所有函数调用的列表,包含多个文件。这张单子是无序的。我的递归算法试图从main方法开始,对所有调用的函数建立一个树
这对于较小的程序来说效果非常好,但是当我用较大的程序进行测试时,我发现了这个错误。我读到这个问题可能是因为我超过了cstack限制?因为我已经尝试在python中提高递归限制
非常感谢您的帮助,谢谢
functions=包含函数调用及其信息列表的集合,键入function。节点中的数据类型为Function
@dataclass
class Function:
name : str
file : str
id : int
calls : set
....
这是算法
def order_functions(node, functions, defines):
calls = set()
# Checking if the called function is user-defined
for call in node.data.calls:
if call in defines:
calls.add(call)
node.data.calls = calls
if len(calls) == 0:
return node
for call in node.data.calls:
child = Node(next((f for f in functions if f.name == call), None))
node.add_child(child)
Parser.order_functions(child, functions, defines)
return node
如果超过了调用堆栈大小的预定义限制,最好的办法可能是重写程序的迭代版本。如果您不知道递归的深度,那么就不要使用递归
更多的information here,如果您需要实现一个迭代版本,您可以从this post获得灵感
这里的主要信息是python doesn't perform any tail recursion elimination。因此,递归函数永远不会处理具有未知/无界层次结构的输入
相关问题 更多 >
编程相关推荐