Best data structure to get maximum values ​​and id?

I have a fairly large number of records of a fixed size. Each record contains many fields, among them ID and value. I am wondering which data structure will be better so that I can

  • very quickly find the record by identifier (unique),

  • specify 100 records with the highest values.

Max-heap seems to be work, but far from perfect; Do you have a more reasonable solution?

Thank.

+1
source share
3 answers

, . ID , , -. 100- . . 100 , , / -100, 100 . , O, .

+2

. , - . . 100 ?

:

add(Item t)
{
    //Add the same object instance to both data structures
    heap.add(t);
    hash.add(t);
}
remove(int id)
{
    heap.removeItemWithId(id);//this is gonna be slow
    hash.remove(id);
}
getTopN(int n)
{
    return heap.topNitems(n);
}
getItemById(int id)
{
    return hash.getItemById(id);
}
updateValue(int id, String value)
{
    Item t = hash.getItemById(id);
    //now t is the same object referred to by the heap and hash
    t.value = value;
    //updated both.
}
0

, , Java, , TreeSet, ID, .

, 100 100 . ID .

, Binary-Tree Balanced Tree .

0

Source: https://habr.com/ru/post/1676758/


All Articles