Binary Search Tree

 

Binary Search Tree(BST)

Handwritten Notes- Click Here


A Binary Search Tree (BST) is a type of binary tree in which each node is arranged according to a specific ordering rule.

  • All values in the left subtree are smaller than the value of the root node.
  • All values in the right subtree are greater than the value of the root node.
  • The same rule is followed recursively for every node in the tree.

Because of this ordering, searching, insertion, and deletion can be performed efficiently.

Insertion:
                      Insertion is used to add a new value to the BST.
Steps
  • Start from the root.
  • Compare the new value with the current node.
  • If the new value is smaller, move to the left subtree.
  • If the new value is greater, move to the right subtree.
  • Continue until an empty position is reached.
  • Insert the new node at that position.

Let us construct a Binary Search Tree by inserting the following values one by one:
                                  50, 30, 70, 20, 40, 60, 80

Step 1: Insert 50
  • There is no node in the tree.
  • Therefore, 50 becomes the root node.
       50

Step 2: Insert 30
  • Compare 30 with 50.
  • 30 < 50, so 30 is placed on the left side of 50.
       50
      /
    30

Step 3: Insert 70
  • Compare 70 with 50.
  • 70 > 50, so 70 is placed on the right side of 50.
        50
      /    \
    30    70

Step 4: Insert 20
  • First compare 20 with 50.
  • 20 < 50, so move to the left.
  • Now compare 20 with 30.
  • 20 < 30, so 20 is placed on the left side of 30.
        50
       /    \
     30    70
    /
  20

Step 5: Insert 40
  • First compare 40 with 50.
  • 40 < 50, so move to the left.
  • Now compare 40 with 30.
  • 40 > 30, so 40 is placed on the right side of 30.
        50
       /  \
     30    70
    /    \
  20    40


Step 6: Insert 60
  • First compare 60 with 50.
  • 60 > 50, so move to the right.
  • Now compare 60 with 70.
  • 60 < 70, so 60 is placed on the left side of 70.
          50
       /      \
     30      70
    /   \       /
  20   40 60

Step 7: Insert 80
  • First compare 80 with 50.
  • 80 > 50, so move to the right.
  • Now compare 80 with 70.
  • 80 > 70, so 80 is placed on the right side of 70.

           50
       /        \
     30        70
    /  \        /    \
  20   40 60    80

Deletion

Deletion in a Binary Search Tree (BST) means removing a particular node while maintaining the BST property.

The deletion operation has three main cases:

  • Deletion of a leaf node
  • Deletion of a node with one child
  • Deletion of a node with two children

Case 1: Deletion of a Leaf Node
A leaf node is a node that does not have any children.

Example
Suppose we want to delete 20.
        50
       /    \
     30    70
    /  \
  20   40

Node 20 has no children.Remove 20 directly from the tree.

After Deletion
        50
       /   \
     30    70
       \
        40


Case 2: Deletion of a Node with One Child
 In this case, the node to be deleted has exactly one child.

Example
Suppose we want to delete 30

        50
       /    \
     30    70
       \
        40

Node 30 has only one child, which is 40. After deleting 30, node 40 takes the position of node 30.

After Deletion

        50
       /    \
     40    70


Case 3: Deletion of a Node with Two Children
In this case, the node to be deleted has two children.
  • Find the Inorder Successor(Value in right subtree)
  • Copy sucessor value to node
  • Delete Sucessor node
Example:
Suppose we want to delete 70.

          50
        /    \
      30      70
             /      \
            60   80

After Deletion

          50                                     
        /    \
      30      80
             /     \
            60   80




          50
        /    \
      30      80
             /     
            60  
       



Video Explanation




Comments

Popular posts from this blog

Queue ADT

Entity-Relationship(ER) Model

Normalization in DBMS,(1NF,2NF,3NF,BCNF,4NF,5NF)