summaryrefslogtreecommitdiffstats
path: root/PriorityQueue.cs
diff options
context:
space:
mode:
authorToby Vincent <tobyv13@gmail.com>2021-03-29 22:06:57 +0000
committerToby Vincent <tobyv13@gmail.com>2021-03-29 22:06:57 +0000
commit8a056874d97a210f1bd48e8045d548061683ab7f (patch)
tree75cb37d5325fbbe46ea444db48e2f7c93125728d /PriorityQueue.cs
parentb0981d54a1a599d11b2c78a5a734071e7cbf8abe (diff)
refactored and clean up some functions
Diffstat (limited to 'PriorityQueue.cs')
-rwxr-xr-xPriorityQueue.cs13
1 files changed, 8 insertions, 5 deletions
diff --git a/PriorityQueue.cs b/PriorityQueue.cs
index 72663be..a6538c0 100755
--- a/PriorityQueue.cs
+++ b/PriorityQueue.cs
@@ -35,6 +35,7 @@ namespace PriorityQueue
Location[Array[second].Id] = second;
}
+ // insert a new node and bubble up
public void Insert(T node)
{
int index = ++HeapSize;
@@ -46,9 +47,11 @@ namespace PriorityQueue
ChangeKey(index, key);
}
+ // return most significant
public T Peek() =>
Array[0];
+ // check if heap is empty
public bool IsEmpty() =>
HeapSize < 0;
@@ -66,20 +69,20 @@ namespace PriorityQueue
return sig;
}
- protected int CompareKey(U that, U other) =>
- that.CompareTo(other) * (IsMin ? -1 : 1);
+ protected bool LessSignificant(U that, U other) =>
+ that.CompareTo(other) * (IsMin ? -1 : 1) > 0;
public void ChangeKey(int id, U key)
{
int index = Location[id];
- // key <= Array[index].Key
- if (CompareKey(key, Array[index].Key) < 0)
+ // key < Array[index].Key
+ if (LessSignificant(key, Array[index].Key))
throw new System.ArgumentException($"New key ({key}) is less significant than node {id}'s current key ({Array[index].Key}).");
Array[index].Key = key;
- while (index > 0 && CompareKey(Array[index], Array[Parent(index)]) > 0)
+ while (index > 0 && MoreSignificant(Array[index], Array[Parent(index)]))
{
Swap(index, Parent(index));
index = Parent(index);