views:

338

answers:

3

Here's the code:

class BinaryTree:
    def __init__(self,rootObj):
        self.key = rootObj
        self.left = None
        self.right = None
        root = [self.key, self.left, self.right]

    def getRootVal(root):
        return root[0]

    def setRootVal(newVal):
        root[0] = newVal

    def getLeftChild(root):
        return root[1]

    def getRightChild(root):
        return root[2]

    def insertLeft(self,newNode):
        if self.left == None:
                self.left = BinaryTree(newNode)
        else:
            t = BinaryTree(newNode)
            t.left = self.left
            self.left = t

    def insertRight(self,newNode):
        if self.right == None:
            self.right = BinaryTree(newNode)
        else:
            t = BinaryTree(newNode)
            t.right = self.right
            self.right = t

def buildParseTree(fpexp):
    fplist = fpexp.split()
    pStack = Stack()
    eTree = BinaryTree('')
    pStack.push(eTree)
    currentTree = eTree
    for i in fplist:
        if i == '(':
            currentTree.insertLeft('')
            pStack.push(currentTree)
            currentTree = currentTree.getLeftChild()
        elif i not in '+-*/)':
            currentTree.setRootVal(eval(i))
            parent = pStack.pop()
            currentTree = parent
        elif i in '+-*/':
            currentTree.setRootVal(i)
            currentTree.insertRight('')
            pStack.push(currentTree)
            currentTree = currentTree.getRightChild()
        elif i == ')':
            currentTree = pStack.pop()
        else:
            print "error:  I don't recognize " + i
    return eTree

def postorder(tree):
    if tree != None:
        postorder(tree.getLeftChild())
        postorder(tree.getRightChild())
        print tree.getRootVal()

def preorder(self):
    print self.key
    if self.left:
        self.left.preorder()
    if self.right:
        self.right.preorder()

def inorder(tree):
    if tree != None:
        inorder(tree.getLeftChild())
        print tree.getRootVal()
        inorder(tree.getRightChild())

class Stack:
    def __init__(self):
        self.items = []

    def isEmpty(self):
        return self.items == []

    def push(self, item):
        self.items.append(item)

    def pop(self):
        return self.items.pop()

    def peek(self):
        return self.items[len(self.items)-1]

    def size(self):
        return len(self.items)

def main():
    parseData = raw_input( "Please enter the problem you wished parsed.(NOTE: problem must have parenthesis to seperate each binary grouping and must be spaced out.) " )
    tree = buildParseTree(parseData)
    print( "The post order is: ", + postorder(tree))
    print( "The post order is: ", + postorder(tree))
    print( "The post order is: ", + preorder(tree))
    print( "The post order is: ", + inorder(tree))

main()

And here is the error:

Please enter the problem you wished parsed.(NOTE: problem must have parenthesis to seperate each binary grouping and must be spaced out.) ( 1 + 2 )
Traceback (most recent call last):
  File "C:\Users\Kevin\Desktop\Python Stuff\Assignment 11\parseTree.py", line 108, in 
    main()
  File "C:\Users\Kevin\Desktop\Python Stuff\Assignment 11\parseTree.py", line 102, in main
    tree = buildParseTree(parseData)
  File "C:\Users\Kevin\Desktop\Python Stuff\Assignment 11\parseTree.py", line 46, in buildParseTree
    currentTree = currentTree.getLeftChild()
  File "C:\Users\Kevin\Desktop\Python Stuff\Assignment 11\parseTree.py", line 15, in getLeftChild
    return root[1]
AttributeError: BinaryTree instance has no attribute '__getitem__'
+7  A: 

Because you declared your methods wrong:

Lets have a look what happens if you call tree.getLeftChild(). .getLeftChild() is declared this way:

def getRootVal(root):
    return root[0]

As you probably now, the first parameter provided to a class method is always the instance and it is provided implicitly. So you basically try to treat an instance of BinarTree as a sequence(root[0]). You have to specify it this way:

class BinaryTree:
    def __init__(self,rootObj):
        self.key = rootObj
        self.left = None
        self.right = None
        self.root = [self.key, self.left, self.right]   # self.root

    def getRootVal(self):
        return self.root[0]   # access self.root

    def setRootVal(self, newVal):
        self.root[0] = newVal

    # and also the other functions

The first parameter to an objects method does not have to be called self. But helps to do it this way in order to avoid errors like you did.

Interesting that you declared insertLeft and insertRight correctly ;)

Felix Kling
+1  A: 

Your first problem is that root should be self.root.

Your second problem is that here:

def getLeftChild(root):
    return root[1]

You are redefining root with a new meaning.

Mark Byers
A: 

As PreludeAndFugue says, you should fix the formatting. But from what I can gather, there is at least one error in your class methods in BinaryTree: most of them don't even take self as a parameter. Fix that and maybe you might be a bit better off.

inspectorG4dget
I had just actually done that very same thing and it worked before looking back here. Thank you two for your feedback. I plan on using this site alot more for my snags.
Kevin Yusko
But now im getting another problem. In my postorder(tree) it finds the post order of a binary tree given to it, BUT im getting an error along the lines of:Traceback (most recent call last): File "<pyshell#0>", line 1, in <module> main() File "C:\Users\Kevin\Desktop\Python Stuff\Assignment 11\parseTree.py", line 106, in main print( "The post order is: ", + postorder(tree))TypeError: bad operand type for unary +: 'NoneType'Any ideas? Condidering the check at the beginning of it i dont see why its getting a 'NoneType' error.
Kevin Yusko
I think you're trying to print a string and then the value of a variable, yes? I think I see the line where your error is occurring. I would suggest removing the "+". In python, you can just say: <code>print "The value of five is:", 5</code> and it will work as you would expect. If you throw a "+" in there, that's bad syntax. If you really want to use a "+", do <code>print "The value of five is:" + str(5)<code>. The "+" operator is used to concatenate strings and is therefore used badly in your example.
inspectorG4dget
PS: in the future, please add a comment in your code annotating the line number. It makes it easier to trace your code back to the exact error line
inspectorG4dget
thank you good sir...and i will
Kevin Yusko