当前位置: 首页>>代码示例>>C#>>正文


C# PriorityQueue.ExtractRoot方法代码示例

本文整理汇总了C#中System.Collections.Generic.PriorityQueue.ExtractRoot方法的典型用法代码示例。如果您正苦于以下问题:C# PriorityQueue.ExtractRoot方法的具体用法?C# PriorityQueue.ExtractRoot怎么用?C# PriorityQueue.ExtractRoot使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Collections.Generic.PriorityQueue的用法示例。


在下文中一共展示了PriorityQueue.ExtractRoot方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ExtractRootMax

        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]);
        }
开发者ID:albertk78,项目名称:Study,代码行数:13,代码来源:PriorityQueueTests.cs

示例2: Find

        /// <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;
        }
开发者ID:albertk78,项目名称:Study,代码行数:44,代码来源:NearestPoints.cs

示例3: ExtractRootMin

        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]);
        }
开发者ID:albertk78,项目名称:Study,代码行数:14,代码来源:PriorityQueueTests.cs


注:本文中的System.Collections.Generic.PriorityQueue.ExtractRoot方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。