Heap
Heap 操作
插入: 将新元素放到
heap[size+1]的位置每次比较它的它父亲元素,如果小于它的父亲,证明现在不满 足堆的性质,然后向上Sift Up删除: 将根节点和最后一个节点进行交换如果该节点大于其中一个儿子,那么将其与其较小的儿子进行交换做Sift Down,直到该节点的儿子均大于它的值,或者它的儿子为空
Heap
Interface 接口 • O(logN) Push -> Sift Up • O(logN) Pop -> Sift Down • O(1) Top • O(N) Delete
Heap Application
Priority Queue: Priority queues can be efficiently implemented using Binary Heap because it supports insert(), delete() and extractmax(), decreaseKey() operations in O(logn) time.
https://www.geeksforgeeks.org/binary-heap/
Heap Template
Priority Queue in Java
https://stackoverflow.com/questions/6065710/how-does-javas-priorityqueue-differ-from-a-min-heap
The default PriorityQueue is implemented with Min-Heap, that is the top element is the minimum one in the heap. In order to implement a max-heap, you can create your own Comparator:
import java.util.Comparator;
public class MyComparator implements Comparator<Integer>
{
public int compare( Integer x, Integer y )
{
return y - x;
}
}HashHeap
https://github.com/awangdev/LintCode/blob/master/Java/HashHeap.java
Heap Definition
(Binary) Heap is a special case of balanced binary tree data structure where the root-node key is compared with its children and arranged accordingly.
Min-Heap − Where the value of the root node is less than or equal to either of its children.
Max-Heap − Where the value of the root node is greater than or equal to either of its children.
https://www.tutorialspoint.com/data_structures_algorithms/heap_data_structure.htm
Heap Construction
Max Heap Construction Algorithm
Note − In Min Heap construction algorithm, we expect the value of the parent node to be less than that of the child node.
Max Heap Deletion Algorithm
Deletion in Max (or Min) Heap always happens at the root to remove the Maximum (or minimum) value.
Reference
[Heap Data Structures](https://www.tutorialspoint.com/data_structures_algorithms/heap_data_structure.htm\
[GeeksforGeeks](https://www.geeksforgeeks.org/binary-heap/\
[HackerRank Heaps](https://www.hackerrank.com/topics/heaps
PriorityQueue: https://algs4.cs.princeton.edu/24pq/
Last updated
Was this helpful?