title: How do I implement a pre-created generic tree?
body: <pre><code>Main code:
  
  import trees as trs
  
  def main():
      while True:
          comando = input(&quot; &quot;)
          if not comando:
              break
          commands = comando.split(&quot; &quot;)
  
          if (commands[0] == &quot;RP&quot;):
              commandRP(commands, trs)
  
          elif(commands[0] == &quot;RU&quot;):
              commandRU(commands, trs)
  
          elif(commands[0] == &quot;RF&quot;):
              commandRF(commands, trs)
  
          elif(commands[0] == &quot;AF&quot;):
              commandAF(commands, trs)
  
          elif(commands[0] == &quot;DF&quot;):
              commandDF(commands, trs)
          
          elif(commands[0] == &quot;LP&quot;):
              commandLP(commands, trs)
  
          elif(commands[0] == &quot;LU&quot;):
              commandLU(commands, trs)
  
          elif(commands[0] == &quot;LF&quot;):
              commandLF(commands, trs)
  
          elif(commands[0] == &quot;MF&quot;):
              commandMF(commands, trs)
  
          elif(commands[0] == &quot;MC&quot;):
              commandMC(commands, trs)
  
          elif(commands[0] == &quot;CC&quot;):
              commandCC(commands, trs)
  
          elif(commands[0] == &quot;LCU&quot;):
              commandLCU(commands, trs)
  
          elif(commands[0] == &quot;LFC&quot;):
              commandLFC(commands, trs)
          
          elif(commands[0] == &quot;LSP&quot;):
              commandLSP(commands, trs)
  
          elif(commands[0] == &quot;LMS&quot;):
              commandLMS(commands, trs)
  
          elif(commands[0] == &quot;G&quot;):
              commandG(commands, trs)
  
          elif(commands[0] == &quot;L&quot;):
              commandL(commands, trs)
  
          else:
              print(&quot;Invalid Instruction&quot;)
  
  
  def commandRP(commands, trs):
      data = commands[1]
      t = trs.Tree()
      t.insert(data)
  
  def commandRU(commands, trs):
      pass
  
  def commandRF(commands, trs):
      pass
  
  def commandAF(commands, trs):
      pass
  
  def commandDF(commands, trs):
      pass
  
  def commandLP(commands, trs):
      pass
  
  def commandLU(commands, trs):
      pass
  
  def commandLF(commands, trs):
      pass
  
  def commandMF(commands, trs):
      pass
  
  def commandMC(commands, trs):
      pass
  
  def commandCC(commands, trs):
      pass
  
  def commandLCU(commands, trs):
      pass
  
  def commandLFC(commands, trs):
      pass
  
  def commandLSP(commands, trs):
      pass
  
  def commandLMS(commands, trs):
      pass
  
  def commandG(commands, trs):
      pass
  
  def commandL(commands, trs):
      pass
  
  if __name__ == &quot;__main__&quot;:
      t = trs.Tree()
      a = t.insert(&quot;US&quot;)
      t.insert(&quot;Patient&quot;, a)
      main()
  </code></pre>
  <p>Tree implementation code:</p>
  <pre><code>class TreeNode:
      def __init__(self, data, children = None, parent = None):
          self.data = data
          self.children = []
          self.parent = None
  
      def is_leaf(self):
          if self.children == 0:
              return True
          else:
              return False
  
      def is_root(self):
          if self.parent == None:
              return True
          else:
              return False
      
      def add_child(self, child):
          self.parent = self
          self.children.append(child)
  
      def depth(self):
          if self.is_root():
              return 0
          else:
              return 1 + self.parent.depth
  
  class Tree:
      def __init__(self):
          self.root = None
          self.height = 0
          self.nodes = []
  
      def insert(self, data, parent = None):
          if parent is not None:
              parent.add_child(data)
          else:
              if parent is None:
                  self.root = data
              self.nodes.append(data)
  </code></pre>
  <p>What I'm trying to do is, by the time I run the code I already have a pre created <strong>Root</strong> with <strong>2 childs</strong>.</p>
  <p>When I run the code it gives me the following error: &quot;AttributeError: 'str' object has no attribute 'add_child'&quot; and I can't seem to figure what's wrong.</p>
  <p>Thanks in advance!</p>
  <p>EDIT: they are in 2 different files</p>