1

This is my code for creating a BST in python everything is working fine but when i accesses height function it gives error like "AttributeError: 'NoneType' object has no attribute height"i am new to creating datastructures in python any help will be appriciated

 class Node:
        def __init__(self, data):
            self.left = None
            self.right = None
            self.data = data
        def insert(self, data):
            if self.data:
                if data < self.data:
                    if self.left is None:
                        self.left = Node(data)
                    else:
                        self.left.insert(data)
                elif data > self.data:
                    if self.right is None:
                        self.right = Node(data)
                    else:
                        self.right.insert(data)
            else:
                self.data = data

        def print_tree(self):

            if self.left:
                self.left.print_tree()
            print (self.data)
            if self.right:
                self.right.print_tree()

        def height(self):
            if self.data is None:
                return 0
            else:
                return 1 + max(self.left.height(),self.right.height())

    root = Node(8)
    root.insert(3)
    root.insert(10)
    root.insert(1)
    root.insert(6)
    root.insert(4)
    root.insert(7)
    root.insert(14)
    root.insert(13)
    root.print_tree()
    root.height()
1
  • For leaf nodes, self.data is not None , but self.left and self.right would be None , hence the issue. Commented Aug 7, 2017 at 17:19

2 Answers 2

1

For leaf nodes in your tree, self.data would be set to the value of the leaf node , but self.left and self.right would be None. But even before that, there can be nodes where either left or right child is None and since we try that node and get its height we get the AttributeError on NoneType .

In the code for height -

def height(self):
    if self.data is None:
        return 0
    else:
        return 1 + max(self.left.height(),self.right.height())

when the recursion reaches a node, where either left or right node is None, the above code would fail because it would try to access self.left.height() or self.right.height() , and one of them is None.

We can add a simple check to see if either self.left or self.right is None , and based on that get the height from the it.

Sign up to request clarification or add additional context in comments.

Comments

0

If you get an error like that it means you try to use a field of a None. Because your tree is finite you have a leaf that doesn't have any data. I see you have a place in the code where self.data field is assigned to a value but self.left and self.right don't. At the same time, you get values of the fields in height method, you check only self.data. That doesn't make sense for me.

Also, I suggest trying pdb or another debugging tool.

Comments

Your Answer

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.