Advertisement
Guest User

Priority Queue in 100 lines

a guest
Aug 14th, 2014
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.26 KB | None | 0 0
  1.     using System;
  2.     using System.Collections;
  3.     using System.Collections.Generic;
  4.     public class PriorityQueue<T> : IEnumerable<T> where T : IComparable<T>
  5.     {
  6.         private const int DefaultCapacity = 16;
  7.         private T[] elements;
  8.         public PriorityQueue()
  9.         {
  10.             this.elements = new T[DefaultCapacity];
  11.         }
  12.         public int Capacity
  13.         {
  14.             get { return this.elements.Length; }
  15.         }
  16.         public int Count { get; private set; }
  17.         public T Dequeue()
  18.         {
  19.             T value = this.Peek();
  20.             this.Count--;
  21.             if (this.Count > 0)
  22.                 this.BalanceThreeOnDequeue();
  23.             return value;
  24.         }
  25.         public void Enqueue(T item)
  26.         {
  27.             if (this.Count + 1 > this.elements.Length - 1)
  28.                 this.Resize();
  29.             this.elements[this.Count + 1] = item;
  30.             this.BalanceThreeOnEnqueue(this.Count + 1);
  31.             this.Count++;
  32.         }
  33.         public IEnumerator<T> GetEnumerator()
  34.         {
  35.             for (int i = 1; i <= this.Count; i++)
  36.                 yield return this.elements[i];
  37.         }
  38.         public T Peek()
  39.         {
  40.             if (this.Count == 0)
  41.                 throw new InvalidOperationException("Queue empty.");
  42.             return this.elements[1];
  43.         }
  44.         IEnumerator IEnumerable.GetEnumerator()
  45.         {
  46.             return this.GetEnumerator();
  47.         }
  48.         private void BalanceThreeOnDequeue()
  49.         {
  50.             int index = 1;
  51.             this.elements[index] = this.elements[this.Count + 1];
  52.             while (true)
  53.             {
  54.                 int left = index * 2;
  55.                 int right = left + 1;
  56.                 if (left >= this.Count)
  57.                     break;
  58.  
  59.                 if (right >= this.Count)
  60.                 {
  61.                     this.Swap(index, left);
  62.                     break;
  63.                 }
  64.                 T leftElement = this.elements[left];
  65.                 T rightElement = this.elements[right];
  66.                 if (leftElement.CompareTo(rightElement) == 1)
  67.                     index = this.Swap(index, left);
  68.                 else if (leftElement.CompareTo(rightElement) == -1)
  69.                     index = this.Swap(index, right);
  70.                 else
  71.                     break;
  72.             }
  73.         }
  74.         private void BalanceThreeOnEnqueue(int index)
  75.         {
  76.             while (index > 1)
  77.             {
  78.                 T element = this.elements[index];
  79.                 int parentIndex = index / 2;
  80.                 T parentElement = this.elements[parentIndex];
  81.                 if (element.CompareTo(parentElement) == 1)
  82.                     index = this.Swap(index, parentIndex);
  83.                 else
  84.                     break;
  85.             }
  86.         }
  87.         private void Resize()
  88.         {
  89.             T[] newElements = new T[this.elements.Length * 2];
  90.             this.elements.CopyTo(newElements, 0);
  91.             this.elements = newElements;
  92.         }
  93.         private int Swap(int index1, int index2)
  94.         {
  95.             T first = this.elements[index1];
  96.             this.elements[index1] = this.elements[index2];
  97.             this.elements[index2] = first;
  98.             return index2;
  99.         }
  100.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement