0

Why can't this python program run correctly. I would like to build a binary tree and then traverse it in pre-order. When I call the PreOrder method, it returns nothing.

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

class BTree:
    def __init__(self, root):
        self.root = root

    def CreateTree(self, root):
        self.root.data = raw_input("Enter data,'*' means empty: ")
        if self.root.data == '*':
            return
        self.root.left = Node()
        self.root.right = Node()
        self.CreateTree(self.root.left)
        self.CreateTree(self.root.right)

    def PreOrder(self, root):
        if self.root != None:
            if self.root.data != '*':
                print self.root.data,
                PreOrder(self, self.root.left)
                PreOrder(self, self.root.right)

if __name__ == '__main__':
    t = Node()
    bt = BTree(t)
    bt.CreateTree(t)
    bt.PreOrder(t)
0

4 Answers 4

3

PreOrder is a method of the class BTree, you might want to change the recursive calls, like so:

def PreOrder(self):
    if self.root != None:
        if self.root.data != '*':
            print self.root.data,
            self.root.left.PreOrder()
            self.root.right.PreOrder()

You also don't need to pass root to CreateTree, if you are only using self.root which you saved in __init__

Sign up to request clarification or add additional context in comments.

Comments

0

Your CreateTree method does not mention the parameter root at all. This method always operate on self.root.

Something like this may work:

def CreateTree(self, current_node):
    current_node.data = raw_input("Enter data,'*' means empty: ")
    if current_node.data == '*':
        return
    current_node.left = Node()
    current_node.right = Node()
    self.CreateTree(current_node.left)
    self.CreateTree(current_node.right)

Comments

0

Because you are setting self.root.data = '*' in the CreateTree function by doing this self.root.data = raw_input("Enter data,'*' means empty: "). And to terminate the input prompt you have to enter '*' in the end. So last value that your self.root.data has is '*'.

Try this

    print self.root.data
    root.data = raw_input("Enter data,'*' means empty: ")
    print self.root.data
    if root.data == '*':
        return

and this

    def CreateTree(self,root):
        print self.root.data
        root.data = raw_input("Enter data,'*' means empty: ")
        print self.root.data
        if root.data == '*':
            return
        self.root.left = Node()
        self.root.right = Node()
        self.CreateTree(self.root.left)
        self.CreateTree(self.root.right)

    def PreOrder(self, root):
        print root
        print self.root
        print self.root.data
        if self.root != None:
            if self.root.data != '*':
                print self.root.data,
                PreOrder(self, self.root.left)
                PreOrder(self, self.root.right)
            else:
                print 'what the hell'

if __name__ == '__main__':
    t = Node(10,Node(),Node())
    print t.data
    bt = BTree(t)
    bt.CreateTree(t)
    bt.PreOrder(t)

to know where it went wrong.

Comments

0

I you look at your CreateTree method, you will see, that you never use root and therefore self.root.data will always be *. If you fix that, your code has more problems, all related to self and scopes. You might want to check this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.