Академический Документы
Профессиональный Документы
Культура Документы
C. Aravindan
<AravindanC@ssn.edu.in>
1 Introduction
1 Introduction
2 Hash Functions
1 Introduction
2 Hash Functions
3 Separate Chaining
1 Introduction
2 Hash Functions
3 Separate Chaining
4 Summary
Index
Hash ( KeyType Key , i n t T a b l e S i z e )
{
r e t u r n Key % T a b l e S i z e ;
}
For categorical data, such as strings, we may first convert the key to
an integer
For categorical data, such as strings, we may first convert the key to
an integer
A simple idea for strings will be to just add up the ASCII values of the
characters
For categorical data, such as strings, we may first convert the key to
an integer
A simple idea for strings will be to just add up the ASCII values of the
characters
Index
Hash1 ( const char ∗ Key , i n t T a b l e S i z e )
{
unsigned i n t HashVal = 0 ;
w h i l e ( ∗ Key != ’ \0 ’ )
HashVal += ∗Key++;
r e t u r n HashVal % T a b l e S i z e ;
}
This idea may be bad when TableSize is large and length of string is
short
This idea may be bad when TableSize is large and length of string is
short
Example: TableSize = 10007 and strings are 8 characters at most
This idea may be bad when TableSize is large and length of string is
short
Example: TableSize = 10007 and strings are 8 characters at most
Only indexes up to (127 * 8 = 1016) are used!
Index
Hash2 ( const char ∗ Key , i n t T a b l e S i z e )
{
r e t u r n ( Key [ 0 ] + 27 ∗ Key [ 1 ] + 729 ∗ Key [ 2 ] )
% TableSize ;
}
Index
Hash3 ( const char ∗Key , i n t T a b l e S i z e )
{
unsigned i n t HashVal = 0 ;
w h i l e ( ∗ Key != ’ \0 ’ )
HashVal = ( HashVal << 5 ) + ∗Key++;
r e t u r n HashVal % T a b l e S i z e ;
}
Idea here is to use another collection data structure to keep all the
pairs that hash to the same index
A simple linked list will be an ideal choice!
typedef s t r u c t P a i r S t r u c t {
KeyType k e y ;
ValType v a l u e ;
} ∗ Pair ;
typedef s t r u c t P a i r S t r u c t {
KeyType k e y ;
ValType v a l u e ;
} ∗ Pair ;
struct ListNode
{
Pair Element ;
s t r u c t L i s t N o d e ∗ Next ;
};
typedef s t r u c t ListNode ∗ P o s i t i o n ;
typedef P o s i t i o n L i s t ;
s t r u c t HashTbl
{
int TableSize ;
List ∗ TheLists ;
};
t y p e d e f s t r u c t HashTbl ∗ HashTable ;
// C o n s t r u c t o r
HashTable I n i t i a l i z e T a b l e ( i n t T a b l e S i z e ) ;
// D e s t r u c t o r
v o i d D e s t r o y T a b l e ( HashTable H ) ;
// I n s e r t a key−v a l u e p a i r
v o i d I n s e r t ( P a i r p a i r , HashTable H ) ;
// F i n d and R e t r i e v e
P o s i t i o n F i n d ( KeyType Key , HashTable H ) ;
Pair Retrieve ( Position P );
P a i r F i n d R e t r i e v e ( KeyType Key , HashTable H ) ;
HashTable
I n i t i a l i z e T a b l e ( int TableSize )
{
HashTable H ;
int i ;
/∗ A l l o c a t e t a b l e ∗/
/∗ 4 ∗/ H = m a l l o c ( s i z e o f ( s t r u c t HashTbl ) ) ;
/∗ 5 ∗/ i f ( H == NULL )
/∗ 6 ∗/ F a t a l E r r o r ( " Out␣ o f ␣ s p a c e ! ! ! " ) ;
/∗ 7 ∗/ H−>T a b l e S i z e = NextPrime ( T a b l e S i z e ) ;
/∗ A l l o c a t e a r r a y o f l i s t s ∗/
/∗ 8 ∗/ H−>T h e L i s t s = m a l l o c ( s i z e o f ( L i s t )
∗ H−>T a b l e S i z e ) ;
/∗ 9 ∗/ i f ( H−>T h e L i s t s == NULL )
/∗ 10 ∗/ F a t a l E r r o r ( " Out␣ o f ␣ s p a c e ! ! ! " ) ;
/∗ A l l o c a t e l i s t h e a d e r s ∗/
/∗ 11 ∗/ f o r ( i = 0 ; i < H−>T a b l e S i z e ; i++ )
{
/∗ 12 ∗/ H−>T h e L i s t s [ i ] =
malloc ( sizeof ( struct ListNode )
/∗ 13 ∗/ i f ( H−>T h e L i s t s [ i ] == NULL )
/∗ 14 ∗/ F a t a l E r r o r ( " Out␣ o f ␣ s p a c e ! ! ! " ) ;
else
/∗ 15 ∗/ H−>T h e L i s t s [ i ]−>Next = NULL ;
}
/∗ 16 ∗/ return H;
}
Position
F i n d ( KeyType Key , HashTable H )
{
Position P;
List L;
Pair
Retrieve ( Position P )
{
r e t u r n P−>E l e m e n t ;
}
Pair
F i n d R e t r i e v e ( KeyType Key , HashTable H )
{
Position P;
List L;
void
I n s e r t ( P a i r p a i r , HashTable H )
{
P o s i t i o n Pos , N e w C e l l ;
List L;
/∗ 1 ∗/ Pos = F i n d ( Key , H ) ;