9 Hashtables2
9 Hashtables2
2
0 100 Suppose you delete 121 from the table.
1 121
2 289
3 361
4 144
5 225
6 196
7 256
8 324
9 169
3
0 100 Suppose you delete 121 from the table.
2 289
3 361
4 144
5 225
6 196
7 256
8 324
9 169
5
0 100 Suppose you delete 121 from the
table by marking the cell as deleted
1 121
9
11
0 100 Insert 100
1
9
12
0 100 Insert 121
1 121
9
13
0 100 Insert 144
1 121
4 144
5
9
14
0 100 Insert 169
1 121
4 144
5
9 169
15
0 100 Insert 196
1 121
4 144
5
6 196
7
9 169
16
0 100 Insert 225
1 121
4 144
5 225
6 196
7
9 169
17
0 100 Insert 256 COLLISION because location
1 121 6 is full. Try location 6+1=7
2
4 144
5 225
6 196
7 256
8
9 169
18
0 100 Insert 289 COLLISION because location
1 121
9 is full.
2
4 144
5 225
6 196
7 256
8
9 169
19
0 100 Insert 289 COLLISION because location
1 121
9 is full.
2
9 169
20
0 100 Insert 289 COLLISION because location
1 121
9 is full.
2
9 169
21
0 100 Insert 324 COLLISION because location
1 121
4 is full.
2
9 169
22
0 100 Insert 324 COLLISION because location
1 121
4 is full.
2
i mod M j mod M
2
2
(Why ?)
28
2 j
( hash ( x ) i ) mod M ( hash ( x ) ) mod M
2
i j 0 mod M
2 2
( i j )( i j ) 0 mod M
29
( i j )( i j ) 0 mod M
( i j )( i j ) kM
30
( i j )( i j ) 0 mod M
( i j )( i j ) kM
M
1 i,j
2
31
32
template <class HashedObj>
class HashTable
{
public:
explicit HashTable( const HashedObj & notFound, int size = 101 );
HashTable( const HashTable & rhs )
: ITEM_NOT_FOUND( rhs.ITEM_NOT_FOUND ),
array( rhs.array ), currentSize( rhs.currentSize ) { }
void makeEmpty( );
void insert( const HashedObj & x );
void remove( const HashedObj & x );
void makeEmpty( );
void insert( const HashedObj & x );
void remove( const HashedObj & x );
void makeEmpty( );
void insert( const HashedObj & x );
void remove( const HashedObj & x );
vector<HashEntry> array;
int currentSize;
const HashedObj ITEM_NOT_FOUND;
37
/**
* Method that performs quadratic probing resolution.
* Return the position where the search for x terminates.
*/
template <class HashedObj>
int HashTable<HashedObj>::findPos( const HashedObj & x ) const
{
int collisionNum = 0;
int currentPos = hash( x, array.size( ) );
39
/**
* Remove item x from the hash table.
* x has to be in the table
*/
template <class HashedObj>
void HashTable<HashedObj>::remove( const HashedObj & x )
{
int currentPos = findPos( x );
if ( isActive( currentPos ) )
array[ currentPos ].info = DELETED;
}
40
/**
* Find item x in the hash table.
* Return the matching item, or ITEM_NOT_FOUND, if not found.
*/
template <class HashedObj>
const HashedObj & HashTable<HashedObj>::find( const HashedObj & x )
const
{
int currentPos = findPos( x );
if (isActive( currentPos ))
return array[ currentPos ].element;
return ITEM_NOT_FOUND;
}
41
/**
* Insert item x into the hash table. If the item is
* already present, then do nothing.
*/
template <class HashedObj>
void HashTable<HashedObj>::insert( const HashedObj & x )
{
// Insert x as active
int currentPos = findPos( x );
if ( isActive( currentPos ) )
return;
array[ currentPos ] = HashEntry( x, ACTIVE );