0

I'm struggling to get the hang of traversing a binary search tree I think I have my code right but I keep getting the following error.

C:\Python33\python.exe "C:/Users/koopt_000/Desktop/College/Sophomore Semester 2/Computer Science 231/Chapter7/Test.py"
Traceback (most recent call last):
  File "C:/Users/koopt_000/Desktop/College/Sophomore Semester 2/Computer Science 231/Chapter7/Test.py", line 16, in <module>
    print(BST.preOrder(tree))
TypeError: preOrder() missing 1 required positional argument: 'tree'

Process finished with exit code 1

I'm trying to print out my pre order of my tree and it just doesn't want to work, here is my code.

class TreeNode(object):

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

        self.item = data
        self.left = left
        self.right = right

    def __str__(self):
        return str(self.item)


#------------------------------------------------------------

from TreeNode import TreeNode


class BST(object):

    #------------------------------------------------------------

    def __init__(self):



        self.root = None
        self.size = 0

    #------------------------------------------------------------

    def preOrder(self, tree):
        if tree is None:
            pass
        else:
            print(tree.item)
            preOrder(tree.left)
            preOrder(tree.right)

    def postOrder(self, root):
        if root is None:
            pass
        else:
            self.postOrder(root.left)
            self.postOrder(root.right)
            print(root.item)

    def inOrder(self, root):
        if root is None:
            pass
        else:
            self.inOrder(root.left)
            print(root.item)
            self.inOrder(root.right)

Here is my testing code:

from BinarySearchTree import BST
from TreeNode import TreeNode

tree = TreeNode(1,
                TreeNode(2,
                         TreeNode(4,
                                  TreeNode(7,None,None),
                                  None),
                         TreeNode(5, None, None)),
                TreeNode(3,
                         TreeNode(6,
                                  TreeNode(8, None, None),
                                  TreeNode(9, None, None)),
                         None))

print(BST.preOrder(tree))

I know it has to do something with me putting something for 'self' in my print statement, but I'm pretty stumped and my brain is not functioning properly at the moment. Any ideas?

1 Answer 1

3

Worked fine for me with some changes.

bst.py:

class TreeNode(object):

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

        self.item = data
        self.left = left
        self.right = right

    def __str__(self):
        return str(self.item)


#------------------------------------------------------------


class BST(object):

    #------------------------------------------------------------

    def __init__(self):
        self.root = None
        self.size = 0

    #------------------------------------------------------------

    def preOrder(self, tree):
        if tree is None:
            pass
        else:
            print(tree.item)
            self.preOrder(tree.left)
            self.preOrder(tree.right)

    def postOrder(self, root):
        if root is None:
            pass
        else:
            self.postOrder(root.left)
            self.postOrder(root.right)
            print(root.item)

    def inOrder(self, root):
        if root is None:
            pass
        else:
            self.inOrder(root.left)
            print(root.item)
            self.inOrder(root.right)

something.py

from bst import BST
from bst import TreeNode

tree = TreeNode(1,
                TreeNode(2,
                         TreeNode(4,
                                  TreeNode(7,None,None),
                                  None),
                         TreeNode(5, None, None)),
                TreeNode(3,
                         TreeNode(6,
                                  TreeNode(8, None, None),
                                  TreeNode(9, None, None)),
                         None))

a = BST()

print(a.preOrder(tree))

Output:

bob@squids:~/Desktop$ python something.py
1
2
4
7
5
3
6
8
9
None
Sign up to request clarification or add additional context in comments.

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.