1
$\begingroup$

I'm studying elementary data structures like Linked List, Doubly Linked List and Binary Trees like Binary Search Trees.

Both runs in worst case O(n) in the same operations, so why don't we use only one data structure?

$\endgroup$

marked as duplicate by Hendrik Jan, Evil, David Richerby, Community Jun 16 '16 at 11:47

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ Well, search in a BST is $O(\log n)$ which is technically also $O(n)$ but it's misleading to suggest that all the data structures you list have the same running time for the same operations. $\endgroup$ – David Richerby Jun 15 '16 at 18:53
3
$\begingroup$

Aspects other than asymptotic worst case time are also important. For example

  • Actual speed in practice
  • Memory consumption
  • Implementation difficulty

Algorithmic analysis almost never tells you the complete story and never should be used to justify blanket statements like "this data structure is the best if you need operations x,y,z".

$\endgroup$
1
$\begingroup$

$O(n)$ in BST means it was case degraded to Single Linked List, but it still has two pointers per node, while linked list has only one. But for balanced or almost balanced tree, the operations are $O(\log n)$. You can keep it balanced (using for example AVL tree or Red Black Tree, where both differ in cost of insert and delete by constant factor).
Linked list allows you to traverse in one direction, Doubly Linked List allows you to move in the both directions, it has two pointers per node, also if you do not use traversal in one direction, the cost of insert / delete is lower for Single Linked List. The notation makes this constant disappear. So taking memory footprint, actual time per unit operation, size of data stored, your needs and time to implement given data structure makes them differ a lot.

Also there are structures like Fibonacci heap, very good asymptotically, but rather heavy on small data and harder to implement.

There is no such structure that fits every situation and optimizes every possible feature, otherwise we would use only this one and learn about others for educational purposes only (or to stop people from reinventing the wheel).

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.