Skip to content

Commit

Permalink
Added new methods to the MinPriorityQueue<T> class.
Browse files Browse the repository at this point in the history
  • Loading branch information
aalhour committed Jul 30, 2015
1 parent 05a38c2 commit bdba3fd
Showing 1 changed file with 49 additions and 6 deletions.
55 changes: 49 additions & 6 deletions DataStructures/Heaps/MinPriorityQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,26 @@ public TKey PeekAtMinPriority()
throw new ArgumentOutOfRangeException("Queue is empty.");
}

return _heap.Peek().Value;
return _heap.Peek().Key;
}

/// <summary>
/// Checks for the existence of a key in the queue
/// </summary>
public bool Contains(TKey key)
{
var status = false;

// O(N) operation
for (int i = 0; i < _heap.Count; ++i) {
if (_heap[0].Key.IsEqualTo(key)) {
status = true;
break;
}
}

return status;
}

/// <summary>
/// Enqueue the specified key, with the default-max-priority value.
Expand Down Expand Up @@ -196,7 +213,7 @@ public TKey DequeueMin()
if (_heap.IsEmpty)
throw new ArgumentOutOfRangeException("Queue is empty.");

return _heap.ExtractMin().Value;
return _heap.ExtractMin().Key;
}

/// <summary>
Expand All @@ -211,7 +228,7 @@ public void UpdatePriority(TKey key, TPriority newPriority)
int index = -1;
for (int i = 0; i < _heap.Count; ++i)
{
if (_heap[i].Value.IsEqualTo(key))
if (_heap[i].Key.IsEqualTo(key))
{
index = i;
break;
Expand All @@ -222,7 +239,33 @@ public void UpdatePriority(TKey key, TPriority newPriority)
throw new KeyNotFoundException();

_heap[index].Priority = newPriority;
//_heap.RebuildHeap();
}

/// <summary>
/// Adds a new key-priority pair if the key doesn't exist already. Otherwise, updates it with the new priority.
/// </summary>
public void AddOrUpdateWithPriority(TKey key, TPriority priority)
{
int index = -1;
for (int i = 0; i < _heap.Count; ++i)
{
if (_heap[i].Key.IsEqualTo(key))
{
index = i;
break;
}
}

// Add the new key-priority if index is not a valid one (item was not found).
// Otherwise update it.
if (_heap.IsEmpty || index == -1) {
var newNode = new PriorityQueueNode<TKey, TPriority>(key, priority);
_heap.Add(newNode);
}
else if(index > -1)
{
_heap[index].Priority = priority;
}
}

/// <summary>
Expand Down Expand Up @@ -265,14 +308,14 @@ public class PriorityQueueNode<TKey, TPriority> : IComparable<PriorityQueueNode<
where TKey : IComparable<TKey>
where TPriority : IComparable<TPriority>
{
public TKey Value { get; set; }
public TKey Key { get; set; }
public TPriority Priority { get; set; }

public PriorityQueueNode() : this(default(TKey), default(TPriority)) { }

public PriorityQueueNode(TKey value, TPriority priority)
{
this.Value = value;
this.Key = value;
this.Priority = priority;
}

Expand Down

0 comments on commit bdba3fd

Please sign in to comment.