adding a node in Binary Search Tree

  binary-search-tree, python

I am currently working on implementing a Binary search tree but I have a problem with creating the add_node() function. I tried to make it in a recursive way, but the result was weird. For example, if I run add_node(1), add_node(5), and add_node(0), my tree has only 1, no 5 and 0.

I would be happy if you tell me what the problem is.

def add_node(self, value: int) -> None:
        
    if self.root == None:
        self.root = Node(value)
        return

    else:
        return self.add_recursion(self.root, value)

def add_recursion(self, node: Node, value: int) -> None:
        
    if node == None:
        node = Node(value)
        return

    elif value < node.value:
        return self.add_recursion(node.left, value)

    else:
        return self.add_recursion(node.right, value)

Source: Python Questions

LEAVE A COMMENT