0% found this document useful (0 votes)
12 views48 pages

9 Hashtables2

Uploaded by

Taha Çakmak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views48 pages

9 Hashtables2

Uploaded by

Taha Çakmak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 48

1

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.

1 Now you want to search for 361, you hash to


position 1, it is empty so you decide 361 is not
2 289
in the table. NOT GOOD.
3 361
4 144
5 225
6 196
7 256
8 324
9 169
4
0 100 Suppose you delete 121 from the
table by marking the cell as deleted
1 121

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

2 289 Now you want to search for 361, you hash to


3 361 position 1, it is deleted so you keep on going
based on your collision detection strategy
4 144 and find 361 two probes later.
5 225
6 196
7 256
8 324
9 169
6
0 100 Suppose you want to insert 521
1 121
to the hash table
2 289
3 361
4 144
5 225
6 196
7 256
8 324
9 169
7
0 100 Suppose you want to insert 521
1 521
to the hash table
2 289
3 361
4 144
5 225
6 196
7 256
8 324
9 169
8
9
10
0 Initially the hash table is empty
1

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

3 Try location (9+1)mod 10= 0 FULL


4 144
5 225
6 196
7 256
8

9 169
20
0 100 Insert 289 COLLISION because location
1 121
9 is full.
2

3 289 Try location (9+1)mod 10= 0 FULL


4 144
5 225
Try location (9+4)mod 10= 3 AVAILABLE
6 196
7 256
8

9 169
21
0 100 Insert 324 COLLISION because location
1 121
4 is full.
2

3 289 Try location (4+1)mod 10= 5 FULL


4 144
5 225
6 196
7 256
8

9 169
22
0 100 Insert 324 COLLISION because location
1 121
4 is full.
2

3 289 Try location (4+1)mod 10= 5 FULL


4 144
5 225
Try location (4+4)mod 10= 8 AVAILABLE
6 196
7 256
8 324
9 169
23
0 100 Insert 361 COLLISION because location
1 121
1 is full.
2 361
3 289 Try location (1+1)mod 10= 2 AVAILABLE
4 144
5 225
6 196
7 256
8 324
9 169
24
25
26
27
2 j
( hash ( x )  i ) mod M  ( hash ( x )  ) mod M
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 ) { }

const HashedObj & find( const HashedObj & x ) const;

void makeEmpty( );
void insert( const HashedObj & x );
void remove( const HashedObj & x );

const HashTable & operator=( const HashTable & rhs );

enum EntryType { ACTIVE, EMPTY, DELETED }; 33


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 ) { }

const HashedObj & find( const HashedObj & x ) const;

void makeEmpty( );
void insert( const HashedObj & x );
void remove( const HashedObj & x );

const HashTable & operator=( const HashTable & rhs );

enum EntryType { ACTIVE, EMPTY, DELETED }; 34


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 ) { }

const HashedObj & find( const HashedObj & x ) const;

void makeEmpty( );
void insert( const HashedObj & x );
void remove( const HashedObj & x );

const HashTable & operator=( const HashTable & rhs );

enum EntryType { ACTIVE, EMPTY, DELETED }; 35


private:
struct HashEntry
{
HashedObj element;
EntryType info;

HashEntry( const HashedObj & e = HashedObj( ),


EntryType i = EMPTY )
: element( e ), info( i ) { }
};

vector<HashEntry> array;
int currentSize;
const HashedObj ITEM_NOT_FOUND;

bool isActive( int currentPos ) const;


int findPos( const HashedObj & x ) const;
void rehash( );
}; 36
/**
* Construct the hash table.
*/
template <class HashedObj>
HashTable<HashedObj>::HashTable( const HashedObj & notFound,
int size )
: ITEM_NOT_FOUND( notFound ), array( nextPrime( size ) )
{
makeEmpty( );
}

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( ) );

while ( array[ currentPos ].info != EMPTY &&


array[ currentPos ].element != x )
{
currentPos += pow(++collisionNum, 2) ; //add the difference
if ( currentPos >= array.size( ) ) // perform the mod
currentPos –= array.size( ); // if necessary
}
return currentPos;
} 38
/**
* Return true if currentPos exists and is active.
*/
template <class HashedObj>
bool HashTable<HashedObj>::isActive( int currentPos ) const
{
return array[ currentPos ].info == ACTIVE;
}

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 );

// enlarge the hash table if necessary


if ( ++currentSize >= array.size( ) / 2 )
rehash( );
} 42
43
/**
* Expand the hash table.
*/
template <class HashedObj>
void HashTable<HashedObj>::rehash( )
{
vector<HashEntry> oldArray = array;

// Create new double-sized, empty table


array.resize( nextPrime( 2 * oldArray.size( ) ) );
for ( int j = 0; j < array.size( ); j++ )
array[ j ].info = EMPTY;

// Copy table over


currentSize = 0;
for ( int i = 0; i < oldArray.size( ); i++ )
if ( oldArray[ i ].info == ACTIVE )
insert( oldArray[ i ].element );
} 44
45
46
47
48

You might also like