Implementing priority queues in C #

Source: Internet
Author: User

The priority queue (priority queue) is a very important data structure. I used to use her when I was doing the ACM problem. C + + STL includes priority_queue. Java also has priorityqueue classes. Unfortunately, priority queues are not included in the. NET Framework Base Class Library. So I had to write one myself in the C # language, as follows:

Using System;
Using System.Collections.Generic;

Namespace Skyiv.util
{
Class priorityqueue<t>
{
Icomparer<t> comparer;
T[] Heap;

public int Count {get; private set;}

Public Priorityqueue (): this (null) {}
public priorityqueue (int capacity): This (capacity, null) {}
Public Priorityqueue (icomparer<t> comparer): this (comparer) {}

public priorityqueue (int capacity, icomparer<t> comparer)
{
This.comparer = (comparer = null)? Comparer<t>. Default:comparer;
This.heap = new T[capacity];
}

public void Push (T v)
{
if (Count >= heap. Length) array.resize (ref heap, Count * 2);
Heap[count] = v;
Siftup (count++);
}

Public T Pop ()
{
var v = top ();
Heap[0] = Heap[--count];
if (Count > 0) siftdown (0);
return v;
}

Public T Top ()
{
if (Count > 0) return heap[0];
throw new InvalidOperationException ("Priority queue is empty");
}

void Siftup (int n)
{
var v = heap[n];
for (var n2 = n/2 n > 0 && comparer.compare (V, heap[n2)) > 0; n = n2, N2/= 2) heap[n] = heap[n2];
Heap[n] = v;
}

void Siftdown (int n)
{
var v = heap[n];
for (var n2 = n * 2; n2 < Count; n = n2, N2 *= 2)
{
if (n2 + 1 < Count && Comparer.compare (heap[n2 + 1], heap[n2)) > 0 n2++;
if (Comparer.compare (V, heap[n2]) >= 0) break;
Heap[n] = heap[n2];
}
Heap[n] = v;
}
}
}

As shown above, this priorityqueue<t>

The generic class provides four public constructors, the first is an parameterless constructor, and the remaining constructors allow you to specify the number of initial elements (capacity) included in the precedence queue, and how to compare the keys (comparer).

This program uses a heap (heap) to implement priority queues. Therefore, the required space is minimal. The time complexity of the Count property and the top method is O (1), and the time complexity of the Push and Pop methods is O (Logn).

I used to implement a priority queue with the List<t> generic class, see my Blog Timus1016. A Cube on the Walk. Although simpler, the program code has only 23 lines, but the efficiency is not high, and its Push and Pop methods have a time complexity of O (N).

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.