推荐排序集合以搜索左右最接近的值
.NET 3.5 中是否有现成的数据结构来执行以下
按十进制键排序的存储值,允许重复
获取最接近给定键左右的下一个值(枚举器)
示例:
汽车经销商有汽车,客户要求找到最贵的车但便宜不到1000$
Is there ready data structure in .NET 3.5 to do the following
store values sorted by decimal key, dublicates allowed
get next value (enumerator) closest to given key left and right
An example:
car dealer has cars, client asks to find the most expensive car but cheaper than 1000$
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在寻找允许重复键的二叉树(又名 多集 )。 .NET 库中没有这样的东西,但它们很容易实现(并且免费提供,例如 此处或此处)。
另请参阅 .Net 是否有 multiset 的实现?
You are looking for a binary tree allowing duplicate keys (aka multi-set). There is no such thing in the .NET library, but they are easy to implement (and freely available, eg here or here).
See also Are there any implementations of multiset for .Net?
您只需使用 List 来存储汽车价格,并在每次添加新车价格时对其进行排序。
You just use List to store car price and sort it every time you add new car price.
我建议您使用 SQLite 进行此类查询。
您的查询将是:
I recommend you to use SQLite in for such queries.
And your query will be:
我知道您正在寻找现成的结构,但可能值得探索的一个选择是 van Emde蟒蛇树。这种结构使您可以在 O(lg lg n) 时间内进行查找、查找后继、查找前驱和删除所有内容,这比平衡搜索树的速度呈指数级增长。我不熟悉 C# 中此结构的任何实现,但这可能是解决问题的渐近最优方法。如果您要存储大量整数,它也可以非常节省空间。
I know that you're looking for ready-made structures, but one option that might be worth exploring is a van Emde Boas Tree. This structure gives you lookup, find successor, find predecessor, and delete all in O(lg lg n), time, which is exponentially faster than a balanced search tree. I'm not familiar with any implementations of this structure in C#, but it's probably the asymptotically optimal way of solving your problem. If you're storing a large number of integers, it can also be very space-efficient.