简体   繁体   中英

Recommend sorted collection to search closest value left and right

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$

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?

I recommend you to use SQLite in for such queries. And your query will be:

from car in cars where car.Price < 1000 
order by car.Price descending
select car

You just use List to store car price and sort it every time you add new car price.

List<int> PriceList= new List<int>();
PriceList.Sort();

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM