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
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
Max Heap Solution
8
5
2
6
1
9
Steps:
0 | 1 | 2 | 3 | 4 | 5 | 6 |
n/a | 9 | 8 | 6 | 5 | 2 | 1 |
Array Representation
Idx
Val
Max Heap Solution
8
5
2
6
9
Steps:
1
0 | 1 | 2 | 3 | 4 | 5 | 6 |
n/a | 1 | 8 | 6 | 5 | 2 | 9 |
Array Representation
Idx
Val
Max Heap Solution
8
5
2
6
1
Steps:
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
Max Heap Solution
8
5
2
6
1
Steps:
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
Max Heap Solution
8
5
2
6
1
Steps:
Sink to the max child
0 | 1 | 2 | 3 | 4 | 5 | 6 |
n/a | 8 | 1 | 6 | 5 | 2 | NULL |
Array Representation
Idx
Val
Max Heap Solution
8
5
2
6
1
Steps:
Compare again!
0 | 1 | 2 | 3 | 4 | 5 | 6 |
n/a | 8 | 1 | 6 | 5 | 2 | NULL |
Array Representation
Idx
Val
Max Heap Solution
8
5
2
6
1
Steps:
Sink!
0 | 1 | 2 | 3 | 4 | 5 | 6 |
n/a | 8 | 5 | 6 | 1 | 2 | NULL |
Array Representation
Idx
Val
Max Heap Solution
8
5
2
6
1
Steps:
DONE!
0 | 1 | 2 | 3 | 4 | 5 | 6 |
n/a | 8 | 5 | 6 | 1 | 2 | NULL |
Array Representation
Idx
Val
Binary Heap Overview
Questions?
Check-In
(unrelated to CS pls)
Announcements
Micro-teach: Hashing
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
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.
HashTable Example Intermediate States
Code lines 1-5
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.
HashTable Example Intermediate States
Code lines 1-9
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.
HashTable Example
Final State:
HashTable Example
Hash Code: (string length)
For this example, the underlying array has size 4.
What does map.size() return?
HashTable Overview
Questions?
Case Study #4: Augmentative and Alternative Communication (AAC)
What is an AAC?
Augmentative and Alternative Communication (AAC)
Design Considerations: Motor Plans
Reducing the need to learn many different motor plans
Design Considerations: Common Words/Phrases
Access to common words/phrases quickly
Designing an AAC: HashMap
Should we use a recursive HashMap? Think about this design using a HashMap:
class AAC {
HashMap<Button, AAC> grid;
}
Designing an AAC: Fundamentals
What is a problem of using a recursive HashMap solution?
Remember the design features we want to build in:
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!
Designing an AAC: Fundamentals
What is a problem of using a recursive HashMap solution?
Remember the design features we want to build in:
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/ |
Access to common words/phrases quickly
Remember the design features we want to build in:
Let’s Design!
commonWords
Choose a data structure + write out an approach in a few English sentences. What is the runtime?
commonWords(List<String> words)
How would we integrate this into the AAC design we made earlier?
Connecting back to the AAC
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/ |
Project Priority Queues Overview
Include each DELIVERABLE in your presentation!
Closing Announcements
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!