title: Printing a decision tree in py
body: <p>I got a <code>class Node</code>:</p>
  <pre><code>class Node:
  def __init__(self, label):
      self.label = label
      self.children = []
      self.answer = ''
      
      self.isPruned = False
  </code></pre>
  <p>where <code>label</code> is set to be the attribute e.g(<code>Outlook</code>).</p>
  <p><code>children</code> is an array in which I store a pair <code>(value, Node)</code> where <code>value</code> is the split decision, and <code>Node</code> is the next node
  <code>answer</code> contains the classification at the end of a path</p>
  <p>I would like to print it as a Tree, where on each edge there will be the <code>value</code> of the split.</p>
  <pre><code>def print_tree(node, level = 0):
      if node.answer != '':
          print(' '*level, node.answer)
          return
      print(''*level, node.label)
      for value, n in node.children:
          print(''*(level+1), value)
          print_tree(n, level+2)
    
  </code></pre>
  <p>that's what I came up with, but is not what I expected.</p>
  <p>Any idea on how I can easily displaying it showing each nodes and edges in the right place?</p>
  <p><a href="https://i.stack.imgur.com/ridCl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ridCl.png" alt="enter image description here" /></a></p>
  <p>EDIT:</p>
  <p>here's the code for generating the tree:</p>
  <pre><code>def build_tree(df, attributes, target, parent = None):
      
      if df.empty:
          node = Node('')
          node.is_leaf = True
          node.answer = plurality_value(df[target])
          return node
      
      elif len(np.unique(df[target])) &lt;= 1: #Tutti gli esempi sono uguali ritorna quella classificazione
          node = Node('')
          node.is_leaf = True
          node.answer = np.unique(df[target])[0]
          return node
      
      elif len(attributes) == 0:
          node = Node('')
          node.is_leaf = True
          node.answer = plurality_value(parent)
          return node
      
      else:
          best_split = importance(df, target)
          tree = Node(best_split)
          tree.is_internal = True
          
          new_attributes = [i for i in attributes if i != best_split]
          new_parent = df[target]
          
          for value in np.unique(df[best_split]):
              min_df = df.where(df[best_split] == value).dropna()
              sub_tree = build_tree(min_df, new_attributes, target, new_parent)
              tree.children.append((value, sub_tree))
  
          return tree
  </code></pre>