Here are some constraints for a data structure I need. It seems like none of the common data structures (I will mention the ones I've thought of below) fit these all that well. Can anyone suggest one that I maybe haven't thought of?
An array is the simplest and most widely used data structure. Other data structures like stacks and queues are derived from arrays.
Trie. A trie, also known as a keyword tree, is a data structure that stores strings as data items that can be organized in a visual graph.
To answer your question, if time to delete is the most important perspective, the LinkedList should be chosen.
When N is small a simple array or single linked list with key + value as payload is very efficient. Even if it is not the best when N gets larger.
You get O(N) lookup time which means lookups take k * N
time. A O(1) lookup takes a constant K
time. So you get better performance with O(N) for N < K/k
. Here k
is very small so you can get to interesting values of N. Remember that the Big O notation only describes behavior for large N
s, not what you are after. For small tables
void *lookup(int key_to_lookup)
{
int n = 0;
while (table_key[n] != key_to_lookup)
n++;
return table_data[n];
}
can be hard to beat.
Benchmark your hash tables, balanced tree and simple array/linked list and see at which values of N they each start to be better. Then you will know which is better for you.
I almost forgot: keep the frequently accessed keys at the beginning of your array. Given your description that means keep it sorted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With