1

If I have a HashMap/HashTable and I insert the Key:c Value:14

[0] -> NULL
[1] -> NULL
[2] -> NULL
[3] -> NULL
[4] -> NULL
[5] -> NULL
[6] -> NULL
[7] -> NULL
[8] -> NULL
[9] -> (c/14) -> NULL
[0] -> NULL
[1] -> NULL
[2] -> NULL
[3] -> NULL
[4] -> NULL
[5] -> NULL

Let's say I insert the Key:z Value:6 and the hash and modulus have it land on index 9.

My question is: do I insert at the end of the singly linkedlist (meaning (c/14) -> (z/6) -> NULL) or at the front of the LinkedList.

If I insert the new collisions at the front of each bucket, then I don't need to loop over the entire LinkedList chain. This makes insertion O(1), since no matter how big the bucket is, I don't need to loop over all the elements.

If I insert the new collisions at the end of each bucket, I must loop over the entire singly list until I reach the last element. Then I append the last element.

No matter which one I choose, retrieval from the List will be the same. I might still need to loop over all nodes in a bucket. Unless you would argue that it's more likely for the user to call get() on a key that was added earlier.

Despite this, I see all examples and implementations of HashTable's and HashMap's add the element at the end of the bucket. Why?

Hatefiend
  • 3,416
  • 6
  • 33
  • 74
  • Does this answer your question? [In a hashmap, the addition of a new element to the internal linked list of a bucket is always at the end. Why?](https://stackoverflow.com/questions/57921060/in-a-hashmap-the-addition-of-a-new-element-to-the-internal-linked-list-of-a-buc) – dgupta3091 Aug 18 '20 at 07:05

1 Answers1

1

There isn't a clearly right or wrong way to do this. If your hash table is used to represent a set or a map, you'd have to scan the entire bucket anyway in order to find out whether you're inserting a duplicate element, so the cost of inserting at the beginning and at the end will probably be identical. And, since the hash table likely won't have buckets that are very full, the relative difference in lookup costs probably won't be too high.

I'd just go with whatever is easiest.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • 1
    Oh wait you're right, you HAVE to insert at the end because only after looping to the end is the way to determine if you have a duplicate element or not. If you insert at the front, you risk accidentally inserting a duplicate. Thanks a lot man. – Hatefiend Mar 14 '17 at 01:43
  • You could also insert at the beginning after scanning the list if you wanted to. Might be a bit easier to code up. :-) – templatetypedef Mar 14 '17 at 07:00
  • Yeah yeah I guess so. Anyways thanks for making that clear. Can't believe I didn't realize it – Hatefiend Mar 15 '17 at 16:45