Binary Heap
Presentation At Code Level
Tree Nodewith pointers to parenttype HeapNode struct { Val int Left *HeapNode Right *HeapNode Parent *HeapNode }ArrayTo use
Arrayto present a binary tree, the binary tree MUST be aComplete Tree, that's why we say thatBinary Heapis actually aComplete Tree. Since we usually use theArrayto presentBinary HeapWhy?
- Additional space consuming for the
pointers - The time complexity. For example, how to the lowest-rightest
nodein the heap? WithHeapNode, the complexity is O(lgN), while withArray, we can get the data with array index, with O(1) complexity. (ThePushandPopAPI would be affected)
- Additional space consuming for the
Application Scenarios
Priority QueueKey Points of
binary-heapbased priority queuebinary-heapactually is aComplete Tree. Need to keep the property ofComplete Treeduring the opeartionsWhen
Push, append the value to theendof the array (to match the requirement ofComplete Tree), and doswimoperation.When
Pop, return theheadvalue of the array, and move theendof the array to theheadlocation, and dosinkoperation
Follow the property of
max heapormin heap
Heap SortWork 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 QueueV.S.Heap SortHeap Sortonly usesinkoperation. Because there is actually ONLYPopoperation (When the heap top element found, move it to the heap end).Priority Queueuse bothsinkandswimoperation. Because there isPopandPushoperation. WhenPushan element into the queue, it it appended to the heap end, andswimto the correct location.Why not insert into the heap top, and reuse the
sinkoperation?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 和代码实现相较于二叉搜索树更简单,所以一般能用优先级队列解决的问题,我们没必要用二叉搜索树。