Search code examples
c++addressbooktrie

Is a radix tree (Patricia Trie) an efficient data structre for a mobile-phone address book


I have been thinking on implementing an address book in C++. Since it's developed for mobile application the address book should use as less memory as possible and also the user should still be able to search or sort contacts by name fast ( paradox I know ).

After I've researched a bit I found that most of the people suggest that a Trie would be the best data structure tp fit my needs. More precisely a radix tree( Patricia Trie ). Using this data structure would also be great for implementing autocomplete too.

Are there other viable solutions or is it ok if I start coding using this idea ?


Solution

  • Beware of tries for small collections. Though they do offer good asymptotical behavior, their hidden constant in both time and space might be too big.

    Especially, tries tend to have poor cache performace, which should be the main concern for small collections.

    Assuming your data is relatively small [<10,000 entries], a std::vector can offer good cache performance, which will probably be much more influence then the size factor. So even the search time for it is asymptotically higher then trie or a std::set, practically - it might be better then both, thanks to good caching behavior.

    If you can also maintain the vector sorted, using binary search - you can benefit from both logarithmic search time and good cache behavior.

    (*)This answer assumes the hardware where the app will be deployed on has CPU-Cache.