









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










Introduction ..................................................................................................................... 1 Hash Tables ..................................................................................................................... 1 EXERCISE: Looking at hash tables............................................................................... 6 Hash Maps ....................................................................................................................... 7 EXERCISE: Looking at a HashMap.............................................................................. 11
The linear search (sometimes called the sequential search ) is the simplest algorithm to search for a specific target key in a data collection, for example to search for a specific integer value in an array of integers. It is also the least efficient. It simply examines each element in turn, starting with the first element, until it finds the target element or it reaches the end of the array. A linear search does not require the array to be sorted.
The binary search is the standard method for searching through a sorted array. It is much more efficient than the linear search, but it does require that the elements be in order.
The time taken for a search using each of these methods depends on the size of the collection. The time for a linear search is proportional to the size of the collection – it takes 10 times as long on average to find an element in an array of 100 elements as it does in an array of 10 elements. The binary search time depends on the logarithm of the collection size – it takes twice as long on average to find an element in an array of 100 elements as it does in an array of 10 elements.
A further search method is hashing. Hash data structures allow the storage and retrieval of data in an average time which does not depend at all on the collection size.
The simplest has data structure is the hash table. The basis of a hash table is a hash function. A hash function is a function that takes an element of whatever data type you are storing (integer, string, etc) and outputs an integer in a certain range. This integer is used to determine the location in the table for that element. The hash table itself consists of an array whose indices are the range of the hash function.
For example, a hash table for a car database might make use of the registration plate as a key. Let’s assume the table has a size of 20. A possible hash function would be Value Mod 20, for example
(sum of numeric values of the characters) % 20
where % is the modulus operator, giving the remainder after dividing.
This would give translations like this:
SD52DFG 28+13+5+2+13+15+16 = 92: Hashcode = 12 FG02GTH 15+16+0+2+16+29+17 = 95: Hashcode = 15 DR52GHY 13+27+5+2+16+17+34 = 114: Hashcode = 14 FT52RET 15+29+5+2+27+14+29 = 121: Hashcode = 1 NN52FRT 23+23+5+2+15+27+29 = 124: Hashcode = 4
A hash table with size 20 which has had records with these keys added could look like this:
Hash table contents 0: empty 1: occupied Make:Honda Reg:FT52RET 2: empty 3: empty 4: occupied Make:Jaguar Reg:NN52FRT 5: empty 6: empty 7: empty 8: empty 9: empty 10: empty 11: empty 12: occupied Make:Mazda Reg:SD52DFG 13: empty 14: occupied Make:Toyota Reg:DR52GHY 15: occupied Make:Rover Reg:FG02GTH 16: empty 17: empty 18: empty 19: empty
A good hash function is one that gets a fairly even distribution of the numbers in the output range, even if the input values are very poorly distributed (for example, English words are poorly distributed since they only use 26 different letters and some letters and some combinations of letter occur far more frequently than others.
Searching a hash table is easy and extremely fast: just find the hash value for the item you're looking for, then go to that index and start searching the array until you find what you're looking for or you hit an empty location.
Note that these examples are based on the use of linear probing to handle collisions.
Example: search for car with registration NN52FRT (table uses linear probing)
Calculate hashcode: 23+23+5+2+15+27+29 = 124: Hashcode = 4
Go to location 4.
Compare key in location 4 with target
Target was found, index = 4
Example: search for car with registration SD52QWS
Calculate hashcode: 28+13+5+2+26+32+28 = 134: Hashcode = 14
Go to location 14
Compare key in location 14 with target
Key not equal to target, go to next location
Compare key in location 15 with target
Key not equal to target, go to next location
Compare key in location 16 with target
Target was found, index = 16
Example: search for car with registration FD52HBC
Calculate hashcode: 15+13+5+2+17+11+12 = 75: Hashcode = 15
Go to location 15
Compare key in location 15 with target
Key not equal to target, go to next location
Compare key in location 16 with target
Key not equal to target, go to next location
Compare key in location 17 with target
Location 17 is empty – this is where the key would have been if it was in the table Target not in table
Deleting an element from a hash table involves searching for the key, then deleting the element when it is found. The location must be marked as deleted , rather than empty , as searching will not work correctly otherwise. Locations marked deleted can be re- used.
The following segment of the table shows the result of adding SD52QWS using linear probing – the hash function of this key is 14 , but the element is stored in the next empty location, 16.
… 12: occupied Make:Mazda Reg:SD52DFG 13: empty 14: occupied Make:Toyota Reg:DR52GHY 15: occupied Make:Rover Reg:FG02GTH 16: occupied Make:VW Reg:SD52QWS 17: empty …
If we now delete the element in location 15, we get:
… 12: occupied Make:Mazda Reg:SD52DFG 13: empty 14: occupied Make:Toyota Reg:DR52GHY 15: deleted 16: occupied Make:VW Reg:SD52QWS 17: empty … We now search for key SD52QWS. If location 15 was empty , the search algorithm would stop and report that the key is not in the table.
A map data structure stores (key, value) pairs. Values are retrieved by searching for the appropriate key. Maps are also sometimes called dictionaries or associative arrays.
Maps can be implemented in a number of ways. One of the most common ways is to store the (key, value) pairs in a hash table. This is called a hash map. The following code shows a simple Java implementation of a HashMap class.
This class uses linear probing to handle collisions and includes methods to store and retrieve entries. Any kind of object can be used as a key, and any kind of object can be stored as a value.
We can’t use exactly the same hash function as we used in the examples above since we want to be able to use any kind of key, not just strings representing car registration numbers. The function used now makes use of the fact that every Java object inherits a method hashCode from Object.
The highlighted System.out.println commands in the put method are for the purpose of the following exercise only.
This HashMap is much simpler and less robust than the Java Collections Framework. The framework class:
Create a new BlueJ project called hashmaps , and add a new class HashMap using the above code. Add new classes Car and CarTest using the following code:
Create a new instance of HashMap called hashmap1. Create a new instance of CarTest called carTest1 and supply hashMap1 as the parameter for the constructor. You can now use carTest1 to store and find Car objects in hashMap1 , and also inspect the map directly.
Call the storeCar method of carTest1 repeatedly to add the following entries:
carTest1.storeCar("DF52TMU", "Audi", "TT"); carTest1.storeCar("RT02GHT", "Volvo", "S50"); carTest1.storeCar("FD52HBC", "Saab", "9-5"); carTest1.storeCar("DR02TRG", "Mini", "Cooper"); carTest1.storeCar("TU02XYZ", "Renault", "Megane"); carTest1.storeCar("YU51TRH", "Hyundai", "Coupe");
These are similar to the hash table entries you used in the previous exercise. They will not be stored in the same array positions since the hash function is different here.
Look at the output in the terminal window. This shows the hashcodes calculated during execution of the put method of the HashMap.
Were there any collisions?
What steps were taken to handle collisions?
Inspect hashMap1. You should see keys , values and status arrays. Inspect the keys array.
Choose a position which is not empty. Note the position number and the registration number it contains. Fro example, in the figure, position 10 contains “YU51TRH”.
Now inspect the values array.
Inspect the object reference at the position number you noted above.
What kind of object does the values array contain?
Does this object have the correct attributes?
Repeat this for another non-empty position.
Call the findCar method of carTest1 with “TU02XYZ” s a parameter.