Java minimum heap implementation of priority queue and maximum n number problem

Source: Internet
Author: User

Heap in the implementation of the priority queue and the minimum number of N to find the problem, has a great advantage!

Definitions for maximum heap and minimum heap are not mentioned here, lesson reference online article: http://blog.csdn.net/genios/article/details/8157031

This paper is mainly for the minimum heap implementation and application, only for novice reference.


Priority queue

The priority queue is a very useful data structure, the operating system's process scheduling has priority queue application, if the minimum value to indicate the highest priority, the minimum heap is used, otherwise the maximum heap is used.


the Top-n value is the problem:

For the maximum number of n, can be implemented with the smallest heap, the idea is: the number of n is constructed into a minimum heap, if the remaining number is greater than the value of the top of the heap, then replace the top element of the heap, and then adjust to the minimum heap, until all the numbers are traversed.


Paste the source code:

Key classes:

Package com.algorithms;/** * Minimum heap, with array implementation, solve top-n problem * @author "ZHSHL" * @date2014 -10-22 * @param <T> */public class Min _heap {private int heap[];p rivate int maxSize;////can hold a maximum number of private int n;///elements/** * @param num Heap size */public min_heap (in T num) {n=0;maxsize=num;heap=new int[maxsize];} /**//** * The initial heap is a complete binary tree in any order, starting at (n-2)/2 Down * @param heap * @param n *//*public void Createheap (int[] heap2,int N) {Heap=hea p2;for (int i= (n-2)/2;i>=0;i--) {////from (n-2)/2 at the beginning of the downward adjustment 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;} The element is inserted at the end of the heap[n]=v;n++;///adjustment adjustup (n-1);} /** * Remove heap top element * @return */public int serve () {if (IsEmpty ()) {System.out.println ("heap is empty!"); return integer.min_value;} int temp=heap[0];////replaces the first element with the last element heap[0]=heap[n-1];n--;//downward adjusts adjustdown (0, n-1); return temp;} /** * for maximum n data * @param 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;///constructs the initial heap for (int i=0;i<n;i++) {append (Data[i]);} for (int i=n;i<data.length;i++) {////If larger than the top element of the heap, replace the top element of the heap and adjust if (data[i]>heap[0]) {heap[0]=data[i];adjustdown (0, n-1);}} return heap;} /** * Adjustment to element I, adjusting when deleting elements * @param i * @param j */private void Adjustdown (int i, int j) {//TODO auto-generated Method St Ubint Child=2*i+1;int temp=heap[i];///Record the value of the node to be adjusted while (CHILD&LT;J) {////traverse adjustment within the range if (heap[child]> heap[child+1]) {///If the left child is larger than the right child, point to the smaller right child child++;} if (heap[child]>temp) {///If the smaller children are larger than themselves, exit the break;} heap[(child-1)/2]=heap[child];child=child*2+1;} The end of the loop, child heap[(child-1)/2]=temp to the left of the node that points to the final position;} /** * The element at I is adjusted upward to the heap for insertion time * @param i */private void adjustup (int i) {int temp=heap[i];while (i>0&&heap[(i-1)/2] > Temp) {///When the parent node is larger than temp, the interchange value heap[i]=heap[(i-1)/2];i= (i-1)/2;} Heap[i]=temp;} /** * Heap is full * @return */public boolean isfull () {return n>=maxsize;} /** * is 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 clas s Min_heap_test {/** * @param args */public static void main (string[] args) {//TODO auto-generated method Stubmin_heap He Ap=new Min_heap, Heap.append (2); Heap.append (2); Heap.append (+); Heap.append (+); Heap.append (2); Heap.append (2) ; Heap.append (+); Heap.append (6); int Temp;while (!heap.isempty ()) {Temp=heap.serve (); SYSTEM.OUT.PRINTLN (temp);} System.out.println ("\ n Ask top-n question:"), int data[]={4,51,52,12,123,52,7643,234,123,33,44,2};d ata=heap.gettopn (data, 5); for (int i:data) {System.out.println (i);}}}








Java minimum heap implementation of priority queue and maximum n number problem

Related Article

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.