Binary Heap
Presentation At Code Level
Tree Node
with pointers to parenttype HeapNode struct { Val int Left *HeapNode Right *HeapNode Parent *HeapNode }
Array
To use
Array
to present a binary tree, the binary tree MUST be aComplete Tree
, that's why we say thatBinary Heap
is actually aComplete Tree
. Since we usually use theArray
to presentBinary Heap
Why?
- Additional space consuming for the
pointers
- The time complexity. For example, how to the lowest-rightest
node
in the heap? WithHeapNode
, the complexity is O(lgN), while withArray
, we can get the data with array index, with O(1) complexity. (ThePush
andPop
API would be affected)
- Additional space consuming for the
Application Scenarios
Priority Queue
Key Points of
binary-heap
based priority queuebinary-heap
actually is aComplete Tree
. Need to keep the property ofComplete Tree
during the opeartionsWhen
Push
, append the value to theend
of the array (to match the requirement ofComplete Tree
), and doswim
operation.When
Pop
, return thehead
value of the array, and move theend
of the array to thehead
location, and dosink
operation
Follow the property of
max heap
ormin heap
Heap Sort
Work flow of
heap sorting
- Heapify a heap.
- Put the heap top to the heap end and
Restore the heap with array[:len(array)-2]
Priority Queue
V.S.Heap Sort
Heap Sort
only usesink
operation. Because there is actually ONLYPop
operation (When the heap top element found, move it to the heap end).Priority Queue
use bothsink
andswim
operation. Because there isPop
andPush
operation. WhenPush
an element into the queue, it it appended to the heap end, andswim
to the correct location.Why not insert into the heap top, and reuse the
sink
operation?Because this would cause element migration. Say that, the new one is the smallest one, then, for the
min heap
, need to move ALL the elements to the next location, with time complexity O(N)
About Dynamic Sorting
二叉堆就是一种能够动态排序的数据结构。所谓动态排序,就是说我们可以不断往数据结构里面添加或删除元素,数据结构会自动调整元素的位置,使得我们可以有序地从数据结构中读取元素,这是一般的排序算法做不到的。
能动态排序的常用数据结构其实只有两个,一个是优先级队列(底层用二叉堆实现),另一个是二叉搜索树。二叉搜索树的用途更广泛,优先级队列能做的事情,二叉搜索树其实都能做。但优先级队列的 API 和代码实现相较于二叉搜索树更简单,所以一般能用优先级队列解决的问题,我们没必要用二叉搜索树。