title: Flattening a binary search tree
body: <p>I want to define a function <code>flatten(tree)</code> such that it visits the left branch, and then entry, and then finally the right branch.</p>
  
  <p>I've tried:</p>
  
  <pre><code>def flatten(tree):
      if is_empty_tree(tree):
          return tree
      else:
          return [left_branch(tree)]+[entry(tree)]+[right_branch(tree)]
  </code></pre>
  
  <p>but this isn't anywhere near to my desired output.</p>
  
  <p>When tree is <code>[5, [2, [1, [], []], []], [7, [6, [], []], [10, [], []]]]</code> I'm supposed to get <code>[1, 2, 3, 5, 6, 7, 10]</code> but I got <code>[[2, [1, [], []], []], 5, [7, [6, [], []], [10, [], []]]]</code> instead.</p>
  
  <p>How can I implement this function such that it visits the left branch, entry, and then the right branch and get the list that I want?</p>
  
  <p>I have the following functions defined:</p>
  
  <pre><code>def build_tree(entry, left, right):
      return [entry, left, right]
  
  def entry(tree):
      return tree[0]
  
  def left_branch(tree):
      return tree[1]
  
  def right_branch(tree):
      return tree[2]
  
  def is_empty_tree(tree):
      if tree==[]:
          return True
      else:
          return False
  </code></pre>