Algorithm series (10) Heap implementation Priority queue

Source: Internet
Author: User

The AVL tree is introduced in the algorithm series (nine) balanced binary lookup tree AVL tree. This article mainly explains the priority queue.

Typically, a two-fork heap is used to implement a priority queue. For the time being, this realization.

definition and nature of the heap

The heap is actually a completely binary tree, with any one of its non-leaf nodes satisfying the nature:

Key[i]<=key[2i+1]&&key[i]<=key[2i+2] or key[i]>=key[2i+1]&&key>=key[2i+2]

That is, any non-leaf node keyword is not greater than or less than the key of the child node.

Heap divided into large top heaps and small top heaps, satisfying key[i]>=key[2i+1]&&key>=key[2i+2] called large top heaps,

Meet Key[i]<=key[2i+1]&&key[i]<=key[2i+2] called the small top heap.

By the above-mentioned properties, it is known that the key word of the heap top of the big Top heap is the largest of all the keywords, and the top of the heap of the small top heap is the smallest keyword in all the keywords.

Storage of Heaps

The heap is usually represented by an array, and the parent node of the I node is labeled (i–1)/2. The index of the left and right sub-nodes is 2 * i + 1 and 2 * i + 2 respectively. such as the No. 0 node of the left and right sub-nodes subscript 1 and 2 respectively.


Basic operation of the heap--insert Delete


The inserted strategy is called filtering, and the removal strategy is called filtering.

Heap SortThe first thing you can see is that the No. 0 data in the heap after the heap is built is the smallest data in the heap. Remove this data and perform the next heap delete operation. So the No. 0 data in the heap is the smallest data in the heap, repeating the above steps until there is only one data in the heap to take out the data directly.

Code Implementation

Inserts an element into the heap public static void Insert (List heap, int value) {Heap.add (value);//Start Ascent Operation HeapUp2 (Heap, Heap.size ()-1);//Heap Up (heap, heap.size ()-1);}
Non-recursive implementation public static void HeapUp2 (List heap, int index) {int parent = 0;for (; index > 1; index/= 2) {//Gets the parent node of index Subscript parent = index/2;//Gets the value of the parent node int parentvalue = (integer) heap.get (parent);//Gets the value of the index position int indexvalue = (integer) heap. Get (index),//if greater than on Exchange if (Parentvalue > Indexvalue) {swap (heap, parent, index);}}}
/** * Delete the smallest value in the heap , that is, the deletion position is a value of 1, that is, the root node value operation principle: When the value of the root node is deleted, the original position will appear a hole * fill this hole, the value of the last leaf is assigned to the hole, and finally remove the leaf * * @param heap */public Static V The OID deletemin (List heap) {//assigns the value of the last leaf to a position heap.set (1, Heap.get (Heap.size ()-1));//Lower filter operation HeapDown2 (heap, 1);// Heapdown (heap, 1);//Remove the number from the last position Heap.remove (Heap.size ()-1);} 
Non-recursive implementation public static void HeapDown2 (List heap, int index) {int child = 0;//stores the position of the left son int temp = (Integer) heap.get (index); int n = heap.size ()-2;//If there is a son for (; 2 * index <= n; index = child) {//Gets the position of the left son = 2 * index;//If only the left son if (chi LD = = N) {child = 2 * INDEX;}//If the right son is smaller than the left son's value, else if ((integer) heap.get (Child) > (integer) heap.get (children + 1)) {Chil d++;} If the number of the smallest son is smaller than the value of temp if ((Integer) heap.get (child) < temp) {//swap the child in the heap, and the value of the index position swap (heap, child, index);} else {BR Eak;}}}


Algorithm implementation code GitHub address is Https://github.com/robertjc/simplealgorithm

Follow-up will continue to add, some places may have problems in writing, please advise.


Welcome to scan QR Code, follow my public account



Algorithm series (10) Heap implementation Priority queue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.