python how to implement a list into a tree?
<p>I have a list of data indicates what direction is going next like:</p>

<pre><code>[[0,1,0,0,1],[0,0,1],[0,0],[0,1,1,1,0]]
</code></pre>

<p>I want to implement this data into a tree structure like:
<a href="http://i.stack.imgur.com/NA80y.jpg" rel="nofollow"><img src="http://i.stack.imgur.com/NA80y.jpg" alt="enter image description here"></a></p>

<p>The number inside the node is how many people walked on this direction.</p>

<p>I have a Tree class that I write myself like this:</p>

<pre><code>class Tree(object):
    def __init__(self):
        self.left = None
        self.right = None
        self.data = 0
def insert(self,num):
    self.data = self.data + 1
    if num == 0:
        if self.left == None:
            self.left = Tree()
        return self.left
    elif num == 1:
        if self.right == None:
            self.right = Tree()
        return self.right
</code></pre>

<p>How can I do this? I tried to make it in a recursive way but turns out it's not saving under <code>root</code> but <code>build_tree</code> which is a variable that I tried to make as the recursive pointer.</p>

<pre><code>root = Tree()
for route in data:
    build_tree = root
    for i in range (0,len(route)):
        num = route[i]
        build_tree = build_tree.insert(num)
</code></pre>

<p>Thanks!</p>

<p>Edit: This code actually works just like Blender said in comment. I think I had something wrong when I implemented it to a more complex code.</p>

<p>Also thanks John La Rooy for the suggestion and Kevin K. for the example!</p>

<p>Try making a separate class for the node like so</p>

<pre><code>class Node:
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right = None
</code></pre>

<p>Then in your <code>Tree</code> class initialize <code>self.root</code> and declare your functions with recursion within <code>Tree</code></p>

<p>Edit: <a href="https://github.com/kkirchhoff01/MiscScripts/blob/master/binary_tree.py" rel="nofollow">Here</a> is an example.</p>

python data-structures