Tree traversals python
<p>I have to define three functions: <code>preorder(t):</code>, <code>postorder(t):</code>, and <code>inorder(t):</code>.</p>

<p>Each function will take a binary tree as input and return a list. The list should then be ordered in same way the tree elements would be visited in the respective traversal (post-order, pre-order, or in-order)</p>

<p>I have written a code for each of them, but I keep getting an error when I call another function (<code>flat_list()</code>), I get an index error thrown by</p>

<pre><code>if not x or len(x) &lt; 1 or  n &gt; len(x) or x[n] == None:
    IndexError: list index out of range
</code></pre>

<p>The code for my traversal methods is as follows:</p>

<pre><code>def postorder(t):
    pass
    if t != None:
        postorder(t.get_left())
        postorder(t.get_right())
    print(t.get_data())

def pre_order(t):
    if t != None:
        print(t.get_data())
        pre_order(t.get_left())
        pre_order(t.get_right())

def in_order(t):
    pass
    if t != None:
        in_order(t.get_left())
        print(t.get_data())
        in_order(t.get_right())

def flat_list2(x,n):
  if not x or len(x) &lt; 1 or  n &gt; len(x) or x[n] == None:
    return None

   bt = BinaryTree( x[n] )
   bt.set_left( flat_list2(x, 2*n))
   bt.set_right(flat_list2(x, 2*n + 1))
 return bt
</code></pre>

<p>this is how i call flat_list2 </p>

<pre><code>flat_node_list = [None, 55, 24, 72, 8, 51, None, 78, None, None, 25]
bst = create_tree_from_flat_list2(flat_node_list,1)

    pre_order_nodes = pre_order(bst)

    in_order_nodes = in_order(bst)

    post_order_nodes = post_order(bst)

    print( pre_order_nodes)

    print( in_order_nodes)

    print( post_order_nodes)
</code></pre>