recursion - python recursive iteration exceeding limit for tree implementation -


i'm implementing tree dynamically in python. have defined class below

class nodeobject():      def __init__(self,presentnode=none,parent=none):         self.currentnode = presentnode         self.parentnode = parent         self.childs = [] 

i have function gets possible childs every node pool

def findchildren(node, childs):` `# no need write whole function on how gets childs 

now have recursive function starts head node (no parent) , moves down chain recursively every node (base case being last node having no children)

def tree(dad,children):      child in children:         childobject = nodeobject(child,dad)         dad.childs.append(childobject)         newchilds = findchildren(child, children)         if len(newchilds) == 0:             lastchild = nodeobject(newchilds,childobject)             childobject.childs.append(lastchild)             loopchild = copy.deepcopy(lastchild)             while loopchild.parentnode != none:                 print "last child"                 result.append(loopchild.currentnode) # result global store values                 loopchild = copy.deepcopy(loopchild.parentnode)         else:             tree(childobject,newchilds) 

the tree formation works number of inputs only. once pool gets bigger, results "maximum recursion depth exceeded"

i have tried setting recursion limit set.recursionlimit() , doesn't work. program crashes. want implement stack recursion, can please help, have gone no after trying long time ?? also, there other way fix other stack ?

when try change recursion depth, program crashes because exceeding hard recursion limit imposed size of stack on system. setting sys.recursionlimit() makes python less strict depth, doesn't affect platform support.

python has naïve implementation recursion means useful when can guarantee recursion depth going low. first check tree deep enough blow stack; if nodes have children parents/ancestors, code try run ever until exhausts stack. 1 way check keep track of nodes returned findchildren() , make sure never repeat.

if data correct , stack isn't deep enough, have translate code iterative version , manually build own stack. here code explicit stack (i have not tested there may bugs, should give idea of how it):

def tree(dad, children):     stack = [(dad, children)]     while stack:         dad, children = stack.pop()         index, child in enumerate(children):             childobject = nodeobject(child,dad)             dad.childs.append(childobject)             newchilds = findchildren(child, children)             if len(newchilds) == 0:                 lastchild = nodeobject(newchilds,childobject)                 childobject.childs.append(lastchild)                 loopchild = copy.deepcopy(lastchild)                 while loopchild.parentnode != none:                     print "last child"                     result.append(loopchild.currentnode) # result global store values                     loopchild = copy.deepcopy(loopchild.parentnode)             else:                 stack.append((dad, children[index:]))                 stack.append((childobject,newchilds))                 break 

one important feature note have push children, have not yet been processed in loop, onto stack before push new children nodes.

@peter gibson makes point don't want deepcopy nodes, should not cause stack blow (just use lots , lots of memory without benefit can see).


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -