0

how do I assign a name to the left child of the left child of the root? I am new with classes. the last two lines are where I try to make the assignment. Help is appreciated!

class BinaryTree:
 def __init__(self,rootName):
 self.root = rootName
 self.leftChild = None
 self.rightChild = None
 def insertLeft(self,newNode):
 if self.leftChild == None:
 self.leftChild = BinaryTree(newNode)
 else:
 t = BinaryTree(newNode)
 t.leftChild = self.leftChild
 self.leftChild = t
 def insertRight(self,newNode):
 if self.rightChild == None:
 self.rightChild = BinaryTree(newNode)
 else:
 t = BinaryTree(newNode)
 t.rightChild = self.rightChild
 self.rightChild = t
 def getRightChild(self):
 return self.rightChild
 def getLeftChild(self):
 return self.leftChild
 def setRootVal(self,obj):
 self.root = obj
 def getRootVal(self):
 return self.root
r = BinaryTree('a')
r.leftChild = 'b'
r.rightChild = 'c'
r.insertLeft = BinaryTree('b')
r.insertRight = BinaryTree('c')
r.getLeftChild().leftChild = 'd' 
r.leftChild.leftChild = 'd'

both of the last two statements give me this error: 'str' object has no attribute 'getRootVal' I am trying to create a tree that looks like this: enter image description here

asked May 25, 2020 at 13:57
4
  • 2
    You don't seem to understand how to use the BinaryTree class. What does the homework require you to do? Commented May 25, 2020 at 14:04
  • hi, trying to create a tree that looks like this above Commented May 25, 2020 at 14:13
  • 1
    insertLeft and insertRight are functions, not attributes. As such you should be doing r.insertLeft('b') and r.insertRight('c'). The code runs after this modification, although I agree with @Sweeper and you could implement this better. Commented May 25, 2020 at 14:19
  • thank you! first time doing OOP. What would be the better implementation? Commented May 25, 2020 at 14:22

1 Answer 1

1

Looking at the interface of BinaryTree, you are supposed to call insertLeft and insertRight to add children to the tree, instead of:

r.leftChild = 'b'
r.rightChild = 'c'

leftChild and rightChild are supposed to be BinaryTree objects, not strings. Although you could write:

r.leftChild = BinaryTree('b')
r.rightChild = BinaryTree('c')

It doesn't handle the case when leftChild and rightChild are not None. This is why you should use insertLeft and insertRight, which does handle these cases for you:

r.insertLeft('b')
r.insertRight('c')

To insert d, e and f, we do it the same way using insertLeft and insertRight:

r.getLeftChild().insertRight('d')
r.getRightChild().insertLeft('e')
r.getRightChild().insertRight('f')
answered May 25, 2020 at 14:28
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.