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()
Attachments
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):
        return root[0]
    
    def setRootVal(root,newVal):
        root[0] = newVal
    
    def getLeftChild(root):
        return root[1]
    
    def getRightChild(root):
        return root[2]
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)

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 .

Edited 5 Years Ago by Gribouillis: n/a

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.

Edited 5 Years Ago by Gribouillis: n/a

Attachments tree.png 4.1 KB
This question has already been answered. Start a new discussion instead.