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
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Week 8 - Friday

slide-2
SLIDE 2

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

slide-3
SLIDE 3
slide-4
SLIDE 4

Infix to Postfix Converter

slide-5
SLIDE 5
slide-6
SLIDE 6

 Wednesdays at 5 p.m. in The Point 113

  • Already started!

 Saturdays at noon in The Point 113

  • Starting this week!
slide-7
SLIDE 7
slide-8
SLIDE 8

 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

slide-9
SLIDE 9

 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

slide-10
SLIDE 10
slide-11
SLIDE 11

 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
slide-12
SLIDE 12

 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
slide-13
SLIDE 13

 With linear probing, you add a step size until you reach an

empty location or visit the entire hash table

 Example: Add 6 with a step size of 5

104

3 19 7 89 1 2 3 4 5 6 7 8 9 10 11 12

104

3 19 7 6 89 1 2 3 4 5 6 7 8 9 10 11 12

slide-14
SLIDE 14

 For quadratic probing, use a quadratic function to try new

locations:

 h(k,i) = h(k) + c1i + c2i2, for i = 0, 1, 2, 3…  Example: Add 6 with c1 = 0 and c2 = 1

104

3 19 7 89 1 2 3 4 5 6 7 8 9 10 11 12

104

3 19 7 6 89 1 2 3 4 5 6 7 8 9 10 11 12

slide-15
SLIDE 15

 For double hashing, do linear probing, but with a step size

dependent on the data:

 h(k,i) = h1(k) + i∙h2(k), for i = 0, 1, 2, 3…  Example: Add 6 with h2(k) = (k mod 7) + 1

104

3 19 7 89 1 2 3 4 5 6 7 8 9 10 11 12

104

6 3 19 7 89 1 2 3 4 5 6 7 8 9 10 11 12

slide-16
SLIDE 16

 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

slide-17
SLIDE 17

 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
slide-18
SLIDE 18

 Deletion can be a huge problem  Easy for chaining  Highly non-trivial for linear probing  Consider our example with a step size of 5  Delete 19  Now see if 6 exists

104

3 19 7 6 89 1 2 3 4 5 6 7 8 9 10 11 12

slide-19
SLIDE 19

 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

slide-20
SLIDE 20
slide-21
SLIDE 21

 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

slide-22
SLIDE 22

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; } … }

slide-23
SLIDE 23

 Get the number of elements stored in the hash table  Say whether or not the hash table is empty

public boolean isEmpty() public int size()

slide-24
SLIDE 24

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

slide-25
SLIDE 25

 If the hash table contains the given key, return true  Otherwise return false

public boolean contains(int key)

slide-26
SLIDE 26

 Return the object with the given key  If none found, return null

public Object get(int key)

slide-27
SLIDE 27

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

slide-28
SLIDE 28
slide-29
SLIDE 29
slide-30
SLIDE 30

 Finish implementing hash tables  Hash table time trials  Map in the JCF

  • HashMap
  • TreeMap

 Introduction to graphs

slide-31
SLIDE 31

 Finish Project 2  Start Assignment 4

  • Get help on Saturday!

 Keep reading 3.3  Read 4.1