L5_HashTable

L5_HashTable - Hash Tables In the last lecture we...

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

View Full Document Right Arrow Icon
Hash Tables In the last lecture we introduced hash tables; and decided to implement symbol tables using hash tables. Page 1 of 38 "Waiter" Value 17 "Principal" H H H a a a s s s h h h F F F u u u n n n c c c - - - t t t i i o o o n n n i 17 wp "Principal" "Waiter" bucket vp
Background image of page 1

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

View Full DocumentRight Arrow Icon
public interface SymbolTable { public void enter(String key, Object value); public Object lookup(String key); } Key Value "Piglet" "Duckling" "Mrs Mak" "Miss Chan" Page 2 of 38
Background image of page 2
public class SymbolTable_v1 implements SymbolTable { private Cell[] buckets = new Cell[101]; "Principal" "Waiter" A cell of class Cell . 0 }; public class Cell { public String key; 17 public Object value; public Cell next; } 100 Page 3 of 38
Background image of page 3

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

View Full DocumentRight Arrow Icon
IMPORTANT STOP "Principal" "Waiter" A cell of class Cell . 0 Question : What are the ‘keys?’ 17 Question : What are the ‘values?’ 100 Page 4 of 38
Background image of page 4
Page 5 of 38 IMPORTANT STOP 0 "Principal" "Waiter" A cell of class Cell . Question : What are the ‘keys?’ Answer : Strings (“Prin- ciple”, “Waiter”, etc. ) 17 Question : What are the ‘values?’ 100 Answer : and .
Background image of page 5

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

View Full DocumentRight Arrow Icon
"Principal" "Waiter" A cell of class Cell . 0 17 "Principal" "Waiter" Value Key 100 Page 6 of 38
Background image of page 6
* public void enter(String key, Object value) { int hashCode; Cell cp; hashCode = Hash(key, 101); cp = FindCell(hashCode, key); if (cp==null) { /* New Entry */ cp = new Cell(); c p . k e y =
Background image of page 7

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

View Full DocumentRight Arrow Icon
Note z Case 1: FindCell returns a reference to a cell. "Principal" 17 17 cp value "Principal" H H H a a a s s s h h h F F F u u u n n n c c c - - - t t t i i o o o n n n i key Page 8 of 38
Background image of page 8
private Cell FindCell(int h, String key) { Cell cp = buckets[h]; while (cp != null && !cp.key.equals(key)) cp = cp.next; return cp; } cp h cp h cp h Page 9 of 38
Background image of page 9

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

View Full DocumentRight Arrow Icon
private Cell FindCell(int h, String key) { Cell cp = buckets[h]; while ( c c p p ! ! = = n n u u l l l l && !cp.key.equals(key) ) cp = cp.next; return cp; } Page 10 of 38 cp h key equals key
Background image of page 10
{ Cell cp = buckets[h]; while ( c c p p ! ! = = n
Background image of page 11

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

View Full DocumentRight Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 38

L5_HashTable - Hash Tables In the last lecture we...

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

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