Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9166781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:13:54+00:00 2026-06-17T15:13:54+00:00

The data structures I know such as b-tree or rb-tree do not change when

  • 0

The data structures I know such as b-tree or rb-tree do not change when I keep looking for the same keys. I’m looking for a data structure which optimizes itself in the runtime for faster lookup for frequently used keys. Now I have a naive implementation like this:

ValueType MyNaiveMap::get(KeyType key) {
  innerMap.addFreq(key);
  if (key == mostFreqUsedKey1) {
    return val1;
  } else if (key == mostFreqUsedKey2) {
    return val2;
  }
  else {
    return innerMap.get(key);
  }
}

The keys are always int in my case.

UPDATE:

I forgot to mention hash map. Because the map may grow to a very large size, I was trying to avoid resizing which is O(n) complex in the runtime.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:13:55+00:00Added an answer on June 17, 2026 at 3:13 pm

    If you know the total size ahead of time, or if O(n) insertions due to resizing (but still O(1) amortized in the average case) are okay for you, then I think hash table is the best option. The one-time hit of copying the whole table when resizing can also be distributed over several operations by using incremental resizing.

    You have to keep the hash table partially empty to avoid collisions, which may feel like wasting memory. But with tree-based maps, you usually have at least one additional pointer per item, which will also “waste” a significant amount of memory. Also, with a hash table, you can tweak how much of it to keep empty, partially trading performance for memory (or vice versa).

    But there also is a tree-based data structure optimized for looking up recently accessed keys: splay tree. It uses rotations to keep recently used keys near the root, while still maintaining amortized worst case of O(log n) for insertion and searching. (That “amortized” bit is important, some operations can still be O(n)).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am beginner in C++, I need to know which data structure to store
I learnt that when we manage a data structure such as a tree or
I have a vector<set<char> > data structure (transactions database) and I want to know
Persistent data structures depend on the sharing of structure for efficiency. For an example,
Which algorithms or data structures are used in auto-suggest features? It seems that edit-distance
Please suggest a data structure to maintain numbers in such a way that i
I'm a C# developer and I use data structures such as List and Dictionary
I'm learning about data structures and abstract data types, and I keep getting stuck
I have a legacy database which contains simple data structures (no CODE refs thank
I've implemented a specialised tree data structure for a ray tracing application. I'm using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.