13-1 - Hashing with Separate Chaining Implementation data...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Hashing with Separate Chaining Implementation – data members public class SCHashTable<T extends KeyedItem> implements HashTableInterface<T> { private List<T>[] table; private int h( long key) // hash function // return index { return ( int )(key % table.length); // typecast to int } public SCHashTable( int size) // recommended size: prime number roughly twice bigger // than the expected number of elements { table = new List[size]; // initialize the lists for ( int i=0; i<size; i++) table[i] = new List<T>(); }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Implementation – insertion public void insert(T item) { int index = h(item.getKey()); List<T> L = table[index]; // insert item to L L.add(1,item); // if linked list is used, // insertion will be efficient }
Background image of page 2
Implementation – search private int findIndex(List<T> L, long key) // search for item with key 'key' in L // return -1 if the item with key 'key' was not found in L { // search of item with key = 'key' for ( int i=1; i<=L.size(); i++) if (L.get(i).getKey() == key) return i; return -1; // not found } public T find( long key) { int index = h(key); List<T> L = table[index]; int list_index = findIndex(L,key); if (index>=0) return L.get(list_index); else return null ; // not found }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Implementation – deletion public T delete( long key) { int index = h(key); List<T> L = table[index]; int list_index = findIndex(L,key); if (index>=0) { T item = L.get(list_index); L.remove(list_index); return item; } else return null ; // not found }
Background image of page 4
Figure: The relative efficiency of four collision-resolution methods Hashing – comparison of different methods
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Comparing hash tables and balanced BSTs With good hash function and load kept low, hash tables perform insertions, deletions and search in O(1) time on average, while balanced BSTs in O(log n) time. However, there are some tasks (order related) for
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/26/2010 for the course CMPT 225 taught by Professor Annelavergne during the Spring '07 term at Simon Fraser.

Page1 / 22

13-1 - Hashing with Separate Chaining Implementation data...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online