C#实现优先队列 基于二叉堆 附使用案例
2016-04-14 19:26
756 查看
前言
想用下C#的优先队列,结果发现居然没有,简直蛋疼。。。感谢 /article/4676280.html 博主的实现
我借用博主的优先队列实现,写了测试程序,感觉还可以,这里放出来当个使用案例吧~
转载实现
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DynamicTrafficAssignment.Core { public class PriorityQueue<T> { IComparer<T> comparer; T[] heap; public int Count { get; private set; } public PriorityQueue() : this(null) { } public PriorityQueue(int capacity) : this(capacity, null) { } public PriorityQueue(IComparer<T> comparer) : this(16, comparer) { } public PriorityQueue(int capacity, IComparer<T> comparer) { this.comparer = (comparer == null) ? Comparer<T>.Default : comparer; this.heap = new T[capacity]; } public void Push(T v) { if (Count >= heap.Length) Array.Resize(ref heap, Count * 2); heap[Count] = v; SiftUp(Count++); } public T Pop() { var v = Top(); heap[0] = heap[--Count]; if (Count > 0) SiftDown(0); return v; } public T Top() { if (Count > 0) return heap[0]; throw new InvalidOperationException("优先队列为空"); } void SiftUp(int n) { var v = heap ; for (var n2 = n / 2; n > 0 && comparer.Compare(v, heap[n2]) > 0; n = n2, n2 /= 2) heap = heap[n2]; heap = v; } void SiftDown(int n) { var v = heap ; for (var n2 = n * 2; n2 < Count; n = n2, n2 *= 2) { if (n2 + 1 < Count && comparer.Compare(heap[n2 + 1], heap[n2]) > 0) n2++; if (comparer.Compare(v, heap[n2]) >= 0) break; heap = heap[n2]; } heap = v; } } }
使用案例
说明
我用了个键值对,作为在优先队列中的元素类型手写了比较类,实现的是一个小值优先的队列
和eps比,是为了防止double的精度问题,具体使用可以改变设置
using System; using System.Collections.Generic; using System.Linq; using System.Text; using DynamicTrafficAssignment.Core; namespace DTATest { class Program { const double eps = 1e-8; public class PairCompare : IComparer<KeyValuePair<double, int>> { public int Compare(KeyValuePair<double, int> x, KeyValuePair<double, int> y) { if (Math.Abs(y.Key - x.Key) < eps) { return 0; } if (x.Key > y.Key) return -1; else return 1; } } static void Main(string[] args) { PriorityQueue<KeyValuePair<double, int>> que = new PriorityQueue<KeyValuePair<double, int>>(new PairCompare()); for (int i = 0; i < 5; i++) { var str = Console.ReadLine().Split(' '); que.Push( new KeyValuePair<double, int>(Double.Parse(str[0]), Int32.Parse(str[1]) ) ); } while (que.Count > 0) { var tmp = que.Pop(); Console.WriteLine(tmp.Key + "," + tmp.Value); } Console.Read(); } } } ///输入 /* 0.1 2 0.1 1 -1.9 200 0 9 10000.42 3 */ ///输出 /* -1.9,200 0,9 0.1,2 0.1,1 10000.42,3 */
相关文章推荐
- C# 使用Tuple传递多个参数
- C# Interlocked 笔记
- C# Timer类详解
- C# get
- C# 单例模式的不同写法对静态变量的影响
- c#XML配置文件辅助类
- C# Socket SSL通讯笔记
- C# DataTable添加行和列
- C# 数组与 list 互相转换案例
- c#概念理解
- 适用于WebForm Mvc的Pager分页组件C#实现
- C#委托与事件的本质区别
- C#中(int)、int.Parse()、int.TryParse()和Convert.ToInt32()的区别
- c#中的保留两位小数并且四舍五入
- C#解析错误代码至错误提示字符串
- C# WinForm 技巧:控件截图
- C#第6周实验类的继承
- C# Dictionary使用
- [C#]exchange发送,收件箱操作类
- C#禁用numericUpDown控件鼠标中键滚轮消息响应