Binary Search Tree : Insertion

  • + 0 comments

    recursive method to insert a value into a binary search tree (BST)

    def __r_insert(self, current_node, value):
        if not current_node:
            current_node = Node(value)
        if value < current_node.info:
            current_node.left = self.__r_insert(current_node.left, value)
        if value > current_node.info:
            current_node.right = self.__r_insert(current_node.right, value)
        return current_node
    
    def insert(self, val):
        if not self.root:
            self.root = Node(val)
            return
        self.__r_insert(self.root, val)