ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP3138Heap Insertion Deletio…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP3138

    Design and Analysis of Algorithms

    Progress0 / 53 done
    Topics53
    1Introduction to Algorithm Design2Data Structures3Efficiency in Algorithms4Analysis of Algorithms5Mathematical Review6Mathematical Analysis of Algorithms7Types of Functions8Order of Growth9Asymptotic Notations10Sorting Algorithms11Selection Sort Algorithm12Example and Analysis of Selection Sort13Insertion Sort Algorithm14Divide and Conquer Algorithms15Merge Sort Algorithm16Quick Sort Algorithm17Bucket Sort Algorithm18Radix Sort Algorithm19Counting Sort Algorithm20Heap Sort Basics21Heap Algorithms22Heap Properties and Examples23Heap Operations24Heap Sort Algorithm Analysis25Heap Insertion and Deletion26Tree Based Algorithms and Hashing27Red Black Tree Basics28Binary Search Tree Basics29Tree Searching Algorithms30Analysis of Tree Searching Algorithms31Analysis of Insertion and Deletion in BST32Hashing Basics33Examples of Hash Functions34Analysis of Collision Resolution Techniques35Dynamic Programming360-1 Knapsack Problem37Fractional Knapsack Problem38Longest Common Subsequence39Shortest Path Finding40Matrix Chain Multiplication41Assembly Line Chain Problem42Greedy Algorithms43Prim's Algorithm44Kruskal's Algorithm45Dijkstra's Algorithm46Huffman Coding47NP-Completeness48Polynomial Time Verification49Reducibility50NP-Completeness Proofs51Randomized Algorithms52Particle Swarm Optimization53Genetic Algorithms
    6 min read
    Topic 25 of 53
    Topic Study Note Illustration
    25

    Heap Insertion and Deletion

    In a heap, the Insertion and Deletion operations are crucial because they ensure the heap property is maintained after adding or removing elements. These operations are fundamental...

    Heap Insertion and Deletion

    In a heap, the Insertion and Deletion operations are crucial because they ensure the heap property is maintained after adding or removing elements. These operations are fundamental in algorithms like Heap Sort, Priority Queues, and Dijkstra's Shortest Path. Let's go through both operations in detail.


    1. Heap Insertion (Insert a New Element)

    When you insert a new element into a heap, you must ensure that the heap property (whether max-heap or min-heap) is maintained after the insertion. The steps for inserting a new element are as follows:

    Steps for Insertion:

    1. Add the Element to the End:

      • Insert the new element at the end of the heap (the next available position in the complete binary tree). This ensures that the heap remains a complete binary tree.
    2. Bubble Up (or Sift Up):

      • After adding the element at the end, bubble it up (or sift it up) to restore the heap property.
      • Start by comparing the newly inserted element with its parent. If the heap property is violated (i.e., the new element is greater than its parent in a max-heap or smaller than its parent in a min-heap), swap the element with its parent.
      • Continue this process until the element is in the correct position or you reach the root.

    Time Complexity:

    • O(log n), because, in the worst case, the element needs to bubble up from the leaf to the root, and the height of the heap is log n.

    Example of Heap Insertion (Max-Heap):

    Suppose we have the following max-heap and want to insert 12:

            20
           /  \
         15    10
        /  \   /
       8   6  5
    
    1. Insert 12 at the end of the heap:

           20
          /  \
        15    10
       /  \   / \
      8   6  5  12
      
    2. Bubble up 12:

      • Compare 12 with its parent 10. Since 12 > 10, swap them:
           20
          /  \
        15    12
       /  \   / \
      8   6  5  10
      
    3. Bubble up 12 again:

      • Compare 12 with its new parent 15. Since 12 < 15, stop. No more swaps are needed.

    The final max-heap after insertion:

            20
           /  \
         15    12
        /  \   / \
       8   6  5  10
    

    2. Heap Deletion (Delete the Root)

    Heap deletion typically refers to the operation where you remove the root element from the heap (either the maximum element in a max-heap or the minimum element in a min-heap) and then restore the heap property. In a max-heap, the root is the maximum element, while in a min-heap, it is the minimum element.

    Steps for Deletion:

    1. Swap the Root with the Last Element:

      • The root of the heap is the element that you want to delete. To remove it, swap the root element with the last element in the heap (the rightmost leaf).
      • This swap places the element to be deleted at the end of the heap.
    2. Remove the Last Element:

      • After swapping, remove the last element (which was originally the root).
    3. Heapify the Root:

      • Now that the root has been replaced with the last element, the heap property is likely violated. You need to restore the heap property by heapifying the root.
      • Start by comparing the new root with its children. If the heap property is violated (i.e., the root is smaller than its largest child in a max-heap or larger than its smallest child in a min-heap), swap the root with the larger (or smaller) child.
      • Continue heapifying down the tree until the heap property is restored.

    Time Complexity:

    • O(log n), because after the swap, we need to heapify the tree starting from the root. Heapifying takes O(log n) time as it involves moving down the tree from the root to the leaf, and the height of the tree is log n.

    Example of Heap Deletion (Max-Heap):

    Consider the following max-heap:

            20
           /  \
         15    10
        /  \   /
       8   6  5
    
    1. Swap the root with the last element:

      • Swap 20 with 5:
           5
          /  \
        15    10
       /  \   /
      8   6  20
      
    2. Remove the last element (which is now 20):

           5
          /  \
        15    10
       /  \   
      8   6
      
    3. Heapify the root (5):

      • Compare 5 with its children (15 and 10). Since 15 is the largest child, swap 5 and 15:
           15
          /  \
        5     10
       /  \   
      8    6
      
      • Heapify the subtree rooted at index 1 (node 5):
        • Compare 5 with its children (8 and 6). Since 8 is the largest child, swap 5 and 8:
           15
          /  \
        8     10
        

      / \
      5 6

    The final max-heap after deletion:

            15
           /  \
         8     10
        /  \   
       5    6
    

    Summary of Heap Insertion and Deletion

    • Insertion:

      • Steps: Add the element at the end → Bubble up (sift up) to restore the heap property.
      • Time Complexity: O(log n), because the element may need to bubble up to the root.
    • Deletion:

      • Steps: Swap the root with the last element → Remove the last element → Heapify the root to restore the heap property.
      • Time Complexity: O(log n), because the heapify operation works by traversing the height of the tree.

    Applications of Heap Insertion and Deletion:

    • Priority Queues: Insertion and deletion operations are fundamental in implementing priority queues, where the root element is always the element with the highest (or lowest) priority.
    • Heap Sort: In heap sort, both insertion and deletion operations are used to extract the maximum or minimum elements from the heap and build the sorted array.
    • Dijkstra’s Shortest Path: Priority queues (using heaps) are used to select the next node with the smallest tentative distance.

    Conclusion:

    Heap insertion and deletion are both logarithmic-time operations that maintain the heap property, ensuring that the heap is always a complete binary tree and that the root element is always the largest (in a max-heap) or smallest (in a min-heap). These operations are essential for applications like heap sort, priority queues, and other graph algorithms.

    ← Previous topic 24
    Heap Sort Algorithm Analysis
    Next topic 26 →
    Tree Based Algorithms and Hashing

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    6mremaining
    1.0kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Design and Analysis of Algorithms