Why am I getting a " instance has no attribute '__getitem__' " error?
- by Kevin Yusko
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__'