mikeizbicki / cmc-csci046

CMC's Data Structures and Algorithms Course Materials
55 stars 155 forks source link

Issue with insert, 'Node' has no attribute 'root' #501

Open norawillett opened 1 year ago

norawillett commented 1 year ago

Hello, I have finished all of the functions for the BST and BinaryTree files, however I am running into the error, and it keeps referencing my insert function:

AttributeError: 'Node' object has no attribute 'root'

Here is what I have for insert:

def insert(self, value):
      if self.root:
          return BST._insert(self.root, value)
      else:
          self.root = Node(value)

Here is my BinaryTree.py init for reference:

    def __init__(self, root=None):
        if root:
            self.root = Node(root)
        else:
            self.root = None

Anybody have any idea how to fix this?

chatch166 commented 1 year ago

I am also getting this... any help would be greatly appreciated!

mikeizbicki commented 1 year ago

In general when reporting error messages, it is helpful to provide the full stack trace and not abbreviate it. In this particular case, for example, you've not provided the particular line number that is causing the error, which makes it much harder to figure out what the problem is.

My guess is that the error is happening on one of the lines that mention self.root. Recall that the insert method is not a static method, and therefore the first self parameter must be a BST object. If the self parameter is a BST object, then self.root will always be defined. But if the self parameter is a Node object (and your error message of

AttributeError: 'Node' object has no attribute 'root'

indicates that it probably is), then .root is not defined because Nodes only have left, right, and value attributes.

So somehow you have to figure out why you are calling the insert method on a Node instead of a BST. The most likely cause is that at some point you were intending to call the static method _insert on a Node, but you called the non-static insert instead.

Again, if we had the full stack trace here, we would be able to look at the function that is calling the insert function in the stack trace to find exactly what line is causing the problem. But since we don't have the full stack trace, you'll have to do that part yourself.