Download presentation
Presentation is loading. Please wait.
1
Hashing
2
Data Structure: Hashing
Outline Hash Function Separate Chaining Open Addressing 7/20/2019 Data Structure: Hashing
3
Data Structure: Hashing
Introduction 1 2 3 4 : 8 9 Somsri Sodsai Saisin Sansai Susie 7/20/2019 Data Structure: Hashing
4
Data Structure: Hashing
Hash Function 1 2 3 4 : 8 9 Somsri Find f Sodsai Saisin Sansai Susie 7/20/2019 Data Structure: Hashing
5
Data Structure: Hashing
Insertion Collision resolution 1 2 3 4 : 8 9 collision 341212 f Somsri Sodsai Insert Songsarn Saisin Sansai Susie 7/20/2019 Data Structure: Hashing
6
Separate Chaining Hash Table
1 2 3 4 : 8 9 Linked list Table of linked lists Hash table 7/20/2019 Data Structure: Hashing
7
Data Structure: Hashing
Hash Table public interface Hashable { int hash( int tableSize ); } public class SeparateChainingHashTable { ... private static final int DEFAULT_TABLE_SIZE=101; private LinkedList [ ] theLists; } 7/20/2019 Data Structure: Hashing
8
Data Structure: Hashing
Constructor public SeparateChainingHashTable( ) { this( DEFAULT_TABLE_SIZE ); } public SeparateChainingHashTable( int size ) { theLists = new LinkedList[ nextPrime( size ) ]; for( int i = 0; i < theLists.length; i++ ) theLists[ i ] = new LinkedList( ); public void makeEmpty( ) { for( int i = 0; i < theLists.length; i++ ) theLists[ i ].makeEmpty( ); 7/20/2019 Data Structure: Hashing
9
Data Structure: Hashing
Hash Function public static int hash( String key, int tableSize ) { int hashVal = 0; for( int i = 0; i < key.length( ); i++ ) hashVal = 37 * hashVal + key.charAt( i ); hashVal %= tableSize; if( hashVal < 0 ) hashVal += tableSize; return hashVal; } 7/20/2019 Data Structure: Hashing
10
Methods insert and remove
public void insert( Hashable x ) { LinkedList whichList = theLists[x.hash(theLists.length)]; LinkedListItr itr = whichList.find( x ); if( itr.isPastEnd( ) ) whichList.insert( x, whichList.zeroth( ) ); } public void remove( Hashable x ) { theLists[x.hash(theLists.length)].remove( x ); 7/20/2019 Data Structure: Hashing
11
Data Structure: Hashing
Methods find public Hashable find( Hashable x ) { int size=theLists.length; LinkedList chain = theLists[x.hash(size)]; return (Hashable)chain.find(x).retrieve(); } 7/20/2019 Data Structure: Hashing
12
Data Structure: Hashing
Methods for Primes private static int nextPrime( int n ) { if( n % 2 == 0 ) n++; while(!isPrime( n )) n = n+2; return n; } private static boolean isPrime( int n ) { if( n == 2 || n == 3 ) return true; if( n == 1 || n % 2 == 0 ) return false; for( int i = 3; i * i <= n; i += 2 ) if( n % i == 0 ) return false; return true; 7/20/2019 Data Structure: Hashing
13
Open Addressing Hashing
Following a linked list or a chain in a hash table is costly. How to reduce this cost. Resolving collision by finding other available entry in the table. 7/20/2019 Data Structure: Hashing
14
Open Addressing Hash Table
1 2 3 4 : 8 9 Somsri Sodsai Saisin Sansai Susie Songsan 7/20/2019 Data Structure: Hashing
15
Data Structure: Hashing
Delete 1 2 3 4 : 8 9 Somsri Sodsai Songsan delete Susie Saisai Sansai Saisin 7/20/2019 Data Structure: Hashing
16
Data Structure: Hashing
Linear Probing 1 2 3 4 : 8 9 7/20/2019 Data Structure: Hashing
17
Data Structure: Hashing
Quadratic Probing 1 2 3 4 : 8 9 1 4 9 7/20/2019 Data Structure: Hashing
18
Open Addressing Hash Table
public interface Hashable { int hash( int tableSize ); } class HashEntry { Hashable element; // the element boolean isActive; // false is deleted public HashEntry( Hashable e ) { this( e, true ); } public HashEntry( Hashable e, boolean i ) { element = e; isActive = i; } } public class QuadraticProbingHashTable { ... private static final int DEFAULT_TABLE_SIZE=101; private HashEntry [ ] theLists; } 7/20/2019 Data Structure: Hashing
19
Data Structure: Hashing
constructor public QuadraticProbingHashTable( ) { this( DEFAULT_TABLE_SIZE ); } public QuadraticProbingHashTable( int size ) { allocateArray( size ); makeEmpty( ); private void allocateArray( int arraySize ) { array = new HashEntry[ arraySize ]; 7/20/2019 Data Structure: Hashing
20
Data Structure: Hashing
Quadratic Probing public QuadraticProbingHashTable( ) { this( DEFAULT_TABLE_SIZE ); } public QuadraticProbingHashTable( int size ) { allocateArray( size ); makeEmpty( ); private void allocateArray( int arraySize ) { array = new HashEntry[ arraySize ]; 7/20/2019 Data Structure: Hashing
21
Data Structure: Hashing
Method findPos private int findPos( Hashable x ) { int collisionNum = 0; int currentPos = x.hash( array.length ); while( array[ currentPos ] != null && !array[ currentPos ].element.equals( x ) ) { currentPos += 2 * ++collisionNum - 1; if( currentPos >= array.length ) currentPos -= array.length; } return currentPos; 7/20/2019 Data Structure: Hashing
22
Methods insert and remove
public void insert( Hashable x ) { int currentPos = findPos( x ); if( isActive( currentPos ) ) return; array[ currentPos ] = new HashEntry( x, true ); // Rehash if( ++currentSize > array.length / 2 ) rehash( ); } public void remove( Hashable x ) if( isActive( currentPos ) ) array[ currentPos ].isActive = false; 7/20/2019 Data Structure: Hashing
23
Methods insert and remove
public Hashable find( Hashable x ) { int currentPos = findPos( x ); if ( isActive(currentPos)) return array[ currentPos ].element; else return null; } private boolean isActive( int currentPos ) { return array[ currentPos ] != null && array[ currentPos ].isActive; 7/20/2019 Data Structure: Hashing
24
Data Structure: Hashing
Method rehash private void rehash( ) { HashEntry [ ] oldArray = array; // Create a new double-sized, empty table allocateArray( nextPrime( 2*oldArray.length ) ); currentSize = 0; // Copy table over for( int i = 0; i < oldArray.length; i++ ) if(oldArray[i]!=null && oldArray[i].isActive) insert( oldArray[ i ].element ); return; } 7/20/2019 Data Structure: Hashing
25
Data Structure: Hashing
7/20/2019 Data Structure: Hashing
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.