Tam211
Tam211

Reputation: 743

Why do I get an error that the attribute doesn't exist in my class?

I'm working in a Binary Search tree class, and when I run my insert method I get this error:

AttributeError: 'BSearch_tree' object has no attribute 'key'

I don't understand what did I do wrong, and I can't figure out what should be fixed!

Here's my code:

class Tree_node():
    def __init__(self, key, val):
        self.key = key
        self.val = val
        self.left = None
        self.right = None

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

    def insert(self, key, val): 
        if self.root is None:
            self.root=Tree_node(key,val)
        elif key == self.root.key:
            self.val=val
        elif key < self.root.key:
            left = BSearch_tree()
            left.root = self.root.left
            self.root.left=BSearch_tree.insert(left,key,val) 

        elif key > self.root.key:
            right = BSearch_tree()
            right.root=self.root.right
            self.root.right=BSearch_tree.insert(right,key,val)
        return self

Upvotes: 1

Views: 1152

Answers (1)

IsoLinearCHiP
IsoLinearCHiP

Reputation: 727

I hope I got this right ;)

I would suggest not bothering about the Tree_Node but just going with BSearch_Tree like this:

import pprint

class BSearch_tree():
    def __init__(self, key=None, val=None, root=None):
        self.root = root
        self.key = key
        self.val = val
        self.left = None
        self.right = None

    def insert(self, key, val): 
        if self.key is None:
            self.root = self
            self.key = key
            self.val = val
        elif key == self.key:
            self.val=val
        elif key < self.key:
            if self.left == None:
                self.left=BSearch_tree(key,val,self.root)
            self.left.insert(key,val) 
        elif key > self.key:
            if self.right == None:
                self.right=BSearch_tree(key,val,self.root)
            self.right.insert(key,val) 
        # return self

    def __repr__(self):
        return '<BSearch_tree: key: "%s", val: "%s", left: "%s", right: "%s" >' % self.key, self.val, self.left, self.right)

    # lazy retrieval with recursion
    def __getitem__(self, key):
        if self.key is None:
            raise KeyError
        elif key == self.key:
            return self.val
        elif key < self.key:
            if self.left == None:
                raise KeyError
            return self.left[key]

        elif key > self.key:
            if self.right == None:
                raise KeyError
            return self.right[key]

bt = BSearch_tree()
bt.insert(1,'1')
print bt[1]
bt.insert(2,'2')
print bt[2]
try:
    print bt[3]
except KeyError:
    print '3 not there as expected'

bt.insert(5,'5')
print bt[5]
bt.insert(3,'3')
print bt[3]
pprint.pprint(bt)

I have the sneaking feeling I should be using the self.root somewhere, but I'm not entirely sure right now.

Upvotes: 1

Related Questions