CSE 373 SP25 AX: Section 5
Heaps and Hashing
9
8
2
6
5
1
Announcements
Micro-teach: Heaps
Binary Heap Overview
Question 1 (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
Q1 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
Note: Make sure you understand the array representation here too! :)
Q1 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
Note: Make sure you understand the array representation here too! :)
Q1 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
Note: Make sure you understand the array representation here too! :)
Q1 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
Note: Make sure you understand the array representation here too! :)
Q1 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
Note: Make sure you understand the array representation here too! :)
Q1 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
Note: Make sure you understand the array representation here too! :)
Q1 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
Note: Make sure you understand the array representation here too! :)
Q1 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
Note: Make sure you understand the array representation here too! :)
Extra Practice: Min-Heap Values
Assume the integers 1-11 were inserted into the min-heap below in an unknown order.
What are all the possible nodes (A-K) where you could find each of the numbers below? Explain your reasoning for both the places you could find each number and the places you could not find each number.
A
B
D
E
F
C
H
G
I
J
K
Extra Practice: Min-Heap Values (hint)
A
B
D
E
F
C
H
G
I
J
K
≤ D,J (*and all their children)
≤ B,F*
≤ I,K*
≤ A,C*
≤ E,G*
Questions?
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 Overview
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?
If two points are equal if and only if their x-values are equal, what would calling contains(Point(1, 3)) on the hash table return?
Explain your answer by explaining how contains uses both the hashCode and equals methods to search for objects.
Hashing Points (Extra Practice)
Explain why each point does or does not collide by using its hashCode to find the index of its bucket.
public boolean equals(Object o) {
Point other = (Point) o;
return this.x == other.x;
}
public class Point {
public final int x, y;
public Point(int x, int y) {
this.x;
this.y;
}
public int hashCode() {
return this.x + this.y;
}
}
Questions?
Gradescope: Autocomplete
AND ON YOUR WORKSHEET!
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!