Group 15 Hash Tables
Group 15 Hash Tables
NAMES
REG NO.
TUMITHO STEVEN 20/U/2871/GIT
KITARA DANIEL 20/U/0835/GIK/PS
TUKASHABA DICKENS 20/2475/GIT
HASHING
• Value:Age
• Hash(“key”) = index
• 9 Hash(“Paul”) = 3
• 20 Hash( “Tina”) = 0
• 60 Hash(“Somebody”) = 2
Definition cont’d……….
Hash Table Data Structure : Purpose
1 Ben int y = 2;
2 Dickson Dickson
int x = (“Aidah”) ;
3
x is now 5
4
int x = 5;
5 Aidah
int z =(“Ben”);
z now 1
FACTORS AFFECTING
HASH TABLE DESIGN
• Hashing functions
• Table size- its usually fixed at the start
• Collision handling scheme- This normally occur
when two or more keys maps to the same array
index.
HASH FUNCTIONS
• where hi(x) is the i^th location tested and f(i; x) is a function that
returns some integer value based on the values of i and x. The idea
is that the hash function h(x) is first used to find a location in the
hash table for x. If we are trying to insert x into the table, and the
index h(x) is empty, we insert it there. Otherwise we need to
search for another place in the table into which we can store x. The
function f(i; x), called the collision resolution function, serves that
purpose. We search the locations until either an empty cell is found
or the search returns to a cell previously visited in the sequence.
The function f(i; x) need not depend on both i and x.
Open addressing cont’d
• h(x) + f(0; x) % M
• h(x) + f(1; x) % M
• h(x) + f(2; x) % M
• :::
• h(x) + f(k; x)% M
• To search for an item, the same collision resolution function is used. The
hash function is applied to find the first index. If the key is there, the
search stops. Otherwise, the table is searched until either the item is
found or an empty cell is reached. If an empty cell is reached, it implies
that the item is not in the table. This raises a question about how to delete
items. If an item is deleted,
Open addressing cont’d
• Hash(89,10) = 9
• Hash(18,10) =8
• Hash(49,10) = 9
• Hash(58,10) = 8
TABLE
0 49 49 49
1 58 58
2 9
3
4
5
6
7
8 18 18 18 18
9 89 89 89 89 89
Quadratic Probing
• If the hash table gets too full it should be resized. The best
way to resize it is to create a new hash table about twice as
large and hash all of the elements of the hash table into the
new table using its hash function.
• Rehashing is expensive, so it should only be done when
necessary:
• 1. When an insertion fails, or
• 2. When the table gets half full, or
• 3. When the table load factor reaches some predefined value.
Load factor
• Load factor λ of a hash table T is defined as follows:
• N = number of elements in T (“current size”)
• M = size of T(“table size”)
• λ= N/M(“ load factor”)
• i.e., λ is the average length of a chain
• Unsuccessful search time: O(λ)
• -Same for insert time
• Successful search time: O(λ/2)
• Ideally, want λ≤ 1 (not a function of N)
implementation
void clear( )
Resets and empties the hash table.
Object clone( )
Returns a duplicate of the invoking object.
Enumeration elements( )
Returns an enumeration of the values contained in the hash table.
Object get(Object key)
Returns the object that contains the value associated with the key. If the key
is not in the hash table, a null object is returned.
boolean isEmpty( )
Returns true if the hash table is empty; returns false if it contains at least one
key.
Enumeration keys( )
Returns an enumeration of the keys contained in the hash table.
Object put(Object key, Object value)
Inserts a key and a value into the hash table. Returns null if the key isn't
already in the hash table; returns the previous value associated with the key if
the key is already in the hash table.
void rehash( )
Increases the size of the hash table and rehashes all of its keys.
Object remove(Object key)
Removes the key and its value. Returns the value associated with the key. If
the key is not in the hash table, a null object is returned.
int size( )
Returns the number of entries in the hash table.
String toString( )
Returns the string equivalent of a hash table.
IMPLEMENTATION
• import java.util.*;
• public class HashTable{
•
• // Deposit 1,000 into Dickson’s account
• bal = ((Double)balance.get(“Dickson")).doubleValue();
• balance.put(“Dickson", new Double(bal + 1000));
• System.out.println(
• “Dickson's new balance: " + balance.get(“Dickson"));
• }
• }
References