Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE) PDF Download

Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)The idea is to do iterative level order traversal of the given tree using queue. If we find a node whose left child is empty, we make new key as left child of the node. Else if we find a node whose right child is empty, we make the new key as right child. We keep traversing the tree until we find a node whose either left or right is empty.

  • C++
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
  • Java
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
  • Python3
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
  • C#
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)

Output

  • Inorder traversal before insertion: 7 11 10 15 9 8
  • Inorder traversal after insertion: 7 11 12 10 15 9 8

Deletion in a Binary Tree

Given a binary tree, delete a node from it by making sure that tree shrinks from the bottom (i.e. the deleted node is replaced by bottom most and rightmost node). This is different from BST deletion. Here we do not have any order among elements, so we replace with last element.

Examples:

  • Delete 10 in below tree
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)Output:
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
  • Delete 20 in below tree
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)    Output:
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
        

Algorithm 

  1. Starting at root, find the deepest and rightmost node in binary tree and node which we want to delete. 
  2. Replace the deepest rightmost node’s data with node to be deleted. 
  3. Then delete the deepest rightmost node.
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
  • C++
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
  • Java
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
  • Python3
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
  • Javascript
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)

Output:

  • Inorder traversal before deletion: 7 11 12 10 15 9 8
  • Inorder traversal after deletion: 7 8 12 10 15 9

Note: We can also replace node’s data that is to be deleted with any node whose left and right child points to NULL but we only use deepest node in order to maintain the Balance of a binary tree.

The document Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Programming and Data Structures.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
119 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Insertion & Deletion in Binary Tree - Programming and Data Structures - Computer Science Engineering (CSE)

1. What is deletion in a binary tree?
Ans. Deletion in a binary tree refers to the process of removing a node from the tree. This operation involves adjusting the pointers and rearranging the remaining nodes to maintain the structure and properties of a binary tree.
2. How is deletion performed in a binary tree?
Ans. Deletion in a binary tree can be performed by following these steps: 1. Start at the root node and search for the node to be deleted. 2. Once the node is found, determine the type of node: leaf node, node with one child, or node with two children. 3. For a leaf node, simply remove it by updating its parent's appropriate child pointer. 4. For a node with one child, connect the child node directly to the parent of the node to be deleted. 5. For a node with two children, find the inorder successor or predecessor of the node to be deleted. Copy the value of the successor/predecessor to the node to be deleted and delete the successor/predecessor.
3. What is the complexity of deletion in a binary tree?
Ans. The complexity of deletion in a binary tree depends on the height of the tree. In the worst-case scenario, when the tree is highly unbalanced, the complexity can be O(n), where n is the number of nodes in the tree. However, in a balanced binary tree, the complexity is O(log n), which is more efficient.
4. Can a node with two children be directly deleted in a binary tree?
Ans. No, a node with two children cannot be directly deleted in a binary tree. Deleting a node with two children requires finding its inorder successor or predecessor and replacing the node's value with the successor/predecessor value. This ensures that the tree's properties are maintained and the binary search tree structure is preserved.
5. What happens to the nodes below the deleted node in a binary tree?
Ans. When a node is deleted in a binary tree, the nodes below the deleted node may need to be rearranged to maintain the tree's structure. If the deleted node is a leaf node, there are no nodes below it. If the deleted node has one child, the child node takes the place of the deleted node. If the deleted node has two children, the successor/predecessor node takes the place of the deleted node, and the remaining nodes are adjusted accordingly to maintain the binary tree structure.
119 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

study material

,

past year papers

,

Previous Year Questions with Solutions

,

Extra Questions

,

Important questions

,

Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)

,

pdf

,

Viva Questions

,

Semester Notes

,

Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)

,

video lectures

,

mock tests for examination

,

shortcuts and tricks

,

Summary

,

MCQs

,

Sample Paper

,

Objective type Questions

,

Free

,

Exam

,

ppt

,

Insertion & Deletion in Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)

,

practice quizzes

;