1 of 38

CSE 373 SP24 AX: Section 5

Heaps and Hashing

Warm Up: What are the operations that take place when removing the root in this max-heap? What does the final heap look like?

9

8

2

6

5

1

2 of 38

Options (On the Worksheet)

Warm Up: What are the operations that take place when removing the root in this max-heap? What does the final heap look like?

8

5

2

6

1

6

8

2

1

5

8

2

1

6

5

1

8

2

6

5

A

B

C

D

9

8

2

6

5

1

3 of 38

Max Heap Solution

8

5

2

6

1

9

Steps:

  • Swap the root with the leaf node closest to the right.

0

1

2

3

4

5

6

n/a

9

8

6

5

2

1

Array Representation

Idx

Val

4 of 38

Max Heap Solution

8

5

2

6

9

Steps:

  • Swap the root with the leaf node closest to the right.
  • Remove the leaf node 9, which was previously the root

1

0

1

2

3

4

5

6

n/a

1

8

6

5

2

9

Array Representation

Idx

Val

5 of 38

Max Heap Solution

8

5

2

6

1

Steps:

  • Swap the root with the leaf node closest to the right.
  • Remove the leaf node 9, which was previously the root
  • Sink!

Compare child nodes to curr node, for the max

0

1

2

3

4

5

6

n/a

1

8

6

5

2

NULL

Array Representation

Idx

Val

6 of 38

Max Heap Solution

8

5

2

6

1

Steps:

  • Swap the root with the leaf node closest to the right.
  • Remove the leaf node 9, which was previously the root
  • Sink!

Compare child nodes to curr node, for the max

0

1

2

3

4

5

6

n/a

1

8

6

5

2

NULL

Array Representation

Idx

Val

7 of 38

Max Heap Solution

8

5

2

6

1

Steps:

  • Swap the root with the leaf node closest to the right.
  • Remove the leaf node 9, which was previously the root
  • Sink!

Sink to the max child

0

1

2

3

4

5

6

n/a

8

1

6

5

2

NULL

Array Representation

Idx

Val

8 of 38

Max Heap Solution

8

5

2

6

1

Steps:

  • Swap the root with the leaf node closest to the right.
  • Remove the leaf node 9, which was previously the root
  • Sink!

Compare again!

0

1

2

3

4

5

6

n/a

8

1

6

5

2

NULL

Array Representation

Idx

Val

9 of 38

Max Heap Solution

8

5

2

6

1

Steps:

  • Swap the root with the leaf node closest to the right.
  • Remove the leaf node 9, which was previously the root
  • Sink!

Sink!

0

1

2

3

4

5

6

n/a

8

5

6

1

2

NULL

Array Representation

Idx

Val

10 of 38

Max Heap Solution

8

5

2

6

1

Steps:

  • Swap the root with the leaf node closest to the right.
  • Remove the leaf node 9, which was previously the root
  • Sink!

DONE!

0

1

2

3

4

5

6

n/a

8

5

6

1

2

NULL

Array Representation

Idx

Val

11 of 38

Binary Heap Overview

  • A Priority Queue ADT Implementation - A Data Structure
    • add(value), removeMin(), peekMin()
  • Min Heap Invariant: Every node is less than or equal to all its children
  • Complete Tree Invariant: A heap is always a complete tree
    • Every level, except possible the bottom level, is completely full
    • The bottom level is filled from left to right with no gap! Remember to swim/percolate up!
    • Binary heap height bounds at Θ( log(n) )
  • Binary Tree Invariants: Every node has at most 2 children
  • A binary heap needs to satisfy all three invariants above

12 of 38

Questions?

13 of 38

Check-In

  • Ask us a question!

(unrelated to CS pls)

  • How many midterms do you have coming up?

14 of 38

Announcements

  • Lectures: Affordance Analysis (tomorrow) - do pre-lecture reading + quiz!
  • Project: Autocomplete analysis due tomorrow night!
  • Exercise 4 is out due EOD Tuesday 4/30, peer review by 5/5
  • Remember that you need to submit a resub form (linked on Ed) and resubmit again to Canvas to be eligible for a resubmission.
    • You can now resubmit any two exercise problems per week!

15 of 38

Micro-teach: Hashing

16 of 38

Hash Tables

One way to implement the dictionary/Map ADT.

Stores item with key x in array index (or “bucket”) h(x).(The choice of h(x)is important for many reasons.)��

In this case h(x) = x; but this is NOT always the case

17 of 38

HashTable Example (On the Worksheet)

Hash Code: (string length)

For this example, the underlying array has size 4.

Draw what the hashmap looks like after lines 1-5 have been run.

18 of 38

HashTable Example Intermediate States

Code lines 1-5

19 of 38

HashTable Example

Hash Code: (string length)

For this example, the underlying array has size 4.

Draw what the hashmap looks like after lines 1-9 have been run.

20 of 38

HashTable Example Intermediate States

Code lines 1-9

21 of 38

HashTable Example

Hash Code: (string length)

For this example, the underlying array has size 4.

Draw what the hashmap looks like after all lines have been run.

22 of 38

HashTable Example

Final State:

23 of 38

HashTable Example

Hash Code: (string length)

For this example, the underlying array has size 4.

What does map.size() return?

24 of 38

HashTable Overview

  • How do we handle collisions?
    • There are many ways! Ex. jumping forward until we find an empty bucket, using another algorithm to find the next available spot…
    • Common solution; use separate chaining (shown in previous example)
  • How do we choose a hash function?
    • Goal; no collisions!!
    • How to achieve this? You want the hash function to distribute the keys evenly.
  • How does this affect runtime??
    • Best Case: No collisions, operations in constant time
    • Worst Case: N Collisions in the bucket or resize, operations in N time

25 of 38

Questions?

26 of 38

Case Study #4: Augmentative and Alternative Communication (AAC)

27 of 38

What is an AAC?

Augmentative and Alternative Communication (AAC)

  • Augmentative: add to someone’s speech/signing
  • Alternative: used instead of speech/signing

  • Click/push buttons on picture/text icons that will be read out loud (think text-to-speech). Some icons are categories that lead to more icons and more choices.

  • Temporary vs. throughout life situations

28 of 38

Design Considerations: Motor Plans

Reducing the need to learn many different motor plans

  • A speech pathologist may want to mimic acquiring language, by hiding words from the display, when helping someone learn an AAC
    • When you were a child learning speech, you tend to filter out big words you don’t know as well.
  • But we should maintain a good layout for the words/phrases that will work later on without causing a largely different motor plan.
    • It would be hard if the layout of our own computer keyboard would change!

29 of 38

Design Considerations: Common Words/Phrases

Access to common words/phrases quickly

  • Frustrating for the user to go through many motions to access a commonly used word/phrase.

30 of 38

Designing an AAC: HashMap

Should we use a recursive HashMap? Think about this design using a HashMap:

  • When displaying the AAC, we could iterate over the hashmap to display they <key, value> pairs into the 2D grid interface format.

class AAC {

HashMap<Button, AAC> grid;

}

31 of 38

Designing an AAC: Fundamentals

What is a problem of using a recursive HashMap solution?

Remember the design features we want to build in:

  • Reducing the need to learn many different motor plans
  • Access to common words/phrases quickly

If we iterate over the recursive HashMap to create the 2D grid for the interface, we will get a different order for the layout everytime! A new motor plan each time!

  • HashMaps are not ordered! The hashing strategy is meant to place objects in a pseudo-random order (into the buckets), to optimize quick placement and look-up time.

32 of 38

Designing an AAC: Fundamentals

What is a problem of using a recursive HashMap solution?

Remember the design features we want to build in:

  • Reducing the need to learn many different motor plans
  • Access to common words/phrases quickly

33 of 38

Designing our AAC

Let’s use a recursive 1D Array approach instead where each box index contains three fields: text, image, display (for hiding words) and a pointer to the next 1D array.

Text: “Food”

Pointer:

Image: ./img/food

Display: True

Text: “Games”

Pointer:

Image: ./img/games

Display: False

Text: “Places”

Pointer:

Image: ./img/places

Text: “Hamburger”

Pointer: null

Image: ./img/hamburger

Display: True

Text: “Pizza”

Pointer: null

Image: ./img/food

Display: True

Text: "Fruit"

Pointer:

Image: /.img/fruit/

Text: "Coffee"

Pointer: null

Image: /.img/coffee/

34 of 38

Access to common words/phrases quickly

Remember the design features we want to build in:

  • Reducing the need to learn many different motor plans
  • Access to common words/phrases quickly

Let’s Design!

35 of 38

commonWords

Choose a data structure + write out an approach in a few English sentences. What is the runtime?

commonWords(List<String> words)

  • words: A long long list of utterances/speech from many different humans.
    • Ex: {"the", "uhh", "hi", “good morning”, “the”, “hi”, “hello world”, “hi”}
  • Returns: The top 50 most common words/phrases a human uses arranged in a 1D array, to add to a separate page of the AAC for easy access.
    • count up and sort what words/phrases are used in words and put into 1D array.

How would we integrate this into the AAC design we made earlier?

36 of 38

Connecting back to the AAC

  • Add all 50 words to the top-level button on the interface
    • Selecting the button leads to a new interface with the 50 words

Text: “Food”

Pointer:

Image: ./img/food

Display: True

Text: “Games”

Pointer:

Image: ./img/games

Display: False

Text: “Places”

Pointer:

Image: ./img/places

Text: “CommonWords”

Pointer:

Image: None

Text: “Hamburger”

Pointer: null

Image: ./img/hamburger

Display: True

Text: “Pizza”

Pointer: null

Image: ./img/food

Display: True

Text: "Fruit"

Pointer:

Image: /.img/fruit/

Text: "Coffee"

Pointer: null

Image: /.img/coffee/

37 of 38

Project Priority Queues Overview

  • Implementation: UnsortedArrayMinPQ, HeapMinPQ, OptimizedHeapMinPQ
    • Explain the parts of HeapMinPQ and OptimizedHeapMinPQ you're most proud of.
    • Length of time tends to increase with each implementation task.
    • ReportAnalyzer for Web Content Accessibility Guideline (WCAG)
  • Analyze and compare
    • Asymptotic Analysis:
      • Analyze the Big-Θ bound for worst-case runtime of removeMin and changePriority
      • Analyze usage of Tree bucket optimization in Hash table
    • Random Testing

Include each DELIVERABLE in your presentation!

38 of 38

Closing Announcements

  • Thanks for coming to section! See you tomorrow in class! Stay safe and stay hydrated :)
  • Turn in today’s worksheet + section reflection + lecture worksheets in by Sunday!
  • Turn in the resubs if you have any! You can submit two exercises problems!
  • Remember analysis for Autocomplete is due tomorrow night!

Please don't hesitate to reach out if you have any

questions or concerns about the course, or if there's

anything else we can help you with! We're here to support

you however we can!