Search code examples
pythonbinary-search-tree

BST Insert function giving issues


I am working on practicing python but I am stuck here. I got my Binary Search Tree to work as it relates to the search function but I am having some issues with my insert function. I keep getting the error TypeError: insert() missing 1 required positional argument: 'key' I cannot seem to see what I am missing:

class Node:

def __init__(self, key, parent = None):
    self.key = key
    self.parent = parent
    self.left = None 
    self.right = None
    # Make sure that the parent's left/right pointer
    # will point to the newly created node.
    if parent != None:
        if key < parent.key:
            assert(parent.left == None),
            parent.left = self
        else:
            assert key > parent.key, 
            assert(parent.right == None ), 
            parent.right = self

# Utility function that keeps traversing left until it finds
# the leftmost descendant
def get_leftmost_descendant(self):
    if self.left != None:
        return self.left.get_leftmost_descendant()
    else:
        return self

def search(self, key):
    if self.key == key:
        return (True, self)
    else:
        if key > self.key and self.right != None:
            return self.right.search(key)
        if key < self.key and self.left != None:
            return self.left.search(key) 
        else:
            return (False, self)

def insert(self, parent, key):
    if parent is None:
        parent = Node(key)
        return parent
    else:
        if parent.data <= key:
            parent.right = self.insert(parent.right, key)
        else:
            parent.left = self.insert(parent.left, key)
        return parent

These are the conditions it tests against. It seems to pass the search with no isuues but I am not entirely sure as to why the insert errors out.

print('-- Testing search -- ')
(b, found_node) = t1.search(18)
assert b and found_node.key == 18, 'test 8 failed'
(b, found_node) = t1.search(25)
assert b and found_node.key == 25, 
(b, found_node) = t1.search(26)
assert(not b), 'test 10 failed'
assert(found_node.key == 40),

print('-- Testing insert -- ')
ins_node = t1.insert(26)
assert ins_node.key == 26, ' test 12 failed '
assert ins_node.parent == t4, ' test 13 failed '
assert t4.left == ins_node, ' test 14 failed '
ins_node2 = t1.insert(33)
assert ins_node2.key == 33, 'test 15 failed'
assert ins_node2.parent == ins_node, 'test 16 failed'
assert ins_node.right == ins_node2, 'test 17 failed'

Solution

  • That's because the insert signature defined in Node is

    def insert(self: Node, parent: Optional[Node], key: int):
    

    and in your tests you're calling

    ins_node = t1.insert(26)
    

    which has the signature

    def insert(self: Node, key: int):
    

    Since the signatures don't match, the error appears. You can refactor to something like this:

    def insert(self, key):
        if self.parent is None:
            self.parent = Node(key)
            return parent
        
        if self.parent.data <= key:
            self.parent.right = self.parent.right.insert(key)
        else:
            self.parent.left = self.parent.left.insert(key)
        return self.parent