week 8 friday what did we talk about last time balancing
play

Week 8 - Friday What did we talk about last time? Balancing trees - PowerPoint PPT Presentation

Week 8 - Friday What did we talk about last time? Balancing trees by construction Hash tables Infix to Postfix Converter Wednesdays at 5 p.m. in The Point 113 Already started! Saturdays at noon in The Point 113 Starting


  1. Week 8 - Friday

  2.  What did we talk about last time?  Balancing trees by construction  Hash tables

  3. Infix to Postfix Converter

  4.  Wednesdays at 5 p.m. in The Point 113  Already started!  Saturdays at noon in The Point 113  Starting this week!

  5.  We can define a symbol table ADT with a few essential operations:  put(Key key, Value value) ▪ Put the key-value pair into the table  get(Key key): ▪ Retrieve the value associated with key  delete(Key key) ▪ Remove the value associated with key  contains(Key key) ▪ See if the table contains a key  isEmpty()  size()  It's also useful to be able to iterate over all keys

  6.  Determine if a string has any duplicate characters  Weak!  Okay, but do it in O( m ) time where m is the length of the string

  7.  What happens when you go to put a value in a bucket and one is already there?  There are a couple basic strategies:  Open Addressing  Chaining  Load factor is the number of items divided by the number of buckets  0 is an empty hash table  0.5 is a half full hash table  1 is a completely full hash table

  8.  With open addressing, we look for some empty spot in the hash table to put the item  There are a few common strategies  Linear probing  Quadratic probing  Double hashing

  9.  With linear probing, you add a step size until you reach an empty location or visit the entire hash table 3 19 7 89 104 0 1 2 3 4 5 6 7 8 9 10 11 12  Example: Add 6 with a step size of 5 3 19 7 6 89 104 0 1 2 3 4 5 6 7 8 9 10 11 12

  10.  For quadratic probing, use a quadratic function to try new locations:  h ( k , i ) = h ( k ) + c 1 i + c 2 i 2 , for i = 0, 1, 2, 3… 3 19 7 89 104 0 1 2 3 4 5 6 7 8 9 10 11 12  Example: Add 6 with c 1 = 0 and c 2 = 1 3 19 7 6 89 104 0 1 2 3 4 5 6 7 8 9 10 11 12

  11.  For double hashing, do linear probing, but with a step size dependent on the data:  h ( k , i ) = h 1 ( k ) + i ∙ h 2 ( k ), for i = 0, 1, 2, 3… 3 19 7 89 104 0 1 2 3 4 5 6 7 8 9 10 11 12  Example: Add 6 with h 2 ( k ) = ( k mod 7) + 1 6 3 19 7 89 104 0 1 2 3 4 5 6 7 8 9 10 11 12

  12.  Open addressing schemes are fast and relatively simple  Linear and quadratic probing can have clustering problems  One collision means more are likely to happen  Double hashing has poor data locality  It is impossible to have more items than there are buckets  Performance degrades seriously with load factors over 0.7

  13.  Make each hash table entry a linked list  If you want to insert something at a location, simply insert it into the linked list  This is the most common kind of hash table  Chaining can behave well even if the load factor is greater than 1  Chaining is sensitive to bad hash functions  No advantage if every item is hashed to the same location

  14.  Deletion can be a huge problem  Easy for chaining  Highly non-trivial for linear probing  Consider our example with a step size of 5 3 19 7 6 89 104 0 1 2 3 4 5 6 7 8 9 10 11 12  Delete 19  Now see if 6 exists

  15.  If you know all the values you are going to see ahead of time, it is possible to create a minimal perfect hash function  A minimal perfect hash function will hash every value without collisions and fill your hash table  Cichelli’s method and the FHCD algorithm are two ways to do it  Both are complex  Look them up if you find yourself in this situation

  16.  We can define a symbol table ADT with a few essential operations:  put(Key key, Value value) ▪ Put the key-value pair into the table  get(Key key): ▪ Retrieve the value associated with key  delete(Key key) ▪ Remove the value associated with key  contains(Key key) ▪ See if the table contains a key  isEmpty()  size()  It's also useful to be able to iterate over all keys

  17. public class HashTable { private int size = 0; private int power = 10; private Node[] table = new Node[1 << power]; private static class Node { public int key; public Object value; public Node next; } … }

  18.  Get the number of elements stored in the hash table public int size()  Say whether or not the hash table is empty public boolean isEmpty()

  19.  It's useful to have a function that finds the appropriate hash value  Take the input integer and swap the low order 16 bits and the high order 16 bits (in case the number is small)  Square the number  Use shifting to get the middle power bits public int hash(int key)

  20.  If the hash table contains the given key, return true  Otherwise return false public boolean contains(int key)

  21.  Return the object with the given key  If none found, return null public Object get(int key)

  22.  If the load factor is above 0.75, double the capacity of the hash table, rehashing all current elements  Then, try to add the given key and value  If the key already exists, update its value and return false  Otherwise add the new key and value and return true public boolean put(int key, Object value)

  23.  Finish implementing hash tables  Hash table time trials  Map in the JCF  HashMap  TreeMap  Introduction to graphs

  24.  Finish Project 2  Start Assignment 4  Get help on Saturday!  Keep reading 3.3  Read 4.1

Recommend


More recommend