title: How to replace a node in an n-ary tree in python?
body: <p>I have created an n-ary tree using python. Tree structure is following</p>
  
  <pre><code>class Tree:
      def __init__(self, data=None):
          self.data=data
          self.child=[]
  </code></pre>
  
  <p>I want to replace a node in a tree. I am using preorder traversal to find the node to replace. Following is the code to replace the node.</p>
  
  <pre><code>    def replace(self, root, node):
          if root is not None:
              if root.data == node.data:
                 root=node
                 root.child=node.child
                 return
              for i in range(0,len(root.child)):
                 self.replace(root.child[i], node)
  </code></pre>
  
  <p>In the replace function, node is the new "node" which i want to replace in the tree rooted at "root". </p>
  
  <p>The above code doesn't replacing the node. before and after replace function, tree is same.</p>
  
  <p>Any help is appreciated!</p>