public void ExtractRootMax() { PriorityQueue<int> q = new PriorityQueue<int>((i, j) => j.CompareTo(i)); q.Storage = new List<int> { 11, 5, 8, 3, 4 }; int max = q.ExtractRoot(); Assert.AreEqual(11, max); Assert.AreEqual(4, q.Storage.Count); Assert.AreEqual(8, q.Storage[0]); Assert.AreEqual(4, q.Storage[2]); }
/// <summary> /// For given 'center' point returns a subset of 'm' stored points that are /// closer to the center than others. /// /// E.g. Stored: (0, 1) (0, 2) (0, 3) (0, 4) (0, 5) /// /// Find(new Point(0, 0), 3) -> (0, 1), (0, 2), (0, 3) /// /// Seems like we can do this in two ways /// /// Quick select - as we do quick sort on distance from center point, /// stop is pivot index == m, then everything on left is closest m /// complexity will be n log n /// /// Max - Heap - reprocess the points into a max - heap with bounded size of m /// Since everything in heap is smaller than max value, this will also ensure we /// get m closet points. complexity will be n log m /// </summary> /// <param name="point"></param> /// <param name="k"></param> public static List<int[]> Find(List<int[]> points, int[] centerPoint, int m) { PriorityQueue<int[]> maxHeap = new PriorityQueue<int[]>((i, j) => //since this is a max heap, we want to compare j against i //and we are comparing distance (sqrt(x^2 + y^2)) from the point (System.Math.Pow(j[0] - centerPoint[0], 2) + System.Math.Pow(j[1] - centerPoint[1], 2)) .CompareTo(System.Math.Pow(i[0] - centerPoint[0], 2) + System.Math.Pow(i[1] - centerPoint[1], 2)) ); foreach(int[] point in points) { maxHeap.Add(point); if (maxHeap.Storage.Count > m) { maxHeap.ExtractRoot(); } } List<int[]> result = new List<int[]>(); while(maxHeap.Storage.Count > 0) { result.Add(maxHeap.ExtractRoot()); } return result; }
public void ExtractRootMin() { PriorityQueue<int> q = new PriorityQueue<int>((i, j) => i.CompareTo(j)); q.Storage = new List<int> { 4, 4, 8, 9, 4, 12, 9, 11, 13 }; int min = q.ExtractRoot(); Assert.AreEqual(4, min); Assert.AreEqual(8, q.Storage.Count); Assert.AreEqual(4, q.Storage[0]); Assert.AreEqual(4, q.Storage[1]); Assert.AreEqual(13, q.Storage[4]); }