Pages

Compare Data Structure

 lists or List :
ArrayDynamic ArrayArray DequeSingly Linked ListDouble Linked List
Push (Front)-O(n)O(1)O(1)O(1)
Pop (Front)-O(n)O(1)O(1)O(1)
Push (Back)-O(1)O(1)O(n), maybe O(1)*O(1)
Pop (Back)-O(1)O(1)O(n)O(1)
Insert before (given iterator)-O(n)O(n)O(n)O(1)
Delete (given iterator)O(n)O(n)O(n)O(1)
Insert after (given iterator)O(n)O(n)O(1)O(1)
Delete after (given iterator)-O(n)O(n)O(1)O(1)
Get nth element (random access)O(1)O(1)O(1)O(n)O(n)
Good for implementing stacksnoyes (back is top)yesyes (front is top)yes
Good for implementing queuesnonoyesmaybe*yes
C++ STLstd::arraystd::vectorstd::dequestd::forward_liststd::list
Java Collectionsjava.util.Arrayjava.util.ArrayListjava.util.ArrayDeque-java.util.LinkedList
* singly-linked lists can push to the back in O(1) with the modification that you keep a pointer to the last node

Associative containers (sets, associative arrays):
Sorted ArraySorted Linked ListSelf-balancing Binary Search TreeHash Table
Find keyO(log n)O(n)O(log n)O(1) average O(n) worst
Insert elementO(n)O(n)O(log n)O(1) average O(n) worst
Erase keyO(n)O(n)O(log n)O(1) average O(n) worst
Erase element (given iterator)O(n)O(1)O(1)O(1)
Can traverse in sorted order?yesyesyesno
Needscomparison functioncomparison functioncomparison functionhash function
C++ STL--std::set
std::map
std::multiset
std::multimap
__gnu_cxx::hash_set
__gnu_cxx::hash_map
__gnu_cxx::hash_multiset
__gnu_cxx::hash_multimap
Java Collections--java.util.TreeSet
java.util.TreeMap
java.util.HashSet
java.util.HashMap
  • Trees
Binary SearchAVL TreeBinary Heap (min)Binomial Queue (min)
Insert elementO(log n)O(log n)O(log n)O(1) (on average)
Erase elementO(log n)O(log n)unavailableunavailable
Delete min elementO(log n)O(log n)O(log n)O(log n)
Find min elementO(log n)O(log n)O(1)O(log n) (can be O(1) if ptr to smallest)
Increase keyunavailableunavailableO(log n)O(log n)
Decrease keyunavailableunavailableO(log n)O(log n)
FindO(log n)O(log n)unavailableunavailable
Delete elementO(log n)O(log n)unavailableunavailable
CreateO(1)O(1)O(1)O(1)
find kth smallestO(log n)O(log n)O((k-1)*log n)O(k*log n)
Hash table:
Hash table (hash map)
Set ValueΩ(1), O(n)
Get ValueΩ(1), O(n)
RemoveΩ(1), O(n)