using hash-tables if array is completely filled than what is the best possible methode/scenario that we can do insertion in Big-O(1)... explain me in detail.
Asked
Active
Viewed 44 times
-2
-
1I'm voting to close this question as off-topic because it smacks of homework and doesn't respect point 3 at http://stackoverflow.com/help/on-topic – Tony Delroy May 11 '16 at 02:03
-
1The question description is not very good and it's not easy to figure out what you are asking. I guess you want to insert an element in hash-table that is implemented using array, but that is not clearly stated. – Nemanja Trifunovic May 15 '16 at 20:46
1 Answers
0
If the array is completely filled (which it should never be allowed to be [Edit: caveat, see comments]), then it's impossible to insert anything at all without expanding it, which cannot be done in O(1) time. Unless you mean amortized O(1) time. But in any case, the best method is not to allow the array to be completely filled before it is expanded, because then all operations deteriorate to O(n) time.

njlarsson
- 2,128
- 1
- 18
- 27
-
1*"should never be allowed to be), then it's impossible"* - that's true for closed hashing (aka "open addressing"), but untrue for open hashing (aka "separate chaining"). – Tony Delroy May 11 '16 at 02:01
-
Right, I assumed from the poster's formulation that we were talking about open addressing. But even with chaining, it's normal to not allow filling of all slots, of course. – njlarsson May 12 '16 at 08:24
-
I now realize that perhaps the intended answer to the (homework?) question lies in *not* interpreting the way I did. I'll refrain from clarifying that further. – njlarsson May 12 '16 at 08:32
-
@njilarsson: tough Q to answer meaningfully. for open hashing the *"best possible method/scenario ... [for] insert in O(1)"* is clearly not to resize at that 1-element-per-bucket threshold and just add the item to the appropriate bucket's list; for closed hashing there is no O(1) solution as your answer explains. *"even with chaining, it's normal to not allow filling of all slots"* - in C++'s Standard LIbrary, `max_load_factor()` defaults to 1.0, but you'd normally need much more to achieve the Q's "array completely filled" scenario given at 1.0 collisions leave other buckets unused. – Tony Delroy May 12 '16 at 08:56