algorithmdata-structurescomputer-scienceproof

Time complexity analysis of data structures


I got a bit confused about analzsis of data structure basic operation like inserting or deleting.

when I am asked about creating an data structure that support deleting operation, or inserting in O(1), (or any big O):

  1. For deleting do I need take in account the searching of this element or only the deleting operation it self, for example changing the pointers?

  2. For the inserting do I need to take in account the searching of the index/point where I need to place the element

I look a bit in the Internet but I got diffrenet time complexity for the basic data structure I learnedd, for example in Array, I saw both O(1) and O(N)

Thanks


Solution

  • To answer both of your questions, what is included in the time complexity of an operation depends on how much extra work you needed to do for that specific operation.

    For instance, given an array and an element, delete would take O(n) time and would include searching the element in the array. However, if that same array is assumed to have properties of a stack, the time complexity of delete operation would become O(1) since we already know the element's position.

    An example with the insert operation would be inserting an element to the end of a linked list. When implemented without a tail pointer, time is O(n), but with a tail pointer it is O(1).

    Hence, analysing time complexities is less about operations having rigid time complexities as such and more about its implementation details.

    Hope this clarifies things. Feel free to discuss any particular eg. you found confusing in the comments :)