Post

Heap

✅ Priority Queue

each data has priority. the data with priority will dequeue first. elements are retrieved based on their priority

✅ Heap

made for priority queue
complete binary tree data structure

Screenshot 2024-07-24 at 11 00 46

  • for every node, value of its children is less/greater than its own value

💡 How to define

  • array
1
2
3
4
5
6
7
8
public class Main {
    public static void main(String[] args) {
        PriorityQueue<Integer> minHeap = new PriorityQueue<>();

        minHeap.add(10);
        minHeap.add(20);
        minHeap.add(15);
    }

💡 appllications

  • priority queue
  • heap sort: sort an array in ascending, descending order
  • Dijkstra’s Algorithm, Prim’s Algorithm

🆚 binary search tree

  • heap allows nodes to be repeated
  • binary search tree does not allow repetition of nodes

💡 Reference

Dijkstra Algorithm: https://soheeparklee.github.io/posts/Algorithm-15Dijkastra/

This post is licensed under CC BY 4.0 by the author.