1. 程式人生 > >Data Structure - Heap

Data Structure - Heap

Min Heap or Max Heap

這篇文章以Min Heap為例

特徵:parent<children, 所以root永遠是最小的元素

Insertion:goes to the next empty spot and then bubble it up

Remove the min: 1. remove the min
                              2.replace with last node and then bubble down

Array Implementation
        Parent index = (index-1)/2;
        left child index = index*2+1;
        right child index = index*2+2;

 

Priority Queue C++

empty();
size();
top();
push();
pop();
emplace();
swap();

 

Reference:

https://www.youtube.com/watch?v=t0Cq6tVNRBA

https://www.fluentcpp.com/2018/03/20/heaps-and-priority-queues-in-c-part-3-queues-and-priority-queues/