0

I tried to code a binary search tree, but I'm stuck with an error saying int object has no attribute value. This implies that cur_node is being considered an int object. I can't seem to figure out why.
This was encountered while learning to code binary search trees from youtube. PS: I'm new to Python so please do bear with me if it's silly.

class node:
    def __init__(self,value=None):
        self.value=value
        self.left_child=None
        self.right_child=None

class binary_search_tree:
    def __init__(self):
        self.root=None

    def insert(self,value):
        if self.root==None:
            self.root=value
        else:
            self._insert(value,self.root)

    def _insert(self,value,cur_node):
        if value<cur_node.value:
            if cur_node.left_child==None:
                cur_node.left_child=node(value)
            else:
                self._insert(value,cur_node.left_child)
        elif value>cur_node.value:
            if cur_node.right_child==None:
                cur_node.right_child=node(value)
            else:
                self._insert(value,cur_node.right_child)
        else:
            print("Value already in tree")

    def print_tree(self):
        if self.root!=None:
            self._print_tree(self.root)

    def _print_tree(self,cur_node):
        self._print_tree(cur_node.left_child)
        print(str(cur_node.value))
        self._print_tree(cur_node.right_child)

    def height(self):
        if self.root!=None:
            self._height(self.root,0)
        else:
            return 0

    def _height(self,cur_node,cur_height):
        if cur_node==None:
            return cur_height
        left=_height(cur_node.left_child,cur_height)
        right=_height(cur_node.right_child,cur_height)
        return max(left,right)

    def search(self,value):
        if(self.root!=None):
            self._search(self.root,value)
        else:
            return 0

    def _search(self,cur_node,value):
        if(cur_node.value==value):
            return 1
        elif value<cur_node.value and cur_node.left_child!=None:
            return self._search(cur_node.left_child,value)
        elif value>cur_node and cur_node.rightchild!=None:
            return self._search(cur_node.right_child,value)
        return 0

tree=binary_search_tree()
tree.insert(6)
tree.insert(8)
tree.insert(3)
tree.insert(17)
tree.insert(1)
tree.insert(4)

tree.print_tree()

tree.height()

tree.search(5)
tree.search(6)

Error Signature is:

Traceback (most recent call last): File "/Users/suprateem/PycharmProjects/TreeOfLIfe/TreeOfLife.py", line 70, in

tree.insert(8)

File "/Users/suprateem/PycharmProjects/TreeOfLIfe/TreeOfLife.py", line 15, in insert

self._insert(value,self.root)

File "/Users/suprateem/PycharmProjects/TreeOfLIfe/TreeOfLife.py", line 18, in _insert

if value<cur_node.value:

AttributeError: 'int' object has no attribute 'value'

1
  • You should learn how to use pdb and step through the program. Commented May 19, 2018 at 12:56

1 Answer 1

2

First of all, replace if x!=None with if x, and if x==None with if not x. x!=None/x==None is not pythonic. If you really need to check for None (not needed in your case), you should use x is None.

The error is in your insert method. You are not creating a node object for the root, but assigning the value to it. Change it in the following way:

def insert(self,value):
    if not self.root:
        self.root=node(value)
    else:
        self._insert(value,self.root)

You have also an error in the print method (you should check if the left/right subtree is not None):

def _print_tree(self,cur_node):
    if cur_node.left_child: self._print_tree(cur_node.left_child)
    print(str(cur_node.value))
    if cur_node.right_child: self._print_tree(cur_node.right_child)
Sign up to request clarification or add additional context in comments.

4 Comments

... and there are other errors as well (functions not always returning values, functions called but their results ignored).
Thanks for the help. Insertion and print works fine now. However, my _height function throws an error "referenced before assignment" on the line where left and right are being assigned. I looked it up, and usually it's a local-global issue, but in my case, I'm not getting exactly why.
PS: I have added "self." after the"=" in those statements (ie left=self._height....)
cannot reproduce your issue. I fixed your source in this way: pastebin.com/3bBVkPLv

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.