Learn GPT

Gallery

    Go lang

    Unit 1

    Basic Data Structures

    Arrays and Slices in Go
    Maps in Go
    Structs and Methods
    Pointers and Memory Management

    Unit 2

    Advanced Data Structures

    Linked Lists Implementation
    Binary Trees and Traversals
    Heaps and Priority Queues
    Concurrent Data Structures (Channels, Mutexes)
    Graphs and Algorithms
    ;

    Unit 2 • Chapter 3

    Heaps and Priority Queues

    Summary

    A heap is a complete binary tree that satisfies the heap property: in a max‑heap each node’s key is at least as large as the keys of its children, while in a min‑heap it is at most as large. Because the tree is complete, it can be stored in an array, where the parent of index i is floor((i‑1)/2) and the children are 2i+1 and 2i+2. Core heap operations include insert (add element at the end and bubble up), delete‑min/delete‑max (replace root with last element and bubble down), and peek (access root). Building a heap from n arbitrary elements can be done in O(n) time using a bottom‑up heapify pass, which is faster than inserting each element individually (O(n log n)). A priority queue is an abstract data type that supports insertion and removal of the highest‑ (or lowest‑) priority element; binary heaps provide an efficient implementation with O(log n) insertion and deletion and O(1) access to the extreme element. Variants such as d‑ary heaps, leftist trees, and Fibonacci heaps trade off operation costs for specific use‑cases, but the binary heap remains the most common due to its simplicity and cache‑friendly array layout.

    Concept Check

    In a binary max‑heap stored in an array, which relationship must hold for every internal node i?

    What is the worst‑case time complexity of building a heap from an unsorted array of n elements using the bottom‑up method?

    Which heap operation always runs in O(1) time regardless of heap size?

    When decreasing a key’s value in a min‑heap, which procedure restores the heap property?

    In a d‑ary heap (d > 2), how does increasing d affect the asymptotic cost of the delete‑min operation?

    PreviousBinary Trees and Traversals
    NextConcurrent Data Structures (Channels, Mutexes)