Binary Heaps and Priority Queue
Binary heap overview
First of all, what does a binary heap have to do with a binary tree? Why do people always draw binary trees as binary heap?
Because a binary heap is a special kind of binary tree (complete binary tree) that is stored in an array.In a general linked list binary tree, we manipulate Pointers to nodes, whereas in an array, we use an array index as a pointer
Binary heap is also divided into maximum heap and minimum heap.The properties of the maximum heap are: each node is greater than or equal to its two children. Similarly, the properties of the minimum heap are:each node is less than or equal to its children.
Both heap cores have the same idea, and this article takes maximum heap as an example.
For a maximum heap, by its nature, it is obvious that the top of the heap, arr[1], must be the largest element of all.
In array represenatation of the binary tree, At any element k
Parent:
k / 2
Children :
2 * k
and2 * k + 1
Binary Heap Considerations
Immutability of keys.
Assumption: client does not change keys while they're on the PQ.
Best practice: use immutable keys.
Exceptions
Underflow: throw exception if deleting from empty PQ.
Overflow: add no-arg constructor and use resizing array.
Other operations
Remove an arbitrary item.
Change the priority of an item.
Priority queue overview
A useful feature of priority queues is that when you insert or delete elements, the elements are sorted automatically, and the underlying principle is binary heap operations.
The function of data structure is nothing more than adding and deleting,Priority queues have two main API, to insert an element and to remove the largest element(If the bottom uses the minimum heap,it will be delMin
).
Let's implement a simplified priority queue, starting with the code framework:
PS:For clarity, Java generics are used here,Key
can be any data type of comparable value,You can think of it as int, char, etc
Swim and Sink Implementation
For a maximum heap, there are two cases where the nature of the heap is destroyed:
If some node A is smaller than its children, then A doesn't deserve to be the parent node, so it should go down, and the larger node down here comes up as the parent node, and that's sink on A
If some node A is bigger than its parent, then A should not be the child node, but the parent node should be replaced and the parent node should be the parent node itself, which is the swim of A
The insert
method first adds the element to be inserted to the bottom of the heap and then floats it up to the correct position
delMax
first swap top element A with bottom element B, then delete A, and finally let B sink to the correct position
Sum up
A binary heap is a complete binary tree, so it is suitable for storing in an array, and the binary heap has some special properties.
Binary heap operation is very simple, mainly floating up and down, to maintain the nature of the heap (heap order), the core code is only 10 lines.
Priority queues are implemented based on binary heap, with the main operations being insert and delete. Insert is to insert to the end first and then float up to the correct position; Deletion is to reverse the position and then delete, and then sink to the correct position. The core code is only ten lines.
Perhaps this is the power of data structure, simple operation can achieve clever functions, really admire the invention of binary heap algorithm people!
Reference
Last updated