private void PercolateDown(int hole) { PriorityQueueElement obj = data[hole]; while ((hole * 2 + 1) < data.Count) { int child = hole * 2 + 1; if (child != data.Count - 1) { if (data[child + 1].CompareTo(data[child]) < 0) { child++; } } PriorityQueueElement childObj = data[child]; if (childObj.CompareTo(obj) < 0) { data[hole] = childObj; childObj.PQIndex = hole; hole = child; } else { break; } } data[hole] = obj; obj.PQIndex = hole; }
// code from https://visualstudiomagazine.com/articles/2012/11/01/priority-queues-with-c/listing4.aspx public virtual T Dequeue() { // assumes pq is not empty; up to calling code int li = data.Count - 1; // last index (before removal) PriorityQueueElement <T> frontItem = data[0]; // fetch the front data[0] = data[li]; data.RemoveAt(li); --li; // last index (after removal) int pi = 0; // parent index. start at front of pq while (true) { int ci = pi * 2 + 1; // left child index of parent if (ci > li) { break; // no children so done } int rc = ci + 1; // right child if (rc <= li && data[rc].ComparePriority(data[ci]) < 0) // if there is a rc (ci + 1), and it is smaller than left child, use the rc instead { ci = rc; } if (data[pi].ComparePriority(data[ci]) <= 0) { break; // parent is smaller than (or equal to) smallest child so done } PriorityQueueElement <T> tmp = data[pi]; data[pi] = data[ci]; data[ci] = tmp; // swap parent and child pi = ci; } return(frontItem.item); }
public void Insert(PriorityQueueElement e) { e.index = n; heap[n] = e; n++; up(n - 1); }
void swap(int i, int j) { PriorityQueueElement temp = heap[i]; heap[i] = heap[j]; heap[j] = temp; heap[i].index = i; heap[j].index = j; }
// this dequeue method results in queue 'travelling' in memory as it grows and items are popped public T DequeueBad() { if (IsEmpty()) { throw new IndexOutOfRangeException("Priority Queue: attempting to pop from an empty queue."); } PriorityQueueElement <T> item = data[0]; data.RemoveAt(0); return(item.item); }
public void Insert(PriorityQueueElement obj) { int hole = data.Count; data.Add(obj); while (hole>0 && obj.CompareTo(data[(hole-1)/2])<0) { data[hole] = data[(hole-1)/2]; data[hole].PQIndex = hole; hole = (hole-1) / 2; } data[hole] = obj; obj.PQIndex = hole; }
public void Insert(PriorityQueueElement obj) { int hole = data.Count; data.Add(obj); while (hole > 0 && obj.CompareTo(data[(hole - 1) / 2]) < 0) { data[hole] = data[(hole - 1) / 2]; data[hole].PQIndex = hole; hole = (hole - 1) / 2; } data[hole] = obj; obj.PQIndex = hole; }
public int CompareTo(PriorityQueueElement other) { ManifoldPoint rec = other as ManifoldPoint; if (this.distance < rec.distance) { return(-1); } if (this.distance > rec.distance) { return(1); } return(0); }
public int CompareTo(PriorityQueueElement other) { NODE rec = other as NODE; if (this.distance < rec.distance) { return(-1); } if (this.distance > rec.distance) { return(1); } return(0); }
// for priority queue public int ComparePriority(PriorityQueueElement <T> pt) { if (this.priority < pt.priority) { return(-1); } else if (this.priority == pt.priority) { return(0); } else { return(1); } }
private void PercolateUp(int hole) { PriorityQueueElement obj = data[hole]; while (hole > 0 && obj.CompareTo(data[(hole - 1) / 2]) < 0) { int parent = (hole - 1) / 2; PriorityQueueElement parentObj = data[parent]; data[hole] = data[parent]; parentObj.PQIndex = hole; hole = parent; } data[hole] = obj; obj.PQIndex = hole; }
public PriorityQueueElement DeleteMin() { if (IsEmpty()) { throw new Exception(); } PriorityQueueElement obj = data[0]; data[0] = data[data.Count - 1]; data.RemoveAt(data.Count - 1); if (data.Count > 0) { data[0].PQIndex = 0; PercolateDown(0); } return(obj); }
// adds highest priority in the end // code from: https://visualstudiomagazine.com/articles/2012/11/01/priority-queues-with-c/listing3.aspx public virtual void Enqueue(T item, float priority) { PriorityQueueElement <T> pqe = new PriorityQueueElement <T>(item, priority); // add item data.Add(pqe); int ci = data.Count - 1; // swap items to maintain priority while (ci > 0) { int pi = (ci - 1) / 2; if (data[ci].ComparePriority(data[pi]) >= 0) { break; } PriorityQueueElement <T> tmp = data[ci]; data[ci] = data[pi]; data[pi] = tmp; ci = pi; } }
public void Update(PriorityQueueElement obj) { PercolateUp(obj.PQIndex); PercolateDown(obj.PQIndex); }