Suprateem Banerjee
Suprateem Banerjee

Reputation: 458

Why am I getting an error while coding a binary search tree?

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'

Upvotes: 0

Views: 452

Answers (1)

fferri
fferri

Reputation: 18940

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)

Upvotes: 2

Related Questions