Title: Hash Files
1Hash Files
2How It Works
rrn
Key Value
Mapping Function
N-1
3Collisions - Synonyms
- Avoid Collisions
- Reduce the Number of Collisions
- Spread out records
- Use extra memory
- Put more than one record at an address
4Hashing Function
- Converting a String to a number
- Prime Number division remainder
- Digit extraction
- Folding
- Radix Conversion
- Mid-square
5Handling Collisions
- Open Addressing
- Linear Probing
- Quadratic Probing
- Double Hashing
- Chaining
- Restructuring the hash file - buckets
6Bucket Algorithms - Insert
// Creates an entry and places the entry on the
page //If it fits and returns 0. //If it does
not fit, the function returns the addr of the
next //bucket int Insert(string key, int
address) if it doesnt fit return
Next_Bucket else add to next slot in
bucket add to NumEntries return 0 //
success indicator
7Bucket Algorithms - Find
// Find looks in the bucket for the key. If it
finds it, it assigns //the associated address to
addr and returns 0. If it doesnt find //it, it
returns the address of the next bucket. int
Find(string key, int addr) while iltNumEntries
entry not the one at i increment i if (i
gt NumEntries) // not in bucket return
NextBucket else assign address at position i
to addr return 0
8Hash File Insert
//Hash the key and call the recursive
insert //Return 1 if success and 0 if failed int
Insert(string key, int address) bucketNo
hash(key) return _Insert(key, address,
bucketNo)
9Hash File Algorithms - Insert
int _Insert(string key, int address, int
bucketNo) read the bucket at bucket
number Insert the key/address in the bucket if
( Insert succeeded) Write the bucket return
1 if (Insert gave back 1 ) //didnt fit, no
next bucket yet get a new bucket insert the
key/address into the new bucket Write the new
bucket link the new bucket to old
bucket Write the old bucket return 1 return
_Insert(key, address, bucketNo back from insert
to bucket call)
10Hash File Search
//Hash the key and call the recursive
search //Return the address associated with key
if success //and -1 if failed int Search(string
key) bucketNo hash(key) return _Search(key,
bucketNo)
11Hash File Algorithms - Search
//Searches at the given bucketNo for key // It
returns associated address if found and 1
otherwise. int _Search(string key, int
bucketNo) read the bucket at address
bucketNo find the value in the bucket if find
succeeded return 1 if find returned
1 return 1 return _Search(key, the return
from find)