應用

在time sharing的作業系統中, 不希望優先權高(執行時間最短的)的process等候過久,
策略使用SJF(shortest-job-first),
利用max-heap每次取出最大優先的process處理。
min-heap c語言寫法只差在compared condition 不同而已。
search time complexity:
\( O(n\log n) \)
C++ code

其他人寫的

Kadai - heap
Chiu CC - heap
Heap Sort | GeeksforGeeks
Youtube - Heap Sort | GeeksforGeeks

Max-Heap

為一完備二元樹(commpele binary tree), 任一節點的資料內容不於子節點的資料內容。

Min-Heap

為一完備二元樹(commpele binary tree), 任一節點的資料內容不於子節點的資料內容。

陣列表示binary tree

Index 0不使用


Insert node into heap

Max-heap
code(c)


Delete max value from max-heap