Insertion in Binary Search Tree

To perform the Insertion operation in a binary search tree we need to follow some conditions because in the binary search tree, the left node has a value less than the root node and the right node has a value greater than the root node.

Suppose we want to insert a new key X in the binary search tree. so first we start at the root node and move down the tree.

  • If X is less than the key in the node then we move to the left child of the node.
  • If X is greater than the key in the node then we move to the right child of the node.
  • If X is equal to the key in the node then the key is a duplicate key. so we don’t need to insert that key in the tree.
  • If we reach a node that has No left or right child then we insert the value.

Let’s say we have a binary search tree as you see in the image given below.

insertion in binary search tree

Let’s say we want to enter a new key that has the value of 36.

so first we start from the root node that has a value of 70. so the value of 36 is less than 70. so we move to the left child of the node which is 40.

again the node has a value of 40 that is greater than 36 so we move to the left child node which is 35.

again the node has a value of 35 which is less than 36 so we move to the right child node is 37.

now node 37 has no left and right child and 37 is greater than 36 so we insert node 36 as a left child of node 37.

insertion in binary search tree

Program to implement Insertion operation in a binary search tree.

class TreeEmptyError(Exception):
    pass

class Node:
    def __init__(self, value):
        self.info = value
        self.lchild = None        self.rchild = None

class BinarySearchTree:

    def __init__(self):
        self.root = None
    def is_empty(self):
        return self.root is None
    def _insert(self, p, x):
        if p is None:
            p = Node(x)
        elif x < p.info:
            p.lchild = self._insert(p.lchild, x)
        elif x > p.info:
            p.rchild = self._insert(p.rchild, x)
        else:
            print(x, " already present in the tree")
        return p

    def insert1(self, x):
        p = self.root
        par = None        while p is not None:
            par = p
            if x < p.info:
                p = p.lchild
            elif x > p.info:
                p = p.rchild
            else:
                print(x + " already present in the tree")
                return
        temp = Node(x)

        if par is None:
            self.root = temp
        elif x < par.info:
            par.lchild = temp
        else:
            par.rchild = temp

    def search(self, x):
        return self._search(self.root, x) is not None
    def _search(self, p, x):
        if p is None:
            return None        if x < p.info:
            return self._search(p.lchild, x)
        if x > p.info:
            return self._search(p.rchild, x)
        return p

    def search1(self, x):
        p = self.root
        while p is not None:
            if x < p.info:
                p = p.lchild
            elif x > p.info:
                p = p.rchild
            else:
                return True        return False
    def delete(self, x):
        self.root = self._delete(self.root, x)

    def _delete(self, p, x):
        if p is None:
            print(x, " not found")
            return p

        if x < p.info:
            p.lchild = self._delete(p.lchild, x)
        elif x > p.info:
            p.rchild = self._delete(p.rchild, x)
        else:
            if p.lchild is not None and p.rchild is not None:
                s = p.rchild
                while s.lchild is not None:
                    s = s.lchild
                p.info = s.info
                p.rchild = self._delete(p.rchild, s.info)
            else:
                if p.lchild is not None:
                    ch = p.lchild
                else:
                    ch = p.rchild
                p = ch
        return p

    def delete1(self, x):
        p = self.root
        par = None
        while p is not None:
            if x == p.info:
                break            par = p
            if x < p.info:
                p = p.lchild
            else:
                p = p.rchild

            if p is None:
                print(x, " not found")
                return
            if p.lchild is not None and p.rchild is not None:
                ps = p
                s = p.rchild

                while s.lchild is not None:
                    ps = s
                    s = s.lchild
                p.info = s.info
                p = s
                par = ps

            if p.lchild is not None:
                ch = p.lchild
            else:
                ch = p.rchild

            if par is None:
                self.root = ch
            elif p == par.lchild:
                par.lchild = ch
            else:
                par.rchild = ch

    def min1(self):
        if self.is_empty():
            raise TreeEmptyError("Tree is empty")
        p = self.root
        while p.lchild is not None:
            p = p.lchild
        return p.info

    def max1(self):
        if self.is_empty():
            raise TreeEmptyError("Tee is empty")
        p = self.root
        while p.rchild is not None:
            p = p.rchild
        return p.info

    def min2(self):
        if self.is_empty():
            raise TreeEmptyError("Tree is empty")
        return self._min(self.root).info

    def _min(self, p):
        if p.lchild is None:
            return p
        return self._min(p.lchild)

    def max2(self):
        if self.is_empty():
            raise TreeEmptyError("Tree is empty")
        return self._max(self.root).info

    def _max(self, p):
        if p.rchild is None:
            return p
        return self._max(p.rchild)

    def display(self):
        self._display(self.root, 0)
        print()

    def _display(self, p, level):
        if p is None:
            return        self._display(p.rchild, level + 1)
        print()

        for i in range(level):
            print("  ", end='')
        print(p.info)
        self._display(p.lchild, level + 1)

    def preorder(self):
        self._preorder(self.root)
        print()

    def _preorder(self, p):
        if p is None:
            return        print(p.info, " ")
        self._preorder(p.lchild)
        self._preorder(p.rchild)

    def inorder(self):
        self._inorder(self.root)
        print()

    def _inorder(self, p):
        if p is None:
            return        self._inorder(p.lchild)
        print(p.info, " ")
        self._inorder(p.rchild)

    def postorder(self):
        self._postorder(self.root)
        print()

    def _postorder(self, p):
        if p is None:
            return        self._postorder(p.lchild)
        self._postorder(p.rchild)
        print(p.info, " ")

    def height(self):
        return self._height(self.root)

    def _height(self, p):
        if p is None:
            return 0
        hL = self._height(p.lchild)
        hR = self._height(p.rchild)

        if hL > hR:
            return 1 + hL
        else:
            return 1 + hR


###################################
bst = BinarySearchTree()

while True:
    print("1.Display Tree")
    print("2.Search(Iterative)")
    print("3.Search(Recursive)")
    print("4.Insert a new node(Iterative)")
    print("5.Insert a noew node(Recursive)")
    print("6.Delete a node(Iterative)")
    print("7.Delete a node(Recursive)")
    print("8.Find Minimum key(Iterative)")
    print("9.Find Minimum key(Recursive)")
    print("10.Find Maximum key(Iterative)")
    print("11.Find Maximum key(Recursive)")
    print("12.Preorder Traversal")
    print("13.Inorder Traversal")
    print("14.Postoder Traversal")
    print("15.Height of tree")
    print("16.Quit")
    choice = int(input("Enter your choice : "))

    if choice == 1:
        bst.display()
    elif choice == 2:
        x = int(input("Enter the key to be searched : "))
        if bst.search1(x):
            print("Key found")
        else:
            print("Key not found")
    elif choice == 3:
        x = int(input("Enter the key to be searched : "))
        if bst.search(x):
            print("Key found")
        else:
            print("Key not found")

    elif choice == 4:
        x = int(input("Etner the key to be inserte : "))
        bst.insert1(x)
    elif choice == 5:
        x = int(input("Enter the key to be inserted : "))
        bst.insert1(x)
    elif choice == 6:
        x = int(input("Enter the element to be deleted : "))
        bst.delete1(x)
    elif choice == 7:
        x = int(input("Enter the element to be deleted : "))
        bst.delete(x)
    elif choice == 8:
        print("Minimum key is ", bst.min1())
    elif choice == 9:
        print("Minimum key is ", bst.min2())
    elif choice == 10:
        print("Maximum key is ", bst.max1())
    elif choice == 11:
        print("Maximum key is ", bst.max2())
    elif choice == 12:
        bst.preorder()
    elif choice == 13:
        bst.inorder()
    elif choice == 14:
        bst.postorder()
    elif choice == 15:
        print("Height of tree is ", bst.height())
    elif choice == 16:
        break    else:
        print("wrong choice")
    print()