title: Can someone explain how the "printtree" function in this python code works
body: <p>I was trying to figure out how to make a binary tree in python and found this code online. Can anyone explain how the <code>printtree</code> function is able to print out all the values</p>
  <pre><code>class node: 
  
      def __init__(self, data):
          self.leftval = None
          self.data = data 
          self.rightval = None 
  
      def insert(self, data):
          if self.data == None:
              self.data = data 
          else:
              if data &lt; self.data:
                  if self.leftval == None:
                      self.leftval = node(data) 
                  else:
                      self.leftval.insert(data)
              elif data &gt; self.data:
                  if self.rightval == None:
                      self.rightval = node(data)
                  else:
                      self.rightval.insert(data)
  
      def printtree(self):
          if self.leftval:
              self.leftval.printtree()
          print (self.data)
          if self.rightval:
              self.rightval.printtree()
         
  
  root = int(input(&quot;Enter the root value&quot;))
  root = node(root)
  
  for i in range (0, 10):
      num = int(input(&quot;Enter a number&quot;))
      root.insert(num)
  
  root.printtree() 
  </code></pre>