Java least heap implements priority queue and calculates the maximum n count
Priority queue
Priority queue is a very useful data structure. The process scheduling in the operating system has priority queue applications. If the minimum value represents the highest priority, the minimum heap is used, otherwise, use the maximum heap.
The top-N value is a problem:
The minimum heap can be used to calculate the maximum n number. The idea is to build n number into a minimum heap. If the remaining number is greater than the value of the heap top element, the heap top element is replaced, and then adjusted to the minimum heap until all numbers are traversed.
Paste the source code:
Key categories:
Package com. algorithms;/*** minimum heap, which is implemented using arrays to solve the top-N Problem * @ author zhshl * @ date2014-10-22 * @ param
*/Public class Min_Heap {private int heap []; private int maxSize; // a maximum of private int n can be accommodated; /// number of elements/*** @ param num heap size */public Min_Heap (int num) {n = 0; maxSize = num; heap = new int [maxSize];}/*** // *** the initial heap is a Complete Binary Tree in any order, from (n-2) at/2, start downward adjustment * @ param heap * @ param n * // * public void createHeap (int [] heap2, int n) {heap = heap2; for (int I = (n-2)/2; I> = 0; I --) {// adjust adjustDown (I, n-1) ;}} * // *** element into heap * @ param v * /Public void append (int v) {if (isFull () {System. out. println (heap is full, can't append elements !); Return;} // Insert the element into the end of heap [n] = v; n ++; // adjust adjustUp (n-1 );} /*** retrieve the heap top element * @ return */public int serve () {if (isEmpty () {System. out. println (heap is empty !); Return Integer. MIN_VALUE;} int temp = heap [0]; // replace heap [0] = heap [n-1] with the last element; n --; // adjust adjustDown (0, n-1); return temp ;} /*** calculate the maximum n pieces of data * @ param n * @ return null if n is bigger than the heap size, otherwise */public int [] getTopN (int [] data, int n) {heap = new int [n]; maxSize = n; this. n = 0; // construct the initial heap for (int I = 0; I
Heap [0]) {heap [0] = data [I]; adjustDown (0, n-1) ;}} return heap ;}/ *** adjusted the element I downwards, used to adjust * @ param I * @ param j */private void adjustDown (int I, int j) When deleting an element) {// TODO Auto-generated method stubint child = 2 * I + 1; int temp = heap [I]; // record the value of the node to be adjusted while (child
Heap [child + 1]) {// if the left child is larger than the right child, it points to the right child ++;} if (heap [child]> temp) {// if the child is larger than the child, exit break;} heap [(child-1)/2] = heap [child]; child = child * 2 + 1;} // The cycle ends. child points to the left child heap of the final node [(child-1)/2] = temp ;} /*** adjust the element at I to heap upwards for * @ param I */private void adjustUp (int I) {int temp = heap [I] during insertion. while (I> 0 & heap [(I-1)/2]> temp) {// when the value of the parent node is greater than temp, swap value heap [I] = heap [(I-1)/2]; I = (I-1)/2;} heap [I] = temp ;} /*** indicates whether the heap is full * @ return */public boolean isFull () {return n> = maxSize ;} /*** empty heap? * @ return */public boolean isEmpty () {return 0 = n ;}}
Test class:
Package com. algorithm. test; import com. algorithms. min_Heap; /*** minimum heap test class * @ author zhshl * @ date2014-10-22 **/public class Min_Heap_Test {/*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stubMin_Heap heap = new Min_Heap (10); heap. append (2); heap. append (2); heap. append (13); heap. append (21); heap. append (2); heap. append (2); heap. append (53); heap. append (6); int temp; while (! Heap. isEmpty () {temp = heap. serve (); System. out. println (temp);} System. out. println (top-N problem); int data [] = {7643,234,123,}; data = heap. getTopN (data, 5); for (int I: data) {System. out. println (I );}}}