ross.c ross.c - 6 months ago 45
Python Question

printing out python list from python binary tree

I want to define a function which returns a list of the tree node values. The list is in level order (top to bottom, left to right), if a
child is missing then in its location, "None" is inserted.

3---+
(l) 29---+
(l) 26---+
(r) 4---+
(r) 2---+
(l) 9---+


For example, the list produced from the tree shown
above would be:
[2, 29, 4, 26, None, None, 2, None, None, None, None, None, None, 9, None.


This is the Binary Tree implementation

class BinaryTree:

def __init__(self, data, left = None, right = None):
self.key = data
self.left = left
self.right = right

def insert_left(self, data):
self.left = BinaryTree(data, left=self.left)

def insert_right(self, data):
self.right = BinaryTree(data, right=self.right)

def get_left_subtree(self):
return self.left

def get_right_subtree(self):
return self.right

def set_value(self, val):
self.key = val

def get_value(self):
return self.key

def create_string(self, indent):
string = str(self.key) + '---+'
if self.left:
string += '\n(l)' + indent + self.left.create_string(indent + ' ')
if self.right:
string += '\n(r)' + indent + self.right.create_string(indent + ' ')
return string

def __str__(self):
return self.create_string(' ')

def return_list(self, templist):
templist.append(self.key)
if self.left is None:
templist.append(None)
else:
self.left.return_list(templist)
if self.right is None:
templist.append(None)
else:
self.right.return_list(templist)

def main():
tree = BinaryTree(3)
tree.insert_left(29)
tree.insert_right(4)
right = tree.get_right_subtree()
left = tree.get_left_subtree()
left.insert_left(26)
right.insert_right(2)
right2 = right.get_right_subtree()
right2.insert_left(9)
templist = []
tree.return_list(templist)
main()


I tried to do it without using "self"

def return_list(tree, templist = []):
templist.append(tree.key)
if tree.left is None:
templist.append(None)
else:
return_list(tree.left, templist)
if tree.right is None:
templist.append(None)
else:
return_list( tree.right, templist)
return templist

Answer

Adding the entire .py file If you run this it should work

from collections import deque

class BinaryTree:
    def __init__(self, data, left = None, right = None):
        self.key = data
        self.left = left
        self.right  = right

    def insert_left(self, data):
        self.left = BinaryTree(data, left=self.left)  

    def insert_right(self, data):
        self.right = BinaryTree(data, right=self.right)

    def get_left_subtree(self):
        return self.left

    def get_right_subtree(self):
        return self.right

    def set_value(self, val):
        self.key = val

    def get_value(self):
        return self.key

    def create_string(self, indent):
        string = str(self.key) + '---+'
        if self.left:
            string += '\n(l)' + indent + self.left.create_string(indent + '    ')
        if self.right:
            string += '\n(r)' + indent + self.right.create_string(indent + '    ')
        return string

    def __str__(self):
        return self.create_string('  ')

    def return_list(self, templist):
        templist.append(self.key)
        if self.left is None:
            templist.append(None)
        else:
            self.left.return_list(templist)
        if self.right is None:
            templist.append(None)
        else:
            self.right.return_list(templist)


def return_b_list(tree,templist,queue):
    if tree is None:
        return;
    queue.append(tree)
    while len(queue) !=0:
        node = queue.popleft()
        if node is None:
            templist.append(None)
        else:
            templist.append(node.key)
            queue.append(node.left)
            queue.append(node.right)




def main():    
    tree = BinaryTree(3) 
    tree.insert_left(29)
    tree.insert_right(4)
    right = tree.get_right_subtree()
    left = tree.get_left_subtree()
    left.insert_left(26)
    right.insert_right(2)
    right2 = right.get_right_subtree()
    right2.insert_left(9)
    templist = []
    queue = deque() # you should do a from collections import deque
    return_b_list(tree,templist,queue)
    print tree.create_string(' ')
    print templist

main()
Comments