Keep getting Attribute Errors with my code any suggestions?
There are two files attatched

from string12 import *
from root import *

def main():
    tree1 = raw_input("enter numbers: ")
    print buildParseTree(tree1)
    

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
main()

Recommended Answers

All 2 Replies

Keep getting Attribute Errors with my code any suggestions?
There are two files attatched

from string12 import *
from root import *

def main():
    tree1 = raw_input("enter numbers: ")
    print buildParseTree(tree1)
    

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
main()

I got something by replacing root.py with

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

    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 getRootVal(root): # accessor functions are java-ic and not very pythonic.
        return root.key
    
    def setRootVal(root,newVal):
        root.key = newVal
    
    def getLeftChild(root):
        return root.left
    
    def getRightChild(root):
        return root.right

""" Result of main -->
enter numbers: ( 3 * 5 + 7 ) - 4
<root.BinaryTree instance at 0x7f08309c2170>
"""

Note that specialized modules exist to parse arithmetic expressions, and languages in general. Also this snippet could draw your binary tree http://www.daniweb.com/code/snippet323792.html .

With the fastgraph snippet above, I replaced your call to main() by

from fastgraph import graph
import webbrowser as web

def links(tree):
    for n in (tree.left, tree.right):
        if n is not None:
            yield tree, n
            for link in links(n):
                yield link

def label(tree):
    return repr(tree.key)

def show(tree, filename="tree.png"):
    g = graph(links(tree), label, directed=True)
    g.draw(filename, prog = 'dot')
    web.open(filename)


if __name__ == "__main__":
    t = buildParseTree("( 3 * 5 + 7 ) - 4")
    show(t)

This pops up a window showing the tree. This tree is probably not what you are expecting, so there are still errors in your program. Remark that fastgraph cannot distinguish left from right.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.