Advertisement
Guest User

PriorityQueue

a guest
Aug 14th, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.74 KB | None | 0 0
  1. namespace PriorityQueueAdt
  2. {
  3.     using System;
  4.     using System.Collections;
  5.     using System.Collections.Generic;
  6.  
  7.     public class PriorityQueue<T> : IEnumerable<T>
  8.         where T : IComparable<T>
  9.     {
  10.         private const int DefaultCapacity = 16;
  11.  
  12.         private T[] elements;
  13.  
  14.         public PriorityQueue()
  15.         {
  16.             this.elements = new T[DefaultCapacity];
  17.         }
  18.  
  19.         public int Capacity
  20.         {
  21.             get
  22.             {
  23.                 return this.elements.Length;
  24.             }
  25.         }
  26.  
  27.         public int Count { get; private set; }
  28.  
  29.         public T Dequeue()
  30.         {
  31.             T value = this.Peek();
  32.  
  33.             this.Count--;
  34.  
  35.             if (this.Count > 0)
  36.             {
  37.                 this.BalanceThreeOnDequeue();
  38.             }
  39.  
  40.             return value;
  41.         }
  42.  
  43.         public void Enqueue(T item)
  44.         {
  45.             if (this.Count + 1 > this.elements.Length - 1)
  46.             {
  47.                 this.Resize();
  48.             }
  49.  
  50.             this.elements[this.Count + 1] = item;
  51.  
  52.             this.BalanceThreeOnEnqueue(this.Count + 1);
  53.  
  54.             this.Count++;
  55.         }
  56.  
  57.         public IEnumerator<T> GetEnumerator()
  58.         {
  59.             for (int i = 1; i <= this.Count; i++)
  60.             {
  61.                 yield return this.elements[i];
  62.             }
  63.         }
  64.  
  65.         public T Peek()
  66.         {
  67.             if (this.Count == 0)
  68.             {
  69.                 throw new InvalidOperationException("Queue empty.");
  70.             }
  71.  
  72.             return this.elements[1];
  73.         }
  74.  
  75.         IEnumerator IEnumerable.GetEnumerator()
  76.         {
  77.             return this.GetEnumerator();
  78.         }
  79.  
  80.         private void BalanceThreeOnDequeue()
  81.         {
  82.             int index = 1;
  83.  
  84.             this.elements[index] = this.elements[this.Count + 1];
  85.  
  86.             while (true)
  87.             {
  88.                 int left = index * 2;
  89.                 int right = left + 1;
  90.  
  91.                 if (left >= this.Count)
  92.                 {
  93.                     break;
  94.                 }
  95.  
  96.                 if (right >= this.Count)
  97.                 {
  98.                     this.Swap(index, left);
  99.                     break;
  100.                 }
  101.  
  102.                 T leftElement = this.elements[left];
  103.                 T rightElement = this.elements[right];
  104.  
  105.                 if (leftElement.CompareTo(rightElement) == 1)
  106.                 {
  107.                     index = this.Swap(index, left);
  108.                 }
  109.                 else if (leftElement.CompareTo(rightElement) == -1)
  110.                 {
  111.                     index = this.Swap(index, right);
  112.                 }
  113.                 else
  114.                 {
  115.                     break;
  116.                 }
  117.             }
  118.         }
  119.  
  120.         private void BalanceThreeOnEnqueue(int index)
  121.         {
  122.             while (index > 1)
  123.             {
  124.                 T element = this.elements[index];
  125.  
  126.                 int parentIndex = index / 2;
  127.                 T parentElement = this.elements[parentIndex];
  128.  
  129.                 if (element.CompareTo(parentElement) == 1)
  130.                 {
  131.                     index = this.Swap(index, parentIndex);
  132.                 }
  133.                 else
  134.                 {
  135.                     break;
  136.                 }
  137.             }
  138.         }
  139.  
  140.         private void Resize()
  141.         {
  142.             T[] newElements = new T[this.elements.Length * 2];
  143.             this.elements.CopyTo(newElements, 0);
  144.             this.elements = newElements;
  145.         }
  146.  
  147.         private int Swap(int index1, int index2)
  148.         {
  149.             T first = this.elements[index1];
  150.             this.elements[index1] = this.elements[index2];
  151.             this.elements[index2] = first;
  152.  
  153.             return index2;
  154.         }
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement