hash table Flashcards
describe operation of adding key-val pair
hash(key) to get hash index
insert & rec key into hash table at hash index
hash collision def
when diff keys have same hash index
good hash function (hint: data, key, hash values)
- must use all input data
- uniformly distributes hash vals across entire set of possible hash values
- small difference in data causes a large difference in hash values
OR
generates very different hash values for similar (but non-identical) keys
conflict resolution (open addressing)
- rehashing function will be called continuously until diff/new location found
- key-value pair will be stored at diff/new location
- lookups for keys should check subsequent locations if first key does not match
conflict resolution (closed addressing)
- linked list can be instantiated at each array position
- key-value pairs are stored in linked list at each hash position
- lookup for keys done by walking through the linked list, checking for matching key
application: why hash table can’t be a stack
- hash table does not remember order of insertion
- each item is hashed to a location that is unique to the index & not in running order
advantage of hash table
time complexity O(1), constant time access as input data grows
(assuming hash collisions x occur v often, mention it esp if prev part of qn talked abt it)
if hash table size n, exp how >n entries wld be stored in hash table
- the current hash table is unable to do so. :. a new hash table w larger capacity has to be created
- current entries have to be extracted, rehashed w the new size & inserted into the new hash table
hash table vs bst
(context: 2023 nyjc prelim 3cii, prev parts mentioned unbalanced tree + hash collision :. need mention in final eval)
- for a large variety of keys, the % of hash collisions wld incr → collision resolution cld incr t complexity of inserting frm O(1) to O(n)
- while for BST, t complexity is O(logn) but can worsen till O(n) if tree is maximally unbalanced whr it can undergo rebalancing. :. given t complexity comparison, BST more suitable