A simple way to implement priority queues

Source: Internet
Author: User
Tags comparable int size stdin

In many cases, we collect elements, handle the most important elements of the current key value, then collect more elements, and then process the elements that are the largest of the current key values, and so forth. In this case, a suitable data structure should support two actions: deleting the maximum element and inserting the element, which is called the priority queue.

A simple implementation method based on heap priority queue:

public class Maxpq<key extends comparable<key>> {private key[] PQ;
    private int n=0;
    Public maxpq (int maxn) {pq= (key[]) new comparable[maxn+1];
    public Boolean IsEmpty () {return n==0;
    public int size () {return N;
        public void Insert (Key v) {pq[++n]=v;
    Swim (N);
        Public key Delmax () {key max=pq[1];
        Exch (1,n--);
        Pq[n+1]=null;
        Sink (1);
        Stdout.println ("deleted" +max);
    return Max;
    Private Boolean less (int i,int j) {return Pq[i].compareto (pq[j)) <0;
        } private void Exch (int i,int j) {Key t=pq[i];
        PQ[I]=PQ[J];
    pq[j]=t;
            private void sink (int k) {while (2*k<=n) {int j=2*k;
            if (j<n&&less (j,j+1)) J + +;
            if (!less (K,J)) break;
            Exch (K,j);
        K=j; }} private void swim (int k) {while K>1&& less (k/2,k)) {exch (k/2,k);
        K=K/2;
        } public static void Main (string[] args) {int m=5;
        Maxpq<transaction> pq=new maxpq<> (m+1);

            while (Stdin.hasnextline ()) {Pq.insert (New Transaction (Stdin.readline ()));
            if (Pq.size () >m) {Pq.delmax ();
        } stack<transaction> stack=new stack<transaction> ();
        while (!pq.isempty ()) Stack.push (Pq.delmax ());
    for (Transaction t:stack) stdout.println (t); }
}

How the index precedence queue is implemented:

public class Indexminpq<key extends comparable<key>> {private int N;
    Private int[] PQ;
    Private int[] QP;

    Private key[] keys;
        Public indexminpq (int maxn) {N = 0;
        PQ = new INT[MAXN + 1];
        QP = new INT[MAXN + 1];
        Keys = (key[]) new COMPARABLE[MAXN + 1];
    for (int i = 0; I <= maxn i++) Qp[i] =-1; 
        public void Insert (int k, key key) {if (contains (k)) throw new RuntimeException (' item is already in PQ ');
        n++;
        Qp[k] = N;
        Pq[n] = k;
        KEYS[K] = key;
    Swim (N);
        The public void change (int k, key key) {if (!contains (k)) throw new RuntimeException ("Item isn't in PQ");
        KEYS[K] = key;
        Swim (qp[k]);
    Sink (Qp[k]);
    public Boolean contains (int k) {return qp[k]!=-1;
        The public void Delete (int i) {if (I < 0 | | I >= N) throw new Indexoutofboundsexception (); if (!contains (i)) throw newRuntimeException ("Index isn't in the priority queue");
        int index = qp[i];
        Exch (Index, n--);
        Swim (index);
        Sink (index);
        Keys[i] = null;
    Qp[i] =-1;
        public int Delmin () {if (N = 0) throw new RuntimeException ("Priority queue underflow");
        int min = pq[1];
        Exch (1, n--);
        Sink (1);
        assert min = = pq[n + 1];
        Qp[min] =-1;
        Keys[min] = null;
        Pq[n + 1] =-1;

    return min;
            public void swim (int k) {while (K > 1 && less (K/2, k)) {Exch (K/2, k);
        K = K/2;
            } private void sink (int k) {while (2 * k <= N) {int J = 2 * k;
            if (J < N && Less (J, j + 1)) J + +;
            if (!less (k, J)) break;
            Exch (k, J);
        K = J;
    } public boolean less (int i, int j) {return Keys[pq[i]].compareto (Keys[pq[j]]) > 0;

}    public void Exch (int i, int j) {int swap = Pq[i];
        Pq[i] = Pq[j];
        PQ[J] = swap;
        Qp[pq[i]] = i;

    QP[PQ[J]] = j;
    public Boolean IsEmpty () {return N = 0;
    public int size () {return N; public static void Main (string[] args) {//Insert a bunch of strings string[] strings = {"It", "W

        As, "the", "best", "of", "Times", "it", "is", "the", "worst"};
        Indexminpq<string> PQ = new indexminpq<string> (strings.length);
        for (int i = 0; i < strings.length i++) {Pq.insert (I, strings[i]);
            }//delete and print each key while (!pq.isempty ()) {int i = pq.delmin ();
        STDOUT.PRINTLN (i + "" + strings[i]);

        } stdout.println ();
        Reinsert the same strings for (int i = 0; i < strings.length; i++) {Pq.insert (I, strings[i]); while (!pq.isempty () {pq.delmin ()); }
    }
}






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.